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