Refactor get_hailo_temps() to use ctxmanager

This commit is contained in:
tigattack 2025-12-01 12:14:57 +00:00
parent 4d0456dcf0
commit f85a1fe3e8

View File

@ -558,13 +558,11 @@ def get_hailo_temps() -> dict[str, float]:
device_ids = Device.scan() device_ids = Device.scan()
for i, device_id in enumerate(device_ids): for i, device_id in enumerate(device_ids):
try: try:
device = Device(device_id) with Device(device_id) as device:
temp_info = device.control.get_chip_temperature() temp_info = device.control.get_chip_temperature()
# Get board name and normalise it # Get board name and normalise it
try:
identity = device.control.identify() identity = device.control.identify()
# Extract board name from identity string (e.g., "Hailo-8" -> "hailo8")
board_name = None board_name = None
for line in str(identity).split("\n"): for line in str(identity).split("\n"):
if line.startswith("Board Name:"): if line.startswith("Board Name:"):
@ -575,19 +573,21 @@ def get_hailo_temps() -> dict[str, float]:
if not board_name: if not board_name:
board_name = f"hailo{i}" board_name = f"hailo{i}"
except Exception:
board_name = f"hailo{i}"
# Use indexed name if multiple devices, otherwise just the board name # Use indexed name if multiple devices, otherwise just the board name
device_name = f"{board_name}-{i}" if len(device_ids) > 1 else board_name device_name = (
f"{board_name}-{i}" if len(device_ids) > 1 else board_name
)
# ts1_temperature is also available, but appeared to be the same as ts0 in testing. # ts1_temperature is also available, but appeared to be the same as ts0 in testing.
temps[device_name] = round(temp_info.ts0_temperature, 1) temps[device_name] = round(temp_info.ts0_temperature, 1)
device.release() except Exception as e:
except Exception: logger.debug(
f"Failed to get temperature for Hailo device {device_id}: {e}"
)
continue continue
except Exception: except Exception as e:
pass logger.debug(f"Failed to scan for Hailo devices: {e}")
return temps return temps