piframe-go/utils/temperature.go

42 lines
1,009 B
Go
Raw Normal View History

package utils
import (
"io/ioutil"
"log"
"os/exec"
"strconv"
"strings"
)
const (
CMD_VCGENCMD = "/opt/vc/bin/vcgencmd"
FILE_CPU_TEMP = "/sys/class/thermal/thermal_zone0/temp"
)
func GetCPUTemp() float64 {
cpuTempFileContents, err := ioutil.ReadFile(FILE_CPU_TEMP)
if err != nil {
log.Fatalf("Error getting CPU temp : %s", err)
}
cpuTempStr := strings.Trim(string(cpuTempFileContents), "\n")
cpuTempInt, err := strconv.Atoi(cpuTempStr)
if err != nil {
log.Fatalf("Error processing CPU temp : %S", err)
}
return float64(cpuTempInt) / 1000.0
}
func GetGPUTemp() float64 {
vcgencmdOut, err := exec.Command(CMD_VCGENCMD, "measure_temp").Output()
if err != nil {
log.Fatalf("Error getting GPU temp : %s", err)
}
gpuTempString := strings.Split(strings.Trim(string(vcgencmdOut), "\n"), "=")[1]
gpuTempString = strings.Trim(gpuTempString, "'C")
gpuTemp, err := strconv.ParseFloat(gpuTempString, 64)
if err != nil {
log.Fatalf("Error parsing GPU Temp : %s", err)
}
return gpuTemp
}