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 = {
20 self.last_trigger_timestamp = {
26 def choose_priority(self, camera: str, age: int) -> int:
27 """Based on the camera name and last trigger age, compute priority."""
28 base_priority_by_camera = {
33 priority = base_priority_by_camera[camera]
35 priority += trigger.trigger.PRIORITY_HIGH
37 priority += trigger.trigger.PRIORITY_NORMAL + age
39 priority += trigger.trigger.PRIORITY_LOW
42 def get_triggered_page_list(self) -> Optional[List[Tuple[str, int]]]:
43 """Return a list of triggered pages with priorities."""
45 num_cameras_with_recent_triggers = 0
46 camera_list = ["driveway", "frontdoor", "cabin_driveway"]
50 # First pass, just see whether each camera is triggered
51 # and, if so, count how many times in the past 7m it has
53 for camera in camera_list:
54 filename = f"/timestamps/last_camera_motion_{camera}"
55 ts = os.stat(filename).st_ctime
56 if ts != self.last_trigger_timestamp[camera] and (now - ts) < 10:
57 print("Camera: %s, age %s" % (camera, (now - ts)))
58 self.last_trigger_timestamp[camera] = ts
59 num_cameras_with_recent_triggers += 1
60 self.triggers_in_the_past_seven_min[camera] = 0
61 filename = f"/timestamps/camera_motion_history_{camera}"
62 with open(filename, "r") as f:
63 contents = f.readlines()
68 self.triggers_in_the_past_seven_min[camera] += 1
70 # Second pass, see whether we want to trigger due to
71 # camera activity we found. All cameras timestamps were
72 # just considered and should be up-to-date. Some logic to
73 # squelch spammy cameras unless more than one is triggered
75 for camera in camera_list:
76 if (now - self.last_trigger_timestamp[camera]) < 10:
78 self.triggers_in_the_past_seven_min[camera] <= 4
79 or num_cameras_with_recent_triggers > 1
81 priority = self.choose_priority(camera, int(age))
83 f"{utils.timestamp()}: *** {camera}[{priority}] CAMERA TRIGGER ***"
87 f"hidden/{camera}.html",
92 print(f"{utils.timestamp()}: Camera {camera} too spammy, squelching it")
93 except Exception as e:
97 if len(triggers) == 0:
103 # x = any_camera_trigger()
104 # print(x.get_triggered_page_list())