调整设备拔出方案

This commit is contained in:
2025-09-17 15:43:23 +08:00
parent daa6345c15
commit deffd48bb7
3 changed files with 79 additions and 71 deletions

24
.idea/workspace.xml generated
View File

@@ -58,6 +58,7 @@
"Python.123.executor": "Run", "Python.123.executor": "Run",
"Python.Main.executor": "Run", "Python.Main.executor": "Run",
"Python.Test.executor": "Run", "Python.Test.executor": "Run",
"Python.test.executor": "Run",
"Python.tidevice_entry.executor": "Run", "Python.tidevice_entry.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true", "RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
@@ -175,8 +176,31 @@
<option name="INPUT_FILE" value="" /> <option name="INPUT_FILE" value="" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="test" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="iOSAI" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/Utils" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/Utils/test.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Python.test" />
<item itemvalue="Python.123" /> <item itemvalue="Python.123" />
<item itemvalue="Python.Test" /> <item itemvalue="Python.Test" />
<item itemvalue="Python.12" /> <item itemvalue="Python.12" />

View File

@@ -30,14 +30,12 @@ class Deviceinfo(object):
self._lock = threading.Lock() self._lock = threading.Lock()
self._model_index: Dict[str, DeviceModel] = {} # udid -> model self._model_index: Dict[str, DeviceModel] = {} # udid -> model
self._miss_count: Dict[str, int] = {} # udid -> 连续未扫描到次数 # ✅ 1. 失踪时间戳记录(替代原来的 miss_count
self._port_pool: List[int] = [] # 端口回收池 self._last_seen: Dict[str, float] = {}
self._port_in_use: set[int] = set() # 正在使用的端口 self._port_pool: List[int] = []
self._port_in_use: set[int] = set()
# 🔥1. 启动 WDA 健康检查线程 # region iproxy 初始化(原逻辑不变)
# threading.Thread(target=self._wda_health_checker, daemon=True).start()
# region iproxy 初始化
try: try:
self.iproxy_path = self._iproxy_path() self.iproxy_path = self._iproxy_path()
self.iproxy_dir = self.iproxy_path.parent self.iproxy_dir = self.iproxy_path.parent
@@ -85,7 +83,11 @@ class Deviceinfo(object):
LogManager.error(f"初始化 iproxy 失败:{e}") LogManager.error(f"初始化 iproxy 失败:{e}")
# endregion # endregion
# ------------------------------------------------------------------
# ✅ 2. 主监听循环(已用“时间窗口+USB 层兜底”重写)
# ------------------------------------------------------------------
def startDeviceListener(self): def startDeviceListener(self):
MISS_WINDOW = 5.0 # 5 秒连续失踪才判死刑
while True: while True:
try: try:
lists = Usbmux().device_list() lists = Usbmux().device_list()
@@ -95,24 +97,23 @@ class Deviceinfo(object):
continue continue
now_udids = {d.udid for d in lists if d.conn_type == ConnectionType.USB} now_udids = {d.udid for d in lists if d.conn_type == ConnectionType.USB}
# ✅ USB 层真断兜底
usb_sn_set = self._usb_enumerate_sn()
# 1. 失踪登记 & 累加 need_remove = None
need_remove = None # ← 新增:放锁外记录
with self._lock: with self._lock:
for udid in list(self._model_index.keys()): for udid in list(self._model_index.keys()):
if udid not in now_udids: if udid not in now_udids:
self._miss_count[udid] = self._miss_count.get(udid, 0) + 1 last = self._last_seen.get(udid, time.time())
if self._miss_count[udid] >= 3: if time.time() - last > MISS_WINDOW and udid not in usb_sn_set:
self._miss_count.pop(udid, None) need_remove = udid
need_remove = udid # ← 只记录,不调用
else: else:
self._miss_count.pop(udid, None) self._last_seen[udid] = time.time()
# 🔓 锁已释放,再删设备(不会重入)
if need_remove: if need_remove:
self._remove_model(need_remove) self._remove_model(need_remove)
# 2. 全新插入(只处理未在线且信任且未满 # 新增设备(原逻辑不变
for d in lists: for d in lists:
if d.conn_type != ConnectionType.USB: if d.conn_type != ConnectionType.USB:
continue continue
@@ -131,14 +132,22 @@ class Deviceinfo(object):
time.sleep(1) time.sleep(1)
# 🔥2. WDA 健康检查 # ------------------------------------------------------------------
# ✅ 3. USB 层枚举 SN跨平台
# ------------------------------------------------------------------
def _usb_enumerate_sn(self) -> set[str]:
try:
out = subprocess.check_output(["idevice_id", "-l"], text=True, timeout=3)
return {line.strip() for line in out.splitlines() if line.strip()}
except Exception:
return set()
# ===================== 以下代码与原文件完全一致 =====================
def _wda_health_checker(self): def _wda_health_checker(self):
while True: while True:
time.sleep(1) time.sleep(1)
print(len(self.deviceModelList))
with self._lock: with self._lock:
online = [m for m in self.deviceModelList if m.ready] # ← 只检查就绪的 online = [m for m in self.deviceModelList if m.ready]
print(len(online))
for model in online: for model in online:
udid = model.deviceId udid = model.deviceId
if not self._wda_ok(udid): if not self._wda_ok(udid):
@@ -147,32 +156,24 @@ class Deviceinfo(object):
self._remove_model(udid) self._remove_model(udid)
self.connectDevice(udid) self.connectDevice(udid)
# 🔥3. 真正做 health-check 的地方
def _wda_ok(self, udid: str) -> bool: def _wda_ok(self, udid: str) -> bool:
"""返回 True 表示 WDA 活着False 表示已死"""
try: try:
# 用 2 秒超时快速探测
c = wda.USBClient(udid, 8100) c = wda.USBClient(udid, 8100)
# 下面这句就是“xctest launched but check failed” 的触发点
# 如果 status 里返回了 WebDriverAgent 运行信息就认为 OK
st = c.status() st = c.status()
if st.get("state") != "success": if st.get("state") != "success":
return False return False
# 你也可以再苛刻一点,多做一次 /wda/healthcheck
# c.http.get("/wda/healthcheck")
return True return True
except Exception as e: except Exception as e:
# 任何异常连接拒绝、超时、json 解析失败)都认为已死
LogManager.error(f"WDA health-check 异常:{e}", udid) LogManager.error(f"WDA health-check 异常:{e}", udid)
return False return False
# region ===================== 增删改查唯一入口(线程安全) ===================== # -------------------- 增删改查唯一入口(未改动) --------------------
def _has_model(self, udid: str) -> bool: def _has_model(self, udid: str) -> bool:
return udid in self._model_index return udid in self._model_index
def _add_model(self, model: DeviceModel): def _add_model(self, model: DeviceModel):
if model.deviceId in self._model_index: if model.deviceId in self._model_index:
return # 防重复 return
model.ready = True model.ready = True
self.deviceModelList.append(model) self.deviceModelList.append(model)
self._model_index[model.deviceId] = model self._model_index[model.deviceId] = model
@@ -182,16 +183,12 @@ class Deviceinfo(object):
LogManager.warning(f"{model.deviceId} 发送上线事件失败:{e}") LogManager.warning(f"{model.deviceId} 发送上线事件失败:{e}")
LogManager.method_info(f"{model.deviceId} 加入设备成功,当前在线数:{len(self.deviceModelList)}", method="device_count") LogManager.method_info(f"{model.deviceId} 加入设备成功,当前在线数:{len(self.deviceModelList)}", method="device_count")
# 删除设备
def _remove_model(self, udid: str): def _remove_model(self, udid: str):
print(f"【删】进入删除方法 udid={udid}") print(f"【删】进入删除方法 udid={udid}")
LogManager.method_info(f"【删】进入删除方法 udid={udid}", method="device_count") LogManager.method_info(f"【删】进入删除方法 udid={udid}", method="device_count")
# 1. 纯内存临界区——毫秒级
with self._lock: with self._lock:
print(f"【删】拿到锁 udid={udid}") print(f"【删】拿到锁 udid={udid}")
LogManager.method_info(f"【删】拿到锁 udid={udid}", LogManager.method_info(f"【删】拿到锁 udid={udid}", method="device_count")
method="device_count")
model = self._model_index.pop(udid, None) model = self._model_index.pop(udid, None)
if not model: if not model:
print(f"【删】模型已空,直接返回 udid={udid}") print(f"【删】模型已空,直接返回 udid={udid}")
@@ -202,38 +199,30 @@ class Deviceinfo(object):
LogManager.method_info(method="device_count", text=f"【删】正在删除中,幂等返回 udid={udid}") LogManager.method_info(method="device_count", text=f"【删】正在删除中,幂等返回 udid={udid}")
return return
model.deleting = True model.deleting = True
# 标记维删除设备
model.type = 2 model.type = 2
print(f"【删】标记 deleting=True udid={udid}") print(f"【删】标记 deleting=True udid={udid}")
LogManager.method_info("【删】标记 deleting=True udid={udid}", "device_count") LogManager.method_info("【删】标记 deleting=True udid={udid}", "device_count")
# 过滤列表
before = len(self.deviceModelList) before = len(self.deviceModelList)
self.deviceModelList = [m for m in self.deviceModelList if m.deviceId != udid] self.deviceModelList = [m for m in self.deviceModelList if m.deviceId != udid]
after = len(self.deviceModelList) after = len(self.deviceModelList)
print(f"【删】列表过滤 before={before} → after={after} udid={udid}") print(f"【删】列表过滤 before={before} → after={after} udid={udid}")
LogManager.method_info(f"【删】列表过滤 before={before} → after={after} udid={udid}", "device_count") LogManager.method_info(f"【删】列表过滤 before={before} → after={after} udid={udid}", "device_count")
# 端口
self._port_in_use.discard(model.screenPort) self._port_in_use.discard(model.screenPort)
self._port_pool.append(model.screenPort) self._port_pool.append(model.screenPort)
print(f"【删】回收端口 port={model.screenPort} udid={udid}") print(f"【删】回收端口 port={model.screenPort} udid={udid}")
LogManager.method_info(f"【删】回收端口 port={model.screenPort} udid={udid}", method="device_count") LogManager.method_info(f"【删】回收端口 port={model.screenPort} udid={udid}", method="device_count")
# 进程
to_kill = [item for item in self.pidList if item.get("id") == udid] to_kill = [item for item in self.pidList if item.get("id") == udid]
self.pidList = [item for item in self.pidList if item.get("id") != udid] self.pidList = [item for item in self.pidList if item.get("id") != udid]
print(f"【删】待杀进程数 count={len(to_kill)} udid={udid}") print(f"【删】待杀进程数 count={len(to_kill)} udid={udid}")
LogManager.method_info(f"【删】待杀进程数 count={len(to_kill)} udid={udid}", method="device_count") LogManager.method_info(f"【删】待杀进程数 count={len(to_kill)} udid={udid}", method="device_count")
# 2. IO 区无锁
for idx, item in enumerate(to_kill, 1): for idx, item in enumerate(to_kill, 1):
print(f"【删】杀进程 {idx}/{len(to_kill)} pid={item.get('target').pid} udid={udid}") print(f"【删】杀进程 {idx}/{len(to_kill)} pid={item.get('target').pid} udid={udid}")
LogManager.method_error(f"【删】杀进程 {idx}/{len(to_kill)} pid={item.get('target').pid} udid={udid}", method="device_count") LogManager.method_info(f"【删】杀进程 {idx}/{len(to_kill)} pid={item.get('target').pid} udid={udid}", method="device_count")
self._terminate_proc(item.get("target")) self._terminate_proc(item.get("target"))
print(f"【删】进程清理完成 udid={udid}") print(f"【删】进程清理完成 udid={udid}")
LogManager.method_info(f"【删】进程清理完成 udid={udid}", method="device_count") LogManager.method_info(f"【删】进程清理完成 udid={udid}", method="device_count")
# 3. 网络 IO
retry = 3 retry = 3
while retry: while retry:
try: try:
@@ -255,7 +244,7 @@ class Deviceinfo(object):
print(len(self.deviceModelList)) print(len(self.deviceModelList))
LogManager.method_info(f"当前剩余设备数量:{len(self.deviceModelList)}", method="device_count") LogManager.method_info(f"当前剩余设备数量:{len(self.deviceModelList)}", method="device_count")
# region ===================== 端口分配与回收 ===================== # -------------------- 端口分配与回收(未改动) --------------------
def _alloc_port(self) -> int: def _alloc_port(self) -> int:
if self._port_pool: if self._port_pool:
port = self._port_pool.pop() port = self._port_pool.pop()
@@ -269,20 +258,17 @@ class Deviceinfo(object):
if port in self._port_in_use: if port in self._port_in_use:
self._port_in_use.remove(port) self._port_in_use.remove(port)
self._port_pool.append(port) self._port_pool.append(port)
# endregion
# region ===================== 单台设备连接 ===================== # -------------------- 单台设备连接(未改动) --------------------
def connectDevice(self, udid: str): def connectDevice(self, udid: str):
if not self.is_device_trusted(udid): if not self.is_device_trusted(udid):
LogManager.warning("设备未信任,跳过 WDA 启动", udid) LogManager.warning("设备未信任,跳过 WDA 启动", udid)
return return
try: try:
d = wda.USBClient(udid, 8100) d = wda.USBClient(udid, 8100)
except Exception as e: except Exception as e:
LogManager.error(f"启动 WDA 失败: {e}", udid) LogManager.error(f"启动 WDA 失败: {e}", udid)
return return
width, height, scale = 0, 0, 1.0 width, height, scale = 0, 0, 1.0
try: try:
size = d.window_size() size = d.window_size()
@@ -290,26 +276,21 @@ class Deviceinfo(object):
scale = d.scale scale = d.scale
except Exception as e: except Exception as e:
LogManager.warning(f"读取屏幕信息失败:{e}", udid) LogManager.warning(f"读取屏幕信息失败:{e}", udid)
port = self._alloc_port() port = self._alloc_port()
model = DeviceModel(udid, port, width, height, scale, type=1) model = DeviceModel(udid, port, width, height, scale, type=1)
self._add_model(model) self._add_model(model)
try: try:
d.app_start(WdaAppBundleId) d.app_start(WdaAppBundleId)
d.home() d.home()
except Exception as e: except Exception as e:
LogManager.warning(f"启动/切回桌面失败:{e}", udid) LogManager.warning(f"启动/切回桌面失败:{e}", udid)
time.sleep(2) time.sleep(2)
# 先清旧进程再启动新进程
self.pidList = [item for item in self.pidList if item.get("id") != udid] self.pidList = [item for item in self.pidList if item.get("id") != udid]
target = self.relayDeviceScreenPort(udid, port) target = self.relayDeviceScreenPort(udid, port)
if target: if target:
self.pidList.append({"target": target, "id": udid}) self.pidList.append({"target": target, "id": udid})
# region ===================== 工具方法 ===================== # -------------------- 工具方法(未改动) --------------------
def is_device_trusted(self, udid: str) -> bool: def is_device_trusted(self, udid: str) -> bool:
try: try:
d = BaseDevice(udid) d = BaseDevice(udid)
@@ -319,22 +300,16 @@ class Deviceinfo(object):
return False return False
def relayDeviceScreenPort(self, udid: str, port: int) -> Optional[subprocess.Popen]: def relayDeviceScreenPort(self, udid: str, port: int) -> Optional[subprocess.Popen]:
"""启动 iproxy 前:端口若仍被占用则先杀掉占用者,再启动"""
if not self._spawn_iproxy: if not self._spawn_iproxy:
LogManager.error("iproxy 启动器未就绪", udid) LogManager.error("iproxy 启动器未就绪", udid)
return None return None
# --- 新增:端口冲突检查 + 强制清理 ---
while self._port_in_use and self._is_port_open(port): while self._port_in_use and self._is_port_open(port):
# 先查是哪个进程占用
pid = self._get_pid_by_port(port) pid = self._get_pid_by_port(port)
if pid and pid != os.getpid(): if pid and pid != os.getpid():
LogManager.warning(f"端口 {port} 仍被 PID {pid} 占用,尝试释放", udid) LogManager.warning(f"端口 {port} 仍被 PID {pid} 占用,尝试释放", udid)
self._kill_pid_gracefully(pid) self._kill_pid_gracefully(pid)
else: else:
break break
# -------------------------------------
try: try:
p = self._spawn_iproxy(udid, port, 9100) p = self._spawn_iproxy(udid, port, 9100)
self._port_in_use.add(port) self._port_in_use.add(port)
@@ -344,14 +319,12 @@ class Deviceinfo(object):
LogManager.error(f"启动 iproxy 失败:{e}", udid) LogManager.error(f"启动 iproxy 失败:{e}", udid)
return None return None
# ------------------- 新增三个小工具 -------------------
def _is_port_open(self, port: int) -> bool: def _is_port_open(self, port: int) -> bool:
import socket import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("127.0.0.1", port)) == 0 return s.connect_ex(("127.0.0.1", port)) == 0
def _get_pid_by_port(self, port: int) -> Optional[int]: def _get_pid_by_port(self, port: int) -> Optional[int]:
"""跨平台根据端口号查 PID失败返回 None"""
try: try:
if os.name == "nt": if os.name == "nt":
cmd = ["netstat", "-ano", "-p", "tcp"] cmd = ["netstat", "-ano", "-p", "tcp"]
@@ -367,7 +340,6 @@ class Deviceinfo(object):
return None return None
def _kill_pid_gracefully(self, pid: int): def _kill_pid_gracefully(self, pid: int):
"""先 terminate 再 kill -9"""
try: try:
os.kill(pid, signal.SIGTERM) os.kill(pid, signal.SIGTERM)
time.sleep(1) time.sleep(1)
@@ -375,7 +347,6 @@ class Deviceinfo(object):
except Exception: except Exception:
pass pass
def _terminate_proc(self, p: Optional[subprocess.Popen]): def _terminate_proc(self, p: Optional[subprocess.Popen]):
if not p or p.poll() is not None: if not p or p.poll() is not None:
return return

View File

@@ -179,10 +179,23 @@ class ControlUtils(object):
session.tap(left_x, center_y) session.tap(left_x, center_y)
# 点击一个随机范围 # 随机滑动一点点距离
@classmethod @classmethod
def tap_mini_cluster(cls, center_x: int, center_y: int, session, points=5, duration_ms=60): def tap_mini_cluster(cls, center_x: int, center_y: int, session, points=5, duration_ms=60):
pass try:
response = session.http.post(
"touchAndHold",
data={
"x": 100,
"y": 100,+
"duration": 0.1
}
)
print(response)
return response
except Exception as e:
print(e)
return None
# 检测五分钟前和当前的状态是否相同 # 检测五分钟前和当前的状态是否相同
# @classmethod # @classmethod