Updated notifications to allow required zones
This commit is contained in:
parent
a7a8089939
commit
fa157287e1
|
@ -2,36 +2,55 @@ import os
|
||||||
import json
|
import json
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
import requests
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
from ntfy import send_rate_limited_notification
|
from ntfy import send_rate_limited_notification
|
||||||
|
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
class FrigateEventListener:
|
||||||
print(f'Connected with return code {rc}')
|
def __init__(self, username, password):
|
||||||
client.subscribe('frigate/events')
|
self.client = mqtt.Client()
|
||||||
|
self.client.username_pw_set(username, password=password)
|
||||||
|
self.client.on_connect = self._on_connect
|
||||||
|
self.client.on_message = self._on_message
|
||||||
|
|
||||||
|
frigate_api_response = requests.get('http://frigate:5000/api/config')
|
||||||
|
frigate_api_response.raise_for_status()
|
||||||
|
frigate_config = json.loads(frigate_api_response.content)
|
||||||
|
self.camera_zones = {camera_name: {required_zone: {object_label for object_label in camera_config['zones'][required_zone]['objects']} for required_zone in camera_config['record']['events']['required_zones']} for camera_name, camera_config in frigate_config['cameras'].items()}
|
||||||
|
|
||||||
def on_message(client, userdata, message):
|
def start(self):
|
||||||
payload = json.loads(message.payload.decode())
|
self.client.connect(host='mqtt', port=1883)
|
||||||
if payload['type'] == 'new' and payload['after']['label'] == 'person':
|
self.client.loop_forever()
|
||||||
event_id = payload['after']['id']
|
|
||||||
|
def _on_connect(self, client, userdata, flags, rc):
|
||||||
|
print(f'Connected with return code {rc}')
|
||||||
|
client.subscribe('frigate/events')
|
||||||
|
|
||||||
|
def _on_message(self, client, userdata, message):
|
||||||
|
payload = json.loads(message.payload.decode())
|
||||||
camera = payload['after']['camera']
|
camera = payload['after']['camera']
|
||||||
object_label = payload['after']['label']
|
object_label = payload['after']['label']
|
||||||
score = payload['after']['top_score']
|
|
||||||
|
|
||||||
send_rate_limited_notification(event_id, camera, object_label, score)
|
if not self.camera_zones[camera]:
|
||||||
|
# No required zones, send notification on receipt of new event
|
||||||
|
if payload['type'] == 'new' and object_label == 'person':
|
||||||
|
event_id = payload['after']['id']
|
||||||
|
score = payload['after']['top_score']
|
||||||
|
|
||||||
|
send_rate_limited_notification(event_id, camera, object_label, score)
|
||||||
|
else:
|
||||||
|
new_zones = set(payload['after']['entered_zones']) - set(payload['before']['entered_zones'])
|
||||||
|
for zone in new_zones:
|
||||||
|
if zone in self.camera_zones[camera] and (not self.camera_zones[camera][zone] or object_label in self.camera_zones[camera][zone]):
|
||||||
|
event_id = payload['after']['id']
|
||||||
|
score = payload['after']['top_score']
|
||||||
|
|
||||||
def subscribe():
|
send_rate_limited_notification(event_id, camera, object_label, score)
|
||||||
client = mqtt.Client()
|
break
|
||||||
client.username_pw_set(os.environ['MQTT_USERNAME'], password=os.environ['MQTT_PASSWORD'])
|
|
||||||
client.on_connect = on_connect
|
|
||||||
client.on_message = on_message
|
|
||||||
|
|
||||||
client.connect(host='mqtt', port=1883)
|
|
||||||
client.loop_forever()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
subscribe()
|
frigate_event_listener = FrigateEventListener(os.environ['MQTT_USERNAME'], os.environ['MQTT_PASSWORD'])
|
||||||
|
frigate_event_listener.start()
|
||||||
|
|
Loading…
Reference in New Issue