"""ProcRunner 独立操作方法实现(从 gui/web_gui.py 抽离)。""" from __future__ import annotations import json import os import subprocess import sys from typing import Any from douyin.core import path_config from .adb_compat import find_adb_executable, resolve_adb_device def get_reservations(self: Any) -> dict: """获取当前预约队列信息。""" try: path = path_config.get_reservations_path() except Exception: base_dir = ( path_config.PROJECT_ROOT if hasattr(path_config, "PROJECT_ROOT") else os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) ) path = os.path.join(base_dir, "douyin", "core", "state", "reservations.json") try: if os.path.exists(path): with open(path, "r", encoding="utf-8") as f: data = json.load(f) if isinstance(data, dict): return data except Exception: return { "reservations": [], "room_pos": 0, "updated_ts": 0.0, "reason": "state_read_or_parse_failed", } return { "reservations": [], "room_pos": 0, "updated_ts": 0.0, "reason": "no_state_file", } def get_high_value_rooms(self: Any) -> dict: """获取永久记录的高价值直播间列表。""" path = path_config.get_high_value_rooms_path() try: if os.path.exists(path): with open(path, "r", encoding="utf-8") as f: data = json.load(f) if isinstance(data, dict): return data if isinstance(data, list): return { "updated_ts": 0.0, "reason": "legacy_list", "total": len(data), "rooms": data, } except Exception: return { "updated_ts": 0.0, "reason": "state_read_or_parse_failed", "total": 0, "rooms": [], } return {"updated_ts": 0.0, "reason": "no_state_file", "total": 0, "rooms": []} def send_back_key(self: Any, device_id: str) -> bool: try: adb = find_adb_executable() if not adb: self._append_log("[错误] 未找到 adb.exe") return False if not device_id: try: device_id = resolve_adb_device(adb) except Exception as e: self._append_log(f"[错误] 设备选择失败: {e}") return False proc = subprocess.run( [adb, "-s", device_id, "shell", "input", "keyevent", "4"], capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=5, check=False, ) if proc.returncode == 0: self._append_log(f"[手动操作] 已发送返回键到设备: {device_id}") return True self._append_log(f"[错误] 发送返回键失败: {(proc.stderr or proc.stdout or '').strip()}") return False except Exception as e: self._append_log(f"[错误] 发送返回键异常: {e}") return False def resolve_device_id_for_action(self: Any, requested_device_id: str) -> tuple[str, str]: """解析动作类接口使用的设备 ID;返回 (device_id, error_msg)。""" device_id = str(requested_device_id or "").strip() adb = find_adb_executable() if not adb: return "", "未找到 adb.exe" if not device_id: try: device_id = resolve_adb_device(adb) except Exception: return "", "未选择设备,且无法自动解析在线设备" return device_id, "" def start_test_yanghao(self: Any, device_id: str, seconds: int) -> None: if self.is_running(): raise RuntimeError("already running") seconds = int(seconds or 0) if seconds <= 0: seconds = 60 run_cli_cmd = path_config.get_run_cli_command() run_cli_path = run_cli_cmd[-1] if run_cli_cmd else path_config.get_run_cli_path() if not os.path.exists(run_cli_path): raise FileNotFoundError( f"run_cli launcher not found.\nExpected path: {run_cli_path}\nPlease ensure run_cli.py is in the source tree or run_cli.exe is in the install directory." ) cmd = [*run_cli_cmd, "--test-yanghao-flow", "--test-yanghao-flow-seconds", str(seconds)] device_id = resolve_adb_device(find_adb_executable(), device_id) cmd += ["--device-id", device_id] self._start_process(cmd) def start_test_comment(self: Any, device_id: str, text: str) -> None: if self.is_running(): raise RuntimeError("already running") text = str(text or "").strip() run_cli_cmd = path_config.get_run_cli_command() run_cli_path = run_cli_cmd[-1] if run_cli_cmd else path_config.get_run_cli_path() if not os.path.exists(run_cli_path): raise FileNotFoundError( f"run_cli launcher not found.\nExpected path: {run_cli_path}\nPlease ensure run_cli.py is in the source tree or run_cli.exe is in the install directory." ) cmd = [*run_cli_cmd, "--test-comment"] if text: cmd += ["--test-comment-text", text] device_id = resolve_adb_device(find_adb_executable(), device_id) cmd += ["--device-id", device_id] self._start_process(cmd)