Changes.
[kiosk.git] / camera_trigger.py
index b47a26ed4dfd9023ff45de49503808a757b7be4a..8582889d285ab49750299facf60ab242a793effd 100644 (file)
@@ -1,13 +1,14 @@
 #!/usr/bin/env python3
 
-from datetime import datetime
-import glob
+import logging
 import os
 import time
 from typing import List, Tuple, Optional
 
 import trigger
-import utils
+
+
+logger = logging.getLogger(__file__)
 
 
 class any_camera_trigger(trigger.trigger):
@@ -15,20 +16,23 @@ class any_camera_trigger(trigger.trigger):
         self.triggers_in_the_past_seven_min = {
             "driveway": 0,
             "frontdoor": 0,
+            "doorbell": 0,
             "cabin_driveway": 0,
         }
         self.last_trigger_timestamp = {
             "driveway": 0,
             "frontdoor": 0,
+            "doorbell": 0,
             "cabin_driveway": 0,
         }
 
     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,
+            "driveway": 3,
             "frontdoor": 2,
-            "cabin_driveway": 1,
+            "doorbell": 1,
+            "cabin_driveway": 3,
         }
         priority = base_priority_by_camera[camera]
         if age < 10:
@@ -43,7 +47,7 @@ class any_camera_trigger(trigger.trigger):
         """Return a list of triggered pages with priorities."""
         triggers = []
         num_cameras_with_recent_triggers = 0
-        camera_list = ["driveway", "frontdoor", "cabin_driveway"]
+        camera_list = ["driveway", "frontdoor", "doorbell", "cabin_driveway"]
 
         now = time.time()
         try:
@@ -53,16 +57,18 @@ class any_camera_trigger(trigger.trigger):
             for camera in camera_list:
                 filename = f"/timestamps/last_camera_motion_{camera}"
                 ts = os.stat(filename).st_ctime
-                if ts != self.last_trigger_timestamp[camera] and (now - ts) < 10:
-                    print("Camera: %s, age %s" % (camera, (now - ts)))
+                age = now - ts
+                if ts != self.last_trigger_timestamp[camera] and age < 10:
+                    logger.info(f'{camera} is triggered; {filename} touched {age}s ago (@{ts}')
                     self.last_trigger_timestamp[camera] = 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.strip()
+                        x = x.strip()
                         age = now - int(x)
                         if age < (60 * 7):
                             self.triggers_in_the_past_seven_min[camera] += 1
@@ -78,25 +84,27 @@ class any_camera_trigger(trigger.trigger):
                         self.triggers_in_the_past_seven_min[camera] <= 4
                         or num_cameras_with_recent_triggers > 1
                     ):
+                        logger.info(f'{camera} has {self.triggers_in_the_past_seven_min[camera]} triggers in the past 7d.')
+                        logger.info(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"{utils.timestamp()}: *** {camera}[{priority}] CAMERA TRIGGER ***"
-                        )
+                        logger.info(f'*** CAMERA TRIGGER (hidden/{camera}.html @ {priority}) ***')
                         triggers.append(
                             (
-                                f"hidden/{camera}.html",
+                                f"hidden/unwrapped_{camera}.html",
                                 priority,
                             )
                         )
                     else:
-                        print(f"{utils.timestamp()}: Camera {camera} too spammy, squelching it")
+                        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