feat(wifi_adb): 启动重连失败时自动扫描子网 + 智能匹配
新增 pick_best_match(devs, last) 纯函数,按优先级匹配上次设备: 1. target 完全一致(IP 没换) 2. 同 model + 同 port(换 IP 但同一台手机)—— 多台同型号时返回 None 让用户选 3. 同主机段 + 同 port(弱兜底,仅唯一时返回) 4. 唯一一台 connected(扫到就一台时认定) GUI _try_reconnect_last_wifi 流程升级: - verify_adb_target 失败(不可达)→ 自动 discover_adb_devices 扫子网 - pick_best_match 命中 → 选中 + 更新 last_wifi_target - 命中且 IP 已变 → 日志写"上次目标 X 已变 → 智能匹配到 Y(已更新记忆)" - 没命中 → 列出候选 + 提示点'扫描 WiFi'手选 实际场景:换 WiFi 网段(家里 → 公司 / 192.168.1 → 192.168.5)后 GUI 启动时自动找回同一台手机,无需任何手动操作。 新增 15 个 pick_best_match 单元测试覆盖所有规则 + 边界。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3d91764f56
commit
5966f461c3
4 files changed
+277
-13
No files matched your search
@@ -30,6 +30,7 @@ from .wifi_adb_scan import (
|
||||
discover_adb_devices,
|
||||
get_local_subnet,
|
||||
parse_subnet_spec,
|
||||
pick_best_match,
|
||||
)
|
||||
from .config import (
|
||||
CONFIG_PATH,
|
||||
@@ -56,6 +57,7 @@ __all__ = [
|
||||
"discover_adb_devices",
|
||||
"get_local_subnet",
|
||||
"parse_subnet_spec",
|
||||
"pick_best_match",
|
||||
"CONFIG_PATH",
|
||||
"read_config",
|
||||
"write_config",
|
||||
|
||||
@@ -354,3 +354,74 @@ def discover_adb_devices(
|
||||
results.append(dev)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 上次目标 → 扫描结果的智能匹配
|
||||
# ============================================================
|
||||
|
||||
def pick_best_match(
|
||||
devs: list[DiscoveredDevice],
|
||||
last: Optional[dict] = None,
|
||||
) -> Optional[DiscoveredDevice]:
|
||||
"""根据上次记住的设备信息,在扫描结果里挑最可能"是同一台设备"的那个。
|
||||
|
||||
匹配优先级(高 → 低,且只考虑 state=="connected" 的设备):
|
||||
1. target 完全一致 (IP 没换最理想)
|
||||
2. 同 model + 同 port (换 IP 但接同一台手机)
|
||||
3. 同主机段 + 同 port (换网段时一般不会命中,留作弱兜底)
|
||||
4. 唯一一台 connected (扫到就一台,可以认定就是它)
|
||||
|
||||
返回:匹配到的 DiscoveredDevice;找不到返回 None。
|
||||
"""
|
||||
connected = [d for d in (devs or []) if d.adb_state == "connected"]
|
||||
if not connected:
|
||||
return None
|
||||
|
||||
if not isinstance(last, dict):
|
||||
last = {}
|
||||
last_target = str(last.get("target") or "").strip()
|
||||
last_model = str(last.get("model") or "").strip()
|
||||
last_host = ""
|
||||
last_port = DEFAULT_ADB_PORT
|
||||
if last_target and ":" in last_target:
|
||||
host_part, port_part = last_target.rsplit(":", 1)
|
||||
last_host = host_part.strip()
|
||||
try:
|
||||
last_port = int(port_part)
|
||||
except ValueError:
|
||||
last_port = DEFAULT_ADB_PORT
|
||||
elif last_target:
|
||||
last_host = last_target
|
||||
|
||||
# 1) target 完全一致
|
||||
if last_target:
|
||||
for d in connected:
|
||||
if d.target == last_target:
|
||||
return d
|
||||
|
||||
# 2) 同 model + 同 port(model 大小写敏感,因为 ro.product.model 是稳定字符串)
|
||||
if last_model:
|
||||
same_model = [d for d in connected if d.model == last_model and d.port == last_port]
|
||||
if len(same_model) == 1:
|
||||
return same_model[0]
|
||||
if len(same_model) > 1:
|
||||
# 多台同型号歧义太大,让用户手选
|
||||
return None
|
||||
|
||||
# 4) 唯一一台 connected(注意:放在 model 之后,避免覆盖多台同型号的歧义判断)
|
||||
if len(connected) == 1:
|
||||
return connected[0]
|
||||
|
||||
# 3) 同主机段(IP 前 3 段相同)+ 同 port —— 弱兜底,仅当唯一时才返回
|
||||
if last_host:
|
||||
last_prefix = ".".join(last_host.split(".")[:3])
|
||||
same_prefix = [
|
||||
d
|
||||
for d in connected
|
||||
if d.port == last_port and d.ip.startswith(last_prefix + ".")
|
||||
]
|
||||
if len(same_prefix) == 1:
|
||||
return same_prefix[0]
|
||||
|
||||
return None
|
||||
Reference in new issue
Block a user