2023-01-13 14:18:15 +01:00
|
|
|
"""Handle communication between Frigate and other applications."""
|
2022-11-24 03:03:20 +01:00
|
|
|
|
2024-04-24 15:44:28 +02:00
|
|
|
import datetime
|
2024-04-30 15:09:50 +02:00
|
|
|
import json
|
2022-11-24 03:03:20 +01:00
|
|
|
import logging
|
|
|
|
from abc import ABC, abstractmethod
|
2024-02-15 01:24:36 +01:00
|
|
|
from typing import Any, Callable, Optional
|
2022-11-24 03:03:20 +01:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
from frigate.camera import PTZMetrics
|
2024-02-19 14:26:59 +01:00
|
|
|
from frigate.comms.config_updater import ConfigPublisher
|
2023-10-26 13:20:55 +02:00
|
|
|
from frigate.config import BirdseyeModeEnum, FrigateConfig
|
2024-02-21 00:26:09 +01:00
|
|
|
from frigate.const import (
|
2024-04-24 15:44:28 +02:00
|
|
|
CLEAR_ONGOING_REVIEW_SEGMENTS,
|
2024-02-21 00:26:09 +01:00
|
|
|
INSERT_MANY_RECORDINGS,
|
|
|
|
INSERT_PREVIEW,
|
|
|
|
REQUEST_REGION_GRID,
|
2024-04-30 15:09:50 +02:00
|
|
|
UPDATE_CAMERA_ACTIVITY,
|
2024-10-10 21:28:43 +02:00
|
|
|
UPDATE_EMBEDDINGS_REINDEX_PROGRESS,
|
2024-06-21 23:30:19 +02:00
|
|
|
UPDATE_EVENT_DESCRIPTION,
|
2024-10-07 22:30:45 +02:00
|
|
|
UPDATE_MODEL_STATE,
|
2024-02-21 00:26:09 +01:00
|
|
|
UPSERT_REVIEW_SEGMENT,
|
|
|
|
)
|
2024-06-21 23:30:19 +02:00
|
|
|
from frigate.models import Event, Previews, Recordings, ReviewSegment
|
2023-07-08 14:04:47 +02:00
|
|
|
from frigate.ptz.onvif import OnvifCommandEnum, OnvifController
|
2024-11-18 19:26:44 +01:00
|
|
|
from frigate.types import ModelStatusTypesEnum, TrackedObjectUpdateTypesEnum
|
2023-10-19 01:21:52 +02:00
|
|
|
from frigate.util.object import get_camera_regions_grid
|
2023-07-06 16:28:50 +02:00
|
|
|
from frigate.util.services import restart_frigate
|
2022-11-24 03:03:20 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Communicator(ABC):
|
|
|
|
"""pub/sub model via specific protocol."""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
|
|
|
|
"""Send data via specific protocol."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def subscribe(self, receiver: Callable) -> None:
|
|
|
|
"""Pass receiver so communicators can pass commands."""
|
|
|
|
pass
|
|
|
|
|
2023-02-04 03:15:47 +01:00
|
|
|
@abstractmethod
|
|
|
|
def stop(self) -> None:
|
|
|
|
"""Stop the communicator."""
|
|
|
|
pass
|
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
|
|
|
|
class Dispatcher:
|
2023-01-13 14:18:15 +01:00
|
|
|
"""Handle communication between Frigate and communicators."""
|
2022-11-24 03:03:20 +01:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config: FrigateConfig,
|
2024-02-19 14:26:59 +01:00
|
|
|
config_updater: ConfigPublisher,
|
2023-04-26 13:08:53 +02:00
|
|
|
onvif: OnvifController,
|
2024-09-27 14:53:23 +02:00
|
|
|
ptz_metrics: dict[str, PTZMetrics],
|
2022-11-24 03:03:20 +01:00
|
|
|
communicators: list[Communicator],
|
|
|
|
) -> None:
|
|
|
|
self.config = config
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater = config_updater
|
2023-04-26 13:08:53 +02:00
|
|
|
self.onvif = onvif
|
2023-07-11 13:23:20 +02:00
|
|
|
self.ptz_metrics = ptz_metrics
|
2022-11-24 03:03:20 +01:00
|
|
|
self.comms = communicators
|
2024-10-10 23:37:43 +02:00
|
|
|
self.camera_activity = {}
|
|
|
|
self.model_state = {}
|
|
|
|
self.embeddings_reindex = {}
|
2022-11-24 03:03:20 +01:00
|
|
|
|
|
|
|
self._camera_settings_handlers: dict[str, Callable] = {
|
2023-07-01 15:18:33 +02:00
|
|
|
"audio": self._on_audio_command,
|
2022-11-24 03:03:20 +01:00
|
|
|
"detect": self._on_detect_command,
|
|
|
|
"improve_contrast": self._on_motion_improve_contrast_command,
|
2023-07-08 14:04:47 +02:00
|
|
|
"ptz_autotracker": self._on_ptz_autotracker_command,
|
2022-11-24 03:03:20 +01:00
|
|
|
"motion": self._on_motion_command,
|
|
|
|
"motion_contour_area": self._on_motion_contour_area_command,
|
|
|
|
"motion_threshold": self._on_motion_threshold_command,
|
2022-11-29 01:58:41 +01:00
|
|
|
"recordings": self._on_recordings_command,
|
2022-11-24 03:03:20 +01:00
|
|
|
"snapshots": self._on_snapshots_command,
|
2023-10-26 13:20:55 +02:00
|
|
|
"birdseye": self._on_birdseye_command,
|
|
|
|
"birdseye_mode": self._on_birdseye_mode_command,
|
2022-11-24 03:03:20 +01:00
|
|
|
}
|
2024-09-10 19:24:44 +02:00
|
|
|
self._global_settings_handlers: dict[str, Callable] = {
|
|
|
|
"notifications": self._on_notification_command,
|
|
|
|
}
|
2022-11-24 03:03:20 +01:00
|
|
|
|
2023-09-01 14:06:59 +02:00
|
|
|
for comm in self.comms:
|
|
|
|
comm.subscribe(self._receive)
|
|
|
|
|
2024-02-15 01:24:36 +01:00
|
|
|
def _receive(self, topic: str, payload: str) -> Optional[Any]:
|
2022-11-24 03:03:20 +01:00
|
|
|
"""Handle receiving of payload from communicators."""
|
2024-10-10 21:28:43 +02:00
|
|
|
|
2024-10-11 02:48:56 +02:00
|
|
|
def handle_camera_command(command_type, camera_name, command, payload):
|
2023-04-26 13:08:53 +02:00
|
|
|
try:
|
2024-10-10 21:28:43 +02:00
|
|
|
if command_type == "set":
|
2024-10-11 02:48:56 +02:00
|
|
|
self._camera_settings_handlers[command](camera_name, payload)
|
2024-10-10 21:28:43 +02:00
|
|
|
elif command_type == "ptz":
|
|
|
|
self._on_ptz_command(camera_name, payload)
|
|
|
|
except KeyError:
|
2024-10-11 02:48:56 +02:00
|
|
|
logger.error(f"Invalid command type or handler: {command_type}")
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_restart():
|
2022-11-24 03:03:20 +01:00
|
|
|
restart_frigate()
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_insert_many_recordings():
|
2023-07-26 12:55:08 +02:00
|
|
|
Recordings.insert_many(payload).execute()
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_request_region_grid():
|
2023-10-19 01:21:52 +02:00
|
|
|
camera = payload
|
2024-02-15 01:24:36 +01:00
|
|
|
grid = get_camera_regions_grid(
|
|
|
|
camera,
|
|
|
|
self.config.cameras[camera].detect,
|
|
|
|
max(self.config.model.width, self.config.model.height),
|
2023-10-19 01:21:52 +02:00
|
|
|
)
|
2024-02-15 01:24:36 +01:00
|
|
|
return grid
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_insert_preview():
|
2023-12-03 15:16:01 +01:00
|
|
|
Previews.insert(payload).execute()
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_upsert_review_segment():
|
|
|
|
ReviewSegment.insert(payload).on_conflict(
|
|
|
|
conflict_target=[ReviewSegment.id],
|
|
|
|
update=payload,
|
|
|
|
).execute()
|
|
|
|
|
|
|
|
def handle_clear_ongoing_review_segments():
|
2024-04-24 15:44:28 +02:00
|
|
|
ReviewSegment.update(end_time=datetime.datetime.now().timestamp()).where(
|
2024-10-10 21:28:43 +02:00
|
|
|
ReviewSegment.end_time.is_null(True)
|
2024-04-24 15:44:28 +02:00
|
|
|
).execute()
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_update_camera_activity():
|
2024-04-30 15:09:50 +02:00
|
|
|
self.camera_activity = payload
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_update_event_description():
|
2024-06-21 23:30:19 +02:00
|
|
|
event: Event = Event.get(Event.id == payload["id"])
|
|
|
|
event.data["description"] = payload["description"]
|
|
|
|
event.save()
|
2024-09-24 16:14:51 +02:00
|
|
|
self.publish(
|
2024-11-18 19:26:44 +01:00
|
|
|
"tracked_object_update",
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"type": TrackedObjectUpdateTypesEnum.description,
|
|
|
|
"id": event.id,
|
|
|
|
"description": event.data["description"],
|
|
|
|
}
|
|
|
|
),
|
2024-09-24 16:14:51 +02:00
|
|
|
)
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_update_model_state():
|
2024-10-11 18:47:23 +02:00
|
|
|
if payload:
|
|
|
|
model = payload["model"]
|
|
|
|
state = payload["state"]
|
|
|
|
self.model_state[model] = ModelStatusTypesEnum[state]
|
|
|
|
self.publish("model_state", json.dumps(self.model_state))
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
def handle_model_state():
|
|
|
|
self.publish("model_state", json.dumps(self.model_state.copy()))
|
|
|
|
|
|
|
|
def handle_update_embeddings_reindex_progress():
|
|
|
|
self.embeddings_reindex = payload
|
|
|
|
self.publish(
|
|
|
|
"embeddings_reindex_progress",
|
|
|
|
json.dumps(payload),
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle_embeddings_reindex_progress():
|
|
|
|
self.publish(
|
|
|
|
"embeddings_reindex_progress",
|
|
|
|
json.dumps(self.embeddings_reindex.copy()),
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle_on_connect():
|
2024-08-06 17:08:43 +02:00
|
|
|
camera_status = self.camera_activity.copy()
|
|
|
|
|
|
|
|
for camera in camera_status.keys():
|
|
|
|
camera_status[camera]["config"] = {
|
|
|
|
"detect": self.config.cameras[camera].detect.enabled,
|
|
|
|
"snapshots": self.config.cameras[camera].snapshots.enabled,
|
|
|
|
"record": self.config.cameras[camera].record.enabled,
|
|
|
|
"audio": self.config.cameras[camera].audio.enabled,
|
|
|
|
"autotracking": self.config.cameras[
|
|
|
|
camera
|
|
|
|
].onvif.autotracking.enabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.publish("camera_activity", json.dumps(camera_status))
|
2024-10-14 03:36:49 +02:00
|
|
|
self.publish("model_state", json.dumps(self.model_state.copy()))
|
|
|
|
self.publish(
|
|
|
|
"embeddings_reindex_progress",
|
|
|
|
json.dumps(self.embeddings_reindex.copy()),
|
|
|
|
)
|
2024-10-10 21:28:43 +02:00
|
|
|
|
|
|
|
# Dictionary mapping topic to handlers
|
|
|
|
topic_handlers = {
|
|
|
|
INSERT_MANY_RECORDINGS: handle_insert_many_recordings,
|
|
|
|
REQUEST_REGION_GRID: handle_request_region_grid,
|
|
|
|
INSERT_PREVIEW: handle_insert_preview,
|
|
|
|
UPSERT_REVIEW_SEGMENT: handle_upsert_review_segment,
|
|
|
|
CLEAR_ONGOING_REVIEW_SEGMENTS: handle_clear_ongoing_review_segments,
|
|
|
|
UPDATE_CAMERA_ACTIVITY: handle_update_camera_activity,
|
|
|
|
UPDATE_EVENT_DESCRIPTION: handle_update_event_description,
|
|
|
|
UPDATE_MODEL_STATE: handle_update_model_state,
|
|
|
|
UPDATE_EMBEDDINGS_REINDEX_PROGRESS: handle_update_embeddings_reindex_progress,
|
|
|
|
"restart": handle_restart,
|
|
|
|
"embeddingsReindexProgress": handle_embeddings_reindex_progress,
|
|
|
|
"modelState": handle_model_state,
|
|
|
|
"onConnect": handle_on_connect,
|
|
|
|
}
|
|
|
|
|
|
|
|
if topic.endswith("set") or topic.endswith("ptz"):
|
|
|
|
try:
|
|
|
|
parts = topic.split("/")
|
|
|
|
if len(parts) == 3 and topic.endswith("set"):
|
|
|
|
# example /cam_name/detect/set payload=ON|OFF
|
|
|
|
camera_name = parts[-3]
|
|
|
|
command = parts[-2]
|
2024-10-11 02:48:56 +02:00
|
|
|
handle_camera_command("set", camera_name, command, payload)
|
2024-10-10 21:28:43 +02:00
|
|
|
elif len(parts) == 2 and topic.endswith("set"):
|
|
|
|
command = parts[-2]
|
|
|
|
self._global_settings_handlers[command](payload)
|
|
|
|
elif len(parts) == 2 and topic.endswith("ptz"):
|
|
|
|
# example /cam_name/ptz payload=MOVE_UP|MOVE_DOWN|STOP...
|
|
|
|
camera_name = parts[-2]
|
2024-10-11 02:48:56 +02:00
|
|
|
handle_camera_command("ptz", camera_name, "", payload)
|
2024-10-10 21:28:43 +02:00
|
|
|
except IndexError:
|
|
|
|
logger.error(
|
|
|
|
f"Received invalid {topic.split('/')[-1]} command: {topic}"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
elif topic in topic_handlers:
|
|
|
|
return topic_handlers[topic]()
|
2023-07-14 02:52:33 +02:00
|
|
|
else:
|
|
|
|
self.publish(topic, payload, retain=False)
|
2022-11-24 03:03:20 +01:00
|
|
|
|
|
|
|
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
|
|
|
|
"""Handle publishing to communicators."""
|
|
|
|
for comm in self.comms:
|
|
|
|
comm.publish(topic, payload, retain)
|
|
|
|
|
2023-02-04 03:15:47 +01:00
|
|
|
def stop(self) -> None:
|
|
|
|
for comm in self.comms:
|
|
|
|
comm.stop()
|
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
def _on_detect_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for detect topic."""
|
|
|
|
detect_settings = self.config.cameras[camera_name].detect
|
2024-02-19 14:26:59 +01:00
|
|
|
motion_settings = self.config.cameras[camera_name].motion
|
2022-11-24 03:03:20 +01:00
|
|
|
|
|
|
|
if payload == "ON":
|
2024-02-19 14:26:59 +01:00
|
|
|
if not detect_settings.enabled:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.info(f"Turning on detection for {camera_name}")
|
|
|
|
detect_settings.enabled = True
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
if not motion_settings.enabled:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.info(
|
|
|
|
f"Turning on motion for {camera_name} due to detection being enabled."
|
|
|
|
)
|
2024-02-19 14:26:59 +01:00
|
|
|
motion_settings.enabled = True
|
|
|
|
self.config_updater.publish(
|
|
|
|
f"config/motion/{camera_name}", motion_settings
|
|
|
|
)
|
2022-11-24 03:03:20 +01:00
|
|
|
self.publish(f"{camera_name}/motion/state", payload, retain=True)
|
|
|
|
elif payload == "OFF":
|
2024-02-19 14:26:59 +01:00
|
|
|
if detect_settings.enabled:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.info(f"Turning off detection for {camera_name}")
|
|
|
|
detect_settings.enabled = False
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/detect/{camera_name}", detect_settings)
|
2022-11-24 03:03:20 +01:00
|
|
|
self.publish(f"{camera_name}/detect/state", payload, retain=True)
|
|
|
|
|
|
|
|
def _on_motion_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for motion topic."""
|
2024-02-19 14:26:59 +01:00
|
|
|
detect_settings = self.config.cameras[camera_name].detect
|
|
|
|
motion_settings = self.config.cameras[camera_name].motion
|
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
if payload == "ON":
|
2024-02-19 14:26:59 +01:00
|
|
|
if not motion_settings.enabled:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.info(f"Turning on motion for {camera_name}")
|
2024-02-19 14:26:59 +01:00
|
|
|
motion_settings.enabled = True
|
2022-11-24 03:03:20 +01:00
|
|
|
elif payload == "OFF":
|
2024-02-19 14:26:59 +01:00
|
|
|
if detect_settings.enabled:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.error(
|
2023-05-29 12:31:17 +02:00
|
|
|
"Turning off motion is not allowed when detection is enabled."
|
2022-11-24 03:03:20 +01:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
if motion_settings.enabled:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.info(f"Turning off motion for {camera_name}")
|
2024-02-19 14:26:59 +01:00
|
|
|
motion_settings.enabled = False
|
2022-11-24 03:03:20 +01:00
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/motion/{camera_name}", motion_settings)
|
2022-11-24 03:03:20 +01:00
|
|
|
self.publish(f"{camera_name}/motion/state", payload, retain=True)
|
|
|
|
|
|
|
|
def _on_motion_improve_contrast_command(
|
|
|
|
self, camera_name: str, payload: str
|
|
|
|
) -> None:
|
|
|
|
"""Callback for improve_contrast topic."""
|
|
|
|
motion_settings = self.config.cameras[camera_name].motion
|
|
|
|
|
|
|
|
if payload == "ON":
|
2024-02-19 14:26:59 +01:00
|
|
|
if not motion_settings.improve_contrast:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.info(f"Turning on improve contrast for {camera_name}")
|
|
|
|
motion_settings.improve_contrast = True # type: ignore[union-attr]
|
|
|
|
elif payload == "OFF":
|
2024-02-19 14:26:59 +01:00
|
|
|
if motion_settings.improve_contrast:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.info(f"Turning off improve contrast for {camera_name}")
|
|
|
|
motion_settings.improve_contrast = False # type: ignore[union-attr]
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/motion/{camera_name}", motion_settings)
|
2022-11-24 03:03:20 +01:00
|
|
|
self.publish(f"{camera_name}/improve_contrast/state", payload, retain=True)
|
|
|
|
|
2023-07-08 14:04:47 +02:00
|
|
|
def _on_ptz_autotracker_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for ptz_autotracker topic."""
|
|
|
|
ptz_autotracker_settings = self.config.cameras[camera_name].onvif.autotracking
|
|
|
|
|
|
|
|
if payload == "ON":
|
2023-11-16 02:25:48 +01:00
|
|
|
if not self.config.cameras[
|
|
|
|
camera_name
|
|
|
|
].onvif.autotracking.enabled_in_config:
|
|
|
|
logger.error(
|
|
|
|
"Autotracking must be enabled in the config to be turned on via MQTT."
|
|
|
|
)
|
|
|
|
return
|
2024-09-27 14:53:23 +02:00
|
|
|
if not self.ptz_metrics[camera_name].autotracker_enabled.value:
|
2023-07-08 14:04:47 +02:00
|
|
|
logger.info(f"Turning on ptz autotracker for {camera_name}")
|
2024-09-27 14:53:23 +02:00
|
|
|
self.ptz_metrics[camera_name].autotracker_enabled.value = True
|
|
|
|
self.ptz_metrics[camera_name].start_time.value = 0
|
2023-07-08 14:04:47 +02:00
|
|
|
ptz_autotracker_settings.enabled = True
|
|
|
|
elif payload == "OFF":
|
2024-09-27 14:53:23 +02:00
|
|
|
if self.ptz_metrics[camera_name].autotracker_enabled.value:
|
2023-07-08 14:04:47 +02:00
|
|
|
logger.info(f"Turning off ptz autotracker for {camera_name}")
|
2024-09-27 14:53:23 +02:00
|
|
|
self.ptz_metrics[camera_name].autotracker_enabled.value = False
|
|
|
|
self.ptz_metrics[camera_name].start_time.value = 0
|
2023-07-08 14:04:47 +02:00
|
|
|
ptz_autotracker_settings.enabled = False
|
|
|
|
|
|
|
|
self.publish(f"{camera_name}/ptz_autotracker/state", payload, retain=True)
|
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
def _on_motion_contour_area_command(self, camera_name: str, payload: int) -> None:
|
|
|
|
"""Callback for motion contour topic."""
|
|
|
|
try:
|
|
|
|
payload = int(payload)
|
|
|
|
except ValueError:
|
|
|
|
f"Received unsupported value for motion contour area: {payload}"
|
|
|
|
return
|
|
|
|
|
|
|
|
motion_settings = self.config.cameras[camera_name].motion
|
|
|
|
logger.info(f"Setting motion contour area for {camera_name}: {payload}")
|
|
|
|
motion_settings.contour_area = payload # type: ignore[union-attr]
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/motion/{camera_name}", motion_settings)
|
2022-11-24 03:03:20 +01:00
|
|
|
self.publish(f"{camera_name}/motion_contour_area/state", payload, retain=True)
|
|
|
|
|
|
|
|
def _on_motion_threshold_command(self, camera_name: str, payload: int) -> None:
|
|
|
|
"""Callback for motion threshold topic."""
|
|
|
|
try:
|
|
|
|
payload = int(payload)
|
|
|
|
except ValueError:
|
|
|
|
f"Received unsupported value for motion threshold: {payload}"
|
|
|
|
return
|
|
|
|
|
|
|
|
motion_settings = self.config.cameras[camera_name].motion
|
|
|
|
logger.info(f"Setting motion threshold for {camera_name}: {payload}")
|
|
|
|
motion_settings.threshold = payload # type: ignore[union-attr]
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/motion/{camera_name}", motion_settings)
|
2022-11-24 03:03:20 +01:00
|
|
|
self.publish(f"{camera_name}/motion_threshold/state", payload, retain=True)
|
|
|
|
|
2024-09-10 19:24:44 +02:00
|
|
|
def _on_notification_command(self, payload: str) -> None:
|
|
|
|
"""Callback for notification topic."""
|
|
|
|
if payload != "ON" and payload != "OFF":
|
|
|
|
f"Received unsupported value for notification: {payload}"
|
|
|
|
return
|
|
|
|
|
|
|
|
notification_settings = self.config.notifications
|
|
|
|
logger.info(f"Setting notifications: {payload}")
|
|
|
|
notification_settings.enabled = payload == "ON" # type: ignore[union-attr]
|
|
|
|
self.config_updater.publish("config/notifications", notification_settings)
|
|
|
|
self.publish("notifications/state", payload, retain=True)
|
|
|
|
|
2023-07-01 15:18:33 +02:00
|
|
|
def _on_audio_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for audio topic."""
|
|
|
|
audio_settings = self.config.cameras[camera_name].audio
|
|
|
|
|
|
|
|
if payload == "ON":
|
|
|
|
if not self.config.cameras[camera_name].audio.enabled_in_config:
|
|
|
|
logger.error(
|
|
|
|
"Audio detection must be enabled in the config to be turned on via MQTT."
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not audio_settings.enabled:
|
|
|
|
logger.info(f"Turning on audio detection for {camera_name}")
|
|
|
|
audio_settings.enabled = True
|
|
|
|
elif payload == "OFF":
|
2024-02-19 14:26:59 +01:00
|
|
|
if audio_settings.enabled:
|
2023-07-01 15:18:33 +02:00
|
|
|
logger.info(f"Turning off audio detection for {camera_name}")
|
|
|
|
audio_settings.enabled = False
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/audio/{camera_name}", audio_settings)
|
2023-07-01 15:18:33 +02:00
|
|
|
self.publish(f"{camera_name}/audio/state", payload, retain=True)
|
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
def _on_recordings_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for recordings topic."""
|
|
|
|
record_settings = self.config.cameras[camera_name].record
|
|
|
|
|
|
|
|
if payload == "ON":
|
2023-05-15 14:36:26 +02:00
|
|
|
if not self.config.cameras[camera_name].record.enabled_in_config:
|
|
|
|
logger.error(
|
2023-05-29 12:31:17 +02:00
|
|
|
"Recordings must be enabled in the config to be turned on via MQTT."
|
2023-05-15 14:36:26 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
if not record_settings.enabled:
|
|
|
|
logger.info(f"Turning on recordings for {camera_name}")
|
|
|
|
record_settings.enabled = True
|
|
|
|
elif payload == "OFF":
|
2024-02-19 14:26:59 +01:00
|
|
|
if record_settings.enabled:
|
2022-11-24 03:03:20 +01:00
|
|
|
logger.info(f"Turning off recordings for {camera_name}")
|
|
|
|
record_settings.enabled = False
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/record/{camera_name}", record_settings)
|
2022-11-24 03:03:20 +01:00
|
|
|
self.publish(f"{camera_name}/recordings/state", payload, retain=True)
|
|
|
|
|
|
|
|
def _on_snapshots_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for snapshots topic."""
|
|
|
|
snapshots_settings = self.config.cameras[camera_name].snapshots
|
|
|
|
|
|
|
|
if payload == "ON":
|
|
|
|
if not snapshots_settings.enabled:
|
|
|
|
logger.info(f"Turning on snapshots for {camera_name}")
|
|
|
|
snapshots_settings.enabled = True
|
|
|
|
elif payload == "OFF":
|
|
|
|
if snapshots_settings.enabled:
|
|
|
|
logger.info(f"Turning off snapshots for {camera_name}")
|
|
|
|
snapshots_settings.enabled = False
|
|
|
|
|
|
|
|
self.publish(f"{camera_name}/snapshots/state", payload, retain=True)
|
2023-04-26 13:08:53 +02:00
|
|
|
|
|
|
|
def _on_ptz_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for ptz topic."""
|
|
|
|
try:
|
|
|
|
if "preset" in payload.lower():
|
|
|
|
command = OnvifCommandEnum.preset
|
2023-07-06 14:42:17 +02:00
|
|
|
param = payload.lower()[payload.index("_") + 1 :]
|
2024-03-23 17:53:33 +01:00
|
|
|
elif "move_relative" in payload.lower():
|
|
|
|
command = OnvifCommandEnum.move_relative
|
|
|
|
param = payload.lower()[payload.index("_") + 1 :]
|
2023-04-26 13:08:53 +02:00
|
|
|
else:
|
|
|
|
command = OnvifCommandEnum[payload.lower()]
|
|
|
|
param = ""
|
|
|
|
|
|
|
|
self.onvif.handle_command(camera_name, command, param)
|
|
|
|
logger.info(f"Setting ptz command to {command} for {camera_name}")
|
|
|
|
except KeyError as k:
|
|
|
|
logger.error(f"Invalid PTZ command {payload}: {k}")
|
2023-10-26 13:20:55 +02:00
|
|
|
|
|
|
|
def _on_birdseye_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for birdseye topic."""
|
|
|
|
birdseye_settings = self.config.cameras[camera_name].birdseye
|
|
|
|
|
|
|
|
if payload == "ON":
|
2024-02-19 14:26:59 +01:00
|
|
|
if not birdseye_settings.enabled:
|
2023-10-26 13:20:55 +02:00
|
|
|
logger.info(f"Turning on birdseye for {camera_name}")
|
|
|
|
birdseye_settings.enabled = True
|
|
|
|
|
|
|
|
elif payload == "OFF":
|
2024-02-19 14:26:59 +01:00
|
|
|
if birdseye_settings.enabled:
|
2023-10-26 13:20:55 +02:00
|
|
|
logger.info(f"Turning off birdseye for {camera_name}")
|
|
|
|
birdseye_settings.enabled = False
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/birdseye/{camera_name}", birdseye_settings)
|
2023-10-26 13:20:55 +02:00
|
|
|
self.publish(f"{camera_name}/birdseye/state", payload, retain=True)
|
|
|
|
|
|
|
|
def _on_birdseye_mode_command(self, camera_name: str, payload: str) -> None:
|
|
|
|
"""Callback for birdseye mode topic."""
|
|
|
|
|
|
|
|
if payload not in ["CONTINUOUS", "MOTION", "OBJECTS"]:
|
|
|
|
logger.info(f"Invalid birdseye_mode command: {payload}")
|
|
|
|
return
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
birdseye_settings = self.config.cameras[camera_name].birdseye
|
|
|
|
|
|
|
|
if not birdseye_settings.enabled:
|
2023-10-26 13:20:55 +02:00
|
|
|
logger.info(f"Birdseye mode not enabled for {camera_name}")
|
|
|
|
return
|
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
birdseye_settings.mode = BirdseyeModeEnum(payload.lower())
|
|
|
|
logger.info(
|
|
|
|
f"Setting birdseye mode for {camera_name} to {birdseye_settings.mode}"
|
|
|
|
)
|
2023-10-26 13:20:55 +02:00
|
|
|
|
2024-02-19 14:26:59 +01:00
|
|
|
self.config_updater.publish(f"config/birdseye/{camera_name}", birdseye_settings)
|
2023-10-26 13:20:55 +02:00
|
|
|
self.publish(f"{camera_name}/birdseye_mode/state", payload, retain=True)
|