Cleanup
[kiosk.git] / camera_trigger.py
1 import glob
2 import os
3 import time
4 import trigger
5 import utils
6 from datetime import datetime
7
8
9 class any_camera_trigger(trigger.trigger):
10     def __init__(self):
11         self.triggers_in_the_past_seven_min = {
12             "driveway": 0,
13             "frontdoor": 0,
14             "cabin_driveway": 0,
15             "backyard": 0,
16         }
17         self.last_trigger = {
18             "driveway": 0,
19             "frontdoor": 0,
20             "cabin_driveway": 0,
21             "backyard": 0,
22         }
23
24     def choose_priority(self, camera, age):
25         base_priority_by_camera = {
26             "driveway": 1,
27             "frontdoor": 2,
28             "cabin_driveway": 1,
29             "backyard": 0,
30         }
31         priority = base_priority_by_camera[camera]
32         if age < 10:
33             priority += trigger.trigger.PRIORITY_HIGH
34         elif age < 30:
35             priority += trigger.trigger.PRIORITY_NORMAL + age
36         else:
37             priority += trigger.trigger.PRIORITY_LOW
38         return priority
39
40     def get_triggered_page_list(self):
41         triggers = []
42         cameras_with_recent_triggers = 0
43         camera_list = ["driveway", "frontdoor", "cabin_driveway", "backyard"]
44
45         now = time.time()
46         try:
47             # First pass, just see whether each camera is triggered and,
48             # if so, count how many times in the past 7m it has triggered.
49             for camera in camera_list:
50                 file = "/timestamps/last_camera_motion_%s" % camera
51                 ts = os.stat(file).st_ctime
52                 if ts != self.last_trigger[camera] and (now - ts) < 10:
53                     print("Camera: %s, age %s" % (camera, (now - ts)))
54                     self.last_trigger[camera] = ts
55                     cameras_with_recent_triggers += 1
56                     self.triggers_in_the_past_seven_min[camera] = 0
57                     file = "/timestamps/camera_motion_history_%s" % camera
58                     f = open(file, "r")
59                     contents = f.readlines()
60                     f.close()
61                     for x in contents:
62                         x.strip()
63                         age = now - int(x)
64                         if age < (60 * 7):
65                             self.triggers_in_the_past_seven_min[camera] += 1
66
67             # Second pass, see whether we want to trigger due to
68             # camera activity we found.  All cameras timestamps were
69             # just considered and should be up-to-date.  Some logic to
70             # squelch spammy cameras unless more than one is
71             # triggered at the same time.
72             for camera in camera_list:
73                 if (now - self.last_trigger[camera]) < 10:
74                     if (
75                         self.triggers_in_the_past_seven_min[camera] <= 4
76                         or cameras_with_recent_triggers > 1
77                     ):
78                         ts = utils.timestamp()
79                         p = self.choose_priority(camera, age)
80                         print(
81                             (
82                                 "%s: ****** %s[%d] CAMERA TRIGGER ******"
83                                 % (ts, camera, p)
84                             )
85                         )
86                         triggers.append(
87                             (
88                                 "hidden/%s.html" % camera,
89                                 self.choose_priority(camera, age),
90                             )
91                         )
92                     else:
93                         print(
94                             ("%s: Camera %s too spammy, squelching it" % (ts, camera))
95                         )
96         except Exception as e:
97             print(e)
98             pass
99
100         if len(triggers) == 0:
101             return None
102         else:
103             return triggers
104
105
106 # x = any_camera_trigger()
107 # print(x.get_triggered_page_list())