Add 50 counted_ids to next batch
This commit is contained in:
+54
-2
@@ -30,10 +30,17 @@ class FrigateCounterService:
|
|||||||
self.shutdown_event = threading.Event()
|
self.shutdown_event = threading.Event()
|
||||||
self.current_state = self.load_state()
|
self.current_state = self.load_state()
|
||||||
|
|
||||||
|
# Previous state
|
||||||
|
self.previous_state = self.current_state
|
||||||
|
|
||||||
# Telenan Batch Label
|
# Telenan Batch Label
|
||||||
self.ignore_batch_label = False
|
self.ignore_batch_label = False
|
||||||
self.ignore_batch_label_timer = None
|
self.ignore_batch_label_timer = None
|
||||||
self.sleep_after_batch_label_detected = int(os.getenv("SLEEP_AFTER_BATCH_LABEL", 10))
|
self.sleep_after_batch_label_detected = int(os.getenv("SLEEP_AFTER_BATCH_LABEL", 10))
|
||||||
|
# Sleep none blocking
|
||||||
|
self.sleep_after_batch_label_timeout = int(os.getenv("SLEEP_AFTER_BATCH_LABEL", 10))
|
||||||
|
self.sleep_after_batch_label_timer = None
|
||||||
|
self.sleep_after_batch_label = False
|
||||||
# ------------------------------------------------------------------ #
|
# ------------------------------------------------------------------ #
|
||||||
# Setup & Config
|
# Setup & Config
|
||||||
# ------------------------------------------------------------------ #
|
# ------------------------------------------------------------------ #
|
||||||
@@ -197,6 +204,27 @@ class FrigateCounterService:
|
|||||||
def start_new_batch(self, counting_date):
|
def start_new_batch(self, counting_date):
|
||||||
batch_number = self.get_next_batch_number(counting_date)
|
batch_number = self.get_next_batch_number(counting_date)
|
||||||
now = datetime.now().isoformat()
|
now = datetime.now().isoformat()
|
||||||
|
|
||||||
|
""" START - Carry last 50 previous counted_ids into next batch """
|
||||||
|
if self.previous_state is not None:
|
||||||
|
try:
|
||||||
|
counted_ids = self.previous_state["counted_event_ids"][-50:0]
|
||||||
|
except:
|
||||||
|
counted_ids = []
|
||||||
|
else:
|
||||||
|
counted_ids = []
|
||||||
|
|
||||||
|
self.current_state = {
|
||||||
|
"counting_date": counting_date,
|
||||||
|
"batch_number": batch_number,
|
||||||
|
"count": 0,
|
||||||
|
"start_time": now,
|
||||||
|
"last_detection_time": now,
|
||||||
|
"counted_event_ids": counted_ids,
|
||||||
|
}
|
||||||
|
""" END - Carry last 50 previous counted_ids into next batch """
|
||||||
|
|
||||||
|
"""
|
||||||
self.current_state = {
|
self.current_state = {
|
||||||
"counting_date": counting_date,
|
"counting_date": counting_date,
|
||||||
"batch_number": batch_number,
|
"batch_number": batch_number,
|
||||||
@@ -205,6 +233,7 @@ class FrigateCounterService:
|
|||||||
"last_detection_time": now,
|
"last_detection_time": now,
|
||||||
"counted_event_ids": [],
|
"counted_event_ids": [],
|
||||||
}
|
}
|
||||||
|
"""
|
||||||
self.save_state()
|
self.save_state()
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
"Started batch #%s for %s (%s)",
|
"Started batch #%s for %s (%s)",
|
||||||
@@ -256,8 +285,12 @@ class FrigateCounterService:
|
|||||||
self._ignore_batch_label()
|
self._ignore_batch_label()
|
||||||
self.end_batch()
|
self.end_batch()
|
||||||
|
|
||||||
self.logger.info("Batch Label detected. Sleep for %s seconds.", self.sleep_after_batch_label_detected)
|
# Sleep Blocking
|
||||||
time.sleep(self.sleep_after_batch_label_detected)
|
#self.logger.info("Batch Label detected. Sleep for %s seconds.", self.sleep_after_batch_label_detected)
|
||||||
|
#time.sleep(self.sleep_after_batch_label_detected)
|
||||||
|
|
||||||
|
# Sleep Non Blocking
|
||||||
|
#self._sleep_after_batch_label()
|
||||||
|
|
||||||
should_reset_timer = True
|
should_reset_timer = True
|
||||||
|
|
||||||
@@ -267,6 +300,19 @@ class FrigateCounterService:
|
|||||||
if should_reset_timer:
|
if should_reset_timer:
|
||||||
self._reset_batch_timer()
|
self._reset_batch_timer()
|
||||||
|
|
||||||
|
def _sleep_after_batch_label(self):
|
||||||
|
if not self.sleep_after_batch_label_timer:
|
||||||
|
self.sleep_after_batch_label = True
|
||||||
|
self.sleep_after_batch_label_timer = threading.Timer(self.sleep_after_batch_label_timeout, self._on_sleep_after_batch_label_timeout)
|
||||||
|
self.sleep_after_batch_label_timer.daemon = True
|
||||||
|
self.sleep_after_batch_label_timer.start()
|
||||||
|
self.logger.info("Sleep (non blocking) after Batch Label for %ss. self.sleep_after_batch_label = %s", self.sleep_after_batch_label_timeout, self.sleep_after_batch_label)
|
||||||
|
|
||||||
|
def _on_sleep_after_batch_label_timeout(self):
|
||||||
|
self.sleep_after_batch_label_timer = None
|
||||||
|
self.sleep_after_batch_label = False
|
||||||
|
self.logger.info("Sleep (non blocking) after Batch Label is done. self.sleep_after_batch_label = %s", self.sleep_after_batch_label)
|
||||||
|
|
||||||
def _ignore_batch_label(self):
|
def _ignore_batch_label(self):
|
||||||
if not self.ignore_batch_label_timer:
|
if not self.ignore_batch_label_timer:
|
||||||
self.ignore_batch_label = True
|
self.ignore_batch_label = True
|
||||||
@@ -293,6 +339,7 @@ class FrigateCounterService:
|
|||||||
self.end_batch()
|
self.end_batch()
|
||||||
|
|
||||||
def end_batch(self):
|
def end_batch(self):
|
||||||
|
self.previous_state = self.current_state
|
||||||
with self.state_lock:
|
with self.state_lock:
|
||||||
self._end_batch_locked()
|
self._end_batch_locked()
|
||||||
|
|
||||||
@@ -438,6 +485,11 @@ class FrigateCounterService:
|
|||||||
self.logger.error("MQTT connection failed, code=%s", rc)
|
self.logger.error("MQTT connection failed, code=%s", rc)
|
||||||
|
|
||||||
def on_message(self, client, userdata, msg):
|
def on_message(self, client, userdata, msg):
|
||||||
|
|
||||||
|
# Sleep Non Blocking after batch label detected
|
||||||
|
if self.sleep_after_batch_label:
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
payload = json.loads(msg.payload.decode("utf-8"))
|
payload = json.loads(msg.payload.decode("utf-8"))
|
||||||
after = payload.get("after", {})
|
after = payload.get("after", {})
|
||||||
|
|||||||
Reference in New Issue
Block a user