Added notification system
This commit is contained in:
1 parent
b0534c481f
commit
d29d9e16f0
7 files changed
+157
-2
No files matched your search
@@ -0,0 +1,11 @@
|
||||
FROM python:3.11
|
||||
WORKDIR /code
|
||||
|
||||
ENTRYPOINT ["python3", "mqtt.py"]
|
||||
|
||||
RUN pip3 install --upgrade pip
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
COPY src .
|
||||
@@ -0,0 +1,2 @@
|
||||
requests==2.30.0
|
||||
paho-mqtt==1.6.1
|
||||
@@ -0,0 +1,37 @@
|
||||
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('frigate_notifications')
|
||||
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='192.168.0.6', port=1883)
|
||||
client.loop_forever()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
subscribe()
|
||||
@@ -0,0 +1,59 @@
|
||||
import json
|
||||
import requests
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
|
||||
last_notification_time = None
|
||||
|
||||
|
||||
def send_rate_limited_notification(event_id, camera, object_label, score, quiet_period=1):
|
||||
global last_notification_time
|
||||
|
||||
now = datetime.now()
|
||||
if last_notification_time is None or now - last_notification_time >= timedelta(minutes=quiet_period):
|
||||
last_notification_time = now
|
||||
send_notification(event_id, camera, object_label, score, priority=3)
|
||||
|
||||
|
||||
def send_notification(event_id, camera, object_label, score, priority=3):
|
||||
camera_location = ' '.join(word.capitalize() for word in camera.split('_'))
|
||||
|
||||
response = requests.post('https://ntfy.homelab.net', json={
|
||||
'topic': 'frigate_notifications',
|
||||
'title': 'Frigate',
|
||||
'message': f'{object_label.capitalize()} at {camera_location} ({score:.0%})',
|
||||
'priority': priority,
|
||||
'click': f'https://frigate.homelab.net/cameras/{camera}',
|
||||
'icon': 'https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/frigate.png',
|
||||
'attach': f'https://frigate.homelab.net/api/events/{event_id}/thumbnail.jpg?format=android',
|
||||
'actions': [
|
||||
{
|
||||
'action': 'http',
|
||||
'label': 'Disable (30m)',
|
||||
'url': 'https://www.google.com/',
|
||||
'method': 'GET',
|
||||
'clear': True
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
response2 = requests.post('https://ntfy.homelab.net', json={
|
||||
'topic': 'frigate_notifications_dad',
|
||||
'title': 'Frigate',
|
||||
'message': f'{object_label.capitalize()} at {camera_location} ({score:.0%})',
|
||||
'priority': priority,
|
||||
'click': f'https://frigate.homelab.net/cameras/{camera}',
|
||||
'icon': 'https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/frigate.png',
|
||||
'attach': f'https://frigate.homelab.net/api/events/{event_id}/thumbnail.jpg?format=android',
|
||||
'actions': [
|
||||
{
|
||||
'action': 'http',
|
||||
'label': 'DBL (30m)',
|
||||
'url': 'https://www.google.com/',
|
||||
'method': 'GET',
|
||||
'clear': True
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
return response.status_code == 200 and response2.status_code == 200
|
||||
Reference in new issue
Block a user