feat: windows adaptation (WMI thermal zones, battery fallback)

This commit is contained in:
snowy-yang committed 2026-09-12 19:53:24 +08:00
1 parent 28add64ae0
commit 8775c022bd
3 files changed
+59 -9

No files matched your search

+34 -1
View File
@@ -65,6 +65,16 @@ def get_power() -> dict:
# 累加所有域(通常 dom0 已包含子域,取最大 dom0)
result["total_watts"] = max(result["domains"].values())
# 3. Windows/通用兜底:电池电量百分比(无功率数据)
if result["total_watts"] is None:
try:
bat = psutil.sensors_battery()
except (AttributeError, OSError):
bat = None
if bat is not None:
result["battery_percent"] = round(bat.percent, 1)
result["plugged"] = bat.power_plugged
return result
@@ -98,11 +108,32 @@ def get_memory() -> dict:
}
def _wmi_temps() -> dict:
"""Windows: 通过 WMI 热区读取温度(部分主板支持,可能需要管理员权限)。"""
import subprocess
try:
out = subprocess.run(
["powershell", "-NoProfile", "-Command",
"Get-CimInstance -Namespace root/wmi -ClassName MSAcpi_ThermalZoneTemperature "
"| Select-Object -ExpandProperty CurrentTemperature"],
capture_output=True, text=True, timeout=10,
encoding="utf-8", errors="replace",
)
values = [float(x) for x in out.stdout.split() if x.replace(".", "", 1).isdigit()]
except (OSError, subprocess.SubprocessError, ValueError):
return {}
# WMI 单位是 0.1 开尔文
return {"ThermalZone": [
{"label": f"zone{i}", "current": round(v / 10 - 273.15, 1)}
for i, v in enumerate(values)
]}
def get_temps() -> dict:
try:
sensors = psutil.sensors_temperatures()
except (AttributeError, OSError):
return {}
sensors = {}
out = {}
for name, entries in sensors.items():
out[name] = [
@@ -110,6 +141,8 @@ def get_temps() -> dict:
for i, e in enumerate(entries)
if e.current is not None
]
if not out and os.name == "nt":
out = _wmi_temps()
return out