From 0e5d316731483360ff8d631374d213a4c4c0ac34 Mon Sep 17 00:00:00 2001 From: KemoNine Date: Fri, 4 Sep 2020 14:34:35 -0400 Subject: [PATCH] Add temp util ; break up temp lookups into their own mini-module --- ui/config.go | 42 +++++++++++++++++------------------------- utils/README.md | 3 +++ utils/temp.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 utils/README.md create mode 100644 utils/temp.go diff --git a/ui/config.go b/ui/config.go index 985ade4..7b71939 100644 --- a/ui/config.go +++ b/ui/config.go @@ -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()) diff --git a/utils/README.md b/utils/README.md new file mode 100644 index 0000000..89413d9 --- /dev/null +++ b/utils/README.md @@ -0,0 +1,3 @@ +# Utils + +Misc utilities used elsewhere by PiFrame go code. diff --git a/utils/temp.go b/utils/temp.go new file mode 100644 index 0000000..274b84a --- /dev/null +++ b/utils/temp.go @@ -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 +}