Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forwarding Frigate events to Ntfy #635

Merged
merged 31 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ac25d51
Forwarding Frigate events to Ntfy: Add baseline material and docs
amotl Apr 12, 2023
de00c85
Frigate-to-Ntfy: Fix event publishing example
amotl Apr 12, 2023
30d7bbd
Frigate-to-Ntfy: Add example about publishing and receiving an image
amotl Apr 12, 2023
2fd8c3d
Fix: Don't crash when receiving non-UTF8 payloads
amotl Apr 12, 2023
c262364
Frigate-to-Ntfy: Remove debug messages
amotl Apr 12, 2023
274b9a1
Frigate-to-Ntfy: Fix try/except in `frigate_events_filter`
amotl Apr 12, 2023
f35ccd1
Frigate-to-Ntfy: Extend example by different inbound event messages
amotl Apr 12, 2023
58f716d
Frigate-to-Ntfy: Fix filter function using serialized rule evaluation
amotl Apr 12, 2023
c258840
Improve exception logging in context functions
amotl Apr 12, 2023
3026359
This and that, code formatting
amotl Apr 12, 2023
5eb4c17
[core] Allow processing of binary, non-UTF8 message payloads
amotl Apr 13, 2023
60c6229
[file] Allow writing of binary content
amotl Apr 13, 2023
a58f7eb
Frigate-to-Ntfy: Configure `decode_utf8 = False` on `store-jpeg` service
amotl Apr 13, 2023
3fa79ee
[core] Fix JSON decoding and filtering after accepting binary payloads
amotl Apr 13, 2023
b93790d
[test] Add end-to-end test for receiving and processing binary data
amotl Apr 13, 2023
1612508
Frigate-to-Ntfy: Naming things
amotl Apr 13, 2023
a38d414
Frigate-to-Ntfy: Add `publish.sh` program
amotl Apr 13, 2023
2932acb
Frigate-to-Ntfy: Naming things
amotl Apr 13, 2023
f61c3a1
Frigate-to-Ntfy: Refactor `frigate_events` function
amotl Apr 13, 2023
63dda29
Frigate-to-Ntfy: Add `filename_template` option to `apprise-ntfy` svc
amotl Apr 13, 2023
8434441
Chore: This and that
amotl Apr 13, 2023
1bf91d4
Frigate-to-Ntfy: Adjust attachment filename
amotl Apr 18, 2023
c4e422a
Frigate-to-Ntfy: Disable sending attachments for now
amotl Apr 18, 2023
b6ac3fa
Frigate-to-Ntfy: Add Ntfy service and software test
amotl Apr 18, 2023
0233887
Frigate-to-Ntfy: Assume `frame_time` to be in UTC
amotl Apr 18, 2023
b3540c0
Frigate-to-Ntfy: Update to apprise>=1.3.0
amotl Apr 18, 2023
d8a2e45
Frigate-to-Ntfy: Decode `frigate/+/+/snapshot` in a generic way
amotl Apr 18, 2023
29e9382
Frigate-to-Ntfy: Also process "update" events
amotl Apr 18, 2023
d710460
Frigate-to-Ntfy: Limit notification/alert to specific cameras/zones
amotl Apr 18, 2023
575a345
Frigate-to-Ntfy: Refactor test case files/assets
amotl Apr 18, 2023
c32190f
Frigate-to-Ntfy: Improve formatting of outbound title and message
amotl Apr 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
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`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this handles both new and update. There are 3 types, of new, update, and end, so the filter will handle both new and update if it only excludes end.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Feel free to add any improvements on this detail. I am not so much into those aspects right now.

Copy link

@sevmonster sevmonster Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mqttwarn filter function to only use Frigate events of type `new`.
mqttwarn filter function to only use `new` and important `update` Frigate events.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've just added 29e9382 which addresses your suggestion.

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.
amotl marked this conversation as resolved.
Show resolved Hide resolved
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