41dc809c300b844b4289acabc2ed3cbe1638e845
[kiosk.git] / camera_trigger.py
1 #!/usr/bin/env python3
2
3 from datetime import datetime
4 import glob
5 import os
6 import time
7 from typing import List, Tuple, Optional
8
9 import trigger
10 import utils
11
12
13 class any_camera_trigger(trigger.trigger):
14     def __init__(self):
15         self.triggers_in_the_past_seven_min = {
16             "driveway": 0,
17             "frontdoor": 0,
18             "doorbell": 0,
19             "cabin_driveway": 0,
20         }
21         self.last_trigger_timestamp = {
22             "driveway": 0,
23             "frontdoor": 0,
24             "doorbell": 0,
25             "cabin_driveway": 0,
26         }
27
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 = {
31             "driveway": 3,
32             "frontdoor": 2,
33             "doorbell": 1,
34             "cabin_driveway": 3,
35         }
36         priority = base_priority_by_camera[camera]
37         if age < 10:
38             priority += trigger.trigger.PRIORITY_HIGH
39         elif age < 30:
40             priority += trigger.trigger.PRIORITY_NORMAL + age
41         else:
42             priority += trigger.trigger.PRIORITY_LOW
43         return priority
44
45     def get_triggered_page_list(self) -> Optional[List[Tuple[str, int]]]:
46         """Return a list of triggered pages with priorities."""
47         triggers = []
48         num_cameras_with_recent_triggers = 0
49         camera_list = ["driveway", "frontdoor", "doorbell", "cabin_driveway"]
50
51         now = time.time()
52         try:
53             # First pass, just see whether each camera is triggered
54             # and, if so, count how many times in the past 7m it has
55             # been triggered.
56             for camera in camera_list:
57                 filename = f"/timestamps/last_camera_motion_{camera}"
58                 ts = os.stat(filename).st_ctime
59                 age = now - ts
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
64
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()
69                     for x in contents:
70                         x = x.strip()
71                         age = now - int(x)
72                         if age < (60 * 7):
73                             self.triggers_in_the_past_seven_min[camera] += 1
74
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
79             # at the same time.
80             for camera in camera_list:
81                 if (now - self.last_trigger_timestamp[camera]) < 10:
82                     if (
83                         self.triggers_in_the_past_seven_min[camera] <= 4
84                         or num_cameras_with_recent_triggers > 1
85                     ):
86                         age = now - self.last_trigger_timestamp[camera]
87                         priority = self.choose_priority(camera, int(age))
88                         print(
89                             f"{utils.timestamp()}: *** {camera}[{priority}] CAMERA TRIGGER ***"
90                         )
91                         triggers.append(
92                             (
93                                 f"hidden/{camera}.html",
94                                 priority,
95                             )
96                         )
97                     else:
98                         print(f"{utils.timestamp()}: Camera {camera} too spammy, squelching it")
99         except Exception as e:
100             print(e)
101             pass
102
103         if len(triggers) == 0:
104             return None
105         else:
106             return triggers
107
108
109 # x = any_camera_trigger()
110 # print(x.get_triggered_page_list())