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:
Nicolas Mowen
2022-11-23 19:03:20 -07:00
committed by GitHub
parent 370276a7b6
commit 6c0978498d
23 changed files with 594 additions and 560 deletions

View File

@@ -13,14 +13,16 @@ from peewee_migrate import Router
from playhouse.sqlite_ext import SqliteExtDatabase
from playhouse.sqliteq import SqliteQueueDatabase
from frigate.config import DetectorTypeEnum, FrigateConfig
from frigate.comms.dispatcher import Communicator, Dispatcher
from frigate.comms.mqtt import MqttClient
from frigate.comms.ws import WebSocketClient
from frigate.config import FrigateConfig
from frigate.const import CACHE_DIR, CLIPS_DIR, RECORD_DIR
from frigate.object_detection import ObjectDetectProcess
from frigate.events import EventCleanup, EventProcessor
from frigate.http import create_app
from frigate.log import log_process, root_configurer
from frigate.models import Event, Recordings
from frigate.mqtt import FrigateMqttClient, MqttSocketRelay
from frigate.object_processing import TrackedObjectProcessor
from frigate.output import output_frames
from frigate.plus import PlusApi
@@ -168,14 +170,15 @@ class FrigateApp:
self.restream = RestreamApi(self.config)
self.restream.add_cameras()
def init_mqtt(self) -> None:
self.mqtt_client = FrigateMqttClient(self.config, self.camera_metrics)
def init_dispatcher(self) -> None:
comms: list[Communicator] = []
def start_mqtt_relay(self) -> None:
self.mqtt_relay = MqttSocketRelay(
self.mqtt_client, self.config.mqtt.topic_prefix
)
self.mqtt_relay.start()
if self.config.mqtt.enabled:
comms.append(MqttClient(self.config))
self.ws_client = WebSocketClient(self.config)
comms.append(self.ws_client)
self.dispatcher = Dispatcher(self.config, self.camera_metrics, comms)
def start_detectors(self) -> None:
for name in self.config.cameras.keys():
@@ -214,7 +217,7 @@ class FrigateApp:
def start_detected_frames_processor(self) -> None:
self.detected_frames_processor = TrackedObjectProcessor(
self.config,
self.mqtt_client,
self.dispatcher,
self.config.mqtt.topic_prefix,
self.detected_frames_queue,
self.event_queue,
@@ -312,7 +315,7 @@ class FrigateApp:
self.stats_emitter = StatsEmitter(
self.config,
self.stats_tracking,
self.mqtt_client,
self.dispatcher,
self.config.mqtt.topic_prefix,
self.stop_event,
)
@@ -350,7 +353,7 @@ class FrigateApp:
self.set_log_levels()
self.init_queues()
self.init_database()
self.init_mqtt()
self.init_dispatcher()
except Exception as e:
print(e)
self.log_process.terminate()
@@ -363,7 +366,6 @@ class FrigateApp:
self.start_camera_capture_processes()
self.init_stats()
self.init_web_server()
self.start_mqtt_relay()
self.start_event_processor()
self.start_event_cleanup()
self.start_recording_maintainer()
@@ -390,7 +392,7 @@ class FrigateApp:
logger.info(f"Stopping...")
self.stop_event.set()
self.mqtt_relay.stop()
self.ws_client.stop()
self.detected_frames_processor.join()
self.event_processor.join()
self.event_cleanup.join()