Browse Source

11

hotfix/redundant-cleanup
root 4 months ago
parent
commit
19ed87fc05
  1. 28
      app/core/device/command_executor.py
  2. 85
      docs/modify.md

28
app/core/device/command_executor.py

@ -179,33 +179,43 @@ class CommandExecutor:
"""
try:
start_time = time.time()
device_id = device["device_id"]
device_source = device["source"]
command = cmd_task.command
logger.debug(f"开始执行单个命令: 设备={device_id}, 来源={device_source}, 命令={command}")
# 根据设备来源选择执行方式
if device.source == "registered":
if device_source == "registered":
logger.debug(f"注册设备执行命令: {device_id} -> {command}")
# 注册设备:通过设备分发器执行
result = await self.device_dispatcher.execute_operation(
device_id=device.device_id,
protocol_type=device.protocol_type,
device_id=device_id,
protocol_type=device["protocol_type"],
operation="execute_command",
command=cmd_task.command,
command=command,
timeout=cmd_task.timeout
)
logger.debug(f"注册设备命令执行完成: {device_id} -> {command}, 结果类型={type(result)}")
else:
logger.debug(f"自动发现设备执行命令: {device_id} -> {command}")
# 自动发现设备:直接调用ADB服务
from app.services.auto_discovery_adb_service import auto_discovery_adb_service
result = await auto_discovery_adb_service.execute_shell_command(
device.device_id,
cmd_task.command,
device_id,
command,
timeout=cmd_task.timeout
)
logger.debug(f"自动发现设备命令执行完成: {device_id} -> {command}, 结果类型={type(result)}")
execution_time = time.time() - start_time
# 统一使用实体返回格式
if isinstance(result, ShellResponse):
# ShellResponse实体格式
logger.debug(f"命令执行成功: {device_id} -> {command}, 输出长度={len(result.output) if result.output else 0}")
return ShellCommandResult(
command=cmd_task.command,
command=command,
success=result.success,
output=result.output,
error=result.error,
@ -216,9 +226,9 @@ class CommandExecutor:
)
else:
# 其他格式统一转换为失败结果
logger.warning(f"命令执行返回格式不符合规范: {type(result)}")
logger.warning(f"命令执行返回格式不符合规范: {device_id} -> {command}, 返回类型={type(result)}")
return ShellCommandResult(
command=cmd_task.command,
command=command,
success=False,
output="",
error=f"返回格式不符合规范: {type(result)}",

85
docs/modify.md

@ -1662,91 +1662,6 @@ channel_name = f"{client_name}_send"
5. ✅ 修复了 `_heartbeat_manager` 不存在的错误引用
6. ✅ 统一了心跳任务管理:由 `send_controller` 统一管理
## 2025-08-14 DeviceManager 重构完成
### 重构概述
按照 `DeviceManager_Refactor_Plan.md` 中的计划,完成了 DeviceManager 的重构,实现了职责分离、代码复用和性能优化。
### 新增文件
1. **app/core/device/event_types.py** - 事件类型枚举
- 定义了 `EventType` 枚举,包含设备连接、断开、状态更新等事件类型
- 消除了硬编码的事件类型字符串
2. **app/core/device/event_logger.py** - 事件日志记录器
- 实现了 `DeviceEventLogger` 类,统一处理事件相关的日志记录
- 提供了事件添加、客户端注册、缓冲事件摘要等日志方法
- 减少了代码重复,提高了日志记录的一致性
3. **app/core/device/lock_manager.py** - 锁管理器
- 实现了 `DeviceLockManager` 类和 `with_lock` 装饰器
- 提供了统一的锁管理机制,确保线程安全
- 简化了锁的使用,减少了死锁风险
4. **app/core/device/registry.py** - 设备注册表
- 实现了 `DeviceRegistry` 类,专门负责设备注册、注销、查询
- 包含设备连接管理功能
- 使用锁保护确保并发安全
5. **app/core/device/event_manager.py** - 事件管理器
- 实现了 `EventManager` 类,专门负责事件缓冲和推送
- 包含 `DeviceEvent` 数据类
- 实现了完整的事件推送循环和WebSocket客户端管理
6. **app/core/device/websocket_bridge.py** - WebSocket桥接器
- 实现了 `WebSocketBridge` 类,专门负责事件推送到WebSocket客户端
- 提供了客户端验证和批量推送功能
### 重构后的主文件
**app/core/device/manager.py** - 重构后的设备管理器
- 使用组合模式,将职责分配给专门的组件
- 大幅简化了主类代码,从721行减少到约300行
- 保持了原有的API接口,确保向后兼容
- 提高了代码的可测试性和可维护性
### 测试验证
创建了 `test_refactored_device_manager.py` 测试文件,验证了以下功能:
1. ✅ 设备注册和注销
2. ✅ 设备状态更新
3. ✅ 设备查询和列表获取
4. ✅ 自动发现设备事件处理
5. ✅ 统一设备列表获取
6. ✅ 资源清理
### 重构收益
1. **代码质量提升**
- 代码重复减少约60%
- 方法复杂度降低约40%
- 职责分离,每个组件专注于单一职责
2. **可维护性提升**
- 模块化设计,便于独立测试和修改
- 统一的日志记录机制
- 清晰的依赖关系
3. **性能优化**
- 锁粒度优化,减少锁竞争
- 事件推送逻辑优化
- 内存使用优化
4. **可扩展性提升**
- 新的事件类型可以轻松添加
- 新的组件可以独立开发和集成
- 支持依赖注入,便于测试
### 技术亮点
1. **装饰器模式** - 使用 `@with_lock` 装饰器简化锁管理
2. **组合模式** - 通过组合多个专门组件实现复杂功能
3. **事件驱动** - 基于事件的设计,支持异步处理
4. **结构化日志** - 统一的日志记录格式和级别
5. **类型安全** - 使用类型注解提高代码质量
### 兼容性
- 保持了原有的公共API接口
- 全局 `device_manager` 实例保持不变
- 所有现有调用代码无需修改
重构完成,系统运行正常,所有测试通过!
## 2025-08-14
## 2025-08-14

Loading…
Cancel
Save