Browse Source

daoru

master
hyh 4 months ago
parent
commit
d9dfa9a750
  1. 39
      app/services/enhanced_adb_service.py
  2. 13
      modify.md

39
app/services/enhanced_adb_service.py

@ -6,7 +6,7 @@ from typing import List, Optional, AsyncGenerator
from app.core.adb import AdbClient
from app.models.adb_models import (
DeviceInfo, DeviceEvent, ForwardInfo, ServerInfo,
ShellCommand, ShellResponse, ForwardRequest
ShellCommand, ShellResponse, ForwardRequest, DeviceStatus
)
from app.utils.log import get_enhanced_logger, LogLevel
from app.core.exceptions import AdbConnectionError, AdbProtocolError
@ -18,7 +18,9 @@ class EnhancedAdbService:
"""增强的ADB服务类"""
def __init__(self, host: Optional[str] = None, port: Optional[int] = None):
self._client = AdbClient(host, port)
self._client = AdbClient()
self._host = host
self._port = port
self._device_monitor_task: Optional[asyncio.Task] = None
# 程序启动时自动开始监听
self._start_monitoring()
@ -33,9 +35,9 @@ class EnhancedAdbService:
"""获取ADB服务器信息"""
try:
logger.debug("获取ADB服务器信息")
info = await self._client.get_server_info()
logger.info("获取ADB服务器信息成功", version=info.version, host=info.host, port=info.port)
return info
version = await self._client.server_version()
logger.info("获取ADB服务器信息成功", version=version, host=self._host, port=self._port)
return ServerInfo(version=version, host=self._host or "localhost", port=self._port or 5037, status="running")
except (AdbConnectionError, AdbProtocolError) as e:
logger.error("获取ADB服务器信息失败", error=str(e))
raise
@ -47,9 +49,20 @@ class EnhancedAdbService:
"""获取设备列表"""
try:
logger.debug("获取设备列表", status_filter=status_filter)
devices = await self._client.list_devices(status_filter)
logger.info("获取设备列表成功", device_count=len(devices))
return devices
devices = await self._client.devices()
# 过滤设备状态
if status_filter:
devices = [d for d in devices if d.status in status_filter]
# 转换为DeviceInfo对象
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))
logger.info("获取设备列表成功", device_count=len(device_infos))
return device_infos
except (AdbConnectionError, AdbProtocolError) as e:
logger.error("获取设备列表失败", error=str(e))
raise
@ -62,7 +75,7 @@ class EnhancedAdbService:
try:
logger.debug("执行Shell命令", device_serial=device_serial, command=command, timeout=timeout)
output = await asyncio.wait_for(
self._client.execute_shell_command(device_serial, command),
self._client.shell(device_serial, command),
timeout=timeout
)
logger.info("Shell命令执行成功", device_serial=device_serial, command=command, output_length=len(output))
@ -107,7 +120,13 @@ class EnhancedAdbService:
"""获取端口转发列表"""
try:
logger.debug("获取端口转发列表")
forwards = await self._client.list_forward_ports()
forwards = []
async for forward in self._client.forward_list():
forwards.append(ForwardInfo(
serial=forward.serial,
local=forward.local,
remote=forward.remote
))
logger.info("获取端口转发列表成功", forward_count=len(forwards))
return forwards
except Exception as e:

13
modify.md

@ -58,4 +58,15 @@
- 将 `from app.core.adb_client import AdbClient` 改为 `from app.core.adb import AdbClient`
- 将 `from app.core.adb_exceptions import ...` 改为 `from app.core.exceptions import ...`
- 修复了 `test_exception_handling.py` 中的多个导入路径错误
- 解决了 "No module named 'app.core.adb_client'" 的导入错误
- 解决了 "No module named 'app.core.adb_client'" 的导入错误
### AdbClient API适配修复
- 修复了 `app/services/enhanced_adb_service.py``AdbClient` 构造函数调用错误
- 将 `AdbClient(host, port)` 改为 `AdbClient()`,并将host和port保存为实例变量
- 修复了方法调用不匹配的问题:
- `get_server_info()` 改为使用 `server_version()`
- `list_devices()` 改为使用 `devices()`
- `execute_shell_command()` 改为使用 `shell()`
- `list_forward_ports()` 改为使用 `forward_list()`
- 添加了状态字符串到枚举的转换逻辑
- 解决了 "AdbClient.__init__() takes 1 positional argument but 3 were given" 错误
Loading…
Cancel
Save