Skip to content

Commit 01bdbba

Browse files
committed
fix(sensors): kelvin to Celsius
The previous implementation was incorrect when temp = 2732, expected 0.05, got -0.14
1 parent e699d49 commit 01bdbba

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

sensors/sensors_windows.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package sensors
55

66
import (
77
"context"
8-
"math"
98

109
"github.com/yusufpapurcu/wmi"
1110

@@ -30,18 +29,16 @@ func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
3029
for _, v := range dst {
3130
ts := TemperatureStat{
3231
SensorKey: v.InstanceName,
33-
Temperature: kelvinToCelsius(v.CurrentTemperature, 2),
32+
Temperature: kelvinToCelsius(v.CurrentTemperature),
3433
}
3534
ret = append(ret, ts)
3635
}
3736

3837
return ret, nil
3938
}
4039

41-
func kelvinToCelsius(temp uint32, n int) float64 {
40+
func kelvinToCelsius(temp uint32) float64 {
4241
// wmi return temperature Kelvin * 10, so need to divide the result by 10,
4342
// and then minus 273.15 to get °Celsius.
44-
t := float64(temp/10) - 273.15
45-
n10 := math.Pow10(n)
46-
return math.Trunc((t+0.5/n10)*n10) / n10
43+
return (float64(temp*10) - 27315) / 100
4744
}

0 commit comments

Comments
 (0)