Add Pico communication timeout tracking

This commit is contained in:
2026-06-03 02:44:30 -06:00
parent ae719be590
commit f3965e017e
2 changed files with 119 additions and 1 deletions
+43 -1
View File
@@ -8,12 +8,32 @@ from .protocol import (
class CommunicationService:
def __init__(self, uart_client, app_state, http_client=None):
def __init__(
self,
uart_client,
app_state,
http_client=None,
clock=None,
timeout_seconds=5,
):
self.uart_client = uart_client
self.http_client = http_client
self.app_state = app_state
self.clock = clock
self.timeout_seconds = timeout_seconds
self.last_messages = []
self.use_http_fallback = False
self.last_status_received_at = None
def now(self):
if self.clock:
return self.clock()
try:
import time
return time.time()
except ImportError:
return 0
def request_status(self):
if self.use_http_fallback and self.http_client:
@@ -40,8 +60,28 @@ class CommunicationService:
for message in messages:
self.handle_message(message)
self.update_connection_state()
return messages
def update_connection_state(self):
if self.last_status_received_at is None:
self.app_state.network["uart_connected"] = False
return
age = self.now() - self.last_status_received_at
self.app_state.network["uart_connected"] = age <= self.timeout_seconds
def should_use_http_fallback(self):
return (
self.http_client is not None
and not self.app_state.network.get("uart_connected", False)
)
def auto_select_transport(self):
self.use_http_fallback = self.should_use_http_fallback()
return self.use_http_fallback
def enable_http_fallback(self):
self.use_http_fallback = True
@@ -50,7 +90,9 @@ class CommunicationService:
def handle_message(self, message):
if is_status_response(message):
self.last_status_received_at = self.now()
self.app_state.update_from_status(message)
self.app_state.network["uart_connected"] = True
return
if is_relay_response(message):