X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=camera_trigger.py;h=3ca7a3cb43f42996324e14a927f9c6e01b622d36;hb=addd4980077f6e3857c5c035b49784dc3ceca49a;hp=74829e32d8b54cd9612e3b9259f515f596be848d;hpb=75b27cc68871343681f01e3f5b04cae84b1b7b2a;p=kiosk.git diff --git a/camera_trigger.py b/camera_trigger.py index 74829e3..3ca7a3c 100644 --- a/camera_trigger.py +++ b/camera_trigger.py @@ -1,31 +1,38 @@ -import glob +#!/usr/bin/env python3 + +import logging import os import time +from typing import List, Tuple, Optional + import trigger -import utils -from datetime import datetime + + +logger = logging.getLogger(__file__) + 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, + "driveway": 0, + "frontdoor": 0, + "doorbell": 0, + "cabin_driveway": 0, } - self.last_trigger = { - "driveway" : 0, - "frontdoor" : 0, - "cabin_driveway" : 0, - "backyard" : 0, + self.last_trigger_timestamp = { + "driveway": 0, + "frontdoor": 0, + "doorbell": 0, + "cabin_driveway": 0, } - def choose_priority(self, camera, age): + def choose_priority(self, camera: str, age: int) -> int: + """Based on the camera name and last trigger age, compute priority.""" base_priority_by_camera = { - "driveway" : 1, - "frontdoor" : 2, - "cabin_driveway" : 1, - "backyard" : 0, + "driveway": 3, + "frontdoor": 2, + "doorbell": 1, + "cabin_driveway": 3, } priority = base_priority_by_camera[camera] if age < 10: @@ -36,58 +43,74 @@ class any_camera_trigger(trigger.trigger): priority += trigger.trigger.PRIORITY_LOW return priority - def get_triggered_page_list(self): + def get_triggered_page_list(self) -> Optional[List[Tuple[str, int]]]: + """Return a list of triggered pages with priorities.""" triggers = [] - cameras_with_recent_triggers = 0 - camera_list = [ "driveway", - "frontdoor", - "cabin_driveway", - "backyard" ] + num_cameras_with_recent_triggers = 0 + camera_list = ["driveway", "frontdoor", "doorbell", "cabin_driveway"] 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. + # First pass, just see whether each camera is triggered + # and, if so, count how many times in the past 7m it has + # been triggered. for camera in camera_list: - file = "/timestamps/last_camera_motion_%s" % camera - ts = os.stat(file).st_ctime + filename = f"/timestamps/last_camera_motion_{camera}" + ts = os.stat(filename).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 + print(f'{camera} => {age}') + if ts != self.last_trigger_timestamp[camera]: + self.last_trigger_timestamp[camera] = ts + if age < 15: + logger.info(f'{camera} is triggered; {filename} touched {age}s ago (@{ts}') + num_cameras_with_recent_triggers += 1 + + self.triggers_in_the_past_seven_min[camera] = 0 + filename = f"/timestamps/camera_motion_history_{camera}" + with open(filename, "r") as f: + contents = f.readlines() + for x in contents: + x = x.strip() + age = now - int(x) + if age < (60 * 7): + self.triggers_in_the_past_seven_min[camera] += 1 + print(f'{camera} past 7m: {self.triggers_in_the_past_seven_min[camera]}') # 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. + # squelch spammy cameras unless more than one is triggered + # at the same time. + print(f'{num_cameras_with_recent_triggers}') 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)) ) + if (now - self.last_trigger_timestamp[camera]) < 15: + if ( + self.triggers_in_the_past_seven_min[camera] <= 4 + or num_cameras_with_recent_triggers > 1 + ): + print(f'{camera} has {self.triggers_in_the_past_seven_min[camera]} triggers in the past 7d.') + print(f'{num_cameras_with_recent_triggers} cameras are triggered right now.') + + age = now - self.last_trigger_timestamp[camera] + priority = self.choose_priority(camera, int(age)) + print(f'*** CAMERA TRIGGER (hidden/{camera}.html @ {priority}) ***') + triggers.append( + ( + f"hidden/unwrapped_{camera}.html", + priority, + ) + ) else: - print(("%s: Camera %s too spammy, squelching it" % ( - ts, camera))) + logger.info(f'{camera} is too spammy; {self.triggers_in_the_past_seven_min[camera]} events in the past 7m. Ignoring it.') except Exception as e: - print(e) - pass + logger.exception(e) if len(triggers) == 0: return None else: + logger.info('There are active camera triggers!') return triggers -#x = any_camera_trigger() -#print(x.get_triggered_page_list()) + +# x = any_camera_trigger() +# print(x.get_triggered_page_list())