From f17524d72a1c2075a7b07ef2b296c0fb694d45ef Mon Sep 17 00:00:00 2001 From: hyh Date: Thu, 7 Aug 2025 16:36:36 +0800 Subject: [PATCH] daoru --- app/services/enhanced_adb_service.py | 29 +++++++++++++++++++------ modify.md | 32 +++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/app/services/enhanced_adb_service.py b/app/services/enhanced_adb_service.py index 565516f..1c332b2 100644 --- a/app/services/enhanced_adb_service.py +++ b/app/services/enhanced_adb_service.py @@ -84,10 +84,25 @@ class EnhancedAdbService: device_infos = [] for d in devices: try: - status = DeviceStatus(d.status) - except ValueError: - status = DeviceStatus.DISCONNECTED - device_infos.append(DeviceInfo(serial=d.serial, status=status)) + # 尝试将字符串状态转换为枚举 + if hasattr(d.status, 'value'): + status_str = d.status.value + else: + status_str = str(d.status) + + # 尝试转换为已知的状态枚举 + try: + status = DeviceStatus(status_str) + except ValueError: + # 如果状态不在已知枚举中,使用默认状态 + logger.warning(f"未知设备状态: {status_str}, 使用默认状态", serial=d.serial) + status = DeviceStatus.OFFLINE + + device_infos.append(DeviceInfo(serial=d.serial, status=status)) + except Exception as e: + logger.error(f"处理设备信息时出错: {d.serial}, 错误: {e}") + # 跳过有问题的设备 + continue logger.info("获取设备列表成功", device_count=len(device_infos)) return device_infos except (AdbConnectionError, AdbProtocolError) as e: @@ -126,7 +141,7 @@ class EnhancedAdbService: logger.info("设备事件", present=event.present, serial=event.serial, - status=event.status.value) + status=event.status) await self._handle_device_event(event) except asyncio.CancelledError: logger.info("设备监控被取消") @@ -139,9 +154,9 @@ class EnhancedAdbService: async def _handle_device_event(self, event: DeviceEvent) -> None: """处理设备事件""" if event.present: - logger.info("设备连接", serial=event.serial, status=event.status.value) + logger.info("设备连接", serial=event.serial, status=event.status) else: - logger.info("设备断开", serial=event.serial, status=event.status.value) + logger.info("设备断开", serial=event.serial, status=event.status) async def list_forward_ports(self) -> List[ForwardInfo]: """获取端口转发列表""" diff --git a/modify.md b/modify.md index c7e2255..2787137 100644 --- a/modify.md +++ b/modify.md @@ -144,4 +144,34 @@ - 监控启动/停止的详细日志 - 任务创建和取消的跟踪日志 - 异常情况的详细错误日志 -- 监控状态的实时反馈 \ No newline at end of file +- 监控状态的实时反馈 + +## 2025-08-07 - 修复设备状态处理错误 + +### 问题描述 +设备监控出现错误:`'str' object has no attribute 'value'` +- 错误发生在处理设备事件时,尝试访问 `event.status.value` +- 但 `event.status` 是字符串而不是枚举对象 + +### 修复方案 + +#### 1. 修复设备事件处理 (`app/services/enhanced_adb_service.py`) +- 修改 `_monitor_devices()` 方法,直接使用 `event.status` 而不是 `event.status.value` +- 修改 `_handle_device_event()` 方法,直接使用 `event.status` 而不是 `event.status.value` + +#### 2. 增强设备列表处理 +- 改进 `list_devices()` 方法中的状态转换逻辑 +- 添加对字符串状态和枚举状态的处理 +- 增加错误处理和日志记录 +- 使用 `DeviceStatus.OFFLINE` 作为默认状态而不是 `DISCONNECTED` + +### 修改内容 +- 修复了设备监控中的状态属性访问错误 +- 增强了设备状态转换的健壮性 +- 添加了详细的错误处理和日志记录 +- 改进了对未知设备状态的处理 + +### 影响 +- 修复了设备监控异常,现在可以正常监控设备连接/断开事件 +- 提高了设备状态处理的稳定性 +- 增强了错误恢复能力 \ No newline at end of file