reveil/api/alarm.go

77 lines
1.8 KiB
Go
Raw Normal View History

2022-10-14 18:08:03 +00:00
package api
import (
2024-07-25 17:38:02 +00:00
"fmt"
"log"
2022-10-14 18:08:03 +00:00
"net/http"
"github.com/gin-gonic/gin"
"git.nemunai.re/nemunaire/reveil/config"
2024-07-25 17:38:02 +00:00
"git.nemunai.re/nemunaire/reveil/model"
2022-10-14 18:08:03 +00:00
"git.nemunai.re/nemunaire/reveil/player"
)
func declareAlarmRoutes(cfg *config.Config, router *gin.RouterGroup) {
router.GET("/alarm", func(c *gin.Context) {
if player.CommonPlayer == nil {
c.JSON(http.StatusOK, false)
} else {
c.JSON(http.StatusOK, true)
}
})
router.POST("/alarm/run", func(c *gin.Context) {
if player.CommonPlayer == nil {
2024-07-25 17:38:02 +00:00
err := player.WakeUp(cfg, nil, true)
2022-10-14 18:08:03 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
} else {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Player already running"})
return
}
c.JSON(http.StatusOK, true)
})
2022-12-06 15:41:38 +00:00
router.POST("/alarm/next", func(c *gin.Context) {
if player.CommonPlayer == nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "No player currently playing"})
return
} else {
player.CommonPlayer.NextTrack()
}
c.JSON(http.StatusOK, true)
})
2022-10-14 18:08:03 +00:00
router.DELETE("/alarm", func(c *gin.Context) {
if player.CommonPlayer != nil {
err := player.CommonPlayer.Stop()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2024-07-25 17:38:02 +00:00
settings, err := reveil.ReadSettings(cfg.SettingsFile)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Errorf("Unable to read settings: %s", err.Error())})
return
}
for k, srv := range settings.Federation {
err = srv.WakeStop()
2024-07-25 17:38:02 +00:00
if err != nil {
log.Printf("Unable to do federated wakeup on %s: %s", k, err.Error())
} else {
log.Printf("Federated wakeup on %s: launched!", k)
}
}
2022-10-14 18:08:03 +00:00
}
c.JSON(http.StatusOK, true)
})
}