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:
parent
d6733c7181
commit
ee03bde2de
4 changed files with 47 additions and 3 deletions
|
@ -3,6 +3,7 @@ package main
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
|
AutoReload bool `yaml:"auto_reload"`
|
||||||
DefaultBrightness int `yaml:"default_brightness"`
|
DefaultBrightness int `yaml:"default_brightness"`
|
||||||
DefaultPage string `yaml:"default_page"`
|
DefaultPage string `yaml:"default_page"`
|
||||||
DisplayOffTime time.Duration `yaml:"display_off_time"`
|
DisplayOffTime time.Duration `yaml:"display_off_time"`
|
||||||
|
@ -24,3 +25,9 @@ type dynamicElement struct {
|
||||||
Type string `yaml:"type"`
|
Type string `yaml:"type"`
|
||||||
Attributes map[string]interface{} `yaml:"attributes"`
|
Attributes map[string]interface{} `yaml:"attributes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newConfig() config {
|
||||||
|
return config{
|
||||||
|
AutoReload: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ replace github.com/Luzifer/streamdeck => ../../
|
||||||
require (
|
require (
|
||||||
github.com/Luzifer/rconfig/v2 v2.2.1
|
github.com/Luzifer/rconfig/v2 v2.2.1
|
||||||
github.com/Luzifer/streamdeck v0.0.0-20191122003228-a2f524a6b22c
|
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/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
|
|
|
@ -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/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 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
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 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
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=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"github.com/Luzifer/rconfig/v2"
|
"github.com/Luzifer/rconfig/v2"
|
||||||
"github.com/Luzifer/streamdeck"
|
"github.com/Luzifer/streamdeck"
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sashko/go-uinput"
|
"github.com/sashko/go-uinput"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -25,9 +26,10 @@ var (
|
||||||
|
|
||||||
currentBrightness int
|
currentBrightness int
|
||||||
|
|
||||||
userConfig config
|
userConfig config
|
||||||
activePage page
|
activePage page
|
||||||
activeLoops []refreshingDisplayElement
|
activePageName string
|
||||||
|
activeLoops []refreshingDisplayElement
|
||||||
|
|
||||||
sd *streamdeck.Client
|
sd *streamdeck.Client
|
||||||
|
|
||||||
|
@ -135,6 +137,17 @@ func main() {
|
||||||
offTimer = time.NewTimer(userConfig.DisplayOffTime)
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case evt := <-sd.Subscribe():
|
case evt := <-sd.Subscribe():
|
||||||
|
@ -158,6 +171,26 @@ func main() {
|
||||||
log.WithError(err).Error("Unable to toggle to blank page")
|
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:
|
case <-sigs:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -175,6 +208,7 @@ func togglePage(page string) error {
|
||||||
activeLoops = nil
|
activeLoops = nil
|
||||||
|
|
||||||
activePage = userConfig.Pages[page]
|
activePage = userConfig.Pages[page]
|
||||||
|
activePageName = page
|
||||||
sd.ClearAllKeys()
|
sd.ClearAllKeys()
|
||||||
|
|
||||||
for idx, kd := range activePage.Keys {
|
for idx, kd := range activePage.Keys {
|
||||||
|
|
Loading…
Reference in a new issue