Files
douyi_guaji_main/gui/dashboard_app/page_data.py
T

170 lines
7.8 KiB
Python

from __future__ import annotations
import tkinter as tk
from . import theme
from .data import DashboardData, fmt_ts
from .page_context import PageContext
from .page_helpers import reservation_rows
from .widgets import flow_grid, log_panel, preview_panel, section, stat_tile, table_panel, text_panel, toolbar
def _set_log_text(text: tk.Text, lines: list[str]) -> None:
try:
at_bottom = text.yview()[1] >= 0.98
except Exception:
at_bottom = True
text.configure(state="normal")
text.delete("1.0", "end")
text.insert("1.0", "\n".join(lines[-1000:]) or "暂无数据")
text.configure(state="disabled")
if at_bottom:
text.yview_moveto(1.0)
def _runner_lines(ctx: PageContext, fallback: list[str]) -> list[str]:
runner = ctx.runner
if runner is not None:
try:
with runner._lock:
lines = list(runner._log[-1000:])
if lines:
return lines
except Exception:
pass
return fallback
def build_run_log(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "运行日志", "实时输出 / 独立滚动 / 刷新")
toolbar(parent, [("刷新", ctx.action("refresh"), "soft"), ("行为日志", ctx.action("show_actions_log"), "soft"), "上一个", "下一个", "清除"])
box, text = log_panel(parent, "运行日志", _runner_lines(ctx, data.run_log), height=24)
box.pack(fill="both", expand=True)
def refresh_live() -> None:
try:
if not text.winfo_exists():
return
except Exception:
return
_set_log_text(text, _runner_lines(ctx, data.run_log))
try:
text.after(1200, refresh_live)
except Exception:
pass
refresh_live()
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)
def build_reservation_queue(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "预约队列", "预约队列总览与等待状态")
toolbar(parent, [("刷新", ctx.action("refresh"), "soft")])
rows = reservation_rows(data)
for row in rows:
row["time"] = fmt_ts(row["time"])
table_panel(parent, "预约队列", (
("idx", "#", 40), ("title", "直播间", 190), ("content", "内容", 250),
("value", "价值", 80), ("status", "状态", 90), ("time", "时间", 170),
), rows, height=18).pack(fill="both", expand=True)
def build_high_value(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "高价值房间", "持久化高价值记录")
toolbar(parent, [("刷新", ctx.action("refresh"), "soft")])
rows = []
for idx, item in enumerate(data.high_value_rooms[:50], 1):
rows.append({
"idx": idx,
"title": item.get("room_title") or item.get("title") or "-",
"content": item.get("content") or item.get("prize_content") or "-",
"value": item.get("prize_value_yuan") or item.get("value") or "-",
"status": item.get("status") or "已记录",
})
table_panel(parent, "高价值房间", (
("idx", "#", 40), ("title", "直播间", 220), ("content", "内容", 280),
("value", "价值", 90), ("status", "状态", 100),
), rows, height=20).pack(fill="both", expand=True)
def build_actions_log(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "行为日志", "actions.log / 独立滚动 / 刷新")
toolbar(parent, [("刷新", ctx.action("refresh"), "soft"), ("运行日志", ctx.action("show_run_log"), "soft"), "上一个", "下一个", "清除"])
box, text = log_panel(parent, "行为日志", data.action_log, height=24)
box.pack(fill="both", expand=True)
def refresh_file() -> None:
try:
if not text.winfo_exists():
return
except Exception:
return
try:
from .data import load_dashboard_data
latest = load_dashboard_data().action_log
except Exception:
latest = data.action_log
_set_log_text(text, latest)
try:
text.after(2000, refresh_file)
except Exception:
pass
refresh_file()
def build_realtime(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "实时状态", "显示动作流 / 显示截图 / 自动滚动 / 查看大图")
toolbar(parent, ["显示动作流", "显示截图", "自动滚动", "查看大图"])
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)
def build_ocr(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "OCR结果", "点击 截图+OCR 后更新")
toolbar(parent, [("截图+OCR", ctx.action("screenshot_ocr"), "primary"), ("刷新", ctx.action("refresh"), "soft")])
lines = []
for p in data.ocr_files[:8]:
lines.append(f"{p.name} {fmt_ts(p.stat().st_mtime)}")
text_panel(parent, "OCR结果", lines or ['暂无OCR结果,请先点击"截图+OCR"按钮'], height=24).pack(fill="both", expand=True)
def build_wins(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "中奖记录", "中奖截图列表与预览")
toolbar(parent, [("刷新列表", ctx.action("refresh"), "soft"), "打开目录", "保存误识别", "加载误识别"])
body = tk.Frame(parent, bg=theme.BG)
body.pack(fill="both", expand=True)
body.grid_columnconfigure(0, weight=4)
body.grid_columnconfigure(1, weight=5)
rows = [{"idx": i + 1, "file": p.name, "time": fmt_ts(p.stat().st_mtime)} for i, p in enumerate(data.win_images[:40])]
table_panel(body, "中奖截图列表", (("idx", "#", 40), ("file", "文件", 260), ("time", "时间", 160)), rows, height=18).grid(row=0, column=0, sticky="nsew", padx=(0, 8))
preview_panel(body, data.win_images[0] if data.win_images else None, "截图预览").grid(row=0, column=1, sticky="nsew", padx=(8, 0))
def build_mapping(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "映射表", "显示原始标题 / 刷新映射表 / 搜索 / 复制条目")
toolbar(parent, [("自动刷新(5s)", None, "soft"), ("刷新映射表", ctx.action("refresh"), "soft"), "显示原始标题", "清除", "复制条目"])
table_panel(parent, "运行时映射表", (
("pos", "位置", 80), ("title", "标题", 360), ("status", "状态", 100), ("updated", "更新时间", 180),
), data.mapping_rows, height=22).pack(fill="both", expand=True)
def build_wifi_adb(parent: tk.Misc, data: DashboardData, ctx: PageContext) -> None:
section(parent, "WiFi ADB", "扫描 WiFi / USB 授权提示 / 连接状态")
toolbar(parent, [("扫描WiFi", ctx.action("scan_wifi"), "primary"), ("WiFi ADB", ctx.action("wifi_adb"), "soft"), ("刷新设备", ctx.action("refresh_devices"), "soft")])
metrics = flow_grid(parent, 4)
for idx, item in enumerate([
("USB设备", "待检测", "首次启用", theme.TEAL, theme.TEAL_SOFT),
("扫描状态", "未扫描", "局域网", theme.BLUE, theme.BLUE_SOFT),
("默认端口", "5555", "ADB TCP", theme.AMBER, theme.AMBER_SOFT),
("当前设备", "设备选择器", "顶部下拉", theme.GREEN, theme.GREEN_SOFT),
]):
stat_tile(metrics, *item[:3], color=item[3], soft=item[4]).grid(row=0, column=idx, sticky="ew", padx=5)