-
Notifications
You must be signed in to change notification settings - Fork 12
/
notifier.py
113 lines (96 loc) · 4.81 KB
/
notifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import threading
from enum import Enum
import token_bucket
from termcolor import colored
from lib.ha_mqtt import HaMQTT
from lib.ha_webhook import HaWebHook
from lib.timer import RepeatedTimer
import logging
log = logging.getLogger(__name__)
class NotificationTypes(Enum):
OBJECT_DETECTED = 1
PATTERN_DETECTED = 2
DOOR_STATE_CHANGED = 3
MOTION_STATE_CHANGED = 4
class Notifier():
def __init__(self, config, notify_q):
self.notify_q = notify_q
self.config = config
if self.config.send_mqtt:
log.info("mqtt init")
self.mqtt = HaMQTT(self.config.mqtt_host, self.config.mqtt_port,
self.config.mqtt_username, self.config.mqtt_password)
self.mqtt_heartbeat_timer = RepeatedTimer(self.config.mqtt_heartbeat_secs, self.mqtt.publish,
self.config.mqtt_state_topic, "none")
elif self.config.send_webhook:
self.ha_webhook_od = HaWebHook("ha_webhook_od", self.config.ha_webhook_object_detect_url, self.config.ha_webhook_ssh_host,
self.config.ha_webhook_ssh_username, self.config.ha_webhook_target_dir)
self.ha_webhook_pd = HaWebHook("ha_webhook_pd", self.config.ha_webhook_pattern_detect_url, self.config.ha_webhook_ssh_host,
self.config.ha_webhook_ssh_username, self.config.ha_webhook_target_dir)
self.ha_webhook_ot = HaWebHook("ha_webhook_ot", self.config.ha_webhook_state_detect_url)
self.notification_handlers = {
NotificationTypes.OBJECT_DETECTED: self.notify_object_detected,
NotificationTypes.PATTERN_DETECTED: self.notify_pattern_detected,
NotificationTypes.MOTION_STATE_CHANGED: self.notify_state_detected,
NotificationTypes.DOOR_STATE_CHANGED: self.notify_state_detected
}
self.notification_rate_limiters = {}
self.t = threading.Thread(target=self.listen_notify_q)
self.t.daemon = True
self.t.start()
def notify_object_detected(self, state, ts):
label, accuracy, img_path = state
if label is not None:
log.info(colored("object notification: label [%s], accuracy[%s]" % (
label, str(accuracy)
), attrs=['bold']))
if self.config.send_mqtt:
self.mqtt.publish(self.config.mqtt_object_detect_topic, label)
elif self.config.send_webhook:
self.ha_webhook_od.send(label, img_path)
def notify_pattern_detected(self, pattern, pattern_attrs):
img_path = "no.jpg"
if pattern_attrs:
label, accuracy, img_path = pattern_attrs
log.info(colored("pattern notification: %s" % pattern, attrs=['bold']))
if self.config.send_mqtt:
self.mqtt.publish(self.config.mqtt_movement_pattern_detect_topic, str(pattern.name))
elif self.config.send_webhook:
self.ha_webhook_pd.send(str(pattern.name), img_path)
def notify_state_detected(self, state):
log.info(colored("state detection notification: %s" % state, attrs=['bold']))
if self.config.send_mqtt:
self.mqtt.publish(self.config.mqtt_state_detect_topic, str(state))
elif self.config.send_webhook:
self.ha_webhook_ot.send(str(state))
def can_notify(self, notification_type):
if notification_type in self.config.notifier_rate_limits:
notif_rate_limit = self.config.notifier_rate_limits[notification_type]
if notif_rate_limit >= 1:
if notification_type not in self.notification_rate_limiters:
storage = token_bucket.MemoryStorage()
self.notification_rate_limiters[notification_type] = token_bucket.Limiter(notif_rate_limit * 10, 10, storage)
can_notify = self.notification_rate_limiters[notification_type].consume('global', 10)
if not can_notify:
log.info("%s notification rate limited at %f fps" % (str(notification_type), notif_rate_limit))
return can_notify
else:
return True
return True
def listen_notify_q(self):
while True:
task = self.notify_q.dequeue()
if task == -1:
break
notification_type, notification_payload = task
if notification_type in self.config.notifier_notification_types:
if self.can_notify(notification_type):
self.notification_handlers[notification_type](*notification_payload)
def stop(self):
self.notify_q.enqueue(-1)
if self.t.is_alive():
self.t.join()
if self.config.send_mqtt:
self.mqtt_heartbeat_timer.stop()
def is_alive(self):
return self.t.is_alive()