parser/envitron/receive.go

63 lines
1.1 KiB
Go

package envitron
import (
"strconv"
"time"
"git.fuyu.moe/5GPowerQuality/parser/data"
"git.fuyu.moe/5GPowerQuality/parser/shared"
"github.com/labstack/echo"
)
func ReceiveData() {
e := echo.New()
e.Binder = jsonBinder{}
e.POST(`/`, handleReq)
e.Start(`:44444`)
}
type input struct {
Time shared.UnixTimestamp `json:"timestamp"`
UID string `json:"uuid"`
Modules []module `json:"modules"`
}
type module struct {
Port int `json:"port"`
Voltage float64 `json:"volt"`
Ampere float64 `json:"ampere"`
}
func handleReq(c echo.Context) error {
var i []input
err := c.Bind(&i)
if err != nil {
return c.JSON(400, err)
}
sets := data.Sets{}
for _, v := range i {
mID := data.GetMeterID(data.SourceEnvitron, v.UID)
key := data.Key{Meter: mID, Time: time.Time(v.Time)}
if _, ok := sets[key]; !ok {
sets[key] = data.Set{}
}
for _, m := range v.Modules {
p := strconv.Itoa(m.Port)
if p == `4` {
p = `N`
}
sets[key][`UGem`+p] = m.Voltage
sets[key][`IGem`+p] = m.Ampere
}
}
data.InsertSets(sets)
return c.JSONBlob(200, []byte(`{"success": true}`))
}