mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-08-07 03:11:15 +03:00
Abstract MQTT from communication and make mqtt optional (#4462)
* Add option for mqtt config * Setup communication layer * Have a dispatcher which is responsible for handling and sending messages * Move mqtt to communication * Separate ws communications module * Make ws client conform to communicator * Cleanup imports * Migrate to new dispatcher * Clean up * Need to set topic prefix * Remove references to mqtt in dispatcher * Don't start mqtt until dispatcher is subscribed * Cleanup * Shorten package * Formatting * Remove unused * Cleanup * Rename mqtt to ws on web * Fix ws mypy * Fix mypy * Reformat * Cleanup if/else chain * Catch bad set commands
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
"""Handle communication between frigate and other applications."""
|
||||
|
||||
import logging
|
||||
|
||||
from typing import Any, Callable
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from frigate.config import FrigateConfig
|
||||
from frigate.types import CameraMetricsTypes
|
||||
from frigate.util import restart_frigate
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
class Dispatcher:
|
||||
"""Handle communication between frigate and communicators."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: FrigateConfig,
|
||||
camera_metrics: dict[str, CameraMetricsTypes],
|
||||
communicators: list[Communicator],
|
||||
) -> None:
|
||||
self.config = config
|
||||
self.camera_metrics = camera_metrics
|
||||
self.comms = communicators
|
||||
|
||||
for comm in self.comms:
|
||||
comm.subscribe(self._receive)
|
||||
|
||||
self._camera_settings_handlers: dict[str, Callable] = {
|
||||
"detect": self._on_detect_command,
|
||||
"improve_contrast": self._on_motion_improve_contrast_command,
|
||||
"motion": self._on_motion_command,
|
||||
"motion_contour_area": self._on_motion_contour_area_command,
|
||||
"motion_threshold": self._on_motion_threshold_command,
|
||||
"recording": self._on_recordings_command,
|
||||
"snapshots": self._on_snapshots_command,
|
||||
}
|
||||
|
||||
def _receive(self, topic: str, payload: str) -> None:
|
||||
"""Handle receiving of payload from communicators."""
|
||||
if topic.endswith("set"):
|
||||
try:
|
||||
camera_name = topic.split("/")[-3]
|
||||
command = topic.split("/")[-2]
|
||||
self._camera_settings_handlers[command](camera_name, payload)
|
||||
except Exception as e:
|
||||
logger.error(f"Received invalid set command: {topic}")
|
||||
return
|
||||
elif topic == "restart":
|
||||
restart_frigate()
|
||||
|
||||
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)
|
||||
|
||||
def _on_detect_command(self, camera_name: str, payload: str) -> None:
|
||||
"""Callback for detect topic."""
|
||||
detect_settings = self.config.cameras[camera_name].detect
|
||||
|
||||
if payload == "ON":
|
||||
if not self.camera_metrics[camera_name]["detection_enabled"].value:
|
||||
logger.info(f"Turning on detection for {camera_name}")
|
||||
self.camera_metrics[camera_name]["detection_enabled"].value = True
|
||||
detect_settings.enabled = True
|
||||
|
||||
if not self.camera_metrics[camera_name]["motion_enabled"].value:
|
||||
logger.info(
|
||||
f"Turning on motion for {camera_name} due to detection being enabled."
|
||||
)
|
||||
self.camera_metrics[camera_name]["motion_enabled"].value = True
|
||||
self.publish(f"{camera_name}/motion/state", payload, retain=True)
|
||||
elif payload == "OFF":
|
||||
if self.camera_metrics[camera_name]["detection_enabled"].value:
|
||||
logger.info(f"Turning off detection for {camera_name}")
|
||||
self.camera_metrics[camera_name]["detection_enabled"].value = False
|
||||
detect_settings.enabled = False
|
||||
|
||||
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."""
|
||||
if payload == "ON":
|
||||
if not self.camera_metrics[camera_name]["motion_enabled"].value:
|
||||
logger.info(f"Turning on motion for {camera_name}")
|
||||
self.camera_metrics[camera_name]["motion_enabled"].value = True
|
||||
elif payload == "OFF":
|
||||
if self.camera_metrics[camera_name]["detection_enabled"].value:
|
||||
logger.error(
|
||||
f"Turning off motion is not allowed when detection is enabled."
|
||||
)
|
||||
return
|
||||
|
||||
if self.camera_metrics[camera_name]["motion_enabled"].value:
|
||||
logger.info(f"Turning off motion for {camera_name}")
|
||||
self.camera_metrics[camera_name]["motion_enabled"].value = False
|
||||
|
||||
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":
|
||||
if not self.camera_metrics[camera_name]["improve_contrast_enabled"].value:
|
||||
logger.info(f"Turning on improve contrast for {camera_name}")
|
||||
self.camera_metrics[camera_name][
|
||||
"improve_contrast_enabled"
|
||||
].value = True
|
||||
motion_settings.improve_contrast = True # type: ignore[union-attr]
|
||||
elif payload == "OFF":
|
||||
if self.camera_metrics[camera_name]["improve_contrast_enabled"].value:
|
||||
logger.info(f"Turning off improve contrast for {camera_name}")
|
||||
self.camera_metrics[camera_name][
|
||||
"improve_contrast_enabled"
|
||||
].value = False
|
||||
motion_settings.improve_contrast = False # type: ignore[union-attr]
|
||||
|
||||
self.publish(f"{camera_name}/improve_contrast/state", payload, retain=True)
|
||||
|
||||
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}")
|
||||
self.camera_metrics[camera_name]["motion_contour_area"].value = payload
|
||||
motion_settings.contour_area = payload # type: ignore[union-attr]
|
||||
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}")
|
||||
self.camera_metrics[camera_name]["motion_threshold"].value = payload
|
||||
motion_settings.threshold = payload # type: ignore[union-attr]
|
||||
self.publish(f"{camera_name}/motion_threshold/state", payload, retain=True)
|
||||
|
||||
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":
|
||||
if not record_settings.enabled:
|
||||
logger.info(f"Turning on recordings for {camera_name}")
|
||||
record_settings.enabled = True
|
||||
elif payload == "OFF":
|
||||
if record_settings.enabled:
|
||||
logger.info(f"Turning off recordings for {camera_name}")
|
||||
record_settings.enabled = False
|
||||
|
||||
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)
|
||||
@@ -0,0 +1,201 @@
|
||||
import logging
|
||||
import threading
|
||||
|
||||
from typing import Any, Callable
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from frigate.comms.dispatcher import Communicator
|
||||
from frigate.config import FrigateConfig
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MqttClient(Communicator): # type: ignore[misc]
|
||||
"""Frigate wrapper for mqtt client."""
|
||||
|
||||
def __init__(self, config: FrigateConfig) -> None:
|
||||
self.config = config
|
||||
self.mqtt_config = config.mqtt
|
||||
self.connected: bool = False
|
||||
|
||||
def subscribe(self, receiver: Callable) -> None:
|
||||
"""Wrapper for allowing dispatcher to subscribe."""
|
||||
self._dispatcher = receiver
|
||||
self._start()
|
||||
|
||||
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
|
||||
"""Wrapper for publishing when client is in valid state."""
|
||||
if not self.connected:
|
||||
logger.error(f"Unable to publish to {topic}: client is not connected")
|
||||
return
|
||||
|
||||
self.client.publish(
|
||||
f"{self.mqtt_config.topic_prefix}/{topic}", payload, retain=retain
|
||||
)
|
||||
|
||||
def _set_initial_topics(self) -> None:
|
||||
"""Set initial state topics."""
|
||||
for camera_name, camera in self.config.cameras.items():
|
||||
self.publish(
|
||||
f"{camera_name}/recordings/state",
|
||||
"ON" if camera.record.enabled else "OFF",
|
||||
retain=True,
|
||||
)
|
||||
self.publish(
|
||||
f"{camera_name}/snapshots/state",
|
||||
"ON" if camera.snapshots.enabled else "OFF",
|
||||
retain=True,
|
||||
)
|
||||
self.publish(
|
||||
f"{camera_name}/detect/state",
|
||||
"ON" if camera.detect.enabled else "OFF",
|
||||
retain=True,
|
||||
)
|
||||
self.publish(
|
||||
f"{camera_name}/motion/state",
|
||||
"ON",
|
||||
retain=True,
|
||||
)
|
||||
self.publish(
|
||||
f"{camera_name}/improve_contrast/state",
|
||||
"ON" if camera.motion.improve_contrast else "OFF", # type: ignore[union-attr]
|
||||
retain=True,
|
||||
)
|
||||
self.publish(
|
||||
f"{camera_name}/motion_threshold/state",
|
||||
camera.motion.threshold, # type: ignore[union-attr]
|
||||
retain=True,
|
||||
)
|
||||
self.publish(
|
||||
f"{camera_name}/motion_contour_area/state",
|
||||
camera.motion.contour_area, # type: ignore[union-attr]
|
||||
retain=True,
|
||||
)
|
||||
self.publish(
|
||||
f"{camera_name}/motion",
|
||||
"OFF",
|
||||
retain=False,
|
||||
)
|
||||
|
||||
self.publish("available", "online", retain=True)
|
||||
|
||||
def on_mqtt_command(
|
||||
self, client: mqtt.Client, userdata: Any, message: mqtt.MQTTMessage
|
||||
) -> None:
|
||||
self._dispatcher(
|
||||
message.topic.replace(f"{self.mqtt_config.topic_prefix}/", ""),
|
||||
message.payload.decode(),
|
||||
)
|
||||
|
||||
def _on_connect(
|
||||
self,
|
||||
client: mqtt.Client,
|
||||
userdata: Any,
|
||||
flags: Any,
|
||||
rc: mqtt.ReasonCodes,
|
||||
) -> None:
|
||||
"""Mqtt connection callback."""
|
||||
threading.current_thread().name = "mqtt"
|
||||
if rc != 0:
|
||||
if rc == 3:
|
||||
logger.error(
|
||||
"Unable to connect to MQTT server: MQTT Server unavailable"
|
||||
)
|
||||
elif rc == 4:
|
||||
logger.error(
|
||||
"Unable to connect to MQTT server: MQTT Bad username or password"
|
||||
)
|
||||
elif rc == 5:
|
||||
logger.error("Unable to connect to MQTT server: MQTT Not authorized")
|
||||
else:
|
||||
logger.error(
|
||||
"Unable to connect to MQTT server: Connection refused. Error code: "
|
||||
+ str(rc)
|
||||
)
|
||||
|
||||
self.connected = True
|
||||
logger.debug("MQTT connected")
|
||||
client.subscribe(f"{self.mqtt_config.topic_prefix}/#")
|
||||
self._set_initial_topics()
|
||||
|
||||
def _on_disconnect(
|
||||
self, client: mqtt.Client, userdata: Any, flags: Any, rc: mqtt
|
||||
) -> None:
|
||||
"""Mqtt disconnection callback."""
|
||||
self.connected = False
|
||||
logger.error("MQTT disconnected")
|
||||
|
||||
def _start(self) -> None:
|
||||
"""Start mqtt client."""
|
||||
self.client = mqtt.Client(client_id=self.mqtt_config.client_id)
|
||||
self.client.on_connect = self._on_connect
|
||||
self.client.will_set(
|
||||
self.mqtt_config.topic_prefix + "/available",
|
||||
payload="offline",
|
||||
qos=1,
|
||||
retain=True,
|
||||
)
|
||||
|
||||
# register callbacks
|
||||
for name in self.config.cameras.keys():
|
||||
self.client.message_callback_add(
|
||||
f"{self.mqtt_config.topic_prefix}/{name}/recordings/set",
|
||||
self.on_mqtt_command,
|
||||
)
|
||||
self.client.message_callback_add(
|
||||
f"{self.mqtt_config.topic_prefix}/{name}/snapshots/set",
|
||||
self.on_mqtt_command,
|
||||
)
|
||||
self.client.message_callback_add(
|
||||
f"{self.mqtt_config.topic_prefix}/{name}/detect/set",
|
||||
self.on_mqtt_command,
|
||||
)
|
||||
self.client.message_callback_add(
|
||||
f"{self.mqtt_config.topic_prefix}/{name}/motion/set",
|
||||
self.on_mqtt_command,
|
||||
)
|
||||
self.client.message_callback_add(
|
||||
f"{self.mqtt_config.topic_prefix}/{name}/improve_contrast/set",
|
||||
self.on_mqtt_command,
|
||||
)
|
||||
self.client.message_callback_add(
|
||||
f"{self.mqtt_config.topic_prefix}/{name}/motion_threshold/set",
|
||||
self.on_mqtt_command,
|
||||
)
|
||||
self.client.message_callback_add(
|
||||
f"{self.mqtt_config.topic_prefix}/{name}/motion_contour_area/set",
|
||||
self.on_mqtt_command,
|
||||
)
|
||||
|
||||
self.client.message_callback_add(
|
||||
f"{self.mqtt_config.topic_prefix}/restart", self.on_mqtt_command
|
||||
)
|
||||
|
||||
if not self.mqtt_config.tls_ca_certs is None:
|
||||
if (
|
||||
not self.mqtt_config.tls_client_cert is None
|
||||
and not self.mqtt_config.tls_client_key is None
|
||||
):
|
||||
self.client.tls_set(
|
||||
self.mqtt_config.tls_ca_certs,
|
||||
self.mqtt_config.tls_client_cert,
|
||||
self.mqtt_config.tls_client_key,
|
||||
)
|
||||
else:
|
||||
self.client.tls_set(self.mqtt_config.tls_ca_certs)
|
||||
if not self.mqtt_config.tls_insecure is None:
|
||||
self.client.tls_insecure_set(self.mqtt_config.tls_insecure)
|
||||
if not self.mqtt_config.user is None:
|
||||
self.client.username_pw_set(
|
||||
self.mqtt_config.user, password=self.mqtt_config.password
|
||||
)
|
||||
try:
|
||||
# https://stackoverflow.com/a/55390477
|
||||
# with connect_async, retries are handled automatically
|
||||
self.client.connect_async(self.mqtt_config.host, self.mqtt_config.port, 60)
|
||||
self.client.loop_start()
|
||||
except Exception as e:
|
||||
logger.error(f"Unable to connect to MQTT server: {e}")
|
||||
return
|
||||
@@ -0,0 +1,97 @@
|
||||
"""Websocket communicator."""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
|
||||
from typing import Callable
|
||||
|
||||
from wsgiref.simple_server import make_server
|
||||
from ws4py.server.wsgirefserver import (
|
||||
WebSocketWSGIHandler,
|
||||
WebSocketWSGIRequestHandler,
|
||||
WSGIServer,
|
||||
)
|
||||
from ws4py.server.wsgiutils import WebSocketWSGIApplication
|
||||
from ws4py.websocket import WebSocket
|
||||
|
||||
from frigate.comms.dispatcher import Communicator
|
||||
from frigate.config import FrigateConfig
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class WebSocketClient(Communicator): # type: ignore[misc]
|
||||
"""Frigate wrapper for ws client."""
|
||||
|
||||
def __init__(self, config: FrigateConfig) -> None:
|
||||
self.config = config
|
||||
|
||||
def subscribe(self, receiver: Callable) -> None:
|
||||
self._dispatcher = receiver
|
||||
self.start()
|
||||
|
||||
def start(self) -> None:
|
||||
"""Start the websocket client."""
|
||||
|
||||
class _WebSocketHandler(WebSocket): # type: ignore[misc]
|
||||
receiver = self._dispatcher
|
||||
|
||||
def received_message(self, message: WebSocket.received_message) -> None:
|
||||
try:
|
||||
json_message = json.loads(message.data.decode("utf-8"))
|
||||
json_message = {
|
||||
"topic": json_message.get("topic"),
|
||||
"payload": json_message.get("payload"),
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"Unable to parse websocket message as valid json: {message.data.decode('utf-8')}"
|
||||
)
|
||||
return
|
||||
|
||||
logger.debug(
|
||||
f"Publishing mqtt message from websockets at {json_message['topic']}."
|
||||
)
|
||||
self.receiver(
|
||||
json_message["topic"],
|
||||
json_message["payload"],
|
||||
)
|
||||
|
||||
# start a websocket server on 5002
|
||||
WebSocketWSGIHandler.http_version = "1.1"
|
||||
self.websocket_server = make_server(
|
||||
"127.0.0.1",
|
||||
5002,
|
||||
server_class=WSGIServer,
|
||||
handler_class=WebSocketWSGIRequestHandler,
|
||||
app=WebSocketWSGIApplication(handler_cls=_WebSocketHandler),
|
||||
)
|
||||
self.websocket_server.initialize_websockets_manager()
|
||||
self.websocket_thread = threading.Thread(
|
||||
target=self.websocket_server.serve_forever
|
||||
)
|
||||
self.websocket_thread.start()
|
||||
|
||||
def publish(self, topic: str, payload: str, _: bool) -> None:
|
||||
try:
|
||||
ws_message = json.dumps(
|
||||
{
|
||||
"topic": topic,
|
||||
"payload": payload,
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
# if the payload can't be decoded don't relay to clients
|
||||
logger.debug(f"payload for {topic} wasn't text. Skipping...")
|
||||
return
|
||||
|
||||
self.websocket_server.manager.broadcast(ws_message)
|
||||
|
||||
def stop(self) -> None:
|
||||
self.websocket_server.manager.close_all()
|
||||
self.websocket_server.manager.stop()
|
||||
self.websocket_server.manager.join()
|
||||
self.websocket_server.shutdown()
|
||||
self.websocket_thread.join()
|
||||
Reference in New Issue
Block a user