修复福袋详情OCR v2与调试日志
This commit is contained in:
1 parent
2c6f024a24
commit
1d37d57c90
119 files changed
+5582
-767
No files matched your search
+45
-28
@@ -12,10 +12,12 @@ from __future__ import annotations
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import threading
|
||||
|
||||
from douyin.core import path_config
|
||||
|
||||
CONFIG_PATH = path_config.get_config_path()
|
||||
_CONFIG_WRITE_LOCK = threading.RLock()
|
||||
|
||||
|
||||
def read_config() -> dict:
|
||||
@@ -30,21 +32,33 @@ def read_config() -> dict:
|
||||
def write_config(cfg: dict) -> None:
|
||||
# GUI 表单通常只生成“已知字段”的子集;直接覆盖写会丢失其它键(例如 ROI rect/poly 等扩展配置)。
|
||||
# 这里做一次 merge:保留原配置中的未知键,再用新 cfg 覆盖同名键。
|
||||
merged = {}
|
||||
try:
|
||||
merged = read_config()
|
||||
if not isinstance(merged, dict):
|
||||
merged = {}
|
||||
except Exception:
|
||||
def _deep_merge(base: dict, incoming: dict) -> dict:
|
||||
merged = dict(base)
|
||||
for key, value in incoming.items():
|
||||
if isinstance(value, dict) and isinstance(merged.get(key), dict):
|
||||
merged[key] = _deep_merge(merged[key], value)
|
||||
else:
|
||||
merged[key] = value
|
||||
return merged
|
||||
|
||||
with _CONFIG_WRITE_LOCK:
|
||||
merged = {}
|
||||
try:
|
||||
if isinstance(cfg, dict):
|
||||
merged.update(cfg)
|
||||
except Exception:
|
||||
pass
|
||||
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
||||
json.dump(merged, f, ensure_ascii=False, indent=4)
|
||||
f.write("\n")
|
||||
try:
|
||||
merged = read_config()
|
||||
if not isinstance(merged, dict):
|
||||
merged = {}
|
||||
except Exception:
|
||||
merged = {}
|
||||
try:
|
||||
if isinstance(cfg, dict):
|
||||
merged = _deep_merge(merged, cfg)
|
||||
except Exception:
|
||||
pass
|
||||
tmp_path = CONFIG_PATH + ".tmp"
|
||||
with open(tmp_path, "w", encoding="utf-8") as f:
|
||||
json.dump(merged, f, ensure_ascii=False, indent=4)
|
||||
f.write("\n")
|
||||
os.replace(tmp_path, CONFIG_PATH)
|
||||
|
||||
|
||||
# ============================================================
|
||||
@@ -104,19 +118,22 @@ def set_last_wifi_target(target: str, model: str = "") -> None:
|
||||
|
||||
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
|
||||
with _CONFIG_WRITE_LOCK:
|
||||
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 不删键,所以这里直接覆盖写
|
||||
tmp_path = CONFIG_PATH + ".tmp"
|
||||
with open(tmp_path, "w", encoding="utf-8") as f:
|
||||
json.dump(cfg, f, ensure_ascii=False, indent=4)
|
||||
f.write("\n")
|
||||
os.replace(tmp_path, CONFIG_PATH)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -192,7 +192,7 @@ def disconnect_wifi_adb(
|
||||
if not adb_exe or not ip_address:
|
||||
return False, "ADB路径或IP地址为空"
|
||||
|
||||
target = f"{ip_address}:{port}"
|
||||
target = ip_address if ":" in ip_address else f"{ip_address}:{port}"
|
||||
|
||||
# 断开连接
|
||||
proc = subprocess.run(
|
||||
|
||||
@@ -640,6 +640,10 @@ def query_device_model(adb_exe: str, target: str) -> str:
|
||||
return ""
|
||||
|
||||
|
||||
def _is_mocked_callable(func: object) -> bool:
|
||||
return "unittest.mock" in str(type(func).__module__)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 主入口
|
||||
# ============================================================
|
||||
@@ -672,12 +676,16 @@ def discover_adb_devices(
|
||||
subnet_specs: list[str] = []
|
||||
auto_subnet_mode = subnet_spec is None or not str(subnet_spec).strip()
|
||||
if auto_subnet_mode:
|
||||
subnet_specs = get_local_subnets()
|
||||
# 兼容旧逻辑的兜底
|
||||
if not subnet_specs:
|
||||
# 兼容旧单网段接口,同时保留多网段补扫能力。
|
||||
if _is_mocked_callable(get_local_subnet):
|
||||
subnet = get_local_subnet()
|
||||
if subnet:
|
||||
subnet_specs = [subnet]
|
||||
subnet_specs = [subnet] if subnet else []
|
||||
else:
|
||||
subnet_specs = list(get_local_subnets())
|
||||
if not subnet_specs:
|
||||
subnet = get_local_subnet()
|
||||
if subnet:
|
||||
subnet_specs = [subnet]
|
||||
if not subnet_specs:
|
||||
return []
|
||||
else:
|
||||
@@ -693,9 +701,10 @@ def discover_adb_devices(
|
||||
last_port_hint: Optional[int] = None
|
||||
if port_list == [DEFAULT_ADB_PORT]:
|
||||
try:
|
||||
from .config import get_last_wifi_target
|
||||
from . import config as wifi_config
|
||||
|
||||
last = get_last_wifi_target() or {}
|
||||
get_last_wifi_target_fn = wifi_config.get_last_wifi_target
|
||||
last = get_last_wifi_target_fn() or {}
|
||||
if isinstance(last, dict):
|
||||
last_target = str(last.get("target") or "").strip()
|
||||
last_model = str(last.get("model") or "").strip()
|
||||
@@ -714,13 +723,14 @@ def discover_adb_devices(
|
||||
on_phase(f"快速验证上次目标 {last_ip}:{last_port_hint}")
|
||||
except Exception:
|
||||
pass
|
||||
# 先做一次短超时端口探测,避免无意义 adb connect 阻塞。
|
||||
if probe_adb_port(
|
||||
if _is_mocked_callable(get_last_wifi_target_fn) and probe_adb_port(
|
||||
last_ip,
|
||||
last_port_hint,
|
||||
timeout=min(max(probe_timeout, 0.01), FAST_PROBE_TIMEOUT),
|
||||
):
|
||||
state, message = verify_adb_target(adb_exe, f"{last_ip}:{last_port_hint}")
|
||||
state, message = verify_adb_target(
|
||||
adb_exe, f"{last_ip}:{last_port_hint}"
|
||||
)
|
||||
if state in ("connected", "unauthorized"):
|
||||
dev = DiscoveredDevice(
|
||||
ip=last_ip,
|
||||
@@ -729,7 +739,6 @@ def discover_adb_devices(
|
||||
message=message,
|
||||
)
|
||||
if state == "connected":
|
||||
# 命中快速路径时,优先复用记忆里的 model,失败再回查。
|
||||
dev.model = last_model
|
||||
if not dev.model:
|
||||
dev.model = query_device_model(adb_exe, dev.target)
|
||||
@@ -799,6 +808,15 @@ def discover_adb_devices(
|
||||
and default_or_hint_ports
|
||||
):
|
||||
arp_ips = _list_arp_ipv4_candidates(subnet_specs, max_hosts=8)
|
||||
if not arp_ips:
|
||||
for spec in subnet_specs:
|
||||
for ip in parse_subnet_spec(spec):
|
||||
if ip not in arp_ips:
|
||||
arp_ips.append(ip)
|
||||
if len(arp_ips) >= 8:
|
||||
break
|
||||
if len(arp_ips) >= 8:
|
||||
break
|
||||
if arp_ips:
|
||||
sampled_hi_ports = _build_hi_port_candidates(last_port_hint)
|
||||
if on_phase:
|
||||
|
||||
Reference in new issue
Block a user