import glob import os import time import trigger import utils from datetime import datetime class any_camera_trigger(trigger.trigger): def __init__(self): self.triggers_in_the_past_seven_min = { "driveway" : 0, "frontdoor" : 0, "cabin_driveway" : 0, "backyard" : 0, } self.last_trigger = { "driveway" : 0, "frontdoor" : 0, "cabin_driveway" : 0, "backyard" : 0, } def choose_priority(self, camera, age): base_priority_by_camera = { "driveway" : 1, "frontdoor" : 2, "cabin_driveway" : 1, "backyard" : 0, } priority = base_priority_by_camera[camera] if age < 10: priority += trigger.trigger.PRIORITY_HIGH elif age < 30: priority += trigger.trigger.PRIORITY_NORMAL + age else: priority += trigger.trigger.PRIORITY_LOW return priority def get_triggered_page_list(self): triggers = [] cameras_with_recent_triggers = 0 camera_list = [ "driveway", "frontdoor", "cabin_driveway", "backyard" ] now = time.time() try: # First pass, just see whether each camera is triggered and, # if so, count how many times in the past 7m it has triggered. for camera in camera_list: file = "/timestamps/last_camera_motion_%s" % camera ts = os.stat(file).st_ctime age = now - ts # print "Camera: %s, age %s" % (camera, age) if age < 60: cameras_with_recent_triggers += 1 time_since_last_trigger = now - self.last_trigger[camera] self.last_trigger[camera] = now if time_since_last_trigger < (60 * 7): self.triggers_in_the_past_seven_min[camera] += 1 else: self.triggers_in_the_past_seven_min[camera] = 1 # Second pass, see whether we want to trigger due to # camera activity we found. All cameras timestamps were # just considered and should be up-to-date. Some logic to # squelch spammy cameras unless more than one is # triggered at the same time. for camera in camera_list: if self.last_trigger[camera] == now: ts = utils.timestamp() if (self.triggers_in_the_past_seven_min[camera] <= 4 or cameras_with_recent_triggers > 1): p = self.choose_priority(camera, age) print "%s: ****** %s[%d] CAMERA TRIGGER ******" % ( ts, camera, p) triggers.append( ( "hidden/%s.html" % camera, self.choose_priority(camera, age)) ) else: print "%s: Camera %s too spammy, squelching it" % ( ts, camera) except Exception as e: print e pass if len(triggers) == 0: return None else: return triggers #x = any_camera_trigger() #print x.get_triggered_page_list()