-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp.go
30 lines (24 loc) · 804 Bytes
/
temp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package argononefan
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
// File path in sysfs containing current CPU temperature
const temperatureFilePath = "/sys/class/thermal/thermal_zone0/temp"
// The temperature multiplier
const multiplier = float32(1000)
// ReadCPUTemperature reads the current CPU temperature
func ReadCPUTemperature() (float32, error) {
content, err := ioutil.ReadFile(temperatureFilePath)
if err != nil {
return 0, fmt.Errorf("unable to read temperature file %s : %w", temperatureFilePath, err)
}
stringTemperature := strings.TrimSuffix(string(content), "\n")
rawTemperature, err := strconv.Atoi(stringTemperature)
if err != nil {
return 0, fmt.Errorf("unable to parse temperature %s : %w", content, err)
}
return (float32(rawTemperature) / multiplier), nil
}