6 from datetime import datetime
8 class any_camera_trigger(trigger.trigger):
10 self.triggers_in_the_past_seven_min = {
23 def choose_priority(self, camera, age):
24 base_priority_by_camera = {
30 priority = base_priority_by_camera[camera]
32 priority += trigger.trigger.PRIORITY_HIGH
34 priority += trigger.trigger.PRIORITY_NORMAL + age
36 priority += trigger.trigger.PRIORITY_LOW
39 def get_triggered_page_list(self):
41 cameras_with_recent_triggers = 0
42 camera_list = [ "driveway",
49 # First pass, just see whether each camera is triggered and,
50 # if so, count how many times in the past 7m it has triggered.
51 for camera in camera_list:
52 file = "/timestamps/last_camera_motion_%s" % camera
53 ts = os.stat(file).st_ctime
54 if (ts != self.last_trigger[camera] and
56 print("Camera: %s, age %s" % (camera, (now - ts)))
57 self.last_trigger[camera] = ts
58 cameras_with_recent_triggers += 1
59 self.triggers_in_the_past_seven_min[camera] = 0
60 file = "/timestamps/camera_motion_history_%s" % camera
62 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
74 # triggered at the same time.
75 for camera in camera_list:
76 if (now - self.last_trigger[camera]) < 10:
77 if (self.triggers_in_the_past_seven_min[camera] <= 4 or
78 cameras_with_recent_triggers > 1):
79 ts = utils.timestamp()
80 p = self.choose_priority(camera, age)
81 print(("%s: ****** %s[%d] CAMERA TRIGGER ******" % (
83 triggers.append( ( "hidden/%s.html" % camera,
84 self.choose_priority(camera, age)) )
86 print(("%s: Camera %s too spammy, squelching it" % (
88 except Exception as e:
92 if len(triggers) == 0:
97 #x = any_camera_trigger()
98 #print(x.get_triggered_page_list())