2024-01-14 01:04:01 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
|
|
|
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func EnvironmentSensor(
|
|
|
|
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
|
|
|
resp *types.EnvironmentSensor,
|
|
|
|
) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-01-15 22:31:29 +01:00
|
|
|
body := string(updateEndpointValidator(authDB, validCredentials, w, r))
|
2024-01-14 01:04:01 +01:00
|
|
|
|
|
|
|
// Parse request body
|
2024-01-15 22:31:29 +01:00
|
|
|
newState, err := strconv.ParseFloat(body, 64)
|
2024-01-14 01:04:01 +01:00
|
|
|
if err != nil || math.IsInf(newState, 0) {
|
2024-01-15 22:31:29 +01:00
|
|
|
log.Println("Failed to parse request body from", r.RemoteAddr, "body:", body)
|
2024-01-14 01:04:01 +01:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, _ = io.WriteString(w, "HTTP request body has to be a valid float64 value != +/-Inf")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set SpaceAPI response values
|
|
|
|
resp.Value = newState
|
|
|
|
resp.LastChange = time.Now().Unix()
|
|
|
|
|
|
|
|
_, _ = io.WriteString(w, "Update Successful")
|
|
|
|
}
|
|
|
|
}
|