2020-08-27 14:02:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dietsche/rfsnotify"
|
|
|
|
|
|
|
|
"git.kemonine.info/PiFrame/watchdog"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CMD_SYSTEMCTL = "/usr/bin/systemctl"
|
|
|
|
PATH_PICTURES = "/tank/pictures"
|
|
|
|
TIMEOUT = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
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 {
|
|
|
|
case event, ok := <-watcher.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Print out event (this is where logic will go eventually))
|
|
|
|
log.Printf("event: %#v\n", event)
|
|
|
|
// [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
|
|
|
|
}
|