优化拔出设备逻辑
This commit is contained in:
6
.idea/iOSAI.iml
generated
6
.idea/iOSAI.iml
generated
@@ -1,8 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module type="PYTHON_MODULE" version="4">
|
<module type="PYTHON_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$" />
|
<content url="file://$MODULE_DIR$">
|
||||||
<orderEntry type="jdk" jdkName="Python 3.12 (IOS-AI)" jdkType="Python SDK" />
|
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="Python 3.12" jdkType="Python SDK" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -3,5 +3,5 @@
|
|||||||
<component name="Black">
|
<component name="Black">
|
||||||
<option name="sdkName" value="Python 3.12" />
|
<option name="sdkName" value="Python 3.12" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (IOS-AI)" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
||||||
2
.idea/workspace.xml
generated
2
.idea/workspace.xml
generated
@@ -5,6 +5,8 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="eceeff5e-51c1-459c-a911-d21ec090a423" name="Changes" comment="20250904-初步功能已完成">
|
<list default="true" id="eceeff5e-51c1-459c-a911-d21ec090a423" name="Changes" comment="20250904-初步功能已完成">
|
||||||
|
<change beforePath="$PROJECT_DIR$/.idea/iOSAI.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/iOSAI.iml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" 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" />
|
<change beforePath="$PROJECT_DIR$/Module/DeviceInfo.py" beforeDir="false" afterPath="$PROJECT_DIR$/Module/DeviceInfo.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/Module/FlaskService.py" beforeDir="false" afterPath="$PROJECT_DIR$/Module/FlaskService.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Module/FlaskService.py" beforeDir="false" afterPath="$PROJECT_DIR$/Module/FlaskService.py" afterDir="false" />
|
||||||
|
|||||||
@@ -83,7 +83,6 @@ class Deviceinfo(object):
|
|||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
def startDeviceListener(self):
|
def startDeviceListener(self):
|
||||||
"""死循环监听设备插拔;以 deviceModelList 为准"""
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
lists = Usbmux().device_list()
|
lists = Usbmux().device_list()
|
||||||
@@ -94,41 +93,30 @@ class Deviceinfo(object):
|
|||||||
|
|
||||||
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}
|
||||||
|
|
||||||
# 0. 首次失踪登记:已在线设备若突然扫不到,计数器归零
|
# 1. 失踪登记 & 累加
|
||||||
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 and udid not in self._miss_count:
|
|
||||||
self._miss_count[udid] = 0
|
|
||||||
LogManager.info(f"[DEBUG] 首次失踪登记:{udid}", udid)
|
|
||||||
|
|
||||||
# 1. 处理已在线设备的失联计数
|
|
||||||
with self._lock:
|
|
||||||
for udid in list(self._miss_count.keys()):
|
|
||||||
if udid not in now_udids:
|
if udid not in now_udids:
|
||||||
self._miss_count[udid] += 1
|
self._miss_count[udid] = self._miss_count.get(udid, 0) + 1
|
||||||
LogManager.info(f"[DEBUG] 累加 {udid} -> {self._miss_count[udid]}", udid)
|
|
||||||
if self._miss_count[udid] >= 3:
|
if self._miss_count[udid] >= 3:
|
||||||
print("设备下线了")
|
|
||||||
LogManager.info(f"[DEBUG] 触发下线 {udid}", udid)
|
|
||||||
self._remove_model(udid)
|
self._remove_model(udid)
|
||||||
self._miss_count.pop(udid, None)
|
self._miss_count.pop(udid, None)
|
||||||
else:
|
else:
|
||||||
LogManager.info(f"[DEBUG] 设备仍在,清零 {udid}", udid)
|
self._miss_count.pop(udid, None) # 设备又出现,清零
|
||||||
self._miss_count.pop(udid, None)
|
|
||||||
|
|
||||||
# 2. 处理新插入
|
# 2. 全新插入(只处理未在线且信任且未满)
|
||||||
for d in lists:
|
for d in lists:
|
||||||
if d.conn_type != ConnectionType.USB:
|
if d.conn_type != ConnectionType.USB:
|
||||||
continue
|
continue
|
||||||
udid = d.udid
|
udid = d.udid
|
||||||
with self._lock:
|
with self._lock:
|
||||||
if udid in self._model_index:
|
if udid in self._model_index:
|
||||||
continue # 已存在
|
continue # 已存在,跳过
|
||||||
if not self.is_device_trusted(udid):
|
if not self.is_device_trusted(udid):
|
||||||
LogManager.warning("设备未信任,跳过", udid)
|
|
||||||
continue
|
continue
|
||||||
if len(self.deviceModelList) >= self.maxDeviceCount:
|
if len(self.deviceModelList) >= self.maxDeviceCount:
|
||||||
continue
|
continue
|
||||||
|
port = self._alloc_port()
|
||||||
try:
|
try:
|
||||||
self.connectDevice(udid) # 内部会 _add_model
|
self.connectDevice(udid) # 内部会 _add_model
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -140,11 +128,9 @@ class Deviceinfo(object):
|
|||||||
|
|
||||||
# region ===================== 增删改查唯一入口(线程安全) =====================
|
# region ===================== 增删改查唯一入口(线程安全) =====================
|
||||||
def _has_model(self, udid: str) -> bool:
|
def _has_model(self, udid: str) -> bool:
|
||||||
with self._lock:
|
|
||||||
return udid in self._model_index
|
return udid in self._model_index
|
||||||
|
|
||||||
def _add_model(self, model: DeviceModel):
|
def _add_model(self, model: DeviceModel):
|
||||||
with self._lock:
|
|
||||||
if model.deviceId in self._model_index:
|
if model.deviceId in self._model_index:
|
||||||
return # 防重复
|
return # 防重复
|
||||||
self.deviceModelList.append(model)
|
self.deviceModelList.append(model)
|
||||||
@@ -156,18 +142,41 @@ class Deviceinfo(object):
|
|||||||
LogManager.info(f"设备上线,当前在线数:{len(self.deviceModelList)}", model.deviceId)
|
LogManager.info(f"设备上线,当前在线数:{len(self.deviceModelList)}", model.deviceId)
|
||||||
|
|
||||||
def _remove_model(self, udid: str):
|
def _remove_model(self, udid: str):
|
||||||
|
print("进入删除方法")
|
||||||
model = self._model_index.pop(udid, None)
|
model = self._model_index.pop(udid, None)
|
||||||
|
print(model)
|
||||||
if not model:
|
if not model:
|
||||||
|
print("没有找到model")
|
||||||
return
|
return
|
||||||
model.type = 2
|
model.type = 2
|
||||||
print(model.toDict())
|
|
||||||
# ① 关键:重试 3 次,必须送达,否则崩溃
|
# 内存结构删除
|
||||||
|
try:
|
||||||
|
idx = self.deviceModelList.index(model)
|
||||||
|
self.deviceModelList.pop(idx)
|
||||||
|
except ValueError:
|
||||||
|
print("有错误了")
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 端口回收(关键)
|
||||||
|
self._free_port(model.screenPort)
|
||||||
|
|
||||||
|
# 清理 iproxy
|
||||||
|
survivors = [item for item in self.pidList if item.get("id") != udid]
|
||||||
|
for item in self.pidList:
|
||||||
|
if item.get("id") == udid:
|
||||||
|
self._terminate_proc(item.get("target"))
|
||||||
|
self.pidList = survivors
|
||||||
|
|
||||||
|
# Socket 发送(无锁)
|
||||||
retry = 3
|
retry = 3
|
||||||
while retry:
|
while retry:
|
||||||
try:
|
try:
|
||||||
self.manager.send(model.toDict())
|
self.manager.send(model.toDict())
|
||||||
|
print("删除了")
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print("有问题了", e)
|
||||||
retry -= 1
|
retry -= 1
|
||||||
LogManager.error(f"发送下线事件失败,剩余重试 {retry}:{e}", udid)
|
LogManager.error(f"发送下线事件失败,剩余重试 {retry}:{e}", udid)
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
@@ -175,35 +184,10 @@ class Deviceinfo(object):
|
|||||||
LogManager.error("发送下线事件彻底失败,主动崩溃防止状态不一致", udid)
|
LogManager.error("发送下线事件彻底失败,主动崩溃防止状态不一致", udid)
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
|
|
||||||
# ② 安全删除
|
|
||||||
try:
|
|
||||||
idx = self.deviceModelList.index(model)
|
|
||||||
self.deviceModelList.pop(idx)
|
|
||||||
print(len(self.deviceModelList))
|
|
||||||
except Exception as e:
|
|
||||||
print("22222222")
|
|
||||||
print(f"[FlaskSubprocessManager] 发送失败,异常类型:{type(e).__name__},内容:{e}")
|
|
||||||
|
|
||||||
|
|
||||||
# ③ 回收端口
|
|
||||||
self._free_port(model.screenPort)
|
|
||||||
|
|
||||||
print("继续执行了")
|
|
||||||
|
|
||||||
# ④ 清理 iproxy
|
|
||||||
survivors = [item for item in self.pidList if item.get("id") != udid]
|
|
||||||
for item in self.pidList:
|
|
||||||
if item.get("id") == udid:
|
|
||||||
self._terminate_proc(item.get("target"))
|
|
||||||
self.pidList = survivors
|
|
||||||
print("设备下线。删除设备成功")
|
|
||||||
LogManager.info(f"设备下线,当前在线数:{len(self.deviceModelList)}", udid)
|
LogManager.info(f"设备下线,当前在线数:{len(self.deviceModelList)}", udid)
|
||||||
LogManager.info(f"[Deviceinfo] 下线包已送进队列 -> type=2", udid)
|
|
||||||
# endregion
|
|
||||||
|
|
||||||
# region ===================== 端口分配与回收 =====================
|
# region ===================== 端口分配与回收 =====================
|
||||||
def _alloc_port(self) -> int:
|
def _alloc_port(self) -> int:
|
||||||
with self._lock:
|
|
||||||
if self._port_pool:
|
if self._port_pool:
|
||||||
port = self._port_pool.pop()
|
port = self._port_pool.pop()
|
||||||
else:
|
else:
|
||||||
@@ -213,7 +197,6 @@ class Deviceinfo(object):
|
|||||||
return port
|
return port
|
||||||
|
|
||||||
def _free_port(self, port: int):
|
def _free_port(self, port: int):
|
||||||
with self._lock:
|
|
||||||
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)
|
||||||
@@ -255,11 +238,9 @@ class Deviceinfo(object):
|
|||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
# 先清旧进程再启动新进程
|
# 先清旧进程再启动新进程
|
||||||
with self._lock:
|
|
||||||
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:
|
||||||
with self._lock:
|
|
||||||
self.pidList.append({"target": target, "id": udid})
|
self.pidList.append({"target": target, "id": udid})
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|||||||
@@ -92,21 +92,18 @@ listener_thread.start()
|
|||||||
def deviceList():
|
def deviceList():
|
||||||
try:
|
try:
|
||||||
with listLock:
|
with listLock:
|
||||||
# 1. 一次性消费完队列
|
# 1. 消费完队列
|
||||||
while not dataQueue.empty():
|
while not dataQueue.empty():
|
||||||
obj = dataQueue.get()
|
obj = dataQueue.get()
|
||||||
if obj["type"] == 1:
|
if obj["type"] == 1:
|
||||||
|
# 上线:先踢掉同 deviceId 的旧记录(端口可能变)
|
||||||
|
listData[:] = [d for d in listData if d.get("deviceId") != obj.get("deviceId")]
|
||||||
listData.append(obj)
|
listData.append(obj)
|
||||||
else:
|
else:
|
||||||
# 倒序删除,安全
|
# 下线:只要同 deviceId 就删,不管端口
|
||||||
for i in range(len(listData) - 1, -1, -1):
|
listData[:] = [d for d in listData if d.get("deviceId") != obj.get("deviceId")]
|
||||||
d = listData[i]
|
|
||||||
if d.get("deviceId") == obj.get("deviceId") and \
|
|
||||||
d.get("screenPort") == obj.get("screenPort"):
|
|
||||||
listData.pop(i)
|
|
||||||
break # 同一端口同一设备只删一次
|
|
||||||
|
|
||||||
# 2. 兜底:只保留 type == 1 的在线设备
|
# 2. 兜底:只保留在线
|
||||||
listData[:] = [d for d in listData if d.get('type') == 1]
|
listData[:] = [d for d in listData if d.get('type') == 1]
|
||||||
|
|
||||||
return ResultData(data=listData.copy()).toJson()
|
return ResultData(data=listData.copy()).toJson()
|
||||||
|
|||||||
Reference in New Issue
Block a user