blakeblackshear.frigate/frigate/mqtt.py

400 lines
15 KiB
Python
Raw Permalink Normal View History

2021-06-14 14:31:13 +02:00
import json
2020-11-04 04:26:39 +01:00
import logging
2020-11-04 13:28:07 +01:00
import threading
2021-06-14 14:31:13 +02:00
from wsgiref.simple_server import make_server
2020-11-01 15:06:15 +01:00
2020-11-04 13:31:25 +01:00
import paho.mqtt.client as mqtt
2021-06-14 14:31:13 +02:00
from ws4py.server.wsgirefserver import (
WebSocketWSGIHandler,
WebSocketWSGIRequestHandler,
WSGIServer,
)
from ws4py.server.wsgiutils import WebSocketWSGIApplication
from ws4py.websocket import WebSocket
2020-11-04 13:31:25 +01:00
from frigate.config import FrigateConfig
2021-06-21 17:00:50 +02:00
from frigate.util import restart_frigate
2020-11-03 15:15:58 +01:00
2020-11-04 04:26:39 +01:00
logger = logging.getLogger(__name__)
2021-02-17 14:23:32 +01:00
2021-01-16 03:52:59 +01:00
def create_mqtt_client(config: FrigateConfig, camera_metrics):
mqtt_config = config.mqtt
def on_recordings_command(client, userdata, message):
payload = message.payload.decode()
logger.debug(f"on_recordings_toggle: {message.topic} {payload}")
2021-02-17 14:23:32 +01:00
camera_name = message.topic.split("/")[-3]
record_settings = config.cameras[camera_name].record
2020-12-24 05:16:36 +01:00
2021-02-17 14:23:32 +01:00
if payload == "ON":
if not record_settings.enabled:
logger.info(f"Turning on recordings for {camera_name} via mqtt")
record_settings.enabled = True
2021-02-17 14:23:32 +01:00
elif payload == "OFF":
if record_settings.enabled:
logger.info(f"Turning off recordings for {camera_name} via mqtt")
record_settings.enabled = False
2020-12-24 05:16:36 +01:00
else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}")
2021-01-19 14:41:17 +01:00
state_topic = f"{message.topic[:-4]}/state"
client.publish(state_topic, payload, retain=True)
2020-12-24 05:16:36 +01:00
def on_snapshots_command(client, userdata, message):
payload = message.payload.decode()
logger.debug(f"on_snapshots_toggle: {message.topic} {payload}")
2021-02-17 14:23:32 +01:00
camera_name = message.topic.split("/")[-3]
2020-12-24 05:16:36 +01:00
snapshots_settings = config.cameras[camera_name].snapshots
2021-02-17 14:23:32 +01:00
if payload == "ON":
2020-12-24 05:16:36 +01:00
if not snapshots_settings.enabled:
logger.info(f"Turning on snapshots for {camera_name} via mqtt")
snapshots_settings.enabled = True
2021-02-17 14:23:32 +01:00
elif payload == "OFF":
2020-12-24 05:16:36 +01:00
if snapshots_settings.enabled:
logger.info(f"Turning off snapshots for {camera_name} via mqtt")
snapshots_settings.enabled = False
else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}")
2021-01-19 14:41:17 +01:00
state_topic = f"{message.topic[:-4]}/state"
client.publish(state_topic, payload, retain=True)
2021-02-17 14:23:32 +01:00
2021-01-16 03:52:59 +01:00
def on_detect_command(client, userdata, message):
payload = message.payload.decode()
logger.debug(f"on_detect_toggle: {message.topic} {payload}")
2021-02-17 14:23:32 +01:00
camera_name = message.topic.split("/")[-3]
2021-01-16 03:52:59 +01:00
detect_settings = config.cameras[camera_name].detect
2021-02-17 14:23:32 +01:00
if payload == "ON":
2021-01-16 03:52:59 +01:00
if not camera_metrics[camera_name]["detection_enabled"].value:
logger.info(f"Turning on detection for {camera_name} via mqtt")
camera_metrics[camera_name]["detection_enabled"].value = True
detect_settings.enabled = True
if not camera_metrics[camera_name]["motion_enabled"].value:
logger.info(
f"Turning on motion for {camera_name} due to detection being enabled."
)
camera_metrics[camera_name]["motion_enabled"].value = True
2021-02-17 14:23:32 +01:00
elif payload == "OFF":
2021-01-16 03:52:59 +01:00
if camera_metrics[camera_name]["detection_enabled"].value:
logger.info(f"Turning off detection for {camera_name} via mqtt")
camera_metrics[camera_name]["detection_enabled"].value = False
detect_settings.enabled = False
2021-01-16 03:52:59 +01:00
else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}")
2021-01-19 14:41:17 +01:00
state_topic = f"{message.topic[:-4]}/state"
client.publish(state_topic, payload, retain=True)
def on_motion_command(client, userdata, message):
payload = message.payload.decode()
logger.debug(f"on_motion_toggle: {message.topic} {payload}")
camera_name = message.topic.split("/")[-3]
if payload == "ON":
if not camera_metrics[camera_name]["motion_enabled"].value:
logger.info(f"Turning on motion for {camera_name} via mqtt")
camera_metrics[camera_name]["motion_enabled"].value = True
elif payload == "OFF":
if camera_metrics[camera_name]["detection_enabled"].value:
logger.error(
f"Turning off motion is not allowed when detection is enabled."
)
return
if camera_metrics[camera_name]["motion_enabled"].value:
logger.info(f"Turning off motion for {camera_name} via mqtt")
camera_metrics[camera_name]["motion_enabled"].value = False
else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}")
state_topic = f"{message.topic[:-4]}/state"
client.publish(state_topic, payload, retain=True)
def on_improve_contrast_command(client, userdata, message):
payload = message.payload.decode()
logger.debug(f"on_improve_contrast_toggle: {message.topic} {payload}")
camera_name = message.topic.split("/")[-3]
motion_settings = config.cameras[camera_name].motion
if payload == "ON":
if not camera_metrics[camera_name]["improve_contrast_enabled"].value:
logger.info(f"Turning on improve contrast for {camera_name} via mqtt")
camera_metrics[camera_name]["improve_contrast_enabled"].value = True
motion_settings.improve_contrast = True
elif payload == "OFF":
if camera_metrics[camera_name]["improve_contrast_enabled"].value:
logger.info(f"Turning off improve contrast for {camera_name} via mqtt")
camera_metrics[camera_name]["improve_contrast_enabled"].value = False
motion_settings.improve_contrast = False
else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}")
state_topic = f"{message.topic[:-4]}/state"
2022-04-16 15:42:44 +02:00
client.publish(state_topic, payload, retain=True)
def on_motion_threshold_command(client, userdata, message):
try:
payload = int(message.payload.decode())
except ValueError:
logger.warning(
f"Received unsupported value at {message.topic}: {message.payload.decode()}"
)
return
logger.debug(f"on_motion_threshold_toggle: {message.topic} {payload}")
camera_name = message.topic.split("/")[-3]
motion_settings = config.cameras[camera_name].motion
logger.info(f"Setting motion threshold for {camera_name} via mqtt: {payload}")
camera_metrics[camera_name]["motion_threshold"].value = payload
motion_settings.threshold = payload
state_topic = f"{message.topic[:-4]}/state"
client.publish(state_topic, payload, retain=True)
def on_motion_contour_area_command(client, userdata, message):
try:
payload = int(message.payload.decode())
except ValueError:
logger.warning(
f"Received unsupported value at {message.topic}: {message.payload.decode()}"
)
return
logger.debug(f"on_motion_contour_area_toggle: {message.topic} {payload}")
camera_name = message.topic.split("/")[-3]
motion_settings = config.cameras[camera_name].motion
logger.info(
f"Setting motion contour area for {camera_name} via mqtt: {payload}"
)
camera_metrics[camera_name]["motion_contour_area"].value = payload
motion_settings.contour_area = payload
state_topic = f"{message.topic[:-4]}/state"
client.publish(state_topic, payload, retain=True)
2021-06-21 17:00:50 +02:00
def on_restart_command(client, userdata, message):
2021-07-06 14:22:17 +02:00
restart_frigate()
2021-06-21 17:00:50 +02:00
2020-11-01 15:06:15 +01:00
def on_connect(client, userdata, flags, rc):
2020-11-04 13:28:07 +01:00
threading.current_thread().name = "mqtt"
2020-11-01 15:06:15 +01:00
if rc != 0:
if rc == 3:
2022-04-16 15:42:44 +02:00
logger.error(
"Unable to connect to MQTT server: MQTT Server unavailable"
)
2020-11-01 15:06:15 +01:00
elif rc == 4:
2022-04-16 15:42:44 +02:00
logger.error(
"Unable to connect to MQTT server: MQTT Bad username or password"
)
2020-11-01 15:06:15 +01:00
elif rc == 5:
logger.error("Unable to connect to MQTT server: MQTT Not authorized")
2020-11-01 15:06:15 +01:00
else:
2021-02-17 14:23:32 +01:00
logger.error(
"Unable to connect to MQTT server: Connection refused. Error code: "
2021-02-17 14:23:32 +01:00
+ str(rc)
)
logger.debug("MQTT connected")
client.subscribe(f"{mqtt_config.topic_prefix}/#")
2021-02-17 14:23:32 +01:00
client.publish(mqtt_config.topic_prefix + "/available", "online", retain=True)
2021-02-17 14:23:32 +01:00
client = mqtt.Client(client_id=mqtt_config.client_id)
2020-11-01 15:06:15 +01:00
client.on_connect = on_connect
2021-02-17 14:23:32 +01:00
client.will_set(
mqtt_config.topic_prefix + "/available", payload="offline", qos=1, retain=True
)
# register callbacks
for name in config.cameras.keys():
2021-02-17 14:23:32 +01:00
client.message_callback_add(
f"{mqtt_config.topic_prefix}/{name}/recordings/set", on_recordings_command
2021-02-17 14:23:32 +01:00
)
client.message_callback_add(
f"{mqtt_config.topic_prefix}/{name}/snapshots/set", on_snapshots_command
)
client.message_callback_add(
f"{mqtt_config.topic_prefix}/{name}/detect/set", on_detect_command
)
client.message_callback_add(
f"{mqtt_config.topic_prefix}/{name}/motion/set", on_motion_command
)
client.message_callback_add(
2022-04-16 15:42:44 +02:00
f"{mqtt_config.topic_prefix}/{name}/improve_contrast/set",
on_improve_contrast_command,
)
client.message_callback_add(
f"{mqtt_config.topic_prefix}/{name}/motion_threshold/set",
on_motion_threshold_command,
)
client.message_callback_add(
f"{mqtt_config.topic_prefix}/{name}/motion_contour_area/set",
on_motion_contour_area_command,
)
2021-06-21 17:00:50 +02:00
client.message_callback_add(
f"{mqtt_config.topic_prefix}/restart", on_restart_command
)
if not mqtt_config.tls_ca_certs is None:
2021-06-14 14:31:13 +02:00
if (
not mqtt_config.tls_client_cert is None
and not mqtt_config.tls_client_key is None
):
client.tls_set(
mqtt_config.tls_ca_certs,
mqtt_config.tls_client_cert,
mqtt_config.tls_client_key,
)
else:
client.tls_set(mqtt_config.tls_ca_certs)
if not mqtt_config.tls_insecure is None:
client.tls_insecure_set(mqtt_config.tls_insecure)
if not mqtt_config.user is None:
client.username_pw_set(mqtt_config.user, password=mqtt_config.password)
2020-12-05 16:47:43 +01:00
try:
client.connect(mqtt_config.host, mqtt_config.port, 60)
2020-12-05 16:47:43 +01:00
except Exception as e:
logger.error(f"Unable to connect to MQTT server: {e}")
raise
2020-11-01 15:06:15 +01:00
client.loop_start()
2021-01-16 17:22:24 +01:00
for name in config.cameras.keys():
2021-02-17 14:23:32 +01:00
client.publish(
f"{mqtt_config.topic_prefix}/{name}/recordings/state",
"ON" if config.cameras[name].record.enabled else "OFF",
2021-02-17 14:23:32 +01:00
retain=True,
)
client.publish(
f"{mqtt_config.topic_prefix}/{name}/snapshots/state",
"ON" if config.cameras[name].snapshots.enabled else "OFF",
retain=True,
)
client.publish(
f"{mqtt_config.topic_prefix}/{name}/detect/state",
"ON" if config.cameras[name].detect.enabled else "OFF",
retain=True,
)
client.publish(
f"{mqtt_config.topic_prefix}/{name}/motion/state",
"ON",
retain=True,
)
client.publish(
f"{mqtt_config.topic_prefix}/{name}/improve_contrast/state",
"ON" if config.cameras[name].motion.improve_contrast else "OFF",
retain=True,
2022-04-16 15:42:44 +02:00
)
client.publish(
f"{mqtt_config.topic_prefix}/{name}/motion_threshold/state",
config.cameras[name].motion.threshold,
retain=True,
)
client.publish(
f"{mqtt_config.topic_prefix}/{name}/motion_contour_area/state",
config.cameras[name].motion.contour_area,
retain=True,
)
2022-07-15 15:05:35 +02:00
client.publish(
f"{mqtt_config.topic_prefix}/{name}/motion",
"OFF",
retain=False,
)
2020-11-04 13:31:25 +01:00
return client
2021-06-14 14:31:13 +02:00
class MqttSocketRelay:
def __init__(self, mqtt_client, topic_prefix):
self.mqtt_client = mqtt_client
self.topic_prefix = topic_prefix
def start(self):
class MqttWebSocket(WebSocket):
topic_prefix = self.topic_prefix
mqtt_client = self.mqtt_client
def received_message(self, message):
try:
json_message = json.loads(message.data.decode("utf-8"))
json_message = {
"topic": f"{self.topic_prefix}/{json_message['topic']}",
2021-07-06 14:22:17 +02:00
"payload": json_message.get("payload"),
2021-06-14 14:31:13 +02:00
"retain": json_message.get("retain", False),
}
except Exception as e:
logger.warning("Unable to parse websocket message as valid json.")
return
logger.debug(
f"Publishing mqtt message from websockets at {json_message['topic']}."
)
self.mqtt_client.publish(
json_message["topic"],
json_message["payload"],
retain=json_message["retain"],
)
# 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=MqttWebSocket),
)
self.websocket_server.initialize_websockets_manager()
self.websocket_thread = threading.Thread(
target=self.websocket_server.serve_forever
)
def send(client, userdata, message):
"""Sends mqtt messages to clients."""
try:
logger.debug(f"Received mqtt message on {message.topic}.")
ws_message = json.dumps(
{
"topic": message.topic.replace(f"{self.topic_prefix}/", ""),
"payload": message.payload.decode(),
}
)
except Exception as e:
# if the payload can't be decoded don't relay to clients
logger.debug(
f"MQTT payload for {message.topic} wasn't text. Skipping..."
)
return
self.websocket_server.manager.broadcast(ws_message)
self.mqtt_client.message_callback_add(f"{self.topic_prefix}/#", send)
self.websocket_thread.start()
def stop(self):
self.websocket_server.manager.close_all()
self.websocket_server.manager.stop()
self.websocket_server.manager.join()
self.websocket_server.shutdown()
self.websocket_thread.join()