mirror of
https://github.com/tw93/Mole.git
synced 2026-02-16 12:21:11 +00:00
refactor(status): simplify temperature detection using ioreg battery sensor
This commit is contained in:
BIN
bin/status-go
BIN
bin/status-go
Binary file not shown.
@@ -177,54 +177,20 @@ func collectThermal() ThermalStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Try osx-cpu-temp (most accurate for users without sudo)
|
// 1. Try ioreg battery temperature (simple, no sudo needed)
|
||||||
if commandExists("osx-cpu-temp") {
|
ctxIoreg, cancelIoreg := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||||
ctxTemp, cancelTemp := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
defer cancelIoreg()
|
||||||
defer cancelTemp()
|
if out, err := runCmd(ctxIoreg, "sh", "-c", "ioreg -rn AppleSmartBattery | awk '/\"Temperature\"/ {print $3}'"); err == nil {
|
||||||
if out, err := runCmd(ctxTemp, "osx-cpu-temp"); err == nil {
|
|
||||||
valStr := strings.TrimSpace(out)
|
valStr := strings.TrimSpace(out)
|
||||||
valStr = strings.TrimSuffix(valStr, "°C")
|
if tempRaw, err := strconv.Atoi(valStr); err == nil && tempRaw > 0 {
|
||||||
valStr = strings.TrimSuffix(valStr, "C")
|
thermal.CPUTemp = float64(tempRaw) / 100.0
|
||||||
valStr = strings.TrimSpace(valStr)
|
|
||||||
if t, err := strconv.ParseFloat(valStr, 64); err == nil && t > 0 {
|
|
||||||
thermal.CPUTemp = t
|
|
||||||
return thermal
|
return thermal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Try powermetrics (requires sudo, works on Apple Silicon)
|
// 2. Try thermal level as a proxy (fallback)
|
||||||
if commandExists("powermetrics") {
|
|
||||||
// Only run if we are effectively root to avoid password prompt/failure delays
|
|
||||||
if os.Geteuid() == 0 {
|
|
||||||
ctxPower, cancelPower := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
defer cancelPower()
|
|
||||||
// Use thermal sampler
|
|
||||||
out, err := runCmd(ctxPower, "powermetrics", "-n", "1", "--samplers", "thermal", "-i", "100")
|
|
||||||
if err == nil {
|
|
||||||
lines := strings.Split(out, "\n")
|
|
||||||
for _, line := range lines {
|
|
||||||
if strings.Contains(line, "CPU die temperature") {
|
|
||||||
// Format: "CPU die temperature: 35.43 C"
|
|
||||||
parts := strings.Split(line, ":")
|
|
||||||
if len(parts) == 2 {
|
|
||||||
valStr := strings.TrimSpace(parts[1])
|
|
||||||
valStr = strings.TrimSuffix(valStr, " C")
|
|
||||||
if t, err := strconv.ParseFloat(valStr, 64); err == nil {
|
|
||||||
thermal.CPUTemp = t
|
|
||||||
return thermal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Try thermal level as a proxy (fallback)
|
|
||||||
ctx2, cancel2 := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
ctx2, cancel2 := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||||
defer cancel2()
|
defer cancel2()
|
||||||
|
|
||||||
out2, err := runCmd(ctx2, "sysctl", "-n", "machdep.xcpm.cpu_thermal_level")
|
out2, err := runCmd(ctx2, "sysctl", "-n", "machdep.xcpm.cpu_thermal_level")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
level, _ := strconv.Atoi(strings.TrimSpace(out2))
|
level, _ := strconv.Atoi(strings.TrimSpace(out2))
|
||||||
|
|||||||
Reference in New Issue
Block a user