Weather action

This commit is contained in:
nemunaire 2022-12-08 17:49:15 +01:00
commit 1def1ff67a
2 changed files with 84 additions and 8 deletions

View file

@ -25,11 +25,15 @@ type Player struct {
currentCmd *exec.Cmd
currentCmdCh chan bool
weatherTime time.Duration
weatherAction *reveil.Action
claironTime time.Duration
claironFile string
ntick int64
hasClaironed bool
hasSpokeWeather bool
launched time.Time
volume uint16
dontUpdateVolume bool
@ -62,13 +66,21 @@ func NewPlayer(cfg *config.Config) (*Player, error) {
return nil, fmt.Errorf("Unable to read settings: %w", err)
}
// Load weather action
wact, err := reveil.LoadAction(cfg, settings.WeatherAction)
if err != nil {
log.Println("Unable to load weather action:", err.Error())
}
p := Player{
Stopper: make(chan bool, 1),
currentCmdCh: make(chan bool, 1),
MaxRunTime: settings.MaxRunTime * time.Minute,
claironTime: settings.GongInterval * time.Minute,
claironFile: reveil.CurrentGongPath(cfg),
reverseOrder: int(time.Now().Unix()/86400)%2 == 0,
Stopper: make(chan bool, 1),
currentCmdCh: make(chan bool, 1),
MaxRunTime: settings.MaxRunTime * time.Minute,
weatherTime: settings.WeatherDelay * time.Minute,
weatherAction: wact,
claironTime: settings.GongInterval * time.Minute,
claironFile: reveil.CurrentGongPath(cfg),
reverseOrder: int(time.Now().Unix()/86400)%2 == 0,
}
// Load our track list
@ -98,6 +110,16 @@ func NewPlayer(cfg *config.Config) (*Player, error) {
return &p, nil
}
func (p *Player) launchAction(a *reveil.Action) (err error) {
p.currentCmd, err = a.Launch()
log.Println("Running action ", a.Name)
err = p.currentCmd.Wait()
p.currentCmdCh <- true
return
}
func (p *Player) playFile(filepath string) (err error) {
p.currentCmd = exec.Command("paplay", filepath)
if err = p.currentCmd.Start(); err != nil {
@ -141,6 +163,11 @@ loop:
p.SetVolume(65535)
p.dontUpdateVolume = true
go p.playFile(p.claironFile)
} else if p.weatherAction != nil && !p.hasSpokeWeather && time.Since(p.launched) >= p.weatherTime {
log.Println("weather time!")
p.SetVolume(65535)
p.dontUpdateVolume = true
go p.launchAction(p.weatherAction)
} else {
p.dontUpdateVolume = false
p.volume = 3500 + uint16(math.Log(1+float64(p.ntick)/8)*9500)