package emuProfIILoRaCfg1 // provisioning device with // f_port=1: 01 00 0A 0B 0C 0D 0E 17 18 19 F0 4C // 01 00 08 0B 0C 0D 0E 17 18 19 F0 BE // f_port=2: 01 00 0A 24 26 28 2A 7A // 01 00 08 24 26 28 2A BE import ( "fmt" //"log" "encoding/json" "udi/database" ) /* { "Active Energy Export T1 64bit": { "unit": "Wh", "value": 0 }, "Active Energy Import T1 64bit": { "unit": "Wh", "value": 0 }, "Reactive Energy Export T1 64bit": { "unit": "varh", "value": 0 }, "Reactive Energy Import T1 64bit": { "unit": "varh", "value": 0 }, "medium": { "desc": "Electricity", "type": 1 }, "timeStamp": 1702052100 } */ type emuMessage2 struct { ActiveEnergyExport struct { Value int `json:"value"` Unit string `json:"unit"` } `json:"Active Energy Export T1 64bit"` ReactiveEnergyExport struct { Value int `json:"value"` Unit string `json:"unit"` } `json:"Reactive Energy Export T1 64bit"` ActiveEnergyImport struct { Value int `json:"value"` Unit string `json:"unit"` } `json:"Active Energy Import T1 64bit"` ReactiveEnergyImport struct { Value int `json:"value"` Unit string `json:"unit"` } `json:"Reactive Energy Import T1 64bit"` Medium struct { Desc string `json:"desc"` Type int `json:"type"` } `json:"medium"` Timestamp int `json:"timestamp"` } /* { "Active Power L1": { "cfgphase": 1, "unit": "W", "value": 0 }, "Active Power L123": { "unit": "W", "value": 0 }, "Active Power L2": { "cfgphase": 2, "unit": "W", "value": 0 }, "Active Power L3": { "cfgphase": 3, "unit": "W", "value": 0 }, "Powerfactor L1": { "cfgphase": 1, "unit": "Cos", "value": 0, "value_raw": 0 }, "Powerfactor L2": { "cfgphase": 2, "unit": "Cos", "value": 0, "value_raw": 0 }, "Powerfactor L3": { "cfgphase": 3, "unit": "Cos", "value": 0, "value_raw": 0 }, "errorcode": { "CTRatioChange": false, "ImpulseRatioChange": false, "ImpulseWidthChange": false, "LogbookFull": false, "PowerFail": false, "TimeChanged": false, "VTRatioChange": false, "value": 0 }, "medium": { "desc": "Electricity", "type": 1 }, "timeStamp": 1702392300 } */ type emuMessage1 struct { ActivePowerL1 struct { CfgPhase int `json:"cfgphase"` Unit string `json:"unit"` Value int `json:"value"` } `json:"Active Power L1"` ActivePowerL2 struct { CfgPhase int `json:"cfgphase"` Unit string `json:"unit"` Value int `json:"value"` } `json:"Active Power L2"` ActivePowerL3 struct { CfgPhase int `json:"cfgphase"` Unit string `json:"unit"` Value int `json:"value"` } `json:"Active Power L3"` ActivePowerL123 struct { Unit string `json:"unit"` Value int `json:"value"` } `json:"Active Power L123"` PowerfactorL1 struct { CfgPhase int `json:"cfgphase"` Unit string `json:"unit"` Value float32 `json:"value"` ValueRaw float32 `json:"value_raw"` } `json:"Powerfactor L1"` PowerfactorL2 struct { CfgPhase int `json:"cfgphase"` Unit string `json:"unit"` Value float32 `json:"value"` ValueRaw float32 `json:"value_raw"` } `json:"Powerfactor L2"` PowerfactorL3 struct { CfgPhase int `json:"cfgphase"` Unit string `json:"unit"` Value float32 `json:"value"` ValueRaw float32 `json:"value_raw"` } `json:"Powerfactor L3"` ErrorCode struct { CTRatioChange bool `json:"CTRatioChange"` ImpulseRatioChange bool `json:"ImpulseRatioChange"` ImpulseWidthChange bool `json:"ImpulseWidthChange"` LogbookFull bool `json:"LogbookFull"` PowerFail bool `json:"PowerFail"` TimeChanged bool `json:"TimeChanged"` VTRatioChange bool `json:"VTRatioChange"` Value int `json:"value"` } `json:"errorcode"` Medium struct { Desc string `json:"desc"` Type int `json:"type"` } `json:"medium"` Timestamp int `json:"timestamp"` } func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error { //log.Printf("Parse input: %d, %s", fPort, decodedPayload) switch fPort { case 1: var emuMessage1 emuMessage1 err := json.Unmarshal(decodedPayload, &emuMessage1) if err != nil { return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err) } (*variables)["ActivePowerL1"] = database.VariableType { Variable: "ActivePowerL1", Unit: emuMessage1.ActivePowerL1.Unit, Value: emuMessage1.ActivePowerL1.Value, } (*variables)["ActivePowerL2"] = database.VariableType { Variable: "ActivePowerL2", Unit: emuMessage1.ActivePowerL2.Unit, Value: emuMessage1.ActivePowerL2.Value, } (*variables)["ActivePowerL3"] = database.VariableType { Variable: "ActivePowerL3", Unit: emuMessage1.ActivePowerL3.Unit, Value: emuMessage1.ActivePowerL3.Value, } (*variables)["ActivePowerL123"] = database.VariableType { Variable: "ActivePowerL123", Unit: emuMessage1.ActivePowerL123.Unit, Value: emuMessage1.ActivePowerL123.Value, } (*variables)["PowerfactorL1"] = database.VariableType { Variable: "PowerfactorL1", Unit: emuMessage1.PowerfactorL1.Unit, Value: emuMessage1.PowerfactorL1.Value, } (*variables)["PowerfactorL2"] = database.VariableType { Variable: "PowerfactorL2", Unit: emuMessage1.PowerfactorL2.Unit, Value: emuMessage1.PowerfactorL2.Value, } (*variables)["PowerfactorL3"] = database.VariableType { Variable: "PowerfactorL3", Unit: emuMessage1.PowerfactorL3.Unit, Value: emuMessage1.PowerfactorL3.Value, } return nil case 2: var emuMessage2 emuMessage2 err := json.Unmarshal(decodedPayload, &emuMessage2) if err != nil { return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err) } (*variables)["ActiveEnergyExport"] = database.VariableType { Variable: "ActiveEnergyExport", Unit: emuMessage2.ActiveEnergyExport.Unit, Value: emuMessage2.ActiveEnergyExport.Value, } (*variables)["ActiveEnergyImport"] = database.VariableType { Variable: "ActiveEnergyImport", Unit: emuMessage2.ActiveEnergyImport.Unit, Value: emuMessage2.ActiveEnergyImport.Value, } (*variables)["ReactiveEnergyExport"] = database.VariableType { Variable: "ReactiveEnergyExport", Unit: emuMessage2.ReactiveEnergyExport.Unit, Value: emuMessage2.ReactiveEnergyExport.Value, } (*variables)["ReactiveEnergyImport"] = database.VariableType { Variable: "ReactiveEnergyImport", Unit: emuMessage2.ReactiveEnergyImport.Unit, Value: emuMessage2.ReactiveEnergyImport.Value, } return nil default: return fmt.Errorf("Unexpected fPort %d", fPort) } }