finished temperature querying
This commit is contained in:
1 parent
b1d4f3565d
commit
be44d008c7
7 files changed
+168
-3
No files matched your search
@@ -0,0 +1,43 @@
|
||||
//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()
|
||||
}
|
||||
Reference in new issue
Block a user