Adding type annotations and fixing up formatting.
[kiosk.git] / camera_trigger.py
index 0f42ca20782cd06ecf46e23d68acd017a5d9f9a2..620a5b2fd62f27587cac6c096583eb94f42e90a5 100644 (file)
@@ -1,9 +1,13 @@
+#!/usr/bin/env python3
+
+from datetime import datetime
 import glob
 import os
 import time
+from typing import List, Tuple
+
 import trigger
 import utils
-from datetime import datetime
 
 
 class any_camera_trigger(trigger.trigger):
@@ -12,21 +16,19 @@ class any_camera_trigger(trigger.trigger):
             "driveway": 0,
             "frontdoor": 0,
             "cabin_driveway": 0,
-            "backyard": 0,
         }
-        self.last_trigger = {
+        self.last_trigger_timestamp = {
             "driveway": 0,
             "frontdoor": 0,
             "cabin_driveway": 0,
-            "backyard": 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,
         }
         priority = base_priority_by_camera[camera]
         if age < 10:
@@ -37,27 +39,28 @@ 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) -> 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", "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
-                if ts != self.last_trigger[camera] and (now - ts) < 10:
+                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)))
-                    self.last_trigger[camera] = ts
-                    cameras_with_recent_triggers += 1
+                    self.last_trigger_timestamp[camera] = ts
+                    num_cameras_with_recent_triggers += 1
                     self.triggers_in_the_past_seven_min[camera] = 0
-                    file = "/timestamps/camera_motion_history_%s" % camera
-                    f = open(file, "r")
-                    contents = f.readlines()
-                    f.close()
+                    filename = f"/timestamps/camera_motion_history_{camera}"
+                    with open(filename, "r") as f:
+                        contents = f.readlines()
                     for x in contents:
                         x.strip()
                         age = now - int(x)
@@ -67,32 +70,27 @@ class any_camera_trigger(trigger.trigger):
             # 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.
             for camera in camera_list:
-                if (now - self.last_trigger[camera]) < 10:
+                if (now - self.last_trigger_timestamp[camera]) < 10:
                     if (
                         self.triggers_in_the_past_seven_min[camera] <= 4
-                        or cameras_with_recent_triggers > 1
+                        or num_cameras_with_recent_triggers > 1
                     ):
                         ts = utils.timestamp()
-                        p = self.choose_priority(camera, age)
+                        priority = self.choose_priority(camera, age)
                         print(
-                            (
-                                "%s: ****** %s[%d] CAMERA TRIGGER ******"
-                                % (ts, camera, p)
-                            )
+                            f"{ts}: ****** {camera}[{priority}] CAMERA TRIGGER ******"
                         )
                         triggers.append(
                             (
-                                "hidden/%s.html" % camera,
-                                self.choose_priority(camera, age),
+                                f"hidden/{camera}.html",
+                                priority,
                             )
                         )
                     else:
-                        print(
-                            ("%s: Camera %s too spammy, squelching it" % (ts, camera))
-                        )
+                        print(f"{ts}: Camera {camera} too spammy, squelching it")
         except Exception as e:
             print(e)
             pass