You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.1 KiB

"""
ADB工具模块 - 提供常用的ADB操作函数
"""
import re
import subprocess
from typing import List, Optional, Dict, Any
from app.utils.log import get_logger
logger = get_logger(__name__)
class AdbUtils:
"""ADB工具类"""
@staticmethod
def check_adb_installed() -> bool:
"""检查ADB是否已安装"""
try:
result = subprocess.run(['adb', 'version'],
capture_output=True, text=True, timeout=10)
return result.returncode == 0
except (subprocess.TimeoutExpired, FileNotFoundError):
return False
@staticmethod
def start_adb_server() -> bool:
"""启动ADB服务器"""
try:
result = subprocess.run(['adb', 'start-server'],
capture_output=True, text=True, timeout=30)
return result.returncode == 0
except Exception as e:
logger.error(f"启动ADB服务器失败: {e}")
return False
@staticmethod
def get_device_property(device_serial: str, property_name: str) -> Optional[str]:
"""获取设备属性"""
try:
result = subprocess.run(
['adb', '-s', device_serial, 'shell', 'getprop', property_name],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0:
return result.stdout.strip()
return None
except Exception as e:
logger.error(f"获取设备属性失败: {device_serial}, 属性: {property_name}, 错误: {e}")
return None
@staticmethod
def tap_screen(device_serial: str, x: int, y: int) -> bool:
"""点击屏幕"""
try:
result = subprocess.run(
['adb', '-s', device_serial, 'shell', 'input', 'tap', str(x), str(y)],
capture_output=True, text=True, timeout=10
)
return result.returncode == 0
except Exception as e:
logger.error(f"点击屏幕失败: {device_serial}, 坐标: ({x}, {y}), 错误: {e}")
return False