feat(monitors): 为无内置电池的 Mac 采样整机即时功率

加入整机功率采样、XPC 读取和历史记录,并覆盖无电池设备的采样分支。
This commit is contained in:
2026-11-15 22:03:46 +08:00
parent e2f88023d6
commit c886c4227d
10 changed files with 58 additions and 2 deletions
+10 -1
View File
@@ -1,7 +1,7 @@
import Foundation
import IOKit
// AppleSMC is undocumented. Only read CPU/GPU temperature zones and fixed fan keys; never write keys.
// AppleSMC is undocumented. Only read CPU/GPU temperature zones and fixed fan / system power keys; never write keys.
nonisolated final class CPUTemperatureMonitor {
static let highTemperatureThreshold = 90.0
@@ -99,6 +99,12 @@ nonisolated final class CPUTemperatureMonitor {
}
}
/// PSTR is the SMC system-total sensor, not battery flow or wall-outlet power.
func sampleSystemPower() -> Double? {
guard openConnection(), let watts = readNumber(0x50535452), watts > 0 else { return nil }
return watts
}
private func readNumber(_ key: UInt32) -> Double? {
var request = Request(); request.key = key; request.command = 9
guard let metadata = call(&request), metadata.info.size <= 32 else { return nil }
@@ -116,6 +122,9 @@ nonisolated final class CPUTemperatureMonitor {
return value.isFinite ? value : nil
case (0x66706532, 2): // fpe2, big-endian unsigned 14.2 fixed point
return Double(UInt16(bytes[0]) << 8 | UInt16(bytes[1])) / 4
case (0x73703738, 2): // sp78, signed 8.8 (SMC sign-magnitude)
let magnitude = Double(UInt16(bytes[0] & 0x7f) << 8 | UInt16(bytes[1])) / 256
return bytes[0] & 0x80 == 0 ? magnitude : -magnitude
case (0x75693820, 1): return Double(bytes[0]) // ui8
default: return nil
}