feat(web_gui): WebGUI 对齐桌面 GUI 的 WiFi 扫描和 last_wifi_target 能力

新增 4 个端点:
- POST /api/wifi/scan       扫描局域网 ADB 设备
- GET  /api/wifi/last       查询上次连接记录
- POST /api/wifi/last       同上(POST 方便 fetch)
- POST /api/wifi/last/clear 清除记录

同步 connect / disconnect 路径行为:
- /api/wifi/connect 成功后写入 last_wifi_target(取设备型号)
- /api/wifi/disconnect 如果断的就是上次记住的设备 → 自动清除记录

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 20:33:22 +08:00
1 parent b3a41db029
commit 8c3a162deb
1 file changed
+96
+96
View File
@@ -1628,6 +1628,13 @@ class Handler(BaseHTTPRequestHandler):
if u.path == "/api/devices":
adb = find_adb_executable()
return self._send_json({"adb": adb, "devices": list_adb_devices(adb)})
if u.path == "/api/wifi/last":
# 返回 last_wifi_target 记录(也支持 POST)
try:
from gui.gui_core_pkg import get_last_wifi_target
return self._send_json({"ok": True, "last": get_last_wifi_target()})
except Exception as e:
return self._send_json({"ok": False, "error": str(e)}, status=500)
if u.path == "/api/config":
cfg = read_config()
return self._send_json(cfg)
@@ -2053,6 +2060,20 @@ class Handler(BaseHTTPRequestHandler):
ok, msg = connect_wifi_adb(adb, ip, port)
except Exception as e:
ok, msg = False, str(e)
# 成功后记住,下次启动时自动重连
if ok:
try:
from gui.gui_core_pkg import set_last_wifi_target
from gui.gui_core_pkg.wifi_adb_scan import query_device_model
target = ip if ":" in ip else f"{ip}:{port}"
model = ""
try:
model = query_device_model(adb, target)
except Exception:
pass
set_last_wifi_target(target, model)
except Exception:
pass
devices = list_adb_devices(adb) if adb else []
return self._send_json({"ok": bool(ok), "message": msg, "devices": devices}, status=200 if ok else 500)
@@ -2068,9 +2089,84 @@ class Handler(BaseHTTPRequestHandler):
ok, msg = disconnect_wifi_adb(adb, ip, port)
except Exception as e:
ok, msg = False, str(e)
# 如果断开的就是上次记住的设备,清掉记录
if ok:
try:
from gui.gui_core_pkg import (
clear_last_wifi_target,
get_last_wifi_target,
)
last = get_last_wifi_target()
last_target = str(last.get("target") or "")
if last_target.startswith(ip):
clear_last_wifi_target()
except Exception:
pass
devices = list_adb_devices(adb) if adb else []
return self._send_json({"ok": bool(ok), "message": msg, "devices": devices}, status=200 if ok else 500)
if u.path == "/api/wifi/scan":
"""扫描局域网 ADB 设备。
POST body 可选字段:
- subnet: 192.168.5.0/24 / 192.168.5.1-100 / 192.168.5.5;
留空则自动用本机子网
- probe_timeout: 单 IP 探测超时秒(默认 0.3)
"""
body = self._read_json()
adb = find_adb_executable()
subnet = str(body.get("subnet") or "").strip() or None
probe_timeout = float(body.get("probe_timeout") or 0.3)
try:
from gui.gui_core_pkg import discover_adb_devices, get_local_subnet
devs = discover_adb_devices(
adb, subnet_spec=subnet, probe_timeout=probe_timeout
)
payload = [
{
"target": d.target,
"ip": d.ip,
"port": d.port,
"state": d.adb_state,
"model": d.model,
"message": d.message,
}
for d in devs
]
return self._send_json(
{
"ok": True,
"subnet": subnet or get_local_subnet() or "",
"devices": payload,
}
)
except Exception as e:
return self._send_json(
{"ok": False, "error": str(e)}, status=500
)
if u.path == "/api/wifi/last":
"""返回 last_wifi_target 记录(GET 也支持)。"""
try:
from gui.gui_core_pkg import get_last_wifi_target
last = get_last_wifi_target()
return self._send_json({"ok": True, "last": last})
except Exception as e:
return self._send_json(
{"ok": False, "error": str(e)}, status=500
)
if u.path == "/api/wifi/last/clear":
"""清除 last_wifi_target 记录。"""
try:
from gui.gui_core_pkg import clear_last_wifi_target
clear_last_wifi_target()
return self._send_json({"ok": True})
except Exception as e:
return self._send_json(
{"ok": False, "error": str(e)}, status=500
)
if u.path == "/api/test-yanghao":
if RUNNER.is_running():
return self._send_json({"ok": False, "error": "running"}, status=409)