Files
auto_control/web/common.py
T

47 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""跨模块共享工具(web 层)。"""
from core import device_pool
from core.adb_helper import _adb
from core.logger import get_logger
_log = get_logger("web")
def _merged_device_list():
"""本地 adb(含 USB)+ 设备池的合并列表([{serial, state}])。
供维护终端/剪贴板注入/应用版本管理等设备选择场景共用。
状态:device/offline=本机 adb 实际状态;pool=设备池已配置但本机未连接。
"""
states = {}
out = _adb("devices")
for line in (out or "").splitlines()[1:]:
parts = line.split()
if len(parts) >= 2 and parts[0] and not parts[0].startswith("*"):
states[parts[0]] = parts[1]
try:
for serial in device_pool.list_configured():
if serial not in states:
states[serial] = "pool"
except Exception:
pass
return [{"serial": s, "state": st} for s, st in sorted(states.items())]
def _device_screen_state(serial):
"""设备屏幕状态:on/off/unknown(dumpsys power 只读查询)。
用 mWakefulness 判断(Asleep=熄屏):MIUI 的 dumpsys display 没有
mScreenState 字段,power 字段全机型存在。USB 设备查询失败返回 unknown。
"""
try:
if ":" not in serial:
return "unknown"
out = _adb("-s", serial, "shell", "dumpsys", "power") or ""
import re
m = re.search(r"mWakefulness=(\w+)", out)
if m:
return "off" if m.group(1).lower() == "asleep" else "on"
except Exception:
pass
return "unknown"