From 9c63de343af21ed5ed7cee920bb0b804753074e0 Mon Sep 17 00:00:00 2001 From: matt Date: Fri, 16 Oct 2026 00:13:07 -0700 Subject: [PATCH] add another timeout check on read_data --- api/ble.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/api/ble.py b/api/ble.py index 1cdd675..1500f21 100644 --- a/api/ble.py +++ b/api/ble.py @@ -38,6 +38,7 @@ SCAN_TIMEOUT = 10.0 # seconds to scan for each MAC CONNECT_TIMEOUT = 20.0 READ_TIMEOUT = 5.0 SERVICE_DISCOVERY_TIMEOUT = 10.0 +DISCOVER_TIMEOUT = SCAN_TIMEOUT + 5.0 # Timeout for discovering device # --------------------------------------------------------------------------- # # Logging @@ -138,10 +139,23 @@ def scan_macs(): # Core async read functions (unchanged from the refactored version) # --------------------------------------------------------------------------- # async def read_data(mac: str) -> Optional[SensorReading]: - address = await _discover_device(mac) + + try: + address = await asyncio.wait_for( + _discover_device(mac), + timeout=DISCOVER_TIMEOUT, + ) + except asyncio.TimeoutError: + log.warning("Device discovery timed out for %s after %.1fs", mac, DISCOVER_TIMEOUT) + return None + if not address: return None + #address = await _discover_device(mac) + #if not address: + # return None + try: async with BleakClient(address, timeout=CONNECT_TIMEOUT) as client: # <--- NEW BLOCK ----------------------------------------------