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
60 if ts != self.last_trigger_timestamp[camera] and age < 10:
61 print(f'Camera: {camera}, age {age}')
62 self.last_trigger_timestamp[camera] = ts
63 num_cameras_with_recent_triggers += 1
65 self.triggers_in_the_past_seven_min[camera] = 0
66 filename = f"/timestamps/camera_motion_history_{camera}"
67 with open(filename, "r") as f:
68 contents = f.readlines()
73 self.triggers_in_the_past_seven_min[camera] += 1
75 # Second pass, see whether we want to trigger due to
76 # camera activity we found. All cameras timestamps were
77 # just considered and should be up-to-date. Some logic to
78 # squelch spammy cameras unless more than one is triggered
80 for camera in camera_list:
81 if (now - self.last_trigger_timestamp[camera]) < 10:
83 self.triggers_in_the_past_seven_min[camera] <= 4
84 or num_cameras_with_recent_triggers > 1
86 age = now - self.last_trigger_timestamp[camera]
87 priority = self.choose_priority(camera, int(age))
89 f"{utils.timestamp()}: *** {camera}[{priority}] CAMERA TRIGGER ***"
93 f"hidden/{camera}.html",
98 print(f"{utils.timestamp()}: Camera {camera} too spammy, squelching it")
99 except Exception as e:
103 if len(triggers) == 0:
109 # x = any_camera_trigger()
110 # print(x.get_triggered_page_list())