Add temp util ; break up temp lookups into their own mini-module

This commit is contained in:
KemoNine 2020-09-04 14:34:35 -04:00
parent fffd67f8c0
commit 0e5d316731
3 changed files with 61 additions and 25 deletions

View file

@ -2,14 +2,12 @@ package ui
import (
"fmt"
"io/ioutil"
"log"
"math"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/gdamore/tcell"
@ -17,14 +15,13 @@ import (
"github.com/knadh/koanf"
"github.com/rivo/tview"
"git.kemonine.info/PiFrame/utils"
"git.kemonine.info/PiFrame/wifi"
)
const (
CMD_SYSTEMCTL = "/usr/bin/systemctl"
CMD_FINDMNT = "/usr/bin/findmnt"
CMD_VCGENCMD = "/opt/vc/bin/vcgencmd"
FILE_CPU_TEMP = "/sys/class/thermal/thermal_zone0/temp"
SYNCTHING_FOLDER_SKIP = ".stfolder"
)
@ -60,23 +57,10 @@ func ConfigGui(config *koanf.Koanf) {
filesystems := strings.Split(strings.Trim(string(findmntOut), "\n"), "\n")
// GPU Temp
vcgencmdOut, err := exec.Command(CMD_VCGENCMD, "measure_temp").Output()
if err != nil {
log.Fatalf("Error getting GPU temp : %s", err)
}
gpuTemp := strings.Split(strings.Trim(string(vcgencmdOut), "\n"), "=")[1]
gpuTemp := fmt.Sprintf("%.2f'C", utils.GetGPUTemp())
// CPU Temp
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)
}
cpuTemp := fmt.Sprintf("%.2f'C", float64(cpuTempInt)/1000.0)
cpuTemp := fmt.Sprintf("%.2f'C", utils.GetCPUTemp())
// Get list of all folders that can be used as albums
var albums []string
@ -192,8 +176,12 @@ func ConfigGui(config *koanf.Koanf) {
intervalsForm := tview.NewForm()
configSlideInterval := config.String(CONFIG_KEY_SLIDESHOW_INTERVAL)
configRestartInterval := config.String(CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL)
intervalsForm.AddInputField("Slide", configSlideInterval, 0, nil, nil)
intervalsForm.AddInputField("Restart/Reshuffle", configRestartInterval, 0, nil, nil)
intervalsForm.AddInputField("Slide", configSlideInterval, 0, nil, func(value string) {
configSlideInterval = value
})
intervalsForm.AddInputField("Restart/Reshuffle", configRestartInterval, 0, nil, func(value string) {
configRestartInterval = value
})
intervalsForm.AddButton("Apply", nil)
intervalsForm.AddButton("Cancel", func() {
main.Clear()
@ -224,8 +212,12 @@ func ConfigGui(config *koanf.Koanf) {
hdmiForm := tview.NewForm()
configHDMIOff := config.String(CONFIG_KEY_HDMI_OFF)
configHDMIOn := config.String(CONFIG_KEY_HDMI_ON)
hdmiForm.AddInputField("HDMI Off Schedule", configHDMIOff, 0, nil, nil)
hdmiForm.AddInputField("HDMI On Schedule", configHDMIOn, 0, nil, nil)
hdmiForm.AddInputField("HDMI Off Schedule", configHDMIOff, 0, nil, func(value string) {
configHDMIOff = value
})
hdmiForm.AddInputField("HDMI On Schedule", configHDMIOn, 0, nil, func(value string) {
configHDMIOn = value
})
hdmiForm.AddButton("Apply", nil)
hdmiForm.AddButton("Cancel", func() {
main.Clear()
@ -416,10 +408,10 @@ func ConfigGui(config *koanf.Koanf) {
intervalField, intervalButton := intervalsForm.GetFocusedItemIndex()
wifiField, wifiButton := wifiConfigForm.GetFocusedItemIndex()
hdmiField, hdmiButton := hdmiForm.GetFocusedItemIndex()
if (wifiField != -1 || wifiButton != -1 ||
if wifiField != -1 || wifiButton != -1 ||
albumField != -1 || albumButton != -1 ||
intervalField != -1 || intervalButton != -1 ||
hdmiField != -1 || hdmiButton != -1) {
hdmiField != -1 || hdmiButton != -1 {
switch event.Key() {
case tcell.KeyUp:
return tcell.NewEventKey(tcell.KeyBacktab, 0, event.Modifiers())

3
utils/README.md Normal file
View file

@ -0,0 +1,3 @@
# Utils
Misc utilities used elsewhere by PiFrame go code.

41
utils/temp.go Normal file
View file

@ -0,0 +1,41 @@
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
}