Event on next alarm reached

This commit is contained in:
nemunaire 2022-10-06 14:17:58 +02:00
parent 9a06d04ce0
commit fffdccc7b8
3 changed files with 58 additions and 18 deletions

View File

@ -11,7 +11,7 @@ import (
"git.nemunai.re/nemunaire/reveil/model" "git.nemunai.re/nemunaire/reveil/model"
) )
func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, 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(db) alarm, err := reveil.GetNextAlarm(db)
if err != nil { if err != nil {
@ -49,6 +49,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, alarm) c.JSON(http.StatusOK, alarm)
}) })
@ -74,6 +76,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, alarm) c.JSON(http.StatusOK, alarm)
}) })
@ -116,6 +120,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, alarm) c.JSON(http.StatusOK, alarm)
}) })
@ -156,6 +162,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, alarm) c.JSON(http.StatusOK, alarm)
}) })
singleAlarmsRoutes.DELETE("", func(c *gin.Context) { singleAlarmsRoutes.DELETE("", func(c *gin.Context) {
@ -166,6 +174,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, nil) c.JSON(http.StatusOK, nil)
}) })
@ -210,6 +220,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, alarm) c.JSON(http.StatusOK, alarm)
}) })
repeatedAlarmsRoutes.DELETE("", func(c *gin.Context) { repeatedAlarmsRoutes.DELETE("", func(c *gin.Context) {
@ -220,6 +232,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, nil) c.JSON(http.StatusOK, nil)
}) })
@ -260,6 +274,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, alarm) c.JSON(http.StatusOK, alarm)
}) })
exceptionAlarmsRoutes.DELETE("", func(c *gin.Context) { exceptionAlarmsRoutes.DELETE("", func(c *gin.Context) {
@ -270,6 +286,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
return return
} }
resetTimer()
c.JSON(http.StatusOK, nil) c.JSON(http.StatusOK, nil)
}) })
} }

View File

@ -7,11 +7,11 @@ import (
"git.nemunai.re/nemunaire/reveil/model" "git.nemunai.re/nemunaire/reveil/model"
) )
func DeclareRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelDBStorage) { func DeclareRoutes(router *gin.Engine, cfg *config.Config, db *reveil.LevelDBStorage, resetTimer func()) {
apiRoutes := router.Group("/api") apiRoutes := router.Group("/api")
declareActionsRoutes(cfg, apiRoutes) declareActionsRoutes(cfg, apiRoutes)
declareAlarmsRoutes(cfg, db, apiRoutes) declareAlarmsRoutes(cfg, db, resetTimer, apiRoutes)
declareGongsRoutes(cfg, apiRoutes) declareGongsRoutes(cfg, apiRoutes)
declareHistoryRoutes(cfg, apiRoutes) declareHistoryRoutes(cfg, apiRoutes)
declareQuotesRoutes(cfg, apiRoutes) declareQuotesRoutes(cfg, apiRoutes)

44
app.go
View File

@ -19,9 +19,10 @@ type App struct {
db *reveil.LevelDBStorage db *reveil.LevelDBStorage
router *gin.Engine router *gin.Engine
srv *http.Server srv *http.Server
nextAlarm *time.Timer
} }
func NewApp(cfg *config.Config) App { func NewApp(cfg *config.Config) *App {
if cfg.DevProxy == "" { if cfg.DevProxy == "" {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} }
@ -38,21 +39,21 @@ func NewApp(cfg *config.Config) App {
log.Fatal("Unable to open the database:", err) log.Fatal("Unable to open the database:", err)
} }
// Register routes // Prepare struct
ui.DeclareRoutes(router, cfg) app := &App{
api.DeclareRoutes(router, cfg, db)
router.GET("/api/version", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"version": Version})
})
// We are ready!
app := App{
cfg: cfg, cfg: cfg,
db: db, db: db,
router: router, router: router,
} }
// Register routes
ui.DeclareRoutes(router, cfg)
api.DeclareRoutes(router, cfg, db, app.ResetTimer)
router.GET("/api/version", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"version": Version})
})
return app return app
} }
@ -62,13 +63,34 @@ func (app *App) Start() {
Handler: app.router, Handler: app.router,
} }
app.ResetTimer()
log.Printf("Ready, listening on %s\n", app.cfg.Bind) log.Printf("Ready, listening on %s\n", app.cfg.Bind)
if err := app.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := app.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err) log.Fatalf("listen: %s\n", err)
} }
} }
func (app *App) ResetTimer() {
if app.nextAlarm != nil {
app.nextAlarm.Stop()
app.nextAlarm = nil
}
if na, err := reveil.GetNextAlarm(app.db); err == nil && na != nil {
app.nextAlarm = time.AfterFunc(time.Until(*na), func() {
app.nextAlarm = nil
log.Println("RUN WAKEUP FUNC")
})
log.Println("Next timer programmed for", *na)
}
}
func (app *App) Stop() { func (app *App) Stop() {
if app.nextAlarm != nil {
app.nextAlarm.Stop()
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
if err := app.srv.Shutdown(ctx); err != nil { if err := app.srv.Shutdown(ctx); err != nil {