Event on next alarm reached
This commit is contained in:
parent
9a06d04ce0
commit
fffdccc7b8
@ -11,7 +11,7 @@ import (
|
||||
"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) {
|
||||
alarm, err := reveil.GetNextAlarm(db)
|
||||
if err != nil {
|
||||
@ -49,6 +49,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, alarm)
|
||||
})
|
||||
|
||||
@ -74,6 +76,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, alarm)
|
||||
})
|
||||
|
||||
@ -116,6 +120,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, alarm)
|
||||
})
|
||||
|
||||
@ -156,6 +162,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, alarm)
|
||||
})
|
||||
singleAlarmsRoutes.DELETE("", func(c *gin.Context) {
|
||||
@ -166,6 +174,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, nil)
|
||||
})
|
||||
|
||||
@ -210,6 +220,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, alarm)
|
||||
})
|
||||
repeatedAlarmsRoutes.DELETE("", func(c *gin.Context) {
|
||||
@ -220,6 +232,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, nil)
|
||||
})
|
||||
|
||||
@ -260,6 +274,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, alarm)
|
||||
})
|
||||
exceptionAlarmsRoutes.DELETE("", func(c *gin.Context) {
|
||||
@ -270,6 +286,8 @@ func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, router *
|
||||
return
|
||||
}
|
||||
|
||||
resetTimer()
|
||||
|
||||
c.JSON(http.StatusOK, nil)
|
||||
})
|
||||
}
|
||||
|
@ -7,11 +7,11 @@ import (
|
||||
"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")
|
||||
|
||||
declareActionsRoutes(cfg, apiRoutes)
|
||||
declareAlarmsRoutes(cfg, db, apiRoutes)
|
||||
declareAlarmsRoutes(cfg, db, resetTimer, apiRoutes)
|
||||
declareGongsRoutes(cfg, apiRoutes)
|
||||
declareHistoryRoutes(cfg, apiRoutes)
|
||||
declareQuotesRoutes(cfg, apiRoutes)
|
||||
|
44
app.go
44
app.go
@ -19,9 +19,10 @@ type App struct {
|
||||
db *reveil.LevelDBStorage
|
||||
router *gin.Engine
|
||||
srv *http.Server
|
||||
nextAlarm *time.Timer
|
||||
}
|
||||
|
||||
func NewApp(cfg *config.Config) App {
|
||||
func NewApp(cfg *config.Config) *App {
|
||||
if cfg.DevProxy == "" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
@ -38,21 +39,21 @@ func NewApp(cfg *config.Config) App {
|
||||
log.Fatal("Unable to open the database:", err)
|
||||
}
|
||||
|
||||
// Register routes
|
||||
ui.DeclareRoutes(router, cfg)
|
||||
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{
|
||||
// Prepare struct
|
||||
app := &App{
|
||||
cfg: cfg,
|
||||
db: db,
|
||||
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
|
||||
}
|
||||
|
||||
@ -62,13 +63,34 @@ func (app *App) Start() {
|
||||
Handler: app.router,
|
||||
}
|
||||
|
||||
app.ResetTimer()
|
||||
|
||||
log.Printf("Ready, listening on %s\n", app.cfg.Bind)
|
||||
if err := app.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
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() {
|
||||
if app.nextAlarm != nil {
|
||||
app.nextAlarm.Stop()
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := app.srv.Shutdown(ctx); err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user