This commit is contained in:
Nick Mowen 2022-11-22 13:51:08 -07:00
parent 5cf3dc4798
commit 602db3dbf8
3 changed files with 32 additions and 27 deletions

View File

@ -177,7 +177,6 @@ class FrigateApp:
comms.append(MqttClient(self.config)) comms.append(MqttClient(self.config))
self.ws_client = WebSocketClient(self.config) self.ws_client = WebSocketClient(self.config)
self.ws_client.start()
comms.append(self.ws_client) comms.append(self.ws_client)
self.dispatcher = Dispatcher(self.config, self.camera_metrics, comms) self.dispatcher = Dispatcher(self.config, self.camera_metrics, comms)
@ -354,7 +353,7 @@ class FrigateApp:
self.set_log_levels() self.set_log_levels()
self.init_queues() self.init_queues()
self.init_database() self.init_database()
self.init_dispatcher self.init_dispatcher()
except Exception as e: except Exception as e:
print(e) print(e)
self.log_process.terminate() self.log_process.terminate()

View File

@ -56,7 +56,7 @@ class MqttClient(Communicator):
) )
self.client.message_callback_add( self.client.message_callback_add(
f"{self.mqtt_config.topic_prefix}/restart", self.on_restart_command f"{self.mqtt_config.topic_prefix}/restart", self.on_mqtt_command
) )
def publish(self, topic: str, payload, retain: bool = False) -> None: def publish(self, topic: str, payload, retain: bool = False) -> None:

View File

@ -19,7 +19,22 @@ from frigate.config import FrigateConfig
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class WebSocketClient(Communicator):
"""Frigate wrapper for ws client."""
def __init__(self, config: FrigateConfig) -> None:
self.config = config
def subscribe(self, receiver) -> None:
self._dispatcher = receiver
self.start()
def start(self):
"""Start the websocket client."""
class _WebSocketHandler(WebSocket): class _WebSocketHandler(WebSocket):
receiver = self._dispatcher
def received_message(self, message): def received_message(self, message):
try: try:
json_message = json.loads(message.data.decode("utf-8")) json_message = json.loads(message.data.decode("utf-8"))
@ -35,21 +50,12 @@ class _WebSocketHandler(WebSocket):
logger.debug( logger.debug(
f"Publishing mqtt message from websockets at {json_message['topic']}." f"Publishing mqtt message from websockets at {json_message['topic']}."
) )
self.publish( self.receiver(
json_message["topic"], json_message["topic"],
json_message["payload"], json_message["payload"],
retain=json_message["retain"], retain=json_message["retain"],
) )
class WebSocketClient(Communicator):
"""Frigate wrapper for ws client."""
def __init__(self, config: FrigateConfig) -> None:
self.config = config
def start(self):
# start a websocket server on 5002 # start a websocket server on 5002
WebSocketWSGIHandler.http_version = "1.1" WebSocketWSGIHandler.http_version = "1.1"
self.websocket_server = make_server( self.websocket_server = make_server(
@ -65,7 +71,7 @@ class WebSocketClient(Communicator):
) )
self.websocket_thread.start() self.websocket_thread.start()
def publish(self, topic: str, payload: str) -> None: def publish(self, topic: str, payload: str, _) -> None:
try: try:
ws_message = json.dumps( ws_message = json.dumps(
{ {