2022-07-13 17:01:09 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-07-13 17:01:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"log"
|
2022-07-13 17:01:09 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.netflux.io/rob/solar-toolkit/inverter"
|
|
|
|
)
|
|
|
|
|
2022-07-13 17:01:09 +00:00
|
|
|
const timestampMinimumYear = 2022
|
|
|
|
|
2022-07-13 17:01:09 +00:00
|
|
|
type Store interface {
|
|
|
|
InsertETRuntimeData(*inverter.ETRuntimeData) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
store Store
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:01:09 +00:00
|
|
|
func New(store Store) *Handler { return &Handler{store: store} }
|
2022-07-13 17:01:09 +00:00
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2022-07-13 17:01:09 +00:00
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Path != "/gateway/et_runtime_data" {
|
|
|
|
http.Error(w, "endpoint not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not read body: %v", err)
|
|
|
|
http.Error(w, "unexpected error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var runtimeData inverter.ETRuntimeData
|
|
|
|
err = json.Unmarshal(body, &runtimeData)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("could not unmarshal body: %v", err)
|
|
|
|
http.Error(w, "unexpected error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtimeData.Timestamp.Year() < timestampMinimumYear {
|
|
|
|
log.Printf("invalid timestamp: %v", runtimeData.Timestamp)
|
|
|
|
http.Error(w, "invalid data", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.store.InsertETRuntimeData(&runtimeData); err != nil {
|
|
|
|
log.Printf("error storing data: %v", err)
|
|
|
|
http.Error(w, "unexpected error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-07-13 17:01:09 +00:00
|
|
|
|
2022-07-13 17:01:09 +00:00
|
|
|
w.Header().Set("content-type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("OK\n"))
|
2022-07-13 17:01:09 +00:00
|
|
|
}
|