Add more guards

This commit is contained in:
2026-03-05 16:09:31 +07:00
parent 5023a583a6
commit ebb1622314

View File

@@ -35,6 +35,8 @@ class FrigateCounter:
self.topic = os.environ.get('TOPIC', f"{self.top_topic}/counter/{self.site_name}") 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.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_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"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}") 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_active_pintu = False
self.timer_start_time_pintu = None self.timer_start_time_pintu = None
self.pintu_buka_timer = False
# Load previous counter value on startup # Load previous counter value on startup
self.load_previous_counter() self.load_previous_counter()
@@ -174,13 +178,13 @@ class FrigateCounter:
logger.debug(f"Received message: camera={camera_name}, type={event_type}, label={label}") logger.debug(f"Received message: camera={camera_name}, type={event_type}, label={label}")
# Handle different object types # 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) 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) self.handle_pintu_kanan_buka(camera_name)
elif label == "karung" and self.timer_active: elif label == "karung" and self.timer_active:
self.handle_karung(camera_name, track_id) 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) self.handle_pintu_tutup(camera_name)
except Exception as e: except Exception as e:
@@ -258,11 +262,12 @@ class FrigateCounter:
logger.info("Start Counting...") logger.info("Start Counting...")
self.timer_active = True self.timer_active = True
self.timer_start_time = datetime.now() self.timer_start_time = datetime.now()
self.pintu_buka_timer = True
# Schedule timer expiration check # Schedule timer expiration check
#timer_thread = threading.Thread(target=self.check_timer_expiration) timer_thread = threading.Thread(target=self.check_timer_expiration_pintu_buka)
#timer_thread.daemon = True timer_thread.daemon = True
#timer_thread.start() timer_thread.start()
# Reset detection flags # Reset detection flags
self.pintu_kiri_buka_detected = False self.pintu_kiri_buka_detected = False
@@ -278,15 +283,13 @@ class FrigateCounter:
self.pintu_kiri_buka_detected = False self.pintu_kiri_buka_detected = False
self.pintu_kanan_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)""" """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: if self.pintu_buka_timer:
logger.info("Timer expired (60 minutes)") logger.info("Timer Pintu Buka expired (30 seconds)")
self.timer_active = False self.pintu_buka_timer = False
#self.publish_result()
#self.reset_counter()
def publish_result(self): def publish_result(self):
"""Publish counter result to MQTT topic""" """Publish counter result to MQTT topic"""