package pv import ( "reflect" "time" "encoding/json" "udi/config" "udi/handlers/handler" "udi/database" ) type PvHandler struct { handler.CommonHandler dbh *database.DatabaseHandle } type PvValue struct { ImportEnergyActive float32 `unit:"Wh" json:"importEnergyActive"` ImportEnergyReactive float32 `unit:"VAh" json:"importEnergyReactive"` ExportEnergyActive float32 `unit:"Wh" json:"exportEnergyActive"` ExportEnergyReactive float32 `unit:"VAh" json:"exportEnergyiReactive"` PowerApparent float32 `unit:"VA" json:"powerApparent"` PowerActive float32 `unit:"W" json:"powerActive"` PowerReactive float32 `unit:"VA" json:"powerReactive"` PowerDemandPositive float32 `unit:"W" json:"powerDemandPositive"` PowerDemandReverse float32 `unit:"W" json:"powerDemandReverse"` Factor float32 `unit:"" json:"factor"` Angle float32 `unit:"degree" json:"angle"` Voltage float32 `unit:"V" json:"voltage"` Current float32 `unit:"A" json:"current"` State int `unit:"" json:"state"` Status string `unit:"" json:"status"` Timestamp string `unit:"" json:"timestamp"` Cnt int `unit:"" json:"cnt"` } func New(id string, config config.HandlerConfigT) handler.Handler { t := &PvHandler { } t.Id = id t.dbh = database.NewDatabaseHandle() return t } func (self *PvHandler) Handle(message handler.MessageT) { //log.Printf("Handler PV %d processing %s -> %s", self.id, message.Topic, message.Payload) var pvValue PvValue err := json.Unmarshal([]byte(message.Payload), &pvValue) if err != nil { self.Lost("Unable to parse payload into pvValue struct", err, message) return } variables := make(map[string]database.VariableType) pvValueStructValue := reflect.ValueOf(pvValue) for i := 0; i < pvValueStructValue.NumField(); i++ { field := pvValueStructValue.Type().Field(i) fieldValue := pvValueStructValue.Field(i) v := database.VariableType { Label: "", Variable: field.Name, Unit: field.Tag.Get("unit"), Value: fieldValue.Interface(), } variables[field.Name] = v } measurement := database.Measurement { Time: time.Now(), Application: "PV", Device: "Powermeter", Values: variables, } self.dbh.StoreMeasurement(&measurement) self.S() }