From 9edfcc2449177449a86dd7f5b0bff17da64ca278 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 21 May 2026 09:22:56 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=88=A0=E9=99=A4=20operations/=20?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E5=BA=9F=E5=BC=83=E5=AD=98=E6=A0=B9=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - device_operations.py:仅是空 stub(__all__ = []),无人使用 - image_operations.py:同上 - screenshot_service_new.py:未被引用的重复实现 operations/__init__.py 不导出这些模块,外部无依赖。 Co-Authored-By: Claude Opus 4.5 --- operations/device_operations.py | 16 ---- operations/image_operations.py | 16 ---- operations/screenshot_service_new.py | 134 --------------------------- 3 files changed, 166 deletions(-) delete mode 100644 operations/device_operations.py delete mode 100644 operations/image_operations.py delete mode 100644 operations/screenshot_service_new.py diff --git a/operations/device_operations.py b/operations/device_operations.py deleted file mode 100644 index f999919..0000000 --- a/operations/device_operations.py +++ /dev/null @@ -1,16 +0,0 @@ -"""设备操作模块 - -包含ADB设备操作,如点击、滑动、输入等。 -""" - -from __future__ import annotations - -from typing import Any, Optional - -# 临时导入:从原文件导入,后续会逐步迁移 -# TODO: 迁移完成后,移除此导入并在此文件中实现 -from douyin.core.underlying_operations import underlying_operations - -# 导出设备操作相关方法 -# TODO: 实现这些函数后添加到__all__列表中 -__all__ = [] diff --git a/operations/image_operations.py b/operations/image_operations.py deleted file mode 100644 index ed273d4..0000000 --- a/operations/image_operations.py +++ /dev/null @@ -1,16 +0,0 @@ -"""图像处理操作模块 - -包含图像裁剪、处理等操作。 -""" - -from __future__ import annotations - -from typing import Any, Optional, Tuple - -# 临时导入:从原文件导入,后续会逐步迁移 -# TODO: 迁移完成后,移除此导入并在此文件中实现 -from douyin.core.underlying_operations import underlying_operations - -# 导出图像处理相关方法 -# TODO: 实现这些函数后添加到__all__列表中 -__all__ = [] diff --git a/operations/screenshot_service_new.py b/operations/screenshot_service_new.py deleted file mode 100644 index 8684781..0000000 --- a/operations/screenshot_service_new.py +++ /dev/null @@ -1,134 +0,0 @@ -"""简化版后台截图服务,避免文件中重复定义导致的 linter 错误。""" - -from __future__ import annotations - -import os -import threading -import time -from typing import Optional -from datetime import datetime -from douyin.core import path_config -from operations.screenshot_operations import ScreenshotOperations - - -class ScreenshotService: - """后台截图服务(简化实现)。""" - - _instance: Optional["ScreenshotService"] = None - _lock = threading.Lock() - - def __init__(self): - self._running = False - self._thread: Optional[threading.Thread] = None - self._screenshot_ops = ScreenshotOperations() - self._current_device_id: Optional[str] = None - self._last_screenshot_time = time.time() - 10.0 - self._screenshot_interval = 3.5 - self._cache_valid_duration = 4.0 - self._screenshot_path: Optional[str] = None - self._last_success = False - - @classmethod - def get_instance(cls) -> "ScreenshotService": - if cls._instance is None: - with cls._lock: - if cls._instance is None: - cls._instance = cls() - assert cls._instance is not None - return cls._instance - - def start(self, device_id: str, interval: float = 1.5): - with self._lock: - if self._running and self._current_device_id == device_id: - return - if self._running: - self.stop() - self._current_device_id = device_id - self._screenshot_interval = max(0.5, interval) - self._running = True - try: - self._screenshot_path = path_config.get_screenshot_path() - except Exception: - self._screenshot_path = os.path.join("pic", "screenshot.png") - self._thread = threading.Thread(target=self._screenshot_loop, daemon=True, name="ScreenshotService") - self._thread.start() - self._last_screenshot_time = time.time() - self._cache_valid_duration - 1.0 - timetag = datetime.now().strftime("%H:%M:%S") - print(f"[{timetag}] [截图服务] 已启动后台截图服务,设备: {device_id}, 间隔: {interval}秒") - - def stop(self): - with self._lock: - if not self._running: - return - self._running = False - if self._thread and self._thread.is_alive(): - self._thread.join(timeout=2.0) - timetag = datetime.now().strftime("%H:%M:%S") - print(f"[{timetag}] [截图服务] 已停止后台截图服务") - - def _screenshot_loop(self): - while self._running: - try: - if not self._current_device_id: - time.sleep(0.1) - continue - start = time.time() - success = self._screenshot_ops.get_screenshot(self._current_device_id, use_service=False) - elapsed = (time.time() - start) * 1000 - timetag = datetime.now().strftime("%H:%M:%S") - if success: - self._last_screenshot_time = time.time() - self._last_success = True - print(f"[{timetag}] [截图服务] 后台截图成功,耗时: {elapsed:.0f}ms") - else: - self._last_success = False - print(f"[{timetag}] [截图服务] 后台截图失败,耗时: {elapsed:.0f}ms") - time.sleep(max(0.5, self._screenshot_interval - elapsed / 1000)) - except Exception as e: - print(f"[截图服务] 异常: {e}") - time.sleep(1.0) - - def get_screenshot(self, device_id: str, force: bool = False) -> bool: - # 优先使用缓存判断,简化版保留核心逻辑 - if self._current_device_id != device_id: - self.start(device_id) - time.sleep(0.1) - now = time.time() - if not force and now - self._last_screenshot_time < self._cache_valid_duration: - if self._screenshot_path and os.path.exists(self._screenshot_path): - try: - if os.path.getsize(self._screenshot_path) >= 1024: - return True - except Exception: - pass - # 回退到直接截图 - return self._screenshot_ops.get_screenshot(device_id, use_service=False) - - def is_running(self) -> bool: - return self._running - - def get_last_screenshot_time(self) -> float: - return self._last_screenshot_time - - def get_cache_info(self) -> dict: - return { - "running": self._running, - "device_id": self._current_device_id, - "last_screenshot_time": self._last_screenshot_time, - "interval": self._screenshot_interval, - "cache_valid_duration": self._cache_valid_duration, - "last_success": self._last_success, - } - - -def get_optimal_workers() -> int: - cpu_count = os.cpu_count() or 4 - if cpu_count >= 12: - return 10 - if cpu_count >= 8: - return 8 - if cpu_count >= 4: - return 6 - if cpu_count >= 2: - return 3 - return 2