Update / adjust config handling code so its more reliable and less prone to crashes ; tweak default values + help text to ensure folks know its only seconds/minutes/hours ; add fim restart handling

This commit is contained in:
KemoNine 2020-09-05 21:27:33 -04:00
parent e979318132
commit 1ccbf4ee03
6 changed files with 75 additions and 42 deletions

View file

@ -22,21 +22,29 @@ func main() {
// Load the config file // Load the config file
pfConfig, _ := config.LoadConfig() pfConfig, _ := config.LoadConfig()
// For some reason the map isn't populated 'right away' and/or doesn't always come in via koanf log.Print("========================================")
// Work around by re-loading the config as necessary to get valid values pfConfig.Print()
for pfConfig.Float64Map(config.CONFIG_KEY_FAN_SPEEDS)[config.CONFIG_MAP_KEY_FAN_SPEED_100] == 0 { log.Print("========================================")
pfConfig, _ = config.LoadConfig() log.Print(pfConfig.Keys())
} log.Print("========================================")
// Get the various fan related config options as local variables // Get the various fan related config options as local variables
speedMap := pfConfig.Float64Map(config.CONFIG_KEY_FAN_SPEEDS) speedMap := pfConfig.Float64Map(config.CONFIG_KEY_FAN_SPEEDS)
POLL_INTERVAL := pfConfig.String(config.CONFIG_KEY_FAN_POLL_INTERVAL) POLL_INTERVAL := pfConfig.String(config.CONFIG_KEY_FAN_POLL_INTERVAL)
SPEED_FULL_TEMP := speedMap[config.CONFIG_MAP_KEY_FAN_SPEED_100] SPEED_FULL_TEMP := pfConfig.Float64(config.CONFIG_KEY_FAN_SPEEDS + "." + config.CONFIG_MAP_KEY_FAN_SPEED_100)
SPEED_SEVENTY_FIVE_PERCENT_TEMP := speedMap[config.CONFIG_MAP_KEY_FAN_SPEED_75] SPEED_SEVENTY_FIVE_PERCENT_TEMP := pfConfig.Float64(config.CONFIG_KEY_FAN_SPEEDS + "." + config.CONFIG_MAP_KEY_FAN_SPEED_75)
SPEED_FIFTY_PERCENT_TEMP := speedMap[config.CONFIG_MAP_KEY_FAN_SPEED_50] SPEED_FIFTY_PERCENT_TEMP := pfConfig.Float64(config.CONFIG_KEY_FAN_SPEEDS + "." + config.CONFIG_MAP_KEY_FAN_SPEED_50)
SPEED_TWENTY_FIVE_PERCENT_TEMP := speedMap[config.CONFIG_MAP_KEY_FAN_SPEED_25] SPEED_TWENTY_FIVE_PERCENT_TEMP := pfConfig.Float64(config.CONFIG_KEY_FAN_SPEEDS + "." + config.CONFIG_MAP_KEY_FAN_SPEED_25)
SPEED_MINIMUM := pfConfig.Int(config.CONFIG_KEY_FAN_MIN_SPEED) SPEED_MINIMUM := pfConfig.Int(config.CONFIG_KEY_FAN_MIN_SPEED)
log.Print(speedMap)
log.Print(POLL_INTERVAL)
log.Print(SPEED_FULL_TEMP)
log.Print(SPEED_SEVENTY_FIVE_PERCENT_TEMP)
log.Print(SPEED_FIFTY_PERCENT_TEMP)
log.Print(SPEED_TWENTY_FIVE_PERCENT_TEMP)
log.Print(SPEED_MINIMUM)
// Setup fan and bail if we can't see it // Setup fan and bail if we can't see it
fan, err := argonFan.New(ADDRESS, BUS) fan, err := argonFan.New(ADDRESS, BUS)
if err != nil { if err != nil {
@ -71,42 +79,52 @@ func main() {
if cpuTemp >= SPEED_FULL_TEMP || gpuTemp >= SPEED_FULL_TEMP { if cpuTemp >= SPEED_FULL_TEMP || gpuTemp >= SPEED_FULL_TEMP {
if SPEED_MINIMUM > 100 { if SPEED_MINIMUM > 100 {
log.Print("MIN speed in 100")
fan.SetSpeed(SPEED_MINIMUM) fan.SetSpeed(SPEED_MINIMUM)
} else { } else {
log.Print("Speed 100%")
fan.SetSpeed(100) fan.SetSpeed(100)
} }
continue continue
} }
if cpuTemp >= SPEED_SEVENTY_FIVE_PERCENT_TEMP || gpuTemp >= SPEED_SEVENTY_FIVE_PERCENT_TEMP { if cpuTemp >= SPEED_SEVENTY_FIVE_PERCENT_TEMP || gpuTemp >= SPEED_SEVENTY_FIVE_PERCENT_TEMP {
if SPEED_MINIMUM > 75 { if SPEED_MINIMUM > 75 {
log.Print("MIN speed in 75")
fan.SetSpeed(SPEED_MINIMUM) fan.SetSpeed(SPEED_MINIMUM)
} else { } else {
log.Print("Speed 75%")
fan.SetSpeed(75) fan.SetSpeed(75)
} }
continue continue
} }
if cpuTemp >= SPEED_FIFTY_PERCENT_TEMP || gpuTemp >= SPEED_FIFTY_PERCENT_TEMP { if cpuTemp >= SPEED_FIFTY_PERCENT_TEMP || gpuTemp >= SPEED_FIFTY_PERCENT_TEMP {
if SPEED_MINIMUM > 50 { if SPEED_MINIMUM > 50 {
log.Print("MIN speed in 50")
fan.SetSpeed(SPEED_MINIMUM) fan.SetSpeed(SPEED_MINIMUM)
} else { } else {
log.Print("Speed 50%")
fan.SetSpeed(50) fan.SetSpeed(50)
} }
continue continue
} }
if cpuTemp >= SPEED_TWENTY_FIVE_PERCENT_TEMP || gpuTemp >= SPEED_TWENTY_FIVE_PERCENT_TEMP { if cpuTemp >= SPEED_TWENTY_FIVE_PERCENT_TEMP || gpuTemp >= SPEED_TWENTY_FIVE_PERCENT_TEMP {
if SPEED_MINIMUM > 25 { if SPEED_MINIMUM > 25 {
log.Print("MIN speed in 25")
fan.SetSpeed(SPEED_MINIMUM) fan.SetSpeed(SPEED_MINIMUM)
} else { } else {
log.Print("Speed 25%")
fan.SetSpeed(25) fan.SetSpeed(25)
} }
continue continue
} }
if cpuTemp < SPEED_TWENTY_FIVE_PERCENT_TEMP || gpuTemp < SPEED_TWENTY_FIVE_PERCENT_TEMP { if cpuTemp < SPEED_TWENTY_FIVE_PERCENT_TEMP || gpuTemp < SPEED_TWENTY_FIVE_PERCENT_TEMP {
log.Print("MIN SPEED")
fan.SetSpeed(SPEED_MINIMUM) fan.SetSpeed(SPEED_MINIMUM)
continue continue
} }
// We should never get here but... // We should never get here but...
// Maxing fan to be on the safe side // Maxing fan to be on the safe side
log.Print("This should never happen")
fan.SetSpeed(100) fan.SetSpeed(100)
} }
} }

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"time"
"github.com/knadh/koanf/providers/confmap" "github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/posflag" "github.com/knadh/koanf/providers/posflag"
@ -15,6 +14,7 @@ import (
) )
func main() { func main() {
log.Print("Starting up")
// Command line flag handler // Command line flag handler
f := flag.NewFlagSet("piframe", flag.ContinueOnError) f := flag.NewFlagSet("piframe", flag.ContinueOnError)
f.Usage = func() { f.Usage = func() {
@ -22,6 +22,7 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// Command line flags // Command line flags
log.Print("Setting up CLI flags")
f.Bool(config.CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow") f.Bool(config.CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow")
cliFlag := f.Lookup(config.CLI_FLAG_CONFIG_ONLY) cliFlag := f.Lookup(config.CLI_FLAG_CONFIG_ONLY)
if cliFlag != nil { if cliFlag != nil {
@ -30,30 +31,25 @@ func main() {
// Process command line flags into handler // Process command line flags into handler
f.Parse(os.Args[1:]) f.Parse(os.Args[1:])
log.Print("Loading config")
// Load the config file // Load the config file
pfConfig, configFileProvider := config.LoadConfig() pfConfig, _ := config.LoadConfig()
log.Printf("%v", pfConfig)
// For some reason the restart interval comes through the config as 0s pfConfig.Print()
// Similar to the fan daemon, keep reloading config until we get a valid value
restartDuration := pfConfig.Duration(config.CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL)
for restartDuration.Milliseconds() < 1 {
pfConfig, configFileProvider = config.LoadConfig()
restartDuration = pfConfig.Duration(config.CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL)
}
// Watch for config changes and re-load config if needed // Watch for config changes and re-load config if needed
configFileProvider.Watch(func(event interface{}, err error) { // configFileProvider.Watch(func(event interface{}, err error) {
if err != nil { // if err != nil {
log.Printf("Error setting up watch of config : %s", err) // log.Printf("Error setting up watch of config : %s", err)
return // return
} // }
// Give the config UI a chance to save and exit clean // // Give the config UI a chance to save and exit clean
time.Sleep(time.Minute) // time.Sleep(time.Minute)
// Bail on slideshow if there is a config change so it restarts with updated config // // Bail on slideshow if there is a config change so it restarts with updated config
log.Fatalf("Config file changed! Exiting!") // log.Fatalf("Config file changed! Exiting!")
}) // })
// Process command line flags // Process command line flags
if err := pfConfig.Load(posflag.Provider(f, ".", pfConfig), nil); err != nil { if err := pfConfig.Load(posflag.Provider(f, ".", pfConfig), nil); err != nil {

View file

@ -2,7 +2,6 @@ package config
import ( import (
"log" "log"
"os"
"github.com/knadh/koanf" "github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml" "github.com/knadh/koanf/parsers/toml"
@ -25,18 +24,20 @@ func LoadConfig() (*koanf.Koanf, *kfile.File) {
CONFIG_KEY_FAN_POLL_INTERVAL: DEFAULT_FAN_POLL_INTERVAL, CONFIG_KEY_FAN_POLL_INTERVAL: DEFAULT_FAN_POLL_INTERVAL,
CONFIG_KEY_FAN_SPEEDS: DEFAULT_FAN_SPEEDS, CONFIG_KEY_FAN_SPEEDS: DEFAULT_FAN_SPEEDS,
CONFIG_KEY_FAN_MIN_SPEED: DEFAULT_FAN_MIN_SPEED, CONFIG_KEY_FAN_MIN_SPEED: DEFAULT_FAN_MIN_SPEED,
}, "."), nil) }, ""), nil)
// Bring in /etc/defaults/pf.toml if it exists // Bring in /etc/defaults/pf.toml if it exists
configFileProvider := kfile.Provider(CONFIG_FILE_PATH) configFileProvider := kfile.Provider(CONFIG_FILE_PATH)
_, err := os.Stat(CONFIG_FILE_PATH) log.Print("========================================")
if os.IsNotExist(err) { if err := pfConfig.Load(configFileProvider, toml.Parser()); err != nil {
//log.Printf("%s does not exist, USING DEFAULTS", CONFIG_FILE_PATH) log.Fatalf("Error loading config : %s", err)
} else {
if errConfigFile := pfConfig.Load(configFileProvider, toml.Parser()); errConfigFile != nil {
log.Fatalf("Error loading config : %s", err)
}
} }
log.Print("========================================")
log.Print("========================================")
log.Print("Loaded Config")
pfConfig.Print()
log.Print("========================================")
return pfConfig, configFileProvider return pfConfig, configFileProvider
} }

View file

@ -24,7 +24,7 @@ const (
const ( const (
DEFAULT_SLIDESHOW_INTERVAL = "300s" DEFAULT_SLIDESHOW_INTERVAL = "300s"
DEFAULT_SLIDESHOW_RESTART_INTERVAL = "7d" DEFAULT_SLIDESHOW_RESTART_INTERVAL = "168h"
DEFAULT_HDMI_OFF = "*-*-* 00:00:00" DEFAULT_HDMI_OFF = "*-*-* 00:00:00"
DEFAULT_HDMI_ON = "*-*-* 06:00:00" DEFAULT_HDMI_ON = "*-*-* 06:00:00"
DEFAULT_ALBUMS_ROOT = "/tank/pictures" DEFAULT_ALBUMS_ROOT = "/tank/pictures"

View file

@ -272,7 +272,7 @@ func ConfigGui(pfconfig *koanf.Koanf) {
main.SetTitle("Configure Intervals") main.SetTitle("Configure Intervals")
main.Clear() main.Clear()
main.AddItem(intervalsForm, 0, 1, true) main.AddItem(intervalsForm, 0, 1, true)
main.AddItem(tview.NewTextView().SetText("Intervals are a number + letter\n\nUse\ns for seconds\nm for minutes\nh for hours\nd for days\nw for weeks"), 0, 1, false) main.AddItem(tview.NewTextView().SetText("Intervals are a number + letter\n\nUse\ns for seconds\nm for minutes\nh for hours"), 0, 1, false)
app.SetFocus(intervalsForm) app.SetFocus(intervalsForm)
} }
if title == "HDMI On/Off" { if title == "HDMI On/Off" {

View file

@ -25,6 +25,7 @@ var fim *exec.Cmd = nil
var stdin io.WriteCloser = nil var stdin io.WriteCloser = nil
func setupFim(PATH_TEMP_FOR_SLIDESHOW string) { func setupFim(PATH_TEMP_FOR_SLIDESHOW string) {
log.Print("Setting up new fim process")
// Prep slideshow command and arguments // Prep slideshow command and arguments
// NOTE: The random flag is seeded with time() ; this is bad as we will be restarting the slideshow at about the same time per the configurd schedule // NOTE: The random flag is seeded with time() ; this is bad as we will be restarting the slideshow at about the same time per the configurd schedule
// We use the non-seeded form to ensure that it's a little more random (or at least hope it's a little more random) // We use the non-seeded form to ensure that it's a little more random (or at least hope it's a little more random)
@ -56,6 +57,7 @@ func Slideshow(pfconfig *koanf.Koanf) {
// /run is a tmpfs so this won't wear on the sd card storage // /run is a tmpfs so this won't wear on the sd card storage
// Create temp folder // Create temp folder
log.Print("Creating temp folder for album selections")
_, err := os.Stat(PATH_TEMP_FOR_SLIDESHOW) _, err := os.Stat(PATH_TEMP_FOR_SLIDESHOW)
if os.IsNotExist(err) { if os.IsNotExist(err) {
errDir := os.MkdirAll(PATH_TEMP_FOR_SLIDESHOW, 0755) errDir := os.MkdirAll(PATH_TEMP_FOR_SLIDESHOW, 0755)
@ -65,6 +67,7 @@ func Slideshow(pfconfig *koanf.Koanf) {
} }
// Cleanup temp folder if it already existed // Cleanup temp folder if it already existed
log.Print("Cleaning up temp folder if it exists")
dirRead, err := os.Open(PATH_TEMP_FOR_SLIDESHOW) dirRead, err := os.Open(PATH_TEMP_FOR_SLIDESHOW)
if err != nil { if err != nil {
log.Fatalf("Error setting up slideshow : %s", err) log.Fatalf("Error setting up slideshow : %s", err)
@ -90,6 +93,7 @@ func Slideshow(pfconfig *koanf.Koanf) {
// Setup symlinks to selected albums to be used with slideshow // Setup symlinks to selected albums to be used with slideshow
// Add albums full paths to command line args for fim // Add albums full paths to command line args for fim
log.Print("Setting up symlinks to selected albums")
albumRootPath := pfconfig.String(config.CONFIG_KEY_ALBUMS_ROOT) albumRootPath := pfconfig.String(config.CONFIG_KEY_ALBUMS_ROOT)
for _, album := range pfconfig.Strings(config.CONFIG_KEY_ALBUMS_SELECTED) { for _, album := range pfconfig.Strings(config.CONFIG_KEY_ALBUMS_SELECTED) {
source := albumRootPath + album source := albumRootPath + album
@ -127,7 +131,13 @@ func Slideshow(pfconfig *koanf.Koanf) {
setupFim(PATH_TEMP_FOR_SLIDESHOW) setupFim(PATH_TEMP_FOR_SLIDESHOW)
// Advance slideshow every interval as defined in const() // Advance slideshow every interval as defined in const()
ticker := time.NewTicker(pfconfig.Duration(config.CONFIG_KEY_SLIDESHOW_INTERVAL)) log.Print("Setting up slideshow advance slide ticker")
slideshowAdvanceDurationString := pfconfig.String(config.CONFIG_KEY_SLIDESHOW_INTERVAL)
slideshowAdvanceDuration, err := time.ParseDuration(slideshowAdvanceDurationString)
if err != nil {
log.Fatalf("Error parsing slide duration : %s", err)
}
ticker := time.NewTicker(slideshowAdvanceDuration)
stop_ticker := make(chan struct{}) stop_ticker := make(chan struct{})
go func() { go func() {
for { for {
@ -162,6 +172,7 @@ func Slideshow(pfconfig *koanf.Koanf) {
STOP_SLIDESHOW := false STOP_SLIDESHOW := false
// Goroutine for tracking which keys are pressed and controlling fim if appropriate // Goroutine for tracking which keys are pressed and controlling fim if appropriate
log.Print("Setting up keyboard listener")
keyboardCtx, keyboardCancel := context.WithCancel(context.Background()) keyboardCtx, keyboardCancel := context.WithCancel(context.Background())
go func(keyboardCtx context.Context) { go func(keyboardCtx context.Context) {
for { for {
@ -211,7 +222,13 @@ func Slideshow(pfconfig *koanf.Koanf) {
}(keyboardCtx) }(keyboardCtx)
// Restart fim after configured timeout ; This is setup as a ticker due to KemoNine not getting CommandWithContext stuff to work properly (lots of pointer related crashes and the like) // Restart fim after configured timeout ; This is setup as a ticker due to KemoNine not getting CommandWithContext stuff to work properly (lots of pointer related crashes and the like)
fimTicker := time.NewTicker(pfconfig.Duration(config.CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL)) log.Print("Setting up fim restart ticker")
fimRestartDurationString := pfconfig.String(config.CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL)
fimRestartDuration, err := time.ParseDuration(fimRestartDurationString)
if err != nil {
log.Fatalf("Error parsing restart duration : %S", err)
}
fimTicker := time.NewTicker(fimRestartDuration)
stop_fim_ticker := make(chan struct{}) stop_fim_ticker := make(chan struct{})
go func() { go func() {
for { for {
@ -234,6 +251,7 @@ func Slideshow(pfconfig *koanf.Koanf) {
// Run fim // Run fim
for !STOP_SLIDESHOW { for !STOP_SLIDESHOW {
log.Print("Top of fim slideshow loop")
if err := fim.Run(); err != nil { if err := fim.Run(); err != nil {
// Unwrap the error a bit so we can find out if a signal killed fim or something else // Unwrap the error a bit so we can find out if a signal killed fim or something else
// An exit code of -1 means the program didn't exit in time or was terminated by a signal (per the docs) // An exit code of -1 means the program didn't exit in time or was terminated by a signal (per the docs)