优化设备数量不对的问题

This commit is contained in:
2025-09-10 16:15:47 +08:00
parent e2e9e0b348
commit 9cb27eb97f
9 changed files with 78 additions and 79 deletions

View File

@@ -214,45 +214,45 @@ class Deviceinfo(object):
def _removeDisconnected(self, current_list):
try:
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)}
# 当前在线的 UDID 集合
now_udids = {d.udid for d in current_list if hasattr(d, 'udid')}
# 上一次记录的 UDID 集合
prev_udids = {d.udid for d in self.deviceArray if hasattr(d, 'udid')}
except Exception as e:
LogManager.error(f"收集 UDID 失败:{e}", "")
return
removed_udids = prev_udids - now_udids
if not removed_udids:
return
if not hasattr(self, "_lock"):
self._lock = threading.RLock()
with self._lock:
for udid in list(removed_udids):
for a in list(self.deviceModelList):
if udid == getattr(a, "deviceId", None):
a.type = 2
try:
self.manager.send(a.toDict())
except Exception as e:
LogManager.warning(f"发送下线事件失败:{e}", udid)
try:
self.deviceModelList.remove(a)
except ValueError:
pass
# 清理 deviceModelList
for model in list(self.deviceModelList):
if model.deviceId in removed_udids:
model.type = 2
try:
self.manager.send(model.toDict())
except Exception as e:
LogManager.warning(f"发送下线事件失败:{e}", model.deviceId)
self.deviceModelList.remove(model)
# 清理 pidList
survivors = []
for k in list(self.pidList):
kid = k.get("id")
if kid in removed_udids:
p = k.get("target")
for item in self.pidList:
if item.get("id") in removed_udids:
p = item.get("target")
try:
self._terminate_proc(p)
except Exception as e:
LogManager.warning(f"关闭 iproxy 异常:{e}", kid)
LogManager.warning(f"关闭 iproxy 异常:{e}", item.get("id"))
else:
survivors.append(k)
survivors.append(item)
self.pidList = survivors
self.deviceArray = [d for d in self.deviceArray if getattr(d, "udid", None) not in removed_udids]
# 清理 deviceArray
self.deviceArray = [d for d in self.deviceArray if d.udid not in removed_udids]
for udid in removed_udids:
LogManager.info("设备已拔出,清理完成(下线通知 + 端口映射关闭 + 状态移除)", udid)