3 from datetime import datetime
7 from typing import List, Tuple, Optional
13 class any_camera_trigger(trigger.trigger):
15 self.triggers_in_the_past_seven_min = {
21 self.last_trigger_timestamp = {
28 def choose_priority(self, camera: str, age: int) -> int:
29 """Based on the camera name and last trigger age, compute priority."""
30 base_priority_by_camera = {
36 priority = base_priority_by_camera[camera]
38 priority += trigger.trigger.PRIORITY_HIGH
40 priority += trigger.trigger.PRIORITY_NORMAL + age
42 priority += trigger.trigger.PRIORITY_LOW
45 def get_triggered_page_list(self) -> Optional[List[Tuple[str, int]]]:
46 """Return a list of triggered pages with priorities."""
48 num_cameras_with_recent_triggers = 0
49 camera_list = ["driveway", "frontdoor", "doorbell", "cabin_driveway"]
53 # First pass, just see whether each camera is triggered
54 # and, if so, count how many times in the past 7m it has
56 for camera in camera_list:
57 filename = f"/timestamps/last_camera_motion_{camera}"
58 ts = os.stat(filename).st_ctime
59 if ts != self.last_trigger_timestamp[camera] and (now - ts) < 10:
60 print("Camera: %s, age %s" % (camera, (now - ts)))
61 self.last_trigger_timestamp[camera] = ts
62 num_cameras_with_recent_triggers += 1
63 self.triggers_in_the_past_seven_min[camera] = 0
64 filename = f"/timestamps/camera_motion_history_{camera}"
65 with open(filename, "r") as f:
66 contents = f.readlines()
71 self.triggers_in_the_past_seven_min[camera] += 1
73 # Second pass, see whether we want to trigger due to
74 # camera activity we found. All cameras timestamps were
75 # just considered and should be up-to-date. Some logic to
76 # squelch spammy cameras unless more than one is triggered
78 for camera in camera_list:
79 if (now - self.last_trigger_timestamp[camera]) < 10:
81 self.triggers_in_the_past_seven_min[camera] <= 4
82 or num_cameras_with_recent_triggers > 1
84 priority = self.choose_priority(camera, int(age))
86 f"{utils.timestamp()}: *** {camera}[{priority}] CAMERA TRIGGER ***"
90 f"hidden/{camera}.html",
95 print(f"{utils.timestamp()}: Camera {camera} too spammy, squelching it")
96 except Exception as e:
100 if len(triggers) == 0:
106 # x = any_camera_trigger()
107 # print(x.get_triggered_page_list())