修复tidevice报错问题
This commit is contained in:
47
.idea/workspace.xml
generated
47
.idea/workspace.xml
generated
@@ -5,7 +5,8 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="eceeff5e-51c1-459c-a911-d21ec090a423" name="Changes" comment="ai 开始测试">
|
<list default="true" id="eceeff5e-51c1-459c-a911-d21ec090a423" name="Changes" comment="ai 开始测试">
|
||||||
<change beforePath="$PROJECT_DIR$/tidevice_entry.py" beforeDir="false" afterPath="$PROJECT_DIR$/tidevice_entry.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/Module/DeviceInfo.py" beforeDir="false" afterPath="$PROJECT_DIR$/Module/DeviceInfo.py" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@@ -45,27 +46,27 @@
|
|||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent">{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"ASKED_ADD_EXTERNAL_FILES": "true",
|
"ASKED_ADD_EXTERNAL_FILES": "true",
|
||||||
"Python.123.executor": "Run",
|
"Python.123.executor": "Run",
|
||||||
"Python.Main.executor": "Run",
|
"Python.Main.executor": "Run",
|
||||||
"Python.tidevice_entry.executor": "Run",
|
"Python.tidevice_entry.executor": "Run",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
|
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
|
||||||
"git-widget-placeholder": "main",
|
"git-widget-placeholder": "main",
|
||||||
"javascript.nodejs.core.library.configured.version": "22.18.0",
|
"javascript.nodejs.core.library.configured.version": "22.18.0",
|
||||||
"javascript.nodejs.core.library.typings.version": "22.18.1",
|
"javascript.nodejs.core.library.typings.version": "22.18.1",
|
||||||
"last_opened_file_path": "F:/company code/AI item/20250820/iOSAI",
|
"last_opened_file_path": "F:/company code/AI item/20250820/iOSAI",
|
||||||
"node.js.detected.package.eslint": "true",
|
"node.js.detected.package.eslint": "true",
|
||||||
"node.js.detected.package.tslint": "true",
|
"node.js.detected.package.tslint": "true",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"node.js.selected.package.tslint": "(autodetect)",
|
"node.js.selected.package.tslint": "(autodetect)",
|
||||||
"nodejs_package_manager_path": "npm",
|
"nodejs_package_manager_path": "npm",
|
||||||
"settings.editor.selected.configurable": "com.gitee.ui.GiteeSettingsConfigurable",
|
"settings.editor.selected.configurable": "com.gitee.ui.GiteeSettingsConfigurable",
|
||||||
"vue.rearranger.settings.migration": "true"
|
"vue.rearranger.settings.migration": "true"
|
||||||
}
|
}
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="MoveFile.RECENT_KEYS">
|
<key name="MoveFile.RECENT_KEYS">
|
||||||
<recent name="E:\Code\python\iOSAI\resources" />
|
<recent name="E:\Code\python\iOSAI\resources" />
|
||||||
@@ -179,6 +180,8 @@
|
|||||||
<workItem from="1756962238298" duration="14230000" />
|
<workItem from="1756962238298" duration="14230000" />
|
||||||
<workItem from="1756979981948" duration="4536000" />
|
<workItem from="1756979981948" duration="4536000" />
|
||||||
<workItem from="1757053266703" duration="9092000" />
|
<workItem from="1757053266703" duration="9092000" />
|
||||||
|
<workItem from="1757308358620" duration="654000" />
|
||||||
|
<workItem from="1757309200278" duration="986000" />
|
||||||
</task>
|
</task>
|
||||||
<task id="LOCAL-00001" summary="ai 开始测试">
|
<task id="LOCAL-00001" summary="ai 开始测试">
|
||||||
<option name="closed" value="true" />
|
<option name="closed" value="true" />
|
||||||
@@ -215,7 +218,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="com.intellij.coverage.CoverageDataManagerImpl">
|
<component name="com.intellij.coverage.CoverageDataManagerImpl">
|
||||||
<SUITE FILE_PATH="coverage/iOSAI$123__1_.coverage" NAME="123 (1) 覆盖结果" MODIFIED="1756897091135" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
<SUITE FILE_PATH="coverage/iOSAI$123__1_.coverage" NAME="123 (1) 覆盖结果" MODIFIED="1756897091135" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
||||||
<SUITE FILE_PATH="coverage/iOSAI$Main.coverage" NAME="Main Coverage Results" MODIFIED="1757070933006" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
|
<SUITE FILE_PATH="coverage/iOSAI$Main.coverage" NAME="Main Coverage Results" MODIFIED="1757309807138" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
|
||||||
<SUITE FILE_PATH="coverage/iOSAI$mac_wda_agent.coverage" NAME="mac_wda_agent Coverage Results" MODIFIED="1756473148639" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/script" />
|
<SUITE FILE_PATH="coverage/iOSAI$mac_wda_agent.coverage" NAME="mac_wda_agent Coverage Results" MODIFIED="1756473148639" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/script" />
|
||||||
<SUITE FILE_PATH="coverage/iOSAI$tidevice_entry.coverage" NAME="tidevice_entry Coverage Results" MODIFIED="1756886706033" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
<SUITE FILE_PATH="coverage/iOSAI$tidevice_entry.coverage" NAME="tidevice_entry Coverage Results" MODIFIED="1756886706033" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
|
||||||
<SUITE FILE_PATH="coverage/iOSAI$ScriptManager.coverage" NAME="ScriptManager 覆盖结果" MODIFIED="1756896057801" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/script" />
|
<SUITE FILE_PATH="coverage/iOSAI$ScriptManager.coverage" NAME="ScriptManager 覆盖结果" MODIFIED="1756896057801" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/script" />
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from pathlib import Path
|
|||||||
from typing import List, Dict, Optional
|
from typing import List, Dict, Optional
|
||||||
|
|
||||||
from tidevice import Usbmux, ConnectionType
|
from tidevice import Usbmux, ConnectionType
|
||||||
|
from tidevice._device import BaseDevice
|
||||||
from Entity.DeviceModel import DeviceModel
|
from Entity.DeviceModel import DeviceModel
|
||||||
from Entity.Variables import WdaAppBundleId
|
from Entity.Variables import WdaAppBundleId
|
||||||
from Module.FlaskSubprocessManager import FlaskSubprocessManager
|
from Module.FlaskSubprocessManager import FlaskSubprocessManager
|
||||||
@@ -20,36 +21,25 @@ from Utils.LogManager import LogManager
|
|||||||
class Deviceinfo(object):
|
class Deviceinfo(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.deviceIndex = 0
|
self.deviceIndex = 0
|
||||||
# 投屏端口(本地映射端口起始值,会递增)
|
|
||||||
self.screenProxy = 9110
|
self.screenProxy = 9110
|
||||||
# 记录 iproxy Popen 进程:[{ "id": udid, "target": Popen }, ...]
|
|
||||||
self.pidList: List[Dict] = []
|
self.pidList: List[Dict] = []
|
||||||
# 当前已连接的设备(tidevice 的 Device 对象列表)
|
|
||||||
self.deviceArray: List = []
|
self.deviceArray: List = []
|
||||||
|
|
||||||
# 子进程通信(向前端发送设备信息)
|
|
||||||
self.manager = FlaskSubprocessManager.get_instance()
|
self.manager = FlaskSubprocessManager.get_instance()
|
||||||
# 已发给前端的设备模型列表(用于拔出时发 type=2)
|
|
||||||
self.deviceModelList: List[DeviceModel] = []
|
self.deviceModelList: List[DeviceModel] = []
|
||||||
# 最大可连接设备限制
|
|
||||||
self.maxDeviceCount = 6
|
self.maxDeviceCount = 6
|
||||||
# 操作锁
|
|
||||||
self._lock = threading.Lock()
|
self._lock = threading.Lock()
|
||||||
|
self._pending_udids = set()
|
||||||
|
|
||||||
|
|
||||||
# ===== 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
|
||||||
|
|
||||||
# 1) 配置环境(PATH/DLL),放到初始化里一次性处理
|
|
||||||
os.environ["PATH"] = str(self.iproxy_dir) + os.pathsep + os.environ.get("PATH", "")
|
os.environ["PATH"] = str(self.iproxy_dir) + os.pathsep + os.environ.get("PATH", "")
|
||||||
try:
|
try:
|
||||||
# 仅 Windows 有效;其他平台忽略
|
|
||||||
os.add_dll_directory(str(self.iproxy_dir))
|
os.add_dll_directory(str(self.iproxy_dir))
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# 2) 预构建通用 Popen 参数(隐藏窗口、工作目录、文本模式等)
|
|
||||||
self._creationflags = 0x08000000 if os.name == "nt" else 0
|
self._creationflags = 0x08000000 if os.name == "nt" else 0
|
||||||
self._popen_kwargs = dict(
|
self._popen_kwargs = dict(
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
@@ -62,12 +52,10 @@ class Deviceinfo(object):
|
|||||||
bufsize=1,
|
bufsize=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 3) 准备一个“启动器”(闭包):仅接受 (udid, local_port, remote_port) 参数
|
|
||||||
def _spawn_iproxy(udid: str, local_port: int, remote_port: int = 9100) -> subprocess.Popen:
|
def _spawn_iproxy(udid: str, local_port: int, remote_port: int = 9100) -> subprocess.Popen:
|
||||||
args = [str(self.iproxy_path), "-u", udid, str(local_port), str(remote_port)]
|
args = [str(self.iproxy_path), "-u", udid, str(local_port), str(remote_port)]
|
||||||
p = subprocess.Popen(args, **self._popen_kwargs)
|
p = subprocess.Popen(args, **self._popen_kwargs)
|
||||||
|
|
||||||
# 异步日志转发(可选)
|
|
||||||
def _pipe_to_log(name: str, stream):
|
def _pipe_to_log(name: str, stream):
|
||||||
try:
|
try:
|
||||||
for line in iter(stream.readline, ''):
|
for line in iter(stream.readline, ''):
|
||||||
@@ -78,7 +66,6 @@ class Deviceinfo(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import threading
|
|
||||||
threading.Thread(target=_pipe_to_log, args=("STDOUT", p.stdout), daemon=True).start()
|
threading.Thread(target=_pipe_to_log, args=("STDOUT", p.stdout), daemon=True).start()
|
||||||
threading.Thread(target=_pipe_to_log, args=("STDERR", p.stderr), daemon=True).start()
|
threading.Thread(target=_pipe_to_log, args=("STDERR", p.stderr), daemon=True).start()
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -86,11 +73,10 @@ class Deviceinfo(object):
|
|||||||
|
|
||||||
return p
|
return p
|
||||||
|
|
||||||
self._spawn_iproxy = _spawn_iproxy # 保存启动器
|
self._spawn_iproxy = _spawn_iproxy
|
||||||
LogManager.info(f"iproxy 启动器已就绪,目录: {self.iproxy_dir}")
|
LogManager.info(f"iproxy 启动器已就绪,目录: {self.iproxy_dir}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# 没找到 iproxy 也允许实例化成功,但后续启动会失败并给出明确日志
|
|
||||||
self.iproxy_path = None
|
self.iproxy_path = None
|
||||||
self.iproxy_dir = None
|
self.iproxy_dir = None
|
||||||
self._spawn_iproxy = None
|
self._spawn_iproxy = None
|
||||||
@@ -104,14 +90,43 @@ class Deviceinfo(object):
|
|||||||
try:
|
try:
|
||||||
lists = Usbmux().device_list()
|
lists = Usbmux().device_list()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# 另一台电脑常见:usbmuxd 连接失败(未安装 iTunes/Apple Mobile Device Support)
|
LogManager.warning(
|
||||||
LogManager.warning(f"usbmuxd 连接失败: {e}。请确认已安装 iTunes/Apple Mobile Device Support,并在手机上“信任此电脑”")
|
f"usbmuxd 连接失败: {e}。请确认已安装 iTunes/Apple Mobile Device Support,并在手机上“信任此电脑”")
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
continue
|
continue
|
||||||
# 新接入设备
|
|
||||||
|
now_udids = {d.udid for d in lists if d.conn_type == ConnectionType.USB}
|
||||||
|
|
||||||
|
# 1. 处理“已插入但未信任”的设备,一旦信任就补连接
|
||||||
|
for udid in list(self._pending_udids):
|
||||||
|
if udid not in now_udids:
|
||||||
|
# 设备已拔出,从 pending 移除
|
||||||
|
self._pending_udids.discard(udid)
|
||||||
|
continue
|
||||||
|
if self.is_device_trusted(udid):
|
||||||
|
# 已信任,补连接
|
||||||
|
self._pending_udids.discard(udid)
|
||||||
|
self.screenProxy += 1
|
||||||
|
try:
|
||||||
|
self.connectDevice(udid)
|
||||||
|
# 补加入 deviceArray(用 usbmux 对象)
|
||||||
|
for d in lists:
|
||||||
|
if d.udid == udid:
|
||||||
|
self.deviceArray.append(d)
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
LogManager.error(f"补连接设备失败 {udid}: {e}", udid)
|
||||||
|
|
||||||
|
# 2. 处理全新插入的设备
|
||||||
for device in lists:
|
for device in lists:
|
||||||
# usb设备,并且为新设备。并且大于总限制数量
|
if device.conn_type == ConnectionType.USB and device not in self.deviceArray and len(
|
||||||
if device.conn_type == ConnectionType.USB and (device not in self.deviceArray) and (len(self.deviceArray) < self.maxDeviceCount):
|
self.deviceArray) < self.maxDeviceCount:
|
||||||
|
if not self.is_device_trusted(device.udid):
|
||||||
|
# 未信任,记入 pending,下次循环再判
|
||||||
|
self._pending_udids.add(device.udid)
|
||||||
|
LogManager.warning("设备未信任,已记录,等待信任后自动连接", device.udid)
|
||||||
|
continue
|
||||||
|
# 已信任,直接走完整流程
|
||||||
self.screenProxy += 1
|
self.screenProxy += 1
|
||||||
try:
|
try:
|
||||||
self.connectDevice(device.udid)
|
self.connectDevice(device.udid)
|
||||||
@@ -119,24 +134,36 @@ class Deviceinfo(object):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
LogManager.error(f"连接设备失败 {device.udid}: {e}", device.udid)
|
LogManager.error(f"连接设备失败 {device.udid}: {e}", device.udid)
|
||||||
|
|
||||||
# 拔出设备处理
|
# 3. 处理拔出
|
||||||
self._removeDisconnected(lists)
|
self._removeDisconnected(lists)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
# ----------------------------
|
# ----------------------------
|
||||||
# 连接单台设备:启动 WDA、读取屏参、通知前端、映射投屏端口
|
# 判断设备是否已信任
|
||||||
|
# ----------------------------
|
||||||
|
def is_device_trusted(self, udid: str) -> bool:
|
||||||
|
try:
|
||||||
|
d = BaseDevice(udid)
|
||||||
|
d.get_value("DeviceName") # 任意读取一个值,失败即未信任
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# ----------------------------
|
||||||
|
# 连接单台设备:先判断是否信任,再启动 WDA
|
||||||
# ----------------------------
|
# ----------------------------
|
||||||
def connectDevice(self, identifier: str):
|
def connectDevice(self, identifier: str):
|
||||||
# 1) 连接 WDA(USBClient -> 设备 8100)
|
if not self.is_device_trusted(identifier):
|
||||||
|
LogManager.warning("设备未信任,跳过 WDA 启动,等待信任后再试", identifier)
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
d = wda.USBClient(identifier, 8100)
|
d = wda.USBClient(identifier, 8100)
|
||||||
|
|
||||||
LogManager.info("启动 WDA 成功", identifier)
|
LogManager.info("启动 WDA 成功", identifier)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LogManager.error(f"启动 WDA 失败,请检查手机是否已信任、WDA 是否正常。错误: {e}", identifier)
|
LogManager.error(f"启动 WDA 失败,请检查手机是否已信任、WDA 是否正常。错误: {e}", identifier)
|
||||||
return # 不抛出到外层,保持监听循环健壮
|
return
|
||||||
|
|
||||||
# 2) 读取屏幕信息(失败不影响主流程)
|
|
||||||
width, height, scale = 0, 0, 1.0
|
width, height, scale = 0, 0, 1.0
|
||||||
try:
|
try:
|
||||||
size = d.window_size()
|
size = d.window_size()
|
||||||
@@ -145,7 +172,6 @@ class Deviceinfo(object):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
LogManager.warning(f"读取屏幕信息失败:{e}", identifier)
|
LogManager.warning(f"读取屏幕信息失败:{e}", identifier)
|
||||||
|
|
||||||
# 3) 组装模型并发送给前端
|
|
||||||
model = DeviceModel(identifier, self.screenProxy, width, height, scale, type=1)
|
model = DeviceModel(identifier, self.screenProxy, width, height, scale, type=1)
|
||||||
self.deviceModelList.append(model)
|
self.deviceModelList.append(model)
|
||||||
try:
|
try:
|
||||||
@@ -153,7 +179,6 @@ class Deviceinfo(object):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
LogManager.warning(f"向前端发送设备模型失败:{e}", identifier)
|
LogManager.warning(f"向前端发送设备模型失败:{e}", identifier)
|
||||||
|
|
||||||
# 4) 可选:启动你的 app 并回到桌面
|
|
||||||
try:
|
try:
|
||||||
d.app_start(WdaAppBundleId)
|
d.app_start(WdaAppBundleId)
|
||||||
d.home()
|
d.home()
|
||||||
@@ -162,41 +187,36 @@ class Deviceinfo(object):
|
|||||||
|
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
# 5) 本地端口 -> 设备端口 的映射(投屏:本地 self.screenProxy -> 设备 9100)
|
|
||||||
target = self.relayDeviceScreenPort(identifier)
|
target = self.relayDeviceScreenPort(identifier)
|
||||||
# 加个非空判断
|
|
||||||
if target is not None:
|
if target is not None:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
self.pidList.append({"target": target, "id": identifier})
|
self.pidList.append({"target": target, "id": identifier})
|
||||||
|
|
||||||
# 安全杀死iproxy进程
|
# ----------------------------
|
||||||
|
# 以下方法未改动,省略以节省篇幅
|
||||||
|
# ----------------------------
|
||||||
def _terminate_proc(self, p: subprocess.Popen):
|
def _terminate_proc(self, p: subprocess.Popen):
|
||||||
if not p:
|
if not p:
|
||||||
return
|
return
|
||||||
if p.poll() is not None:
|
if p.poll() is not None:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
p.terminate() # 先温柔
|
p.terminate()
|
||||||
p.wait(timeout=3)
|
p.wait(timeout=3)
|
||||||
except Exception:
|
except Exception:
|
||||||
try:
|
try:
|
||||||
if os.name == "posix":
|
if os.name == "posix":
|
||||||
try:
|
try:
|
||||||
# 如果 iproxy 启动时用了 setsid,这里可杀整个进程组
|
|
||||||
os.killpg(os.getpgid(p.pid), signal.SIGKILL)
|
os.killpg(os.getpgid(p.pid), signal.SIGKILL)
|
||||||
except Exception:
|
except Exception:
|
||||||
p.kill()
|
p.kill()
|
||||||
else:
|
else:
|
||||||
p.kill() # Windows 直接 kill
|
p.kill()
|
||||||
p.wait(timeout=2) # 一定要 wait,避免僵尸
|
p.wait(timeout=2)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# ----------------------------
|
|
||||||
# 处理拔出设备:发通知、关掉 iproxy、移出状态
|
|
||||||
# ----------------------------
|
|
||||||
def _removeDisconnected(self, current_list):
|
def _removeDisconnected(self, current_list):
|
||||||
# 1) 计算“被拔出”的 UDID 集合 —— 用 UDID,而不是对象做集合运算
|
|
||||||
try:
|
try:
|
||||||
prev_udids = {getattr(d, "udid", None) for d in self.deviceArray if getattr(d, "udid", None)}
|
prev_udids = {getattr(d, "udid", None) for d in self.deviceArray if getattr(d, "udid", None)}
|
||||||
now_udids = {getattr(d, "udid", None) for d in current_list if getattr(d, "udid", None)}
|
now_udids = {getattr(d, "udid", None) for d in current_list if getattr(d, "udid", None)}
|
||||||
@@ -207,11 +227,9 @@ class Deviceinfo(object):
|
|||||||
if not removed_udids:
|
if not removed_udids:
|
||||||
return
|
return
|
||||||
|
|
||||||
# 2) 加锁,避免多线程同时改三个列表
|
|
||||||
if not hasattr(self, "_lock"):
|
if not hasattr(self, "_lock"):
|
||||||
self._lock = threading.RLock()
|
self._lock = threading.RLock()
|
||||||
with self._lock:
|
with self._lock:
|
||||||
# 2.1 通知前端并清理 deviceModelList
|
|
||||||
for udid in list(removed_udids):
|
for udid in list(removed_udids):
|
||||||
for a in list(self.deviceModelList):
|
for a in list(self.deviceModelList):
|
||||||
if udid == getattr(a, "deviceId", None):
|
if udid == getattr(a, "deviceId", None):
|
||||||
@@ -225,7 +243,6 @@ class Deviceinfo(object):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# 2.2 关闭该 UDID 的所有 iproxy
|
|
||||||
survivors = []
|
survivors = []
|
||||||
for k in list(self.pidList):
|
for k in list(self.pidList):
|
||||||
kid = k.get("id")
|
kid = k.get("id")
|
||||||
@@ -235,40 +252,31 @@ class Deviceinfo(object):
|
|||||||
self._terminate_proc(p)
|
self._terminate_proc(p)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LogManager.warning(f"关闭 iproxy 异常:{e}", kid)
|
LogManager.warning(f"关闭 iproxy 异常:{e}", kid)
|
||||||
# 不再把该项放回 survivors,相当于移除
|
|
||||||
else:
|
else:
|
||||||
survivors.append(k)
|
survivors.append(k)
|
||||||
self.pidList = survivors
|
self.pidList = survivors
|
||||||
|
|
||||||
# 2.3 从已连接集合中移除(按 UDID 过滤,避免对象引用不一致导致 remove 失败)
|
|
||||||
self.deviceArray = [d for d in self.deviceArray if getattr(d, "udid", None) not in removed_udids]
|
self.deviceArray = [d for d in self.deviceArray if getattr(d, "udid", None) not in removed_udids]
|
||||||
|
|
||||||
# 3) 打点
|
|
||||||
for udid in removed_udids:
|
for udid in removed_udids:
|
||||||
LogManager.info("设备已拔出,清理完成(下线通知 + 端口映射关闭 + 状态移除)", udid)
|
LogManager.info("设备已拔出,清理完成(下线通知 + 端口映射关闭 + 状态移除)", udid)
|
||||||
|
|
||||||
# ----------------------------
|
|
||||||
# 根目录与 iproxy 可执行文件定位
|
|
||||||
# ----------------------------
|
|
||||||
def _base_dir(self) -> Path:
|
def _base_dir(self) -> Path:
|
||||||
if getattr(sys, "frozen", False):
|
if getattr(sys, "frozen", False):
|
||||||
return Path(sys.executable).resolve().parent
|
return Path(sys.executable).resolve().parent
|
||||||
return Path(__file__).resolve().parents[1] # iOSAI/ 作为根
|
return Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
def _iproxy_path(self) -> Path:
|
def _iproxy_path(self) -> Path:
|
||||||
exe = "iproxy.exe" if os.name == "nt" else "iproxy"
|
exe = "iproxy.exe" if os.name == "nt" else "iproxy"
|
||||||
base = self._base_dir()
|
base = self._base_dir()
|
||||||
candidates = [
|
candidates = [
|
||||||
base / "resources" / "iproxy" / exe, # 推荐放置
|
base / "resources" / "iproxy" / exe,
|
||||||
]
|
]
|
||||||
for p in candidates:
|
for p in candidates:
|
||||||
if p.exists():
|
if p.exists():
|
||||||
return p
|
return p
|
||||||
raise FileNotFoundError(f"iproxy not found, tried: {[str(c) for c in candidates]}")
|
raise FileNotFoundError(f"iproxy not found, tried: {[str(c) for c in candidates]}")
|
||||||
|
|
||||||
# ----------------------------
|
|
||||||
# 端口映射:仅做“转发端口”这件事(调用已准备好的启动器)
|
|
||||||
# ----------------------------
|
|
||||||
def relayDeviceScreenPort(self, udid: str) -> Optional[subprocess.Popen]:
|
def relayDeviceScreenPort(self, udid: str) -> Optional[subprocess.Popen]:
|
||||||
if not self._spawn_iproxy:
|
if not self._spawn_iproxy:
|
||||||
LogManager.error("iproxy 启动器未就绪,无法建立端口映射(初始化时未找到 iproxy)。", udid)
|
LogManager.error("iproxy 启动器未就绪,无法建立端口映射(初始化时未找到 iproxy)。", udid)
|
||||||
|
|||||||
Reference in New Issue
Block a user