Rewrite with asyncio and FastAPI #5
Squashed commit of the following: commit 6832d62dc8360bd20e92df5554dd36426119a50d Author: Ashish D'Souza <[email protected]> Date: Sun Mar 24 03:17:38 2024 -0500 Converted to asyncio
This commit is contained in:
1 parent
b3a2076416
commit
be3bd11632
24 files changed
+345
-246
No files matched your search
@@ -14,10 +14,10 @@ class FrigateEventNotifier:
|
||||
self.mqtt_client.on_connect = self._on_connect
|
||||
self.mqtt_client.on_message = self._on_message
|
||||
|
||||
frigate_api_response = requests.get('http://frigate:5000/api/config')
|
||||
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()}
|
||||
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()}
|
||||
|
||||
self.quiet_period = quiet_period
|
||||
self.last_notification_time = {}
|
||||
@@ -27,55 +27,55 @@ class FrigateEventNotifier:
|
||||
if now - self.last_notification_time.get(camera, dt.datetime.min) >= dt.timedelta(seconds=self.quiet_period):
|
||||
# Quiet period has passed since the last notification for this camera
|
||||
self.last_notification_time[camera] = now
|
||||
camera_location = ' '.join(word.capitalize() for word in camera.split('_'))
|
||||
camera_location = " ".join(word.capitalize() for word in camera.split("_"))
|
||||
|
||||
ntfy_api_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': [
|
||||
ntfy_api_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': f'https://frigate.homelab.net/webcontrol/camera/{camera}/detect',
|
||||
'method': 'POST',
|
||||
'headers': {
|
||||
'Content-Type': 'application/json'
|
||||
"action": "http",
|
||||
"label": "Disable (30m)",
|
||||
"url": f"https://frigate.homelab.net/webcontrol/camera/{camera}/detection",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
'body': json.dumps({
|
||||
'value': False,
|
||||
'duration': 30
|
||||
"body": json.dumps({
|
||||
"detection": False,
|
||||
"duration": 30
|
||||
}),
|
||||
'clear': True
|
||||
"clear": True
|
||||
}
|
||||
]
|
||||
})
|
||||
ntfy_api_response.raise_for_status()
|
||||
|
||||
ntfy_api_response = 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': [
|
||||
ntfy_api_response = 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': f'https://frigate.homelab.net/webcontrol/camera/{camera}/detect',
|
||||
'method': 'POST',
|
||||
'headers': {
|
||||
'Content-Type': 'application/json'
|
||||
"action": "http",
|
||||
"label": "DBL (30m)",
|
||||
"url": f"https://frigate.homelab.net/webcontrol/camera/{camera}/detection",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
'body': json.dumps({
|
||||
'value': False,
|
||||
'duration': 30
|
||||
"body": json.dumps({
|
||||
"detection": False,
|
||||
"duration": 30
|
||||
})
|
||||
}
|
||||
]
|
||||
@@ -83,36 +83,36 @@ class FrigateEventNotifier:
|
||||
ntfy_api_response.raise_for_status()
|
||||
|
||||
def start(self):
|
||||
self.mqtt_client.connect(host='mqtt', port=1883)
|
||||
self.mqtt_client.connect(host="mqtt", port=1883)
|
||||
self.mqtt_client.loop_forever()
|
||||
|
||||
def _on_connect(self, client, userdata, flags, rc):
|
||||
print(f'Connected with return code {rc}')
|
||||
client.subscribe('frigate/events')
|
||||
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']
|
||||
object_label = payload['after']['label']
|
||||
camera = payload["after"]["camera"]
|
||||
object_label = payload["after"]["label"]
|
||||
|
||||
if not self.camera_zones[camera]:
|
||||
# No required zones, send notification on receipt of new event
|
||||
if payload['type'] == 'new':
|
||||
event_id = payload['after']['id']
|
||||
score = payload['after']['top_score']
|
||||
if payload["type"] == "new":
|
||||
event_id = payload["after"]["id"]
|
||||
score = payload["after"]["top_score"]
|
||||
|
||||
self.send_notification(event_id, camera, object_label, score)
|
||||
else:
|
||||
new_zones = set(payload['after']['entered_zones']) - set(payload['before']['entered_zones'])
|
||||
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']
|
||||
event_id = payload["after"]["id"]
|
||||
score = payload["after"]["top_score"]
|
||||
|
||||
self.send_notification(event_id, camera, object_label, score)
|
||||
break
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
frigate_event_notifier = FrigateEventNotifier(os.environ.get('MQTT_USERNAME'), os.environ.get('MQTT_PASSWORD'))
|
||||
if __name__ == "__main__":
|
||||
frigate_event_notifier = FrigateEventNotifier(os.environ.get("MQTT_USERNAME"), os.environ.get("MQTT_PASSWORD"))
|
||||
frigate_event_notifier.start()
|
||||
Reference in new issue
Block a user