mirror of
https://github.com/blakeblackshear/frigate.git
synced 2025-08-08 13:51:01 +02:00
Cleanup dispatcher
This commit is contained in:
parent
d077fe38f3
commit
b3b40f58f4
@ -33,7 +33,7 @@ class DetectionSubscriber(Subscriber):
|
|||||||
|
|
||||||
def check_for_update(
|
def check_for_update(
|
||||||
self, timeout: float | None = None
|
self, timeout: float | None = None
|
||||||
) -> tuple[str, Any] | tuple[None, None]:
|
) -> tuple[str, Any] | tuple[None, None] | None:
|
||||||
return super().check_for_update(timeout)
|
return super().check_for_update(timeout)
|
||||||
|
|
||||||
def _return_object(self, topic: str, payload: Any) -> Any:
|
def _return_object(self, topic: str, payload: Any) -> Any:
|
||||||
|
@ -54,10 +54,9 @@ class Dispatcher:
|
|||||||
self.ptz_metrics = ptz_metrics
|
self.ptz_metrics = ptz_metrics
|
||||||
self.comms = communicators
|
self.comms = communicators
|
||||||
self.camera_activity = CameraActivityManager(config, self.publish)
|
self.camera_activity = CameraActivityManager(config, self.publish)
|
||||||
self.model_state = {}
|
self.model_state: dict[str, ModelStatusTypesEnum] = {}
|
||||||
self.embeddings_reindex = {}
|
self.embeddings_reindex: dict[str, Any] = {}
|
||||||
self.birdseye_layout = {}
|
self.birdseye_layout: dict[str, Any] = {}
|
||||||
|
|
||||||
self._camera_settings_handlers: dict[str, Callable] = {
|
self._camera_settings_handlers: dict[str, Callable] = {
|
||||||
"audio": self._on_audio_command,
|
"audio": self._on_audio_command,
|
||||||
"audio_transcription": self._on_audio_transcription_command,
|
"audio_transcription": self._on_audio_transcription_command,
|
||||||
@ -88,10 +87,12 @@ class Dispatcher:
|
|||||||
(comm for comm in communicators if isinstance(comm, WebPushClient)), None
|
(comm for comm in communicators if isinstance(comm, WebPushClient)), None
|
||||||
)
|
)
|
||||||
|
|
||||||
def _receive(self, topic: str, payload: str) -> Optional[Any]:
|
def _receive(self, topic: str, payload: Any) -> Optional[Any]:
|
||||||
"""Handle receiving of payload from communicators."""
|
"""Handle receiving of payload from communicators."""
|
||||||
|
|
||||||
def handle_camera_command(command_type, camera_name, command, payload):
|
def handle_camera_command(
|
||||||
|
command_type: str, camera_name: str, command: str, payload: str
|
||||||
|
) -> None:
|
||||||
try:
|
try:
|
||||||
if command_type == "set":
|
if command_type == "set":
|
||||||
self._camera_settings_handlers[command](camera_name, payload)
|
self._camera_settings_handlers[command](camera_name, payload)
|
||||||
@ -100,13 +101,13 @@ class Dispatcher:
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
logger.error(f"Invalid command type or handler: {command_type}")
|
logger.error(f"Invalid command type or handler: {command_type}")
|
||||||
|
|
||||||
def handle_restart():
|
def handle_restart() -> None:
|
||||||
restart_frigate()
|
restart_frigate()
|
||||||
|
|
||||||
def handle_insert_many_recordings():
|
def handle_insert_many_recordings() -> None:
|
||||||
Recordings.insert_many(payload).execute()
|
Recordings.insert_many(payload).execute()
|
||||||
|
|
||||||
def handle_request_region_grid():
|
def handle_request_region_grid() -> Any:
|
||||||
camera = payload
|
camera = payload
|
||||||
grid = get_camera_regions_grid(
|
grid = get_camera_regions_grid(
|
||||||
camera,
|
camera,
|
||||||
@ -115,24 +116,24 @@ class Dispatcher:
|
|||||||
)
|
)
|
||||||
return grid
|
return grid
|
||||||
|
|
||||||
def handle_insert_preview():
|
def handle_insert_preview() -> None:
|
||||||
Previews.insert(payload).execute()
|
Previews.insert(payload).execute()
|
||||||
|
|
||||||
def handle_upsert_review_segment():
|
def handle_upsert_review_segment() -> None:
|
||||||
ReviewSegment.insert(payload).on_conflict(
|
ReviewSegment.insert(payload).on_conflict(
|
||||||
conflict_target=[ReviewSegment.id],
|
conflict_target=[ReviewSegment.id],
|
||||||
update=payload,
|
update=payload,
|
||||||
).execute()
|
).execute()
|
||||||
|
|
||||||
def handle_clear_ongoing_review_segments():
|
def handle_clear_ongoing_review_segments() -> None:
|
||||||
ReviewSegment.update(end_time=datetime.datetime.now().timestamp()).where(
|
ReviewSegment.update(end_time=datetime.datetime.now().timestamp()).where(
|
||||||
ReviewSegment.end_time.is_null(True)
|
ReviewSegment.end_time.is_null(True)
|
||||||
).execute()
|
).execute()
|
||||||
|
|
||||||
def handle_update_camera_activity():
|
def handle_update_camera_activity() -> None:
|
||||||
self.camera_activity.update_activity(payload)
|
self.camera_activity.update_activity(payload)
|
||||||
|
|
||||||
def handle_update_event_description():
|
def handle_update_event_description() -> None:
|
||||||
event: Event = Event.get(Event.id == payload["id"])
|
event: Event = Event.get(Event.id == payload["id"])
|
||||||
event.data["description"] = payload["description"]
|
event.data["description"] = payload["description"]
|
||||||
event.save()
|
event.save()
|
||||||
@ -148,24 +149,24 @@ class Dispatcher:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle_update_model_state():
|
def handle_update_model_state() -> None:
|
||||||
if payload:
|
if payload:
|
||||||
model = payload["model"]
|
model = payload["model"]
|
||||||
state = payload["state"]
|
state = payload["state"]
|
||||||
self.model_state[model] = ModelStatusTypesEnum[state]
|
self.model_state[model] = ModelStatusTypesEnum[state]
|
||||||
self.publish("model_state", json.dumps(self.model_state))
|
self.publish("model_state", json.dumps(self.model_state))
|
||||||
|
|
||||||
def handle_model_state():
|
def handle_model_state() -> None:
|
||||||
self.publish("model_state", json.dumps(self.model_state.copy()))
|
self.publish("model_state", json.dumps(self.model_state.copy()))
|
||||||
|
|
||||||
def handle_update_embeddings_reindex_progress():
|
def handle_update_embeddings_reindex_progress() -> None:
|
||||||
self.embeddings_reindex = payload
|
self.embeddings_reindex = payload
|
||||||
self.publish(
|
self.publish(
|
||||||
"embeddings_reindex_progress",
|
"embeddings_reindex_progress",
|
||||||
json.dumps(payload),
|
json.dumps(payload),
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle_embeddings_reindex_progress():
|
def handle_embeddings_reindex_progress() -> None:
|
||||||
self.publish(
|
self.publish(
|
||||||
"embeddings_reindex_progress",
|
"embeddings_reindex_progress",
|
||||||
json.dumps(self.embeddings_reindex.copy()),
|
json.dumps(self.embeddings_reindex.copy()),
|
||||||
@ -179,7 +180,7 @@ class Dispatcher:
|
|||||||
def handle_birdseye_layout():
|
def handle_birdseye_layout():
|
||||||
self.publish("birdseye_layout", json.dumps(self.birdseye_layout.copy()))
|
self.publish("birdseye_layout", json.dumps(self.birdseye_layout.copy()))
|
||||||
|
|
||||||
def handle_on_connect():
|
def handle_on_connect() -> None:
|
||||||
camera_status = self.camera_activity.last_camera_activity.copy()
|
camera_status = self.camera_activity.last_camera_activity.copy()
|
||||||
cameras_with_status = camera_status.keys()
|
cameras_with_status = camera_status.keys()
|
||||||
|
|
||||||
@ -219,7 +220,7 @@ class Dispatcher:
|
|||||||
)
|
)
|
||||||
self.publish("birdseye_layout", json.dumps(self.birdseye_layout.copy()))
|
self.publish("birdseye_layout", json.dumps(self.birdseye_layout.copy()))
|
||||||
|
|
||||||
def handle_notification_test():
|
def handle_notification_test() -> None:
|
||||||
self.publish("notification_test", "Test notification")
|
self.publish("notification_test", "Test notification")
|
||||||
|
|
||||||
# Dictionary mapping topic to handlers
|
# Dictionary mapping topic to handlers
|
||||||
@ -266,11 +267,12 @@ class Dispatcher:
|
|||||||
logger.error(
|
logger.error(
|
||||||
f"Received invalid {topic.split('/')[-1]} command: {topic}"
|
f"Received invalid {topic.split('/')[-1]} command: {topic}"
|
||||||
)
|
)
|
||||||
return
|
return None
|
||||||
elif topic in topic_handlers:
|
elif topic in topic_handlers:
|
||||||
return topic_handlers[topic]()
|
return topic_handlers[topic]()
|
||||||
else:
|
else:
|
||||||
self.publish(topic, payload, retain=False)
|
self.publish(topic, payload, retain=False)
|
||||||
|
return None
|
||||||
|
|
||||||
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
|
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
|
||||||
"""Handle publishing to communicators."""
|
"""Handle publishing to communicators."""
|
||||||
@ -373,11 +375,11 @@ class Dispatcher:
|
|||||||
if payload == "ON":
|
if payload == "ON":
|
||||||
if not motion_settings.improve_contrast:
|
if not motion_settings.improve_contrast:
|
||||||
logger.info(f"Turning on improve contrast for {camera_name}")
|
logger.info(f"Turning on improve contrast for {camera_name}")
|
||||||
motion_settings.improve_contrast = True # type: ignore[union-attr]
|
motion_settings.improve_contrast = True
|
||||||
elif payload == "OFF":
|
elif payload == "OFF":
|
||||||
if motion_settings.improve_contrast:
|
if motion_settings.improve_contrast:
|
||||||
logger.info(f"Turning off improve contrast for {camera_name}")
|
logger.info(f"Turning off improve contrast for {camera_name}")
|
||||||
motion_settings.improve_contrast = False # type: ignore[union-attr]
|
motion_settings.improve_contrast = False
|
||||||
|
|
||||||
self.config_updater.publish_update(
|
self.config_updater.publish_update(
|
||||||
CameraConfigUpdateTopic(CameraConfigUpdateEnum.motion, camera_name),
|
CameraConfigUpdateTopic(CameraConfigUpdateEnum.motion, camera_name),
|
||||||
@ -421,7 +423,7 @@ class Dispatcher:
|
|||||||
|
|
||||||
motion_settings = self.config.cameras[camera_name].motion
|
motion_settings = self.config.cameras[camera_name].motion
|
||||||
logger.info(f"Setting motion contour area for {camera_name}: {payload}")
|
logger.info(f"Setting motion contour area for {camera_name}: {payload}")
|
||||||
motion_settings.contour_area = payload # type: ignore[union-attr]
|
motion_settings.contour_area = payload
|
||||||
self.config_updater.publish_update(
|
self.config_updater.publish_update(
|
||||||
CameraConfigUpdateTopic(CameraConfigUpdateEnum.motion, camera_name),
|
CameraConfigUpdateTopic(CameraConfigUpdateEnum.motion, camera_name),
|
||||||
motion_settings,
|
motion_settings,
|
||||||
@ -438,7 +440,7 @@ class Dispatcher:
|
|||||||
|
|
||||||
motion_settings = self.config.cameras[camera_name].motion
|
motion_settings = self.config.cameras[camera_name].motion
|
||||||
logger.info(f"Setting motion threshold for {camera_name}: {payload}")
|
logger.info(f"Setting motion threshold for {camera_name}: {payload}")
|
||||||
motion_settings.threshold = payload # type: ignore[union-attr]
|
motion_settings.threshold = payload
|
||||||
self.config_updater.publish_update(
|
self.config_updater.publish_update(
|
||||||
CameraConfigUpdateTopic(CameraConfigUpdateEnum.motion, camera_name),
|
CameraConfigUpdateTopic(CameraConfigUpdateEnum.motion, camera_name),
|
||||||
motion_settings,
|
motion_settings,
|
||||||
@ -453,7 +455,7 @@ class Dispatcher:
|
|||||||
|
|
||||||
notification_settings = self.config.notifications
|
notification_settings = self.config.notifications
|
||||||
logger.info(f"Setting all notifications: {payload}")
|
logger.info(f"Setting all notifications: {payload}")
|
||||||
notification_settings.enabled = payload == "ON" # type: ignore[union-attr]
|
notification_settings.enabled = payload == "ON"
|
||||||
self.config_updater.publisher.publish(
|
self.config_updater.publisher.publish(
|
||||||
"config/notifications", notification_settings
|
"config/notifications", notification_settings
|
||||||
)
|
)
|
||||||
|
@ -75,7 +75,7 @@ class MqttClient(Communicator):
|
|||||||
)
|
)
|
||||||
self.publish(
|
self.publish(
|
||||||
f"{camera_name}/improve_contrast/state",
|
f"{camera_name}/improve_contrast/state",
|
||||||
"ON" if camera.motion.improve_contrast else "OFF", # type: ignore[union-attr]
|
"ON" if camera.motion.improve_contrast else "OFF",
|
||||||
retain=True,
|
retain=True,
|
||||||
)
|
)
|
||||||
self.publish(
|
self.publish(
|
||||||
@ -85,12 +85,12 @@ class MqttClient(Communicator):
|
|||||||
)
|
)
|
||||||
self.publish(
|
self.publish(
|
||||||
f"{camera_name}/motion_threshold/state",
|
f"{camera_name}/motion_threshold/state",
|
||||||
camera.motion.threshold, # type: ignore[union-attr]
|
camera.motion.threshold,
|
||||||
retain=True,
|
retain=True,
|
||||||
)
|
)
|
||||||
self.publish(
|
self.publish(
|
||||||
f"{camera_name}/motion_contour_area/state",
|
f"{camera_name}/motion_contour_area/state",
|
||||||
camera.motion.contour_area, # type: ignore[union-attr]
|
camera.motion.contour_area,
|
||||||
retain=True,
|
retain=True,
|
||||||
)
|
)
|
||||||
self.publish(
|
self.publish(
|
||||||
|
@ -84,7 +84,7 @@ class Subscriber:
|
|||||||
|
|
||||||
def check_for_update(
|
def check_for_update(
|
||||||
self, timeout: float | None = FAST_QUEUE_TIMEOUT
|
self, timeout: float | None = FAST_QUEUE_TIMEOUT
|
||||||
) -> tuple[str, Any] | tuple[None, None]:
|
) -> tuple[str, Any] | tuple[None, None] | None:
|
||||||
"""Returns message or None if no update."""
|
"""Returns message or None if no update."""
|
||||||
try:
|
try:
|
||||||
has_update, _, _ = zmq.select([self.socket], [], [], timeout)
|
has_update, _, _ = zmq.select([self.socket], [], [], timeout)
|
||||||
@ -103,5 +103,5 @@ class Subscriber:
|
|||||||
|
|
||||||
def _return_object(
|
def _return_object(
|
||||||
self, topic: str, payload: Optional[tuple[str, Any]]
|
self, topic: str, payload: Optional[tuple[str, Any]]
|
||||||
) -> tuple[str, Any] | tuple[None, None]:
|
) -> tuple[str, Any] | tuple[None, None] | None:
|
||||||
return payload or (None, None)
|
return payload
|
||||||
|
@ -80,7 +80,7 @@ class CameraConfig(FrigateBaseModel):
|
|||||||
lpr: CameraLicensePlateRecognitionConfig = Field(
|
lpr: CameraLicensePlateRecognitionConfig = Field(
|
||||||
default_factory=CameraLicensePlateRecognitionConfig, title="LPR config."
|
default_factory=CameraLicensePlateRecognitionConfig, title="LPR config."
|
||||||
)
|
)
|
||||||
motion: Optional[MotionConfig] = Field(
|
motion: MotionConfig = Field(
|
||||||
None, title="Motion detection configuration."
|
None, title="Motion detection configuration."
|
||||||
)
|
)
|
||||||
objects: ObjectConfig = Field(
|
objects: ObjectConfig = Field(
|
||||||
|
Loading…
Reference in New Issue
Block a user