diff --git a/listener/app.py b/listener/app.py index 0c3cec5a..ba33b643 100644 --- a/listener/app.py +++ b/listener/app.py @@ -909,6 +909,10 @@ def manage_vitek_port(config): logging.info(f"[{port_name}] START VITEK SERVICE (Relaxed Mode)...") print(f"[{port_name}] START VITEK SERVICE (Relaxed Mode)...") + # Deteksi stuck dalam window waktu tertentu. + STUCK_WINDOW_SEC = 120 + stuck_count = 0 + stuck_window_start = None while True: try: with serial.Serial( @@ -966,6 +970,8 @@ def manage_vitek_port(config): print(f"[{port_name}] FULL FRAME: {full_frame}") # 1. KIRIM ACK (WAJIB) ser.write(b'\x06') + stuck_count = 0 + stuck_window_start = None # 2. Proses Data try: @@ -986,6 +992,8 @@ def manage_vitek_port(config): logging.info(f"[{port_name}] Session End (EOT).") print(f"[{port_name}] Session End (EOT).") ser.reset_input_buffer() + stuck_count = 0 + stuck_window_start = None else: pass @@ -1030,6 +1038,8 @@ def manage_vitek_port(config): pass if handshake_success: + stuck_count = 0 + stuck_window_start = None # === KIRIM DATA ORDER === logging.info(f"[{port_name}] Handshake OK. Kirim Frames...") frames = create_vitek_order_message(pending_order) @@ -1058,6 +1068,8 @@ def manage_vitek_port(config): print(f"[{port_name}] Order SELESAI Terkirim.") setattr(pending_order, flag_col, True) session.commit() + stuck_count = 0 + stuck_window_start = None else: logging.error(f"[{port_name}] Order Gagal (No ACK).") @@ -1069,6 +1081,26 @@ def manage_vitek_port(config): print(f"[{port_name}] Alat Sibuk/Stuck. Kirim Force EOT.") ser.write(b'\x04') time.sleep(2.0) + now_ts = time.time() + if not stuck_window_start or (now_ts - stuck_window_start) > STUCK_WINDOW_SEC: + stuck_window_start = now_ts + stuck_count = 1 + else: + stuck_count += 1 + if stuck_count >= 3: + # Jika sudah stuck berulang dalam window waktu, restart koneksi serial + logging.critical( + f"[{port_name}] Stuck berulang ({stuck_count}x/{STUCK_WINDOW_SEC}s). Restart koneksi serial." + ) + print( + f"[{port_name}] Stuck berulang ({stuck_count}x/{STUCK_WINDOW_SEC}s). Restart koneksi serial." + ) + try: + ser.reset_input_buffer() + ser.reset_output_buffer() + except Exception: + pass + raise RuntimeError("Vitek stuck berulang, restart port.") except Exception as e: logging.error(f"[{port_name}] Sending Error: {e}")