95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package mbgw3
|
|
|
|
import (
|
|
"time"
|
|
"strconv"
|
|
"encoding/json"
|
|
"udi/config"
|
|
"udi/handlers/handler"
|
|
"udi/database"
|
|
)
|
|
|
|
|
|
type Mbgw3Handler struct {
|
|
handler.CommonHandler
|
|
dbh *database.DatabaseHandle
|
|
}
|
|
|
|
type Observation struct {
|
|
Status string `json:"Status"`
|
|
RequestId string `json:"RequestId"`
|
|
Device string `json:"Device"`
|
|
Errors string `json:"Errors"`
|
|
ErrorRatio string `json:"ErrorRatio"`
|
|
Requests string `json:"Requests"`
|
|
Values map[string]string
|
|
}
|
|
|
|
|
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
|
t := &Mbgw3Handler {
|
|
}
|
|
t.Id = id
|
|
t.dbh = database.NewDatabaseHandle()
|
|
return t
|
|
}
|
|
|
|
func (self *Mbgw3Handler) Handle(message handler.MessageT) {
|
|
//log.Printf("Handler MBGW3 %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
|
|
|
var observation Observation
|
|
err := json.Unmarshal([]byte(message.Payload), &observation)
|
|
if err != nil {
|
|
self.Lost("Unable to parse payload into Observation struct", err, message)
|
|
return
|
|
}
|
|
|
|
var measurement database.Measurement
|
|
measurement.Time = time.Now()
|
|
|
|
if observation.Device == "Gas" {
|
|
measurement.Application = "Gas"
|
|
measurement.Device = "Gasmeter"
|
|
} else {
|
|
measurement.Application = "Power"
|
|
measurement.Device = observation.Device
|
|
}
|
|
|
|
measurement.Attributes = make(map[string]interface{})
|
|
if v, err := strconv.Atoi(observation.RequestId); err == nil {
|
|
measurement.Attributes["RequestId"] = v
|
|
}
|
|
if v, err := strconv.ParseFloat(observation.ErrorRatio, 32); err == nil {
|
|
measurement.Attributes["ErrorRatio"] = v
|
|
}
|
|
if v, err := strconv.Atoi(observation.Errors); err == nil {
|
|
measurement.Attributes["Errors"] = v
|
|
}
|
|
if v, err := strconv.Atoi(observation.Requests); err == nil {
|
|
measurement.Attributes["Requests"] = v
|
|
}
|
|
measurement.Attributes["Status"] = observation.Status
|
|
|
|
measurement.Values = make(map[string]database.VariableType)
|
|
unitMap := map[string]string { "Energy": "Wh", "Power": "W", "Voltage": "V", "Current": "A", "Volume": "m3" }
|
|
for k, v := range observation.Values {
|
|
unit, exists := unitMap[k]
|
|
if ! exists {
|
|
unit = "Unmapped Unit"
|
|
}
|
|
measurement.Values[k] = database.VariableType {
|
|
Label: "",
|
|
Variable: k,
|
|
Unit: unit,
|
|
Value: v,
|
|
}
|
|
}
|
|
|
|
//log.Printf("Prepared measurement item: %s", measurement)
|
|
|
|
self.dbh.StoreMeasurement(&measurement)
|
|
self.S()
|
|
}
|
|
|
|
|