New features

This commit is contained in:
nemunaire 2023-11-13 18:50:42 +01:00
commit a9c6cdcd0f
16 changed files with 584 additions and 19 deletions

View file

@ -2,18 +2,21 @@ package api
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"git.nemunai.re/nemunaire/hathoris/config"
"git.nemunai.re/nemunaire/hathoris/inputs"
"git.nemunai.re/nemunaire/hathoris/sources"
)
type SourceState struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
Active *bool `json:"active,omitempty"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Active *bool `json:"active,omitempty"`
Controlable bool `json:"controlable,omitempty"`
}
func declareSourcesRoutes(cfg *config.Config, router *gin.RouterGroup) {
@ -22,11 +25,13 @@ func declareSourcesRoutes(cfg *config.Config, router *gin.RouterGroup) {
for k, src := range sources.SoundSources {
active := src.IsActive()
_, controlable := src.(inputs.ControlableInput)
ret[k] = &SourceState{
Name: src.GetName(),
Enabled: src.IsEnabled(),
Active: &active,
Name: src.GetName(),
Enabled: src.IsEnabled(),
Active: &active,
Controlable: controlable,
}
}
@ -69,6 +74,21 @@ func declareSourcesRoutes(cfg *config.Config, router *gin.RouterGroup) {
sourcesRoutes.POST("/enable", func(c *gin.Context) {
src := c.MustGet("source").(sources.SoundSource)
if src.IsEnabled() {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "The source is already enabled"})
return
}
// Disable all sources
for k, src := range sources.SoundSources {
if src.IsEnabled() {
err := src.Disable()
if err != nil {
log.Printf("Unable to disable %s: %s", k, err.Error())
}
}
}
err := src.Enable()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to enable the source: %s", err.Error())})
@ -88,6 +108,22 @@ func declareSourcesRoutes(cfg *config.Config, router *gin.RouterGroup) {
c.JSON(http.StatusOK, true)
})
sourcesRoutes.POST("/pause", func(c *gin.Context) {
src := c.MustGet("source").(sources.SoundSource)
if !src.IsActive() {
c.AbortWithStatusJSON(http.StatusNotAcceptable, gin.H{"errmsg": "Source not active"})
return
}
s, ok := src.(inputs.ControlableInput)
if !ok {
c.AbortWithStatusJSON(http.StatusMethodNotAllowed, gin.H{"errmsg": "The source doesn't support"})
return
}
c.JSON(http.StatusOK, s.TogglePause())
})
}
func SourceHandler(c *gin.Context) {