44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
//go:build linux
|
|
|
|
package temperature
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
func getLinuxCPUTemperature() (TemperatureInformation, error) {
|
|
tempFile, err := os.Open("/sys/class/thermal/thermal_zone0/temp")
|
|
if err != nil {
|
|
fmt.Printf("Could not find /sys/class/thermal/thermal_zone0/temp. Trying /sys/class/hwmon/hwmon0/temp1_input...")
|
|
tempFile, err = os.Open("/sys/class/hwmon/hwmon0/temp1_input")
|
|
if err != nil {
|
|
return TemperatureInformation{0, false}, fmt.Errorf("Couldn't find temperature information. System is unsupported.")
|
|
}
|
|
}
|
|
defer tempFile.Close()
|
|
|
|
fileReader := bufio.NewScanner(tempFile)
|
|
|
|
var celsius float64
|
|
|
|
fileReader.Scan()
|
|
if fileReader.Err() != nil {
|
|
return TemperatureInformation{0, false}, fmt.Errorf("Could not read temperature.")
|
|
}
|
|
|
|
data := fileReader.Text()
|
|
celsius, _ = strconv.ParseFloat(data, 64)
|
|
|
|
return TemperatureInformation{
|
|
Celsius: celsius / 1000.0,
|
|
Supported: true,
|
|
}, nil
|
|
}
|
|
|
|
func GetTemperatureInformation() (TemperatureInformation, error) {
|
|
return getLinuxCPUTemperature()
|
|
}
|