piframe-go/cmd/inotify/inotify.go

69 lines
1.4 KiB
Go
Raw Normal View History

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() {
// Create watchdog timer that restarts pf-ui.service on timeout
watchdog := watchdog.New(TIMEOUT, func() {
err := exec.Command(CMD_SYSTEMCTL, "restart", "pf-ui.service").Run()
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
}