Initial commit
[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 class any_camera_trigger(trigger.trigger):
9     def __init__(self):
10         self.triggers_in_the_past_seven_min = {
11             "driveway" :       0,
12             "frontdoor" :      0,
13             "cabin_driveway" : 0,
14             "backyard" :       0,
15         }
16         self.last_trigger = {
17             "driveway" :       0,
18             "frontdoor" :      0,
19             "cabin_driveway" : 0,
20             "backyard" :       0,
21         }
22
23     def choose_priority(self, camera, age):
24         base_priority_by_camera = {
25             "driveway" : 1,
26             "frontdoor" : 2,
27             "cabin_driveway" : 1,
28             "backyard" : 0,
29         }
30         priority = base_priority_by_camera[camera]
31         if age < 10:
32             priority += trigger.trigger.PRIORITY_HIGH
33         elif age < 30:
34             priority += trigger.trigger.PRIORITY_NORMAL + age
35         else:
36             priority += trigger.trigger.PRIORITY_LOW
37         return priority
38
39     def get_triggered_page_list(self):
40         triggers = []
41         cameras_with_recent_triggers = 0
42         camera_list = [ "driveway",
43                         "frontdoor",
44                         "cabin_driveway",
45                         "backyard" ]
46
47         now = time.time()
48         try:
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                 age = now - ts
55                 # print "Camera: %s, age %s" % (camera, age)
56                 if age < 60:
57                     cameras_with_recent_triggers += 1
58                     time_since_last_trigger = now - self.last_trigger[camera]
59                     self.last_trigger[camera] = now
60                     if time_since_last_trigger < (60 * 7):
61                         self.triggers_in_the_past_seven_min[camera] += 1
62                     else:
63                         self.triggers_in_the_past_seven_min[camera] = 1
64
65             # Second pass, see whether we want to trigger due to
66             # camera activity we found.  All cameras timestamps were
67             # just considered and should be up-to-date.  Some logic to
68             # squelch spammy cameras unless more than one is
69             # triggered at the same time.
70             for camera in camera_list:
71                 if self.last_trigger[camera] == now:
72                     ts = utils.timestamp()
73                     if (self.triggers_in_the_past_seven_min[camera] <= 4 or
74                         cameras_with_recent_triggers > 1):
75                         p = self.choose_priority(camera, age)
76                         print "%s: ****** %s[%d] CAMERA TRIGGER ******" % (
77                             ts, camera, p)
78                         triggers.append( ( "hidden/%s.html" % camera,
79                                            self.choose_priority(camera, age)) )
80                     else:
81                         print "%s: Camera %s too spammy, squelching it" % (
82                             ts, camera)
83         except Exception as e:
84             print e
85             pass
86
87         if len(triggers) == 0:
88             return None
89         else:
90             return triggers
91
92 #x = any_camera_trigger()
93 #print x.get_triggered_page_list()