From 3895279c1f04609764c3b22f41e1ce04137864ee Mon Sep 17 00:00:00 2001 From: dsutanto Date: Tue, 9 Jun 2026 20:37:05 +0700 Subject: [PATCH] Add 50 counted_ids to next batch --- counter_service.py | 56 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/counter_service.py b/counter_service.py index 8790e5e..f0ab60a 100644 --- a/counter_service.py +++ b/counter_service.py @@ -30,10 +30,17 @@ class FrigateCounterService: self.shutdown_event = threading.Event() self.current_state = self.load_state() + # Previous state + self.previous_state = self.current_state + # Telenan Batch Label self.ignore_batch_label = False self.ignore_batch_label_timer = None 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 # ------------------------------------------------------------------ # @@ -197,6 +204,27 @@ class FrigateCounterService: def start_new_batch(self, counting_date): batch_number = self.get_next_batch_number(counting_date) 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 = { "counting_date": counting_date, "batch_number": batch_number, @@ -205,6 +233,7 @@ class FrigateCounterService: "last_detection_time": now, "counted_event_ids": [], } + """ self.save_state() self.logger.info( "Started batch #%s for %s (%s)", @@ -256,8 +285,12 @@ class FrigateCounterService: self._ignore_batch_label() self.end_batch() - 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 Blocking + #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 @@ -267,6 +300,19 @@ class FrigateCounterService: if should_reset_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): if not self.ignore_batch_label_timer: self.ignore_batch_label = True @@ -293,6 +339,7 @@ class FrigateCounterService: self.end_batch() def end_batch(self): + self.previous_state = self.current_state with self.state_lock: self._end_batch_locked() @@ -438,6 +485,11 @@ class FrigateCounterService: self.logger.error("MQTT connection failed, code=%s", rc) def on_message(self, client, userdata, msg): + + # Sleep Non Blocking after batch label detected + if self.sleep_after_batch_label: + return + try: payload = json.loads(msg.payload.decode("utf-8")) after = payload.get("after", {})