Add pre-alarm action
This commit is contained in:
parent
86c81396e2
commit
a3b00fe38d
45
app.go
45
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)
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -89,6 +89,40 @@
|
||||
{/if}
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup>
|
||||
<Label for="preAlarmDelay">Lancement action pré-alarme</Label>
|
||||
<InputGroup>
|
||||
<Input
|
||||
type="number"
|
||||
id="preAlarmDelay"
|
||||
placeholder="5"
|
||||
bind:value={settings.pre_alarm_delay}
|
||||
on:input={submitSettings}
|
||||
/>
|
||||
<InputGroupText>min</InputGroupText>
|
||||
</InputGroup>
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup>
|
||||
<Label for="preAlarmRituel">Action pour l'action pré-alarme</Label>
|
||||
{#if $actions.list}
|
||||
<Input
|
||||
type="select"
|
||||
id="preAlarmRituel"
|
||||
bind:value={settings.pre_alarm_action}
|
||||
on:input={submitSettings}
|
||||
>
|
||||
{#each $actions.list as action (action.id)}
|
||||
<option value="{action.path}">{action.name}</option>
|
||||
{/each}
|
||||
</Input>
|
||||
{:else}
|
||||
<div class="d-flex justify-content-center align-items-center gap-2">
|
||||
<Spinner color="primary" /> Chargement en cours…
|
||||
</div>
|
||||
{/if}
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup>
|
||||
<Label for="greetingLanguage">Langue de salutation</Label>
|
||||
<Input
|
||||
|
Loading…
Reference in New Issue
Block a user