优化quart接口。优化添加设备逻辑。

This commit is contained in:
2025-11-26 16:18:02 +08:00
parent 0a200cfc6f
commit 648f6cdfee
2 changed files with 77 additions and 92 deletions

View File

@@ -4,12 +4,10 @@ import socket
import threading
import time
import subprocess
from typing import Dict, Optional
from typing import Dict
import tidevice
import wda
from tidevice import Usbmux, ConnectionType
from Entity.DeviceModel import DeviceModel
from Entity.Variables import WdaAppBundleId, wdaFunctionPort
from Module.FlaskSubprocessManager import FlaskSubprocessManager
@@ -71,31 +69,33 @@ class DeviceInfo:
LogManager.method_info("进入主循环", "listen", udid="system")
print("[Listen] 开始监听设备上下线...")
last_broadcast = 0.0 # 用来做“心跳全量同步”的时间戳
while True:
try:
usb = Usbmux().device_list()
online = {d.udid for d in usb if d.conn_type == ConnectionType.USB}
except Exception as e:
LogManager.warning(f"[device_list] 异常:{e}", udid="system")
time.sleep(1)
continue
print("[Listen] device_list 出错,本轮视为无设备,防止状态脏死")
usb = []
online = set()
with self._lock:
known = set(self._models.keys())
# 1. 新设备
# ---------- 1. 新设备 ----------
now = time.time()
for udid in online:
self._last_seen[udid] = time.time()
self._last_seen[udid] = now
if udid not in known:
try:
self._add_device(udid)
except Exception as e:
# 单设备异常不能干掉整个循环
LogManager.warning(f"[Add] 处理设备 {udid} 异常: {e}", udid=udid)
print(f"[Add] 处理设备 {udid} 异常: {e}")
# 2. 可能离线设备
now = time.time()
# ---------- 2. 可能离线设备 ----------
for udid in list(known):
if udid not in online:
last = self._last_seen.get(udid, 0)
@@ -106,6 +106,15 @@ class DeviceInfo:
LogManager.method_error(f"移除失败:{e}", "listen", udid=udid)
print(f"[Remove] 移除失败 {udid}: {e}")
# ---------- 3. 心跳:每 5 秒强制同步一次到 Flask ----------
if now - last_broadcast > 5.0:
try:
self._manager_send()
except Exception as e:
print(f"[Listen] 周期同步到 Flask 失败: {e}")
else:
last_broadcast = now
time.sleep(1)
# ==========================
@@ -216,7 +225,6 @@ class DeviceInfo:
# 给 WDA 一点启动缓冲时间
time.sleep(2.0)
for _ in range(max_retry):
# 设备已移除就不再尝试
with self._lock: