2018-06-19 15:20:02 +02:00
|
|
|
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)
|
2018-09-28 13:54:28 +02:00
|
|
|
if p == `4` {
|
|
|
|
p = `N`
|
|
|
|
}
|
2018-06-19 15:20:02 +02:00
|
|
|
|
|
|
|
sets[key][`UGem`+p] = m.Voltage
|
|
|
|
sets[key][`IGem`+p] = m.Ampere
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.InsertSets(sets)
|
|
|
|
|
|
|
|
return c.JSONBlob(200, []byte(`{"success": true}`))
|
|
|
|
}
|