New features
This commit is contained in:
parent
c67b43ab3c
commit
a9c6cdcd0f
16 changed files with 584 additions and 19 deletions
87
api/inputs.go
Normal file
87
api/inputs.go
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.nemunai.re/nemunaire/hathoris/config"
|
||||
"git.nemunai.re/nemunaire/hathoris/inputs"
|
||||
)
|
||||
|
||||
type InputState struct {
|
||||
Name string `json:"name"`
|
||||
Active bool `json:"active"`
|
||||
Controlable bool `json:"controlable"`
|
||||
}
|
||||
|
||||
func declareInputsRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
||||
router.GET("/inputs", func(c *gin.Context) {
|
||||
ret := map[string]*InputState{}
|
||||
|
||||
for k, inp := range inputs.SoundInputs {
|
||||
_, controlable := inp.(inputs.ControlableInput)
|
||||
|
||||
ret[k] = &InputState{
|
||||
Name: inp.GetName(),
|
||||
Active: inp.IsActive(),
|
||||
Controlable: controlable,
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, ret)
|
||||
})
|
||||
|
||||
inputsRoutes := router.Group("/inputs/:input")
|
||||
inputsRoutes.Use(InputHandler)
|
||||
|
||||
inputsRoutes.GET("", func(c *gin.Context) {
|
||||
src := c.MustGet("input").(inputs.SoundInput)
|
||||
|
||||
c.JSON(http.StatusOK, &InputState{
|
||||
Name: src.GetName(),
|
||||
Active: src.IsActive(),
|
||||
})
|
||||
})
|
||||
inputsRoutes.GET("/settings", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("input"))
|
||||
})
|
||||
inputsRoutes.GET("/currently", func(c *gin.Context) {
|
||||
src := c.MustGet("input").(inputs.SoundInput)
|
||||
|
||||
if !src.IsActive() {
|
||||
c.AbortWithStatusJSON(http.StatusNotAcceptable, gin.H{"errmsg": "Input not active"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, src.CurrentlyPlaying())
|
||||
})
|
||||
inputsRoutes.POST("/pause", func(c *gin.Context) {
|
||||
input, ok := c.MustGet("input").(inputs.ControlableInput)
|
||||
if !ok {
|
||||
c.AbortWithStatusJSON(http.StatusMethodNotAllowed, gin.H{"errmsg": "The source doesn't support that"})
|
||||
return
|
||||
}
|
||||
|
||||
err := input.TogglePause()
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to pause the input: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, true)
|
||||
})
|
||||
}
|
||||
|
||||
func InputHandler(c *gin.Context) {
|
||||
src, ok := inputs.SoundInputs[c.Param("input")]
|
||||
if !ok {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("Input not found: %s", c.Param("input"))})
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("input", src)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ import (
|
|||
func DeclareRoutes(router *gin.Engine, cfg *config.Config) {
|
||||
apiRoutes := router.Group("/api")
|
||||
|
||||
declareInputsRoutes(cfg, apiRoutes)
|
||||
declareSourcesRoutes(cfg, apiRoutes)
|
||||
declareVolumeRoutes(cfg, apiRoutes)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue