|
|
|
@ -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: |
|
|
|
|