Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 committed Jul 20, 2024
1 parent e3a6218 commit b5b73dd
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 863 deletions.
4 changes: 2 additions & 2 deletions frigate/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
from frigate.api.auth import hash_password
from frigate.comms.config_updater import ConfigPublisher
from frigate.comms.dispatcher import Communicator, Dispatcher
from frigate.comms.firebase import FirebaseClient
from frigate.comms.inter_process import InterProcessCommunicator
from frigate.comms.mqtt import MqttClient
from frigate.comms.webpush import WebPushClient
from frigate.comms.ws import WebSocketClient
from frigate.comms.zmq_proxy import ZmqProxy
from frigate.config import FrigateConfig
Expand Down Expand Up @@ -403,7 +403,7 @@ def init_dispatcher(self) -> None:
comms.append(MqttClient(self.config))

if self.config.notifications.enabled:
comms.append(FirebaseClient(self.config, self.stop_event))
comms.append(WebPushClient(self.config))

comms.append(WebSocketClient(self.config))
comms.append(self.inter_process_communicator)
Expand Down
46 changes: 15 additions & 31 deletions frigate/comms/firebase.py → frigate/comms/webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import json
import logging
import threading
from multiprocessing.synchronize import Event as MpEvent
from typing import Any, Callable

from frigate.comms.dispatcher import Communicator
Expand All @@ -13,12 +11,20 @@
logger = logging.getLogger(__name__)


class FirebaseClient(Communicator): # type: ignore[misc]
class WebPushClient(Communicator): # type: ignore[misc]
"""Frigate wrapper for firebase client."""

def __init__(self, config: FrigateConfig, stop_event: MpEvent) -> None:
self.messenger = FirebaseMessenger(config, stop_event)
self.messenger.start()
def __init__(self, config: FrigateConfig) -> None:
self.config = config
# TODO check for VAPID key

self.tokens = []
self.invalid_tokens = []

users: list[User] = User.select(User.notification_tokens).dicts().iterator()

for user in users:
self.tokens.extend(user["notification_tokens"])

def subscribe(self, receiver: Callable) -> None:
"""Wrapper for allowing dispatcher to subscribe."""
Expand All @@ -27,18 +33,7 @@ def subscribe(self, receiver: Callable) -> None:
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
"""Wrapper for publishing when client is in valid state."""
if topic == "reviews":
self.messenger.send_message(json.loads(payload))

def stop(self) -> None:
pass


class FirebaseMessenger(threading.Thread):
def __init__(self, config: FrigateConfig, stop_event: MpEvent) -> None:
threading.Thread.__init__(self)
self.name = "firebase_messenger"
self.config = config
self.stop_event = stop_event
self.send_message(json.loads(payload))

def send_message(self, payload: dict[str, any]) -> None:
# Only notify for alerts
Expand Down Expand Up @@ -71,16 +66,5 @@ def send_message(self, payload: dict[str, any]) -> None:
direct_url = f"{self.config.notifications.base_url}/review?id={reviewId}"
image = f'{self.config.notifications.base_url}{payload["after"]["thumb_path"].replace("/media/frigate", "")}'

def run(self) -> None:
self.tokens = []
self.invalid_tokens = []

users: list[User] = User.select(User.notification_tokens).dicts().iterator()

for user in users:
self.tokens.extend(user["notification_tokens"])

while self.stop_event.wait(0.1):
# TODO check for and delete invalid tokens
if self.invalid_tokens:
pass
def stop(self) -> None:
pass
Loading

0 comments on commit b5b73dd

Please sign in to comment.