38 lines
1015 B
Python
38 lines
1015 B
Python
import os
|
|
import json
|
|
from time import sleep
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from ntfy import send_rate_limited_notification
|
|
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
print(f'Connected with return code {rc}')
|
|
client.subscribe('frigate/events')
|
|
|
|
|
|
def on_message(client, userdata, message):
|
|
payload = json.loads(message.payload.decode())
|
|
if payload['type'] == 'new' and payload['after']['label'] == 'person':
|
|
event_id = payload['after']['id']
|
|
camera = payload['after']['camera']
|
|
object_label = payload['after']['label']
|
|
score = payload['after']['top_score']
|
|
|
|
send_rate_limited_notification(event_id, camera, object_label, score)
|
|
|
|
|
|
def subscribe():
|
|
client = mqtt.Client()
|
|
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__':
|
|
subscribe()
|