From c1b008571012df54fa70861703f013e455f66e55 Mon Sep 17 00:00:00 2001 From: MarekWo Date: Fri, 6 Mar 2026 09:53:06 +0000 Subject: [PATCH] feat(watchdog): skip USB reset if TCP connection is used Since mc-webui can now connect via TCP to a remote proxy instead of local USB/serial device, the hardware USB bus reset logic in Watchdog will no longer blindly attempt a reset on repeated container crashes. Added \is_tcp_connection()\ helper to read the config and conditionally skip the USB reset if TCP is active. --- scripts/watchdog/watchdog.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/scripts/watchdog/watchdog.py b/scripts/watchdog/watchdog.py index c0b220c..27be8b7 100755 --- a/scripts/watchdog/watchdog.py +++ b/scripts/watchdog/watchdog.py @@ -136,6 +136,23 @@ def auto_detect_usb_device() -> str: log(f"Error during USB device auto-detection: {e}", "ERROR") return None +def is_tcp_connection() -> bool: + """Check if the application is configured to use a TCP connection instead of a serial port.""" + env_file = os.path.join(MCWEBUI_DIR, '.env') + + if os.path.exists(env_file): + try: + with open(env_file, 'r') as f: + for line in f: + if line.startswith('MC_TCP_HOST='): + val = line.split('=', 1)[1].strip().strip('"\'') + if val: + return True + except Exception as e: + log(f"Failed to read .env file for TCP host: {e}", "WARN") + + return False + def reset_esp32_device(): """Perform a hardware reset on ESP32/LoRa device using DTR/RTS lines via ioctl.""" serial_port = auto_detect_serial_port() @@ -416,7 +433,7 @@ def handle_unhealthy_container(container_name: str, status: dict): restart_success = False if container_name == 'mc-webui': recent_restarts = count_recent_restarts(container_name, minutes=8) - if recent_restarts >= 3: + if recent_restarts >= 3 and not is_tcp_connection(): log(f"{container_name} has been restarted {recent_restarts} times in the last 8 minutes. Attempting hardware USB reset.", "WARN") # Stop the container first so it releases the serial port run_compose_command(['stop', container_name]) @@ -425,6 +442,8 @@ def handle_unhealthy_container(container_name: str, status: dict): time.sleep(5) # Give OS time to re-enumerate the device restart_success = start_container(container_name) else: + if recent_restarts >= 3 and is_tcp_connection(): + log(f"{container_name} has been restarted {recent_restarts} times in the last 8 minutes. TCP connection used, skipping hardware USB reset.", "WARN") restart_success = restart_container(container_name) else: # Restart the container @@ -493,7 +512,7 @@ def handle_unresponsive_device(container_name: str, status: dict): restart_success = False if container_name == 'mc-webui': recent_restarts = count_recent_restarts(container_name, minutes=8) - if recent_restarts >= 3: + if recent_restarts >= 3 and not is_tcp_connection(): log(f"{container_name} has been restarted {recent_restarts} times in the last 8 minutes. Attempting hardware USB reset.", "WARN") # Stop the container first so it releases the serial port run_compose_command(['stop', container_name]) @@ -502,6 +521,8 @@ def handle_unresponsive_device(container_name: str, status: dict): time.sleep(5) # Give OS time to re-enumerate the device restart_success = start_container(container_name) else: + if recent_restarts >= 3 and is_tcp_connection(): + log(f"{container_name} has been restarted {recent_restarts} times in the last 8 minutes. TCP connection used, skipping hardware USB reset.", "WARN") restart_success = restart_container(container_name) else: # Restart the container