Browse Source

Use dataclasses for config handling

Use config data classes to eliminate some of the boilerplate associated
with setting up the configuration.  In particular, using dataclasses
removes a lot of the boilerplate around assigning properties to the
object and allows these to be easily immutable by freezing them.  In the
case of simple, non-nested dataclasses, this also provides more
convenient `asdict` helpers.

To set this up, where previously the objects would be parsed from the
config via the `__init__` method, create a `build` classmethod that does
this and calls the dataclass initializer.

Some of the objects are mutated at runtime, in particular some of the
zones are mutated to set the color (this might be able to be refactored
out) and some of the camera functionality can be enabled/disabled.  Some
of the configs with `enabled` properties don't seem to have mqtt hooks
to be able to toggle this, in particular, the clips, snapshots, and
detect can be toggled but rtmp and record configs do not, but all of
these configs are still not frozen in case there is some other
functionality I am missing.

There are a couple other minor fixes here, one that was introduced
by me recently where `max_seconds` was not defined, the other to
properly `get()` the message payload when handling publishing mqtt
messages sent via websocket.
Sean Vig 4 years ago
parent
commit
84a0827aee
6 changed files with 469 additions and 573 deletions
  1. 1 0
      .gitignore
  2. 458 564
      frigate/config.py
  3. 1 0
      frigate/events.py
  4. 6 6
      frigate/mqtt.py
  5. 2 2
      frigate/test/test_config.py
  6. 1 1
      frigate/video.py

+ 1 - 0
.gitignore

@@ -1,5 +1,6 @@
 .DS_Store
 .DS_Store
 *.pyc
 *.pyc
+*.swp
 debug
 debug
 .vscode
 .vscode
 config/config.yml
 config/config.yml

File diff suppressed because it is too large
+ 458 - 564
frigate/config.py


+ 1 - 0
frigate/events.py

@@ -109,6 +109,7 @@ class EventProcessor(threading.Thread):
             earliest_event = datetime.datetime.now().timestamp()
             earliest_event = datetime.datetime.now().timestamp()
 
 
         # if the earliest event is more tha max seconds ago, cap it
         # if the earliest event is more tha max seconds ago, cap it
+        max_seconds = self.config.clips.max_seconds
         earliest_event = max(
         earliest_event = max(
             earliest_event,
             earliest_event,
             datetime.datetime.now().timestamp() - self.config.clips.max_seconds,
             datetime.datetime.now().timestamp() - self.config.clips.max_seconds,

+ 6 - 6
frigate/mqtt.py

@@ -22,11 +22,11 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
         if payload == "ON":
         if payload == "ON":
             if not clips_settings.enabled:
             if not clips_settings.enabled:
                 logger.info(f"Turning on clips for {camera_name} via mqtt")
                 logger.info(f"Turning on clips for {camera_name} via mqtt")
-                clips_settings._enabled = True
+                clips_settings.enabled = True
         elif payload == "OFF":
         elif payload == "OFF":
             if clips_settings.enabled:
             if clips_settings.enabled:
                 logger.info(f"Turning off clips for {camera_name} via mqtt")
                 logger.info(f"Turning off clips for {camera_name} via mqtt")
-                clips_settings._enabled = False
+                clips_settings.enabled = False
         else:
         else:
             logger.warning(f"Received unsupported value at {message.topic}: {payload}")
             logger.warning(f"Received unsupported value at {message.topic}: {payload}")
 
 
@@ -44,11 +44,11 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
         if payload == "ON":
         if payload == "ON":
             if not snapshots_settings.enabled:
             if not snapshots_settings.enabled:
                 logger.info(f"Turning on snapshots for {camera_name} via mqtt")
                 logger.info(f"Turning on snapshots for {camera_name} via mqtt")
-                snapshots_settings._enabled = True
+                snapshots_settings.enabled = True
         elif payload == "OFF":
         elif payload == "OFF":
             if snapshots_settings.enabled:
             if snapshots_settings.enabled:
                 logger.info(f"Turning off snapshots for {camera_name} via mqtt")
                 logger.info(f"Turning off snapshots for {camera_name} via mqtt")
-                snapshots_settings._enabled = False
+                snapshots_settings.enabled = False
         else:
         else:
             logger.warning(f"Received unsupported value at {message.topic}: {payload}")
             logger.warning(f"Received unsupported value at {message.topic}: {payload}")
 
 
@@ -67,12 +67,12 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
             if not camera_metrics[camera_name]["detection_enabled"].value:
             if not camera_metrics[camera_name]["detection_enabled"].value:
                 logger.info(f"Turning on detection for {camera_name} via mqtt")
                 logger.info(f"Turning on detection for {camera_name} via mqtt")
                 camera_metrics[camera_name]["detection_enabled"].value = True
                 camera_metrics[camera_name]["detection_enabled"].value = True
-                detect_settings._enabled = True
+                detect_settings.enabled = True
         elif payload == "OFF":
         elif payload == "OFF":
             if camera_metrics[camera_name]["detection_enabled"].value:
             if camera_metrics[camera_name]["detection_enabled"].value:
                 logger.info(f"Turning off detection for {camera_name} via mqtt")
                 logger.info(f"Turning off detection for {camera_name} via mqtt")
                 camera_metrics[camera_name]["detection_enabled"].value = False
                 camera_metrics[camera_name]["detection_enabled"].value = False
-                detect_settings._enabled = False
+                detect_settings.enabled = False
         else:
         else:
             logger.warning(f"Received unsupported value at {message.topic}: {payload}")
             logger.warning(f"Received unsupported value at {message.topic}: {payload}")
 
 

+ 2 - 2
frigate/test/test_config.py

@@ -156,9 +156,9 @@ class TestConfig(TestCase):
         }
         }
         frigate_config = FrigateConfig(config=config)
         frigate_config = FrigateConfig(config=config)
         assert "dog" in frigate_config.cameras["back"].objects.filters
         assert "dog" in frigate_config.cameras["back"].objects.filters
-        assert len(frigate_config.cameras["back"].objects.filters["dog"]._raw_mask) == 2
+        assert len(frigate_config.cameras["back"].objects.filters["dog"].raw_mask) == 2
         assert (
         assert (
-            len(frigate_config.cameras["back"].objects.filters["person"]._raw_mask) == 1
+            len(frigate_config.cameras["back"].objects.filters["person"].raw_mask) == 1
         )
         )
 
 
     def test_ffmpeg_params_global(self):
     def test_ffmpeg_params_global(self):

+ 1 - 1
frigate/video.py

@@ -100,7 +100,7 @@ def stop_ffmpeg(ffmpeg_process, logger):
 def start_or_restart_ffmpeg(
 def start_or_restart_ffmpeg(
     ffmpeg_cmd, logger, logpipe: LogPipe, frame_size=None, ffmpeg_process=None
     ffmpeg_cmd, logger, logpipe: LogPipe, frame_size=None, ffmpeg_process=None
 ):
 ):
-    if not ffmpeg_process is None:
+    if ffmpeg_process is not None:
         stop_ffmpeg(ffmpeg_process, logger)
         stop_ffmpeg(ffmpeg_process, logger)
 
 
     if frame_size is None:
     if frame_size is None:

Some files were not shown because too many files changed in this diff