graphicek/server/routes/sensor_config.go

47 lines
891 B
Go
Raw Permalink 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" binding:"required"`
2022-08-13 23:33:50 +02:00
}
2022-08-23 23:35:36 +02:00
func PutSensorConfig(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 := bindJSONBodyOrAbort(c, &configValue); err != nil {
return
}
2022-08-13 23:33:50 +02:00
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)
}
}