feat(wifi_adb): 记住上次成功的 WiFi 设备,GUI 启动时自动重连
新增 gui/gui_core_pkg/config.py 三个 helper:
- get_last_wifi_target / set_last_wifi_target / clear_last_wifi_target
配置以 last_wifi_target = {target, model, saved_at} 形式存在 config.json 顶层。
GUI 行为:
- "扫描 WiFi" 选中或自动连上一台 connected 设备 → 写入 last_wifi_target
- WiFi 选择对话框"使用选中"也写入
- 主窗口启动后延迟 300ms 后台尝试 verify_adb_target(last.target)
- 已有设备:不打扰
- 重连成功:自动设为当前设备
- unauthorized:提示需手机端授权
- 不可达:提示换网了,引导点'扫描 WiFi'
新增 12 个 last_wifi_target 单元测试覆盖读写/清除/损坏值/缺文件等场景。
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
db7a78876b
commit
a71f6faed0
4 files changed
+290
-1
No files matched your search
+90
-1
@@ -35,9 +35,13 @@ from gui.gui_core import (
|
||||
)
|
||||
from gui.gui_core_pkg import (
|
||||
DiscoveredDevice,
|
||||
clear_last_wifi_target,
|
||||
discover_adb_devices,
|
||||
get_last_wifi_target,
|
||||
get_local_subnet,
|
||||
set_last_wifi_target,
|
||||
)
|
||||
from gui.gui_core_pkg.wifi_adb_scan import verify_adb_target, query_device_model
|
||||
from douyin.core.path_config import get_logs_dir
|
||||
from gui.components.mapping_table import create_mapping_tab
|
||||
|
||||
@@ -342,6 +346,76 @@ def main():
|
||||
scan_connect_btn.pack(side=LEFT, padx=(0, 20))
|
||||
_scan_busy = {"flag": False}
|
||||
|
||||
def _try_reconnect_last_wifi():
|
||||
"""启动时若 adb devices 为空且配置里有 last_wifi_target,后台尝试重连。
|
||||
|
||||
策略:
|
||||
- 已经有可用设备:什么都不做
|
||||
- 上次记录的 target 当前已 connected:直接选中作为当前设备
|
||||
- 否则后台 adb connect 一次;成功 → 选中;失败 → 日志提示但不打扰
|
||||
"""
|
||||
last = {}
|
||||
try:
|
||||
last = get_last_wifi_target()
|
||||
except Exception:
|
||||
return
|
||||
if not isinstance(last, dict) or not last.get("target"):
|
||||
return
|
||||
|
||||
target = str(last.get("target"))
|
||||
model = str(last.get("model") or "")
|
||||
|
||||
# 已经有设备时不打扰
|
||||
try:
|
||||
existing = list_adb_devices(adb_exe)
|
||||
except Exception:
|
||||
existing = []
|
||||
if target in existing:
|
||||
device_var.set(target)
|
||||
on_line(f"[启动] WiFi 设备已在线: {target}" + (f" ({model})" if model else ""))
|
||||
return
|
||||
if existing:
|
||||
on_line(
|
||||
f"[启动] 当前已有设备 {existing[0]},上次 WiFi 目标 {target} 不抢占"
|
||||
)
|
||||
return
|
||||
|
||||
on_line(f"[启动] 尝试重连上次的 WiFi 设备: {target} ...")
|
||||
|
||||
def worker():
|
||||
state, msg = "failed", ""
|
||||
try:
|
||||
state, msg = verify_adb_target(adb_exe, target)
|
||||
except Exception as e:
|
||||
msg = str(e)
|
||||
|
||||
def render():
|
||||
if state == "connected":
|
||||
refresh_devices()
|
||||
device_var.set(target)
|
||||
on_line(
|
||||
f"[启动] 自动重连成功: {target}"
|
||||
+ (f" ({model})" if model else "")
|
||||
)
|
||||
elif state == "unauthorized":
|
||||
on_line(
|
||||
f"[启动] 上次设备 {target} 未授权,请在手机上确认 ADB 调试"
|
||||
)
|
||||
else:
|
||||
on_line(
|
||||
f"[启动] 上次设备 {target} 不可达,可能换网了。提示:点'扫描 WiFi'重新发现"
|
||||
)
|
||||
|
||||
try:
|
||||
root.after(0, render)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
threading.Thread(target=worker, daemon=True).start()
|
||||
|
||||
# 启动时延迟一点,等界面创建完成再尝试重连
|
||||
root.after(300, _try_reconnect_last_wifi)
|
||||
|
||||
def _on_scan_one_click():
|
||||
"""主窗口"扫描 WiFi"按钮:后台扫描 → 按结果数智能处理。
|
||||
|
||||
@@ -391,9 +465,15 @@ def main():
|
||||
d = connected[0]
|
||||
refresh_devices()
|
||||
device_var.set(d.target)
|
||||
# 记住,下次启动自动重连
|
||||
try:
|
||||
set_last_wifi_target(d.target, d.model or "")
|
||||
except Exception:
|
||||
pass
|
||||
on_line(
|
||||
f"[扫描] 自动连接成功: {d.target}"
|
||||
+ (f" ({d.model})" if d.model else "")
|
||||
+ "(已记住,下次 GUI 启动会自动重连)"
|
||||
)
|
||||
return
|
||||
|
||||
@@ -448,6 +528,9 @@ def main():
|
||||
values = tree.item(sel[0])["values"]
|
||||
target = str(values[0])
|
||||
state = str(values[1])
|
||||
model = str(values[2] or "").strip()
|
||||
if model == "-":
|
||||
model = ""
|
||||
if state != "connected":
|
||||
on_line(
|
||||
f"[扫描] {target} 当前状态={state},请先在手机上授权后重试"
|
||||
@@ -455,7 +538,13 @@ def main():
|
||||
return
|
||||
refresh_devices()
|
||||
device_var.set(target)
|
||||
on_line(f"[扫描] 已设为当前设备: {target}")
|
||||
try:
|
||||
set_last_wifi_target(target, model)
|
||||
except Exception:
|
||||
pass
|
||||
on_line(
|
||||
f"[扫描] 已设为当前设备: {target}(已记住,下次 GUI 启动会自动重连)"
|
||||
)
|
||||
pick_dlg.destroy()
|
||||
|
||||
ttk.Button(bottom, text="使用选中", command=use_selected).pack(side=LEFT)
|
||||
|
||||
Reference in new issue
Block a user