diff --git a/app.go b/app.go index 9d1f823..faaf258 100644 --- a/app.go +++ b/app.go @@ -16,11 +16,12 @@ import ( ) type App struct { - cfg *config.Config - db *reveil.LevelDBStorage - router *gin.Engine - srv *http.Server - nextAlarm *time.Timer + cfg *config.Config + db *reveil.LevelDBStorage + router *gin.Engine + srv *http.Server + nextAlarm *time.Timer + nextPreAlarm *time.Timer } func NewApp(cfg *config.Config) *App { @@ -76,11 +77,45 @@ func (app *App) Start() { func (app *App) ResetTimer() { if app.nextAlarm != nil { app.nextAlarm.Stop() + app.nextPreAlarm = nil app.nextAlarm = nil } + settings, _ := reveil.ReadSettings(app.cfg.SettingsFile) + if na, routines, federated, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil { + if settings != nil && settings.PreAlarmAction != "" { + app.nextPreAlarm = time.AfterFunc(time.Until(*na)-settings.PreAlarmActionDelay*time.Minute, func() { + app.nextPreAlarm = nil + + settings, err := reveil.ReadSettings(app.cfg.SettingsFile) + if err != nil { + log.Println("Unable to read settings:", err.Error()) + return + } + + action, err := reveil.LoadAction(app.cfg, settings.PreAlarmAction) + if err != nil { + log.Println("Unable to load pre-alarm action:", err.Error()) + } + + cmd, err := action.Launch(settings) + if err != nil { + log.Println(err.Error()) + return + } + + go func() { + err := cmd.Wait() + if err != nil { + log.Printf("%q: %s", action.Name, err.Error()) + } + }() + }) + log.Println("Next pre-alarm programmed for", time.Time(*na).Add(settings.PreAlarmActionDelay*-1*time.Minute)) + } app.nextAlarm = time.AfterFunc(time.Until(*na), func() { + app.nextPreAlarm = nil app.nextAlarm = nil reveil.RemoveOldAlarmsSingle(app.db) diff --git a/model/settings.go b/model/settings.go index a0d4030..fb9a02e 100644 --- a/model/settings.go +++ b/model/settings.go @@ -8,13 +8,15 @@ import ( // Settings represents the settings panel. type Settings struct { - Language string `json:"language"` - GongInterval time.Duration `json:"gong_interval"` - WeatherDelay time.Duration `json:"weather_delay"` - WeatherAction string `json:"weather_action"` - MaxRunTime time.Duration `json:"max_run_time"` - MaxVolume uint16 `json:"max_volume"` - Federation map[string]FederationServer `json:"federation"` + Language string `json:"language"` + GongInterval time.Duration `json:"gong_interval"` + WeatherDelay time.Duration `json:"weather_delay"` + WeatherAction string `json:"weather_action"` + PreAlarmActionDelay time.Duration `json:"pre_alarm_delay"` + PreAlarmAction string `json:"pre_alarm_action"` + MaxRunTime time.Duration `json:"max_run_time"` + MaxVolume uint16 `json:"max_volume"` + Federation map[string]FederationServer `json:"federation"` } // ExistsSettings checks if the settings file can by found at the given path. diff --git a/ui/src/lib/settings.js b/ui/src/lib/settings.js index e5f0d54..064cd9a 100644 --- a/ui/src/lib/settings.js +++ b/ui/src/lib/settings.js @@ -5,11 +5,13 @@ export class Settings { } } - update({ language, gong_interval, weather_delay, weather_action, max_run_time, max_volume, federation }) { + update({ language, gong_interval, weather_delay, weather_action, pre_alarm_delay, pre_alarm_action, max_run_time, max_volume, federation }) { this.language = language; this.gong_interval = gong_interval; this.weather_delay = weather_delay; this.weather_action = weather_action; + this.pre_alarm_delay = pre_alarm_delay; + this.pre_alarm_action = pre_alarm_action; this.max_run_time = max_run_time; this.max_volume = max_volume; this.federation = federation; diff --git a/ui/src/routes/settings/+page.svelte b/ui/src/routes/settings/+page.svelte index c17e402..ae0a0aa 100644 --- a/ui/src/routes/settings/+page.svelte +++ b/ui/src/routes/settings/+page.svelte @@ -89,6 +89,40 @@ {/if} + + + + + min + + + + + + {#if $actions.list} + + {#each $actions.list as action (action.id)} + + {/each} + + {:else} +
+ Chargement en cours… +
+ {/if} +
+