From dd23a96d865772de966ac6d149590ee562f5144f Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 6 Oct 2022 14:17:58 +0200 Subject: [PATCH] Event on next alarm reached --- api/alarms.go | 20 +++++++++++++++++++- api/routes.go | 4 ++-- app.go | 52 ++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/api/alarms.go b/api/alarms.go index 1ccdf62..59dfe98 100644 --- a/api/alarms.go +++ b/api/alarms.go @@ -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) }) } diff --git a/api/routes.go b/api/routes.go index 31e6ef7..8c47419 100644 --- a/api/routes.go +++ b/api/routes.go @@ -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) diff --git a/app.go b/app.go index 7f340ae..4095f2d 100644 --- a/app.go +++ b/app.go @@ -15,13 +15,14 @@ import ( ) type App struct { - cfg *config.Config - db *reveil.LevelDBStorage - router *gin.Engine - srv *http.Server + cfg *config.Config + 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 {