1
0
Fork 0
mirror of https://github.com/Luzifer/streamdeck.git synced 2024-12-20 17:51:21 +00:00

Add auto-reload on config change

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-12-21 15:57:51 +01:00
parent d6733c7181
commit ee03bde2de
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
4 changed files with 47 additions and 3 deletions

View file

@ -3,6 +3,7 @@ package main
import "time"
type config struct {
AutoReload bool `yaml:"auto_reload"`
DefaultBrightness int `yaml:"default_brightness"`
DefaultPage string `yaml:"default_page"`
DisplayOffTime time.Duration `yaml:"display_off_time"`
@ -24,3 +25,9 @@ type dynamicElement struct {
Type string `yaml:"type"`
Attributes map[string]interface{} `yaml:"attributes"`
}
func newConfig() config {
return config{
AutoReload: true,
}
}

View file

@ -7,6 +7,7 @@ replace github.com/Luzifer/streamdeck => ../../
require (
github.com/Luzifer/rconfig/v2 v2.2.1
github.com/Luzifer/streamdeck v0.0.0-20191122003228-a2f524a6b22c
github.com/fsnotify/fsnotify v1.4.7
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/pkg/errors v0.8.1

View file

@ -3,6 +3,8 @@ github.com/Luzifer/rconfig/v2 v2.2.1/go.mod h1:OKIX0/JRZrPJ/ZXXWklQEFXA6tBfWaljZ
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=

View file

@ -10,6 +10,7 @@ import (
"github.com/Luzifer/rconfig/v2"
"github.com/Luzifer/streamdeck"
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/sashko/go-uinput"
log "github.com/sirupsen/logrus"
@ -25,9 +26,10 @@ var (
currentBrightness int
userConfig config
activePage page
activeLoops []refreshingDisplayElement
userConfig config
activePage page
activePageName string
activeLoops []refreshingDisplayElement
sd *streamdeck.Client
@ -135,6 +137,17 @@ func main() {
offTimer = time.NewTimer(userConfig.DisplayOffTime)
}
fswatch, err := fsnotify.NewWatcher()
if err != nil {
log.WithError(err).Fatal("Unable to create file watcher")
}
if userConfig.AutoReload {
if err = fswatch.Add(cfg.Config); err != nil {
log.WithError(err).Error("Unable to watch config, auto-reload will not work")
}
}
for {
select {
case evt := <-sd.Subscribe():
@ -158,6 +171,26 @@ func main() {
log.WithError(err).Error("Unable to toggle to blank page")
}
case evt := <-fswatch.Events:
if evt.Op&fsnotify.Write == fsnotify.Write {
log.Info("Detected change of config, reloading")
if err := loadConfig(); err != nil {
log.WithError(err).Error("Unable to reload config")
continue
}
var nextPage = userConfig.DefaultPage
if _, ok := userConfig.Pages[activePageName]; ok {
nextPage = activePageName
}
if err := togglePage(nextPage); err != nil {
log.WithError(err).Error("Unable to reload page")
continue
}
}
case <-sigs:
return
@ -175,6 +208,7 @@ func togglePage(page string) error {
activeLoops = nil
activePage = userConfig.Pages[page]
activePageName = page
sd.ClearAllKeys()
for idx, kd := range activePage.Keys {