Start routine at wakeup end
This commit is contained in:
parent
1def1ff67a
commit
e1f5fbcd6c
@ -20,7 +20,7 @@ func declareAlarmRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
|||||||
|
|
||||||
router.POST("/alarm/run", func(c *gin.Context) {
|
router.POST("/alarm/run", func(c *gin.Context) {
|
||||||
if player.CommonPlayer == nil {
|
if player.CommonPlayer == nil {
|
||||||
err := player.WakeUp(cfg)
|
err := player.WakeUp(cfg, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, resetTimer func(), router *gin.RouterGroup) {
|
func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, resetTimer func(), router *gin.RouterGroup) {
|
||||||
router.GET("/alarms/next", func(c *gin.Context) {
|
router.GET("/alarms/next", func(c *gin.Context) {
|
||||||
alarm, err := reveil.GetNextAlarm(cfg, db)
|
alarm, _, err := reveil.GetNextAlarm(cfg, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
|
4
app.go
4
app.go
@ -80,11 +80,11 @@ func (app *App) ResetTimer() {
|
|||||||
app.nextAlarm = nil
|
app.nextAlarm = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if na, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil {
|
if na, routines, err := reveil.GetNextAlarm(app.cfg, app.db); err == nil && na != nil {
|
||||||
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
|
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
|
||||||
app.nextAlarm = nil
|
app.nextAlarm = nil
|
||||||
reveil.RemoveOldAlarmsSingle(app.db)
|
reveil.RemoveOldAlarmsSingle(app.db)
|
||||||
err := player.WakeUp(app.cfg)
|
err := player.WakeUp(app.cfg, routines)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
return
|
return
|
||||||
|
@ -40,37 +40,57 @@ func (h *Hour) UnmarshalJSON(src []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNextAlarm(cfg *config.Config, db *LevelDBStorage) (*time.Time, error) {
|
func GetNextAlarm(cfg *config.Config, db *LevelDBStorage) (*time.Time, []Identifier, error) {
|
||||||
alarmsRepeated, err := GetAlarmsRepeated(db)
|
alarmsRepeated, err := GetAlarmsRepeated(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var closestAlarm *time.Time
|
var closestAlarm *time.Time
|
||||||
|
var closestAlarmRoutines []Identifier
|
||||||
for _, alarm := range alarmsRepeated {
|
for _, alarm := range alarmsRepeated {
|
||||||
next := alarm.GetNextOccurence(cfg, db)
|
next := alarm.GetNextOccurence(cfg, db)
|
||||||
if next != nil && (closestAlarm == nil || closestAlarm.After(*next)) {
|
if next != nil && (closestAlarm == nil || closestAlarm.After(*next)) {
|
||||||
closestAlarm = next
|
closestAlarm = next
|
||||||
|
closestAlarmRoutines = alarm.FollowingRoutines
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
alarmsSingle, err := GetAlarmsSingle(db)
|
alarmsSingle, err := GetAlarmsSingle(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, alarm := range alarmsSingle {
|
for _, alarm := range alarmsSingle {
|
||||||
if closestAlarm == nil || (closestAlarm.After(alarm.Time) && alarm.Time.After(now)) {
|
if closestAlarm == nil || (closestAlarm.After(alarm.Time) && alarm.Time.After(now)) {
|
||||||
closestAlarm = &alarm.Time
|
closestAlarm = &alarm.Time
|
||||||
|
closestAlarmRoutines = alarm.FollowingRoutines
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return closestAlarm, nil
|
return closestAlarm, closestAlarmRoutines, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNextException(cfg *config.Config, db *LevelDBStorage) (*time.Time, error) {
|
||||||
|
alarmsExceptions, err := GetAlarmExceptions(db)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var closestException *time.Time
|
||||||
|
for _, except := range alarmsExceptions {
|
||||||
|
if except != nil && time.Time(*except.End).After(time.Now()) && (closestException == nil || closestException.After(time.Time(*except.Start))) {
|
||||||
|
tmp := time.Time(*except.Start)
|
||||||
|
closestException = &tmp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return closestException, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DropNextAlarm(cfg *config.Config, db *LevelDBStorage) error {
|
func DropNextAlarm(cfg *config.Config, db *LevelDBStorage) error {
|
||||||
timenext, err := GetNextAlarm(cfg, db)
|
timenext, _, err := GetNextAlarm(cfg, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package reveil
|
package reveil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@ -9,6 +11,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.nemunai.re/nemunaire/reveil/config"
|
"git.nemunai.re/nemunaire/reveil/config"
|
||||||
)
|
)
|
||||||
@ -19,6 +22,10 @@ type RoutineStep struct {
|
|||||||
Args []string `json:"args,omitempty"`
|
Args []string `json:"args,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *RoutineStep) GetAction(cfg *config.Config) (*Action, error) {
|
||||||
|
return LoadAction(cfg, s.Action)
|
||||||
|
}
|
||||||
|
|
||||||
type Routine struct {
|
type Routine struct {
|
||||||
Id Identifier `json:"id"`
|
Id Identifier `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@ -72,6 +79,21 @@ func LoadRoutine(path string, cfg *config.Config) ([]RoutineStep, error) {
|
|||||||
return steps, nil
|
return steps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoadRoutineFromId(id Identifier, cfg *config.Config) (*Routine, error) {
|
||||||
|
routines, err := LoadRoutines(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, routine := range routines {
|
||||||
|
if bytes.Equal(routine.Id, id) {
|
||||||
|
return routine, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("Unable to find routine %x", id)
|
||||||
|
}
|
||||||
|
|
||||||
func LoadRoutines(cfg *config.Config) (routines []*Routine, err error) {
|
func LoadRoutines(cfg *config.Config) (routines []*Routine, err error) {
|
||||||
err = filepath.Walk(cfg.RoutinesDir, func(path string, d fs.FileInfo, err error) error {
|
err = filepath.Walk(cfg.RoutinesDir, func(path string, d fs.FileInfo, err error) error {
|
||||||
if d.IsDir() && path != cfg.RoutinesDir {
|
if d.IsDir() && path != cfg.RoutinesDir {
|
||||||
@ -117,3 +139,28 @@ func (r *Routine) Rename(newName string) error {
|
|||||||
func (a *Routine) Remove() error {
|
func (a *Routine) Remove() error {
|
||||||
return os.Remove(a.Path)
|
return os.Remove(a.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Routine) Launch(cfg *config.Config) error {
|
||||||
|
for _, s := range a.Steps {
|
||||||
|
act, err := s.GetAction(cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Unable to get action: %s: %s", s.Action, err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Duration(s.Delay) * time.Second)
|
||||||
|
|
||||||
|
cmd, err := act.Launch()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Unable to launch the action %q: %s", s.Action, err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err = cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Something goes wrong when waiting for the action %q's end: %s", s.Action, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -31,6 +31,8 @@ type Player struct {
|
|||||||
claironTime time.Duration
|
claironTime time.Duration
|
||||||
claironFile string
|
claironFile string
|
||||||
|
|
||||||
|
endRoutines []*reveil.Routine
|
||||||
|
|
||||||
ntick int64
|
ntick int64
|
||||||
hasClaironed bool
|
hasClaironed bool
|
||||||
hasSpokeWeather bool
|
hasSpokeWeather bool
|
||||||
@ -41,7 +43,7 @@ type Player struct {
|
|||||||
playedItem int
|
playedItem int
|
||||||
}
|
}
|
||||||
|
|
||||||
func WakeUp(cfg *config.Config) (err error) {
|
func WakeUp(cfg *config.Config, routine []reveil.Identifier) (err error) {
|
||||||
if CommonPlayer != nil {
|
if CommonPlayer != nil {
|
||||||
return fmt.Errorf("Unable to start the player: a player is already running")
|
return fmt.Errorf("Unable to start the player: a player is already running")
|
||||||
}
|
}
|
||||||
@ -50,16 +52,16 @@ func WakeUp(cfg *config.Config) (err error) {
|
|||||||
seed -= seed % 172800
|
seed -= seed % 172800
|
||||||
rand.Seed(seed)
|
rand.Seed(seed)
|
||||||
|
|
||||||
CommonPlayer, err = NewPlayer(cfg)
|
CommonPlayer, err = NewPlayer(cfg, routine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
go CommonPlayer.WakeUp()
|
go CommonPlayer.WakeUp(cfg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlayer(cfg *config.Config) (*Player, error) {
|
func NewPlayer(cfg *config.Config, routines []reveil.Identifier) (*Player, error) {
|
||||||
// Load our settings
|
// Load our settings
|
||||||
settings, err := reveil.ReadSettings(cfg.SettingsFile)
|
settings, err := reveil.ReadSettings(cfg.SettingsFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -83,6 +85,17 @@ func NewPlayer(cfg *config.Config) (*Player, error) {
|
|||||||
reverseOrder: int(time.Now().Unix()/86400)%2 == 0,
|
reverseOrder: int(time.Now().Unix()/86400)%2 == 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load routines
|
||||||
|
for _, routine := range routines {
|
||||||
|
r, err := reveil.LoadRoutineFromId(routine, cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Unable to load routine %x: %s", routine, err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
p.endRoutines = append(p.endRoutines, r)
|
||||||
|
}
|
||||||
|
|
||||||
// Load our track list
|
// Load our track list
|
||||||
tracks, err := reveil.LoadTracks(cfg)
|
tracks, err := reveil.LoadTracks(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -136,7 +149,7 @@ func (p *Player) playFile(filepath string) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) WakeUp() {
|
func (p *Player) WakeUp(cfg *config.Config) {
|
||||||
log.Println("Playlist in use:", strings.Join(p.Playlist, " ; "))
|
log.Println("Playlist in use:", strings.Join(p.Playlist, " ; "))
|
||||||
|
|
||||||
// Prepare sound player
|
// Prepare sound player
|
||||||
@ -241,6 +254,11 @@ loopcalm:
|
|||||||
|
|
||||||
CommonPlayer = nil
|
CommonPlayer = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Start Routine if any
|
||||||
|
for _, r := range p.endRoutines {
|
||||||
|
go r.Launch(cfg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) NextTrack() {
|
func (p *Player) NextTrack() {
|
||||||
|
@ -24,7 +24,7 @@ func DeclareNoJSRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelD
|
|||||||
router.SetHTMLTemplate(templ)
|
router.SetHTMLTemplate(templ)
|
||||||
|
|
||||||
router.GET("/nojs.html", func(c *gin.Context) {
|
router.GET("/nojs.html", func(c *gin.Context) {
|
||||||
alarm, err := reveil.GetNextAlarm(cfg, db)
|
alarm, _, err := reveil.GetNextAlarm(cfg, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(http.StatusInternalServerError, "error.tmpl", gin.H{"errmsg": err.Error()})
|
c.HTML(http.StatusInternalServerError, "error.tmpl", gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
@ -109,7 +109,7 @@ func DeclareNoJSRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelD
|
|||||||
|
|
||||||
case "start":
|
case "start":
|
||||||
if player.CommonPlayer == nil {
|
if player.CommonPlayer == nil {
|
||||||
err := player.WakeUp(cfg)
|
err := player.WakeUp(cfg, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(http.StatusInternalServerError, "error.tmpl", gin.H{"errmsg": err.Error()})
|
c.HTML(http.StatusInternalServerError, "error.tmpl", gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user