Skip to content

Commit

Permalink
Frigate-to-Ntfy: Extend example by different inbound event messages
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Apr 12, 2023
1 parent 274b9a1 commit f35ccd1
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 7 deletions.
6 changes: 4 additions & 2 deletions examples/frigate/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ Run mqttwarn::
mkdir -p ./var/media
MQTTWARNINI=frigate.ini mqttwarn

Publish an example event::
Publish a few example events and see how mqttwarn processes them::

cat frigate-event.json | jq -c | mosquitto_pub -t 'frigate/events' -l
cat frigate-event-new.json | jq -c | mosquitto_pub -t 'frigate/events' -l
cat frigate-event-end.json | jq -c | mosquitto_pub -t 'frigate/events' -l
cat frigate-event-false-positive.json | jq -c | mosquitto_pub -t 'frigate/events' -l

Publish an example image::

Expand Down
36 changes: 36 additions & 0 deletions examples/frigate/frigate-event-end.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"before": {
"id": "1680791459.255384-abcdef",
"camera": "camera",
"frame_time": 1680791459.255384,
"snapshot_time": 0,
"label": "car",
"sub_label": null,
"top_score": 0,
"false_positive": true,
"start_time": 1680791459.255384,
"end_time": null,
"current_zones": [],
"entered_zones": [],
"has_clip": false,
"has_snapshot": false
},
"after": {
"id": "1680791459.255384-abcdef",
"camera": "camera",
"frame_time": 1680791506.638857,
"snapshot_time": 1680791506.638857,
"label": "car",
"sub_label": null,
"false_positive": false,
"start_time": 1680791459.255384,
"end_time": null,
"current_zones": [],
"entered_zones": [
"zone1"
],
"has_clip": true,
"has_snapshot": true
},
"type": "end"
}
36 changes: 36 additions & 0 deletions examples/frigate/frigate-event-false-positive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"before": {
"id": "1680791459.255384-abcdef",
"camera": "camera",
"frame_time": 1680791459.255384,
"snapshot_time": 0,
"label": "car",
"sub_label": null,
"top_score": 0,
"false_positive": true,
"start_time": 1680791459.255384,
"end_time": null,
"current_zones": [],
"entered_zones": [],
"has_clip": false,
"has_snapshot": false
},
"after": {
"id": "1680791459.255384-abcdef",
"camera": "camera",
"frame_time": 1680791506.638857,
"snapshot_time": 1680791506.638857,
"label": "car",
"sub_label": null,
"false_positive": true,
"start_time": 1680791459.255384,
"end_time": null,
"current_zones": [],
"entered_zones": [
"zone1"
],
"has_clip": true,
"has_snapshot": true
},
"type": "new"
}
File renamed without changes.
36 changes: 36 additions & 0 deletions examples/frigate/frigate-event-new.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"before": {
"id": "1680791459.255384-abcdef",
"camera": "camera",
"frame_time": 1680791459.255384,
"snapshot_time": 0,
"label": "car",
"sub_label": null,
"top_score": 0,
"false_positive": true,
"start_time": 1680791459.255384,
"end_time": null,
"current_zones": [],
"entered_zones": [],
"has_clip": false,
"has_snapshot": false
},
"after": {
"id": "1680791459.255384-abcdef",
"camera": "camera",
"frame_time": 1680791506.638857,
"snapshot_time": 1680791506.638857,
"label": "car",
"sub_label": null,
"false_positive": false,
"start_time": 1680791459.255384,
"end_time": null,
"current_zones": [],
"entered_zones": [
"zone1"
],
"has_clip": true,
"has_snapshot": true
},
"type": "new"
}
40 changes: 35 additions & 5 deletions examples/frigate/frigate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from datetime import datetime

from mqttwarn.model import Service

try:
import json
except ImportError:
Expand All @@ -23,15 +25,43 @@ def frigate_events(topic, data, srv=None):
})
return r

def frigate_events_filter(topic, message, section, srv=None):

def frigate_events_filter(topic, message, section, srv: Service=None) -> bool:
"""
mqttwarn filter function to only use Frigate events of type `new`.
Additionally, skip, for example, false positives.
:return: True if message should be filtered, i.e. notification should be skipped.
"""

# Decode message.
try:
message = json.loads(message)
except:
message = None
if message and message.get('type', None) != 'end' and 'after' in message:
a = message['after']
message = dict()
message_type = message.get("type", None)
srv.logging.info(f"Received Frigate event message with type={message_type}")

# Determine if message should be used, or not.
use_message = message_type == "new" and "after" in message

# Look at more details.
if use_message:
after = message["after"]
if after.get("false_positive") is True:
srv.logging.info(f"Skipping Frigate event because it's a false positive")
use_message = False

# TODO: Honor more details of inbound event message.
"""
return False not in (x in a and (x == 'current_zones' or a[x]) for x in
('false_positive', 'camera', 'label',
'current_zones', 'entered_zones',
'frame_time'))
return False
"""
else:
use_message = False

# Inverse logic.
filter_message = not use_message
return filter_message

0 comments on commit f35ccd1

Please sign in to comment.