Add fields

This commit is contained in:
Nicolas Mowen 2024-07-19 14:03:06 -06:00
parent c918382d19
commit 4fd9a38be7
2 changed files with 19 additions and 15 deletions

View File

@ -28,7 +28,6 @@ class FirebaseClient(Communicator): # type: ignore[misc]
def publish(self, topic: str, payload: Any, retain: bool = False) -> None:
"""Wrapper for publishing when client is in valid state."""
logger.info(f"got a message on {topic}")
if topic == "reviews":
self.messenger.send_message(json.loads(payload))
@ -44,7 +43,16 @@ class FirebaseMessenger(threading.Thread):
self.stop_event = stop_event
def send_message(self, payload: dict[str, any]) -> None:
# Only notify for alerts
if payload["after"]["severity"] != "alert":
return
state = payload["type"]
# Don't notify if message is an update and important fields don't have an update
if state == "update" and len(payload["before"]["objects"]) == len(payload["after"]["objects"]) and len(payload["before"]["zones"]) == len(payload["after"]["zones"]):
return
sorted_objects: set[str] = set()
for obj in payload["after"]["data"]["objects"]:
@ -53,12 +61,10 @@ class FirebaseMessenger(threading.Thread):
sorted_objects.update(payload["after"]["data"]["sub_labels"])
title = f"{', '.join(sorted_objects).replace('_', ' ')}{' was' if state == 'end' else ''} detected in {', '.join(payload['after']['data']['zones']).replace('_', ' ')}"
logger.info(f"sending message with title {title}")
message = messaging.MulticastMessage(
notification=messaging.Notification(
title=f"{', '.join(sorted_objects).replace('_', ' ')}{' was' if state == 'end' else ''} detected in {', '.join(payload['after']['data']['zones']).replace('_', ' ')}",
body=f"Detected on {payload['after']['camera']}",
title=f"{', '.join(sorted_objects).replace('_', ' ').title()}{' was' if state == 'end' else ''} detected in {', '.join(payload['after']['data']['zones']).replace('_', ' ').title()}",
body=f"Detected on {payload['after']['camera'].replace('_', ' ').title()}",
),
tokens=[
"cNNicZp6S92qn4kAVJnzd7:APA91bGv-MvDmNoZ2xqJTkPyCTmyv2WG0tfwIqWUuNtq3SXlpQJpdPCCjTEehOLDa0Yphv__KdxOQYEfaFvYfTW2qQevX-tSnRCVa_sJazQ_rfTervpo_zBVJD1T5GfYaY6kr41Wr_fP"
@ -67,16 +73,12 @@ class FirebaseMessenger(threading.Thread):
messaging.send_multicast(message)
def run(self) -> None:
logger.info("Starting notifications setup")
try:
firebase_admin.get_app()
except ValueError:
cred = credentials.Certificate("/config/firebase-priv-key.json")
firebase_admin.initialize_app(credential=cred)
logger.info("finished notifications startup")
while self.stop_event.wait(0.1):
# TODO check for a delete invalid tokens
pass

View File

@ -1,8 +1,8 @@
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js');
importScripts("https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js");
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
@ -23,15 +23,17 @@ const messaging = firebase.messaging();
messaging.onBackgroundMessage((payload) => {
console.log(
'[firebase-messaging-sw.js] Received background message ',
"[firebase-messaging-sw.js] Received background message ",
payload
);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
body: payload.notification.body,
icon: '/images/maskable-icon.png',
};
self.registration.showNotification(notificationTitle, notificationOptions);
self.registration.showNotification(
payload.notification.title,
notificationOptions
);
});