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
@@ -33,7 +33,10 @@ from .wifi_adb_scan import (
|
||||
)
|
||||
from .config import (
|
||||
CONFIG_PATH,
|
||||
clear_last_wifi_target,
|
||||
get_last_wifi_target,
|
||||
read_config,
|
||||
set_last_wifi_target,
|
||||
write_config,
|
||||
)
|
||||
from .run_params import RunParams
|
||||
@@ -56,5 +59,8 @@ __all__ = [
|
||||
"CONFIG_PATH",
|
||||
"read_config",
|
||||
"write_config",
|
||||
"get_last_wifi_target",
|
||||
"set_last_wifi_target",
|
||||
"clear_last_wifi_target",
|
||||
"RunParams",
|
||||
]
|
||||
@@ -3,6 +3,9 @@
|
||||
- CONFIG_PATH config.json 绝对路径
|
||||
- read_config 读取并返回 dict
|
||||
- write_config 深度合并写回(保留未传字段)
|
||||
- get_last_wifi_target 读上次成功连接的 WiFi 目标(ip:port / ip / 空)
|
||||
- set_last_wifi_target 写入上次成功连接的 WiFi 目标
|
||||
- clear_last_wifi_target 清除记录
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -44,3 +47,76 @@ def write_config(cfg: dict) -> None:
|
||||
f.write("\n")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# last_wifi_target 持久化(GUI 扫描成功后保存,下次启动自动重连)
|
||||
# ============================================================
|
||||
|
||||
_LAST_WIFI_KEY = "last_wifi_target"
|
||||
|
||||
|
||||
def get_last_wifi_target() -> dict:
|
||||
"""返回上次成功连接的 WiFi 目标(dict);没有返回空 dict。
|
||||
|
||||
存储格式(在 config.json 顶层):
|
||||
"last_wifi_target": {
|
||||
"target": "192.168.5.2:5555",
|
||||
"model": "PEPM00",
|
||||
"saved_at": 1717000000
|
||||
}
|
||||
"""
|
||||
try:
|
||||
cfg = read_config()
|
||||
except Exception:
|
||||
return {}
|
||||
if not isinstance(cfg, dict):
|
||||
return {}
|
||||
val = cfg.get(_LAST_WIFI_KEY)
|
||||
if isinstance(val, dict) and val.get("target"):
|
||||
return dict(val)
|
||||
return {}
|
||||
|
||||
|
||||
def set_last_wifi_target(target: str, model: str = "") -> None:
|
||||
"""保存上次成功连接的 WiFi 目标。
|
||||
|
||||
target 必须是 ip:port 形式;空字符串等价于 clear_last_wifi_target。
|
||||
"""
|
||||
target = (target or "").strip()
|
||||
if not target:
|
||||
clear_last_wifi_target()
|
||||
return
|
||||
|
||||
import time
|
||||
|
||||
try:
|
||||
write_config(
|
||||
{
|
||||
_LAST_WIFI_KEY: {
|
||||
"target": target,
|
||||
"model": (model or "").strip(),
|
||||
"saved_at": int(time.time()),
|
||||
}
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def clear_last_wifi_target() -> None:
|
||||
"""从配置里删除 last_wifi_target 记录。"""
|
||||
try:
|
||||
cfg = read_config()
|
||||
except Exception:
|
||||
return
|
||||
if not isinstance(cfg, dict) or _LAST_WIFI_KEY not in cfg:
|
||||
return
|
||||
try:
|
||||
cfg.pop(_LAST_WIFI_KEY, None)
|
||||
# write_config 会做 merge,但 merge 不删键,所以这里直接覆盖写
|
||||
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
||||
json.dump(cfg, f, ensure_ascii=False, indent=4)
|
||||
f.write("\n")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
Reference in new issue
Block a user