From ebb162231408fc1bf2aa4acf19cf2c80d1f926c3 Mon Sep 17 00:00:00 2001 From: dsutanto Date: Thu, 5 Mar 2026 16:09:31 +0700 Subject: [PATCH] Add more guards --- frigate_counter.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/frigate_counter.py b/frigate_counter.py index e6702f8..c8c59ea 100644 --- a/frigate_counter.py +++ b/frigate_counter.py @@ -35,6 +35,8 @@ class FrigateCounter: self.topic = os.environ.get('TOPIC', f"{self.top_topic}/counter/{self.site_name}") self.camera_name = os.environ.get('CAMERA_NAME', 'kandang_1_karung_masuk') self.pintu_tutup_zone_name = os.environ.get('PINTU_TUTUP_ZONE_NAME', 'pintu_tutup') + self.pintu_kiri_buka_zone_name = os.environ.get('PINTU_KIRI_BUKA_ZONE_NAME', 'pintu_kiri_buka') + self.pintu_kanan_buka_zone_name = os.environ.get('PINTU_KANAN_BUKA_ZONE_NAME', 'pintu_kanan_buka') logger.info(f"FRIGATE_MQTT_HOST: {self.frigate_mqtt_host}:{self.frigate_mqtt_port}") logger.info(f"REPORT_MQTT_HOST: {self.report_mqtt_host}:{self.report_mqtt_port}") @@ -63,6 +65,8 @@ class FrigateCounter: self.timer_active_pintu = False self.timer_start_time_pintu = None + self.pintu_buka_timer = False + # Load previous counter value on startup self.load_previous_counter() @@ -174,13 +178,13 @@ class FrigateCounter: logger.debug(f"Received message: camera={camera_name}, type={event_type}, label={label}") # Handle different object types - if label == "pintu-kiri-buka" and not self.timer_active and not self.pintu_kiri_buka_detected: + if label == "pintu-kiri-buka" and not self.timer_active and not self.pintu_kiri_buka_detected and self.pintu_kiri_buka_zone_name in zones_after: self.handle_pintu_kiri_buka(camera_name) - elif label == "pintu-kanan-buka" and not self.timer_active and not self.pintu_kanan_buka_detected: + elif label == "pintu-kanan-buka" and not self.timer_active and not self.pintu_kanan_buka_detectedi and self.pintu_kanan_buka_zone_name in zones_after: self.handle_pintu_kanan_buka(camera_name) elif label == "karung" and self.timer_active: self.handle_karung(camera_name, track_id) - elif label == "pintu-tutup" and self.timer_active and self.pintu_tutup_zone_name in zones_after: + elif label == "pintu-tutup" and self.timer_active and self.pintu_tutup_zone_name in zones_after and not self.pintu_buka_timer: self.handle_pintu_tutup(camera_name) except Exception as e: @@ -258,11 +262,12 @@ class FrigateCounter: logger.info("Start Counting...") self.timer_active = True self.timer_start_time = datetime.now() + self.pintu_buka_timer = True # Schedule timer expiration check - #timer_thread = threading.Thread(target=self.check_timer_expiration) - #timer_thread.daemon = True - #timer_thread.start() + timer_thread = threading.Thread(target=self.check_timer_expiration_pintu_buka) + timer_thread.daemon = True + timer_thread.start() # Reset detection flags self.pintu_kiri_buka_detected = False @@ -278,15 +283,13 @@ class FrigateCounter: self.pintu_kiri_buka_detected = False self.pintu_kanan_buka_detected = False - def check_timer_expiration(self): + def check_timer_expiration_pintu_buka(self): """Check if timer has expired (60 minutes)""" - time.sleep(60 * 60) # Wait 60 minutes + time.sleep(30 * 1) # Wait 30 seconds - if self.timer_active: - logger.info("Timer expired (60 minutes)") - self.timer_active = False - #self.publish_result() - #self.reset_counter() + if self.pintu_buka_timer: + logger.info("Timer Pintu Buka expired (30 seconds)") + self.pintu_buka_timer = False def publish_result(self): """Publish counter result to MQTT topic"""