Skip to content

Home Information - Your home, your data, organized.

Python

Fix ZM simulator event_id collision across monitors (#338)

Tony Ce489079d9c35
diff --git a/src/hi/simulator/services/zoneminder/zm_event_history.py b/src/hi/simulator/services/zoneminder/zm_event_history.py
index d3526678..4f5d223b 100644
--- a/src/hi/simulator/services/zoneminder/zm_event_history.py
+++ b/src/hi/simulator/services/zoneminder/zm_event_history.py
@@ -1,6 +1,6 @@
from collections import deque
from datetime import datetime
-from typing import List
+from typing import Callable, List
import hi.apps.common.datetimeproxy as datetimeproxy
@@ -9,10 +9,12 @@ from .sim_models import ZmSimEvent, ZmSimMonitor
class ZmSimEventHistory:
- event_counter = 0
-
- def __init__( self, zm_sim_monitor : ZmSimMonitor, max_events: int = 500 ):
+ def __init__( self,
+ zm_sim_monitor : ZmSimMonitor,
+ event_id_allocator : Callable[[], int],
+ max_events : int = 500 ):
self._zm_sim_monitor = zm_sim_monitor
+ self._event_id_allocator = event_id_allocator
self._zm_sim_events = deque( maxlen = max_events )
return
@@ -38,13 +40,13 @@ class ZmSimEventHistory:
return latest_zm_sim_event
def add_zm_sim_event( self ) -> ZmSimEvent:
- self.event_counter += 1
+ event_id = self._event_id_allocator()
zm_sim_event = ZmSimEvent(
zm_sim_monitor = self._zm_sim_monitor,
- event_id = self.event_counter,
+ event_id = event_id,
start_datetime = datetimeproxy.now(),
end_datetime = None,
- name = f'Event {self.event_counter}',
+ name = f'Event {event_id}',
)
self._zm_sim_events.append( zm_sim_event )
return zm_sim_event
diff --git a/src/hi/simulator/services/zoneminder/zm_event_manager.py b/src/hi/simulator/services/zoneminder/zm_event_manager.py
index 94ca49d9..7634a479 100644
--- a/src/hi/simulator/services/zoneminder/zm_event_manager.py
+++ b/src/hi/simulator/services/zoneminder/zm_event_manager.py
@@ -13,8 +13,16 @@ class ZmSimEventManager( Singleton ):
def __init_singleton__( self ):
self._zm_sim_events_map : Dict[ int, ZmSimEventHistory ] = dict()
self._data_lock = threading.Lock()
+ # ZM's wire event_id space is a single non-decreasing counter
+ # across all monitors. Centralizing allocation here keeps ids
+ # unique under simultaneous events on different monitors.
+ self._next_event_id = 0
return
+ def _allocate_event_id( self ) -> int:
+ self._next_event_id += 1
+ return self._next_event_id
+
def add_motion_value( self,
zm_sim_monitor : ZmSimMonitor,
motion_value : bool ):
@@ -23,6 +31,7 @@ class ZmSimEventManager( Singleton ):
if monitor_id not in self._zm_sim_events_map:
self._zm_sim_events_map[monitor_id] = ZmSimEventHistory(
zm_sim_monitor = zm_sim_monitor,
+ event_id_allocator = self._allocate_event_id,
)
self._zm_sim_events_map[monitor_id].add_motion_value( motion_value = motion_value )
return