diff --git a/gui/dashboard_app/data.py b/gui/dashboard_app/data.py index 29f03de..822e295 100644 --- a/gui/dashboard_app/data.py +++ b/gui/dashboard_app/data.py @@ -19,6 +19,7 @@ class DashboardData: high_value_rooms: list[dict[str, Any]] mapping_rows: list[dict[str, Any]] win_images: list[Path] + current_screenshot_path: Path | None screenshot_path: Path | None ocr_files: list[Path] @@ -94,7 +95,7 @@ def load_dashboard_data() -> DashboardData: reservations_raw = _read_json(path_config.get_reservations_path(), {}) high_raw = _read_json(path_config.get_high_value_rooms_path(), {}) pic_dir = Path(path_config.get_pic_dir()) - screenshot = Path(path_config.get_screenshot_path()) + current_screenshot = Path(path_config.get_screenshot_path()) return DashboardData( cfg=cfg if isinstance(cfg, dict) else {}, run_log=_tail(path_config.get_log_file_path(), 240) or _tail(path_config.get_simple_log_path(), 240), @@ -103,7 +104,8 @@ def load_dashboard_data() -> DashboardData: high_value_rooms=_normalize_rows(high_raw, ("rooms", "items", "records")), mapping_rows=_mapping_rows(), win_images=_list_images(pic_dir / "lottery_prize_captures", 60), - screenshot_path=screenshot if screenshot.exists() else None, + current_screenshot_path=current_screenshot if current_screenshot.exists() else None, + screenshot_path=current_screenshot if current_screenshot.exists() else None, ocr_files=sorted((pic_dir / "ocr").glob("*"), key=lambda p: p.stat().st_mtime if p.exists() else 0, reverse=True)[:20] if (pic_dir / "ocr").exists() else [], ) @@ -126,4 +128,3 @@ def fmt_ts(value: Any) -> str: return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts)) except Exception: return str(value or "-") - diff --git a/gui/dashboard_app/page_data.py b/gui/dashboard_app/page_data.py index d6193d9..c2eff69 100644 --- a/gui/dashboard_app/page_data.py +++ b/gui/dashboard_app/page_data.py @@ -175,7 +175,7 @@ def build_run_log(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> Non def build_preview(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: section(parent, "截图预览", "选择图片 / 打开pic目录 / 刷新预览") toolbar(parent, [("刷新预览", ctx.action("refresh"), "soft"), ("截图+OCR", ctx.action("screenshot_ocr"), "primary"), "打开pic目录"]) - preview_panel(parent, data.screenshot_path, "当前截图").pack(fill="both", expand=True) + preview_panel(parent, data.current_screenshot_path, "当前截图").pack(fill="both", expand=True) def build_reservation_queue(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: @@ -242,7 +242,7 @@ def build_realtime(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> No body = tk.Frame(parent, bg=theme.BG) body.pack(fill="both", expand=True) text_panel(body, "动作流", data.action_log[-120:], height=16).pack(fill="both", expand=True, pady=(0, 12)) - preview_panel(body, data.screenshot_path, "实时截图").pack(fill="both", expand=True) + preview_panel(body, data.current_screenshot_path, "实时截图").pack(fill="both", expand=True) def build_ocr(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: diff --git a/gui/dashboard_app/page_home.py b/gui/dashboard_app/page_home.py index 01377ba..773cf56 100644 --- a/gui/dashboard_app/page_home.py +++ b/gui/dashboard_app/page_home.py @@ -99,7 +99,7 @@ def build_home(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: ("value", "价值", 54), ("status", "状态", 58), ("time", "时间", 92), ), rows, height=18).pack(fill="both", expand=True) - preview_panel(right, data.screenshot_path, "实时截图").pack(fill="both", expand=True) + preview_panel(right, data.current_screenshot_path, "实时截图").pack(fill="both", expand=True) def build_statistics(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None: diff --git a/tests/test_dashboard_screenshot_source.py b/tests/test_dashboard_screenshot_source.py new file mode 100644 index 0000000..a129fec --- /dev/null +++ b/tests/test_dashboard_screenshot_source.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +from pathlib import Path + + +def test_dashboard_current_screenshot_uses_runtime_screenshot(tmp_path, monkeypatch): + from gui.dashboard_app import data as dashboard_data + + project_root = tmp_path + pic_dir = project_root / "pic" + logs_dir = project_root / "logs" + state_dir = project_root / "douyin" / "core" / "state" + config_dir = project_root / "config" + for path in (pic_dir, logs_dir, state_dir, config_dir): + path.mkdir(parents=True, exist_ok=True) + + runtime_screenshot = pic_dir / "screenshot.png" + runtime_screenshot.write_bytes(b"runtime") + (pic_dir / "debug_captures").mkdir() + (pic_dir / "debug_captures" / "latest.png").write_bytes(b"debug") + (pic_dir / "lottery_prize_captures").mkdir() + (pic_dir / "lottery_prize_captures" / "win.png").write_bytes(b"win") + + monkeypatch.setattr(dashboard_data.path_config, "get_project_root", lambda: str(project_root)) + monkeypatch.setattr(dashboard_data.path_config, "get_config_path", lambda: str(config_dir / "config.json")) + monkeypatch.setattr(dashboard_data.path_config, "get_reservations_path", lambda: str(state_dir / "reservations.json")) + monkeypatch.setattr(dashboard_data.path_config, "get_high_value_rooms_path", lambda: str(project_root / "state" / "high_value_rooms.json")) + monkeypatch.setattr(dashboard_data.path_config, "get_pic_dir", lambda: str(pic_dir)) + monkeypatch.setattr(dashboard_data.path_config, "get_screenshot_path", lambda: str(runtime_screenshot)) + monkeypatch.setattr(dashboard_data.path_config, "get_log_file_path", lambda: str(logs_dir / "unified.log")) + monkeypatch.setattr(dashboard_data.path_config, "get_simple_log_path", lambda: str(logs_dir / "simple.log")) + monkeypatch.setattr(dashboard_data.path_config, "get_actions_log_path", lambda: str(logs_dir / "actions.log")) + + loaded = dashboard_data.load_dashboard_data() + + assert loaded.current_screenshot_path == runtime_screenshot + assert loaded.screenshot_path == runtime_screenshot + assert all(Path(p).name != "latest.png" for p in loaded.win_images)