2020-08-27 14:02:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dietsche/rfsnotify"
|
|
|
|
|
2020-09-05 20:04:43 +00:00
|
|
|
"git.kemonine.info/PiFrame/config"
|
2020-08-27 14:02:44 +00:00
|
|
|
"git.kemonine.info/PiFrame/watchdog"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CMD_SYSTEMCTL = "/usr/bin/systemctl"
|
|
|
|
TIMEOUT = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-09-05 20:04:43 +00:00
|
|
|
// Load the config file
|
2020-09-20 22:10:14 +00:00
|
|
|
pfConfig, configFileProvider := config.LoadConfig(false)
|
2020-09-06 07:36:35 +00:00
|
|
|
|
|
|
|
// Watch for config changes and re-load config if needed
|
|
|
|
configFileProvider.Watch(func(event interface{}, err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error setting up watch of config : %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail on slideshow if there is a config change so it restarts with updated config
|
|
|
|
log.Fatalf("Config file changed! Exiting!")
|
|
|
|
})
|
2020-09-05 20:04:43 +00:00
|
|
|
|
|
|
|
PATH_PICTURES := pfConfig.String(config.CONFIG_KEY_ALBUMS_ROOT)
|
|
|
|
|
2020-08-29 01:21:04 +00:00
|
|
|
// Create watchdog timer that restarts pf-ui.service on timeout
|
2020-08-27 14:02:44 +00:00
|
|
|
watchdog := watchdog.New(TIMEOUT, func() {
|
2020-08-29 01:21:04 +00:00
|
|
|
err := exec.Command(CMD_SYSTEMCTL, "restart", "pf-ui.service").Run()
|
2020-08-27 14:02:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error running %s : %s", CMD_SYSTEMCTL, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
watchdog.DeferredStart()
|
|
|
|
|
|
|
|
// Create fswatcher
|
|
|
|
watcher, err := rfsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error setting up rfsnotify")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we clean stuff up no matter what
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
// Setup goroutine and listen for events
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2020-09-21 00:36:57 +00:00
|
|
|
case _, ok := <-watcher.Events:
|
2020-08-27 14:02:44 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// [Re]Start timer to restart slideshow after the fs events die down
|
|
|
|
watchdog.Kick()
|
|
|
|
case err, ok := <-watcher.Errors:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("Error: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Setup paths to watch
|
|
|
|
err = watcher.AddRecursive(PATH_PICTURES)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error setting up recursive watch of %s", PATH_PICTURES)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drain done channel...
|
|
|
|
<-done
|
|
|
|
}
|