graphicek/routes/sensor_config.go

48 lines
896 B
Go
Raw Normal View History

2022-08-13 23:39:18 +02:00
package routes
2022-08-13 23:33:50 +02:00
import (
2022-08-13 23:39:18 +02:00
"basic-sensor-receiver/app"
2022-08-13 23:33:50 +02:00
"net/http"
"github.com/gin-gonic/gin"
)
type sensorConfigValue struct {
Value string `json:"value"`
}
2022-08-13 23:39:18 +02:00
func HandlePutSensorConfig(s *app.Server) gin.HandlerFunc {
2022-08-13 23:33:50 +02:00
return func(c *gin.Context) {
var configValue sensorConfigValue
sensor := c.Param("sensor")
key := c.Param("key")
if err := c.BindJSON(&configValue); err != nil {
c.AbortWithError(400, err)
return
}
if err := s.Services.SensorConfig.SetValue(sensor, key, configValue.Value); err != nil {
c.AbortWithError(500, err)
return
}
c.Writer.WriteHeader(http.StatusOK)
}
}
2022-08-13 23:39:18 +02:00
func GetSensorConfig(s *app.Server) gin.HandlerFunc {
2022-08-13 23:33:50 +02:00
return func(c *gin.Context) {
sensor := c.Param("sensor")
config, err := s.Services.SensorConfig.GetValues(sensor)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(http.StatusOK, config)
}
}