Added endpoint for fetching the latest values
This commit is contained in:
parent
508d1630fe
commit
39b7d5e73a
|
|
@ -39,6 +39,7 @@ func main() {
|
||||||
// Routes that are only accessible after logging in
|
// Routes that are only accessible after logging in
|
||||||
loginProtected := router.Group("/", middleware.LoginAuthMiddleware(server))
|
loginProtected := router.Group("/", middleware.LoginAuthMiddleware(server))
|
||||||
loginProtected.GET("/api/sensors", routes.GetSensors(server))
|
loginProtected.GET("/api/sensors", routes.GetSensors(server))
|
||||||
|
loginProtected.GET("/api/sensors/:sensor/values/latest", routes.GetSensorLatestValue(server))
|
||||||
loginProtected.GET("/api/sensors/:sensor/values", routes.GetSensorValues(server))
|
loginProtected.GET("/api/sensors/:sensor/values", routes.GetSensorValues(server))
|
||||||
loginProtected.GET("/api/sensors/:sensor/config", routes.GetSensorConfig(server))
|
loginProtected.GET("/api/sensors/:sensor/config", routes.GetSensorConfig(server))
|
||||||
loginProtected.PUT("/api/sensors/:sensor/config/:key", routes.PutSensorConfig(server))
|
loginProtected.PUT("/api/sensors/:sensor/config/:key", routes.PutSensorConfig(server))
|
||||||
|
|
|
||||||
|
|
@ -55,3 +55,25 @@ func GetSensorValues(s *app.Server) gin.HandlerFunc {
|
||||||
c.JSON(http.StatusOK, values)
|
c.JSON(http.StatusOK, values)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSensorLatestValue(s *app.Server) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
var query getSensorQuery
|
||||||
|
|
||||||
|
sensor := c.Param("sensor")
|
||||||
|
|
||||||
|
if err := c.BindQuery(&query); err != nil {
|
||||||
|
c.AbortWithError(500, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
value, err := s.Services.SensorValues.GetLatest(sensor, query.From, query.To)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(500, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,3 +50,16 @@ func (s *SensorValuesService) GetList(sensor string, from int64, to int64) ([]se
|
||||||
|
|
||||||
return values, nil
|
return values, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SensorValuesService) GetLatest(sensor string, from int64, to int64) (*sensorValue, error) {
|
||||||
|
var value = sensorValue{}
|
||||||
|
|
||||||
|
row := s.ctx.DB.QueryRow("SELECT timestamp, value FROM sensor_values WHERE sensor = ? AND timestamp > ? AND timestamp < ? ORDER BY timestamp DESC LIMIT 1", sensor, from, to)
|
||||||
|
|
||||||
|
err := row.Scan(&value.Timestamp, &value.Value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &value, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue