This commit is contained in:
@ -7,14 +7,11 @@ package emuProfIILoRaCfg1
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
//"log"
|
||||
"encoding/json"
|
||||
"udi/database"
|
||||
)
|
||||
|
||||
type emuVariable struct {
|
||||
Value interface{} `json:"value"`
|
||||
Unit string `json:"unit"`
|
||||
}
|
||||
|
||||
/*
|
||||
{
|
||||
@ -41,46 +38,216 @@ type emuVariable struct {
|
||||
"timeStamp": 1702052100
|
||||
}
|
||||
*/
|
||||
type emuMessage1 struct {
|
||||
ActiveEnergyExport emuVariable `json:"Active Energy Export T1 64bit"`
|
||||
ReactiveEnergyExport emuVariable `json:"Reactive Energy Export T1 64bit"`
|
||||
ActiveEnergyImport emuVariable `json:"Active Energy Import T1 64bit"`
|
||||
ReactiveEnergyImport emuVariable `json:"Reactive Energy Import T1 64bit"`
|
||||
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 string `json:"type"`
|
||||
Type int `json:"type"`
|
||||
} `json:"medium"`
|
||||
Timestamp int `json:"timestamp"`
|
||||
}
|
||||
|
||||
func Parse(fPort int, decodedPayload map[string]interface{}) (map[string]database.VariableType, error) {
|
||||
/*
|
||||
{
|
||||
"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) (map[string]database.VariableType, error) {
|
||||
//log.Printf("Parse input: %d, %s", fPort, decodedPayload)
|
||||
switch fPort {
|
||||
case 1:
|
||||
var emuMessage1 emuMessage1
|
||||
err := json.Unmarshal([]byte(decodedPayload), &emuMessage1)
|
||||
err := json.Unmarshal(decodedPayload, &emuMessage1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to parse payload, fPort %d", fPort)
|
||||
return nil, fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
||||
}
|
||||
variables := make(map[string]database.VariableType)
|
||||
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 variables, nil
|
||||
case 2:
|
||||
var emuMessage2 emuMessage2
|
||||
err := json.Unmarshal(decodedPayload, &emuMessage2)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
||||
}
|
||||
variables := make(map[string]database.VariableType)
|
||||
variables["ActiveEnergyExport"] = database.VariableType {
|
||||
Variable: "ActiveEnergyExport",
|
||||
Unit: emuMessage1.ActiveEnergyExport.Unit,
|
||||
Value: emuMessage1.ActiveEnergyExport.Value,
|
||||
Unit: emuMessage2.ActiveEnergyExport.Unit,
|
||||
Value: emuMessage2.ActiveEnergyExport.Value,
|
||||
}
|
||||
variables["ActiveEnergyImport"] = database.VariableType {
|
||||
Variable: "ActiveEnergyImport",
|
||||
Unit: emuMessage1.ActiveEnergyImport.Unit,
|
||||
Value: emuMessage1.ActiveEnergyImport.Value,
|
||||
Unit: emuMessage2.ActiveEnergyImport.Unit,
|
||||
Value: emuMessage2.ActiveEnergyImport.Value,
|
||||
}
|
||||
variables["ReactiveEnergyExport"] = database.VariableType {
|
||||
Variable: "ReactiveEnergyExport",
|
||||
Unit: emuMessage1.ReactiveEnergyExport.Unit,
|
||||
Value: emuMessage1.ReactiveEnergyExport.Value,
|
||||
Unit: emuMessage2.ReactiveEnergyExport.Unit,
|
||||
Value: emuMessage2.ReactiveEnergyExport.Value,
|
||||
}
|
||||
variables["ReactiveEnergyImport"] = database.VariableType {
|
||||
Variable: "ReactiveEnergyImport",
|
||||
Unit: emuMessage1.ReactiveEnergyImport.Unit,
|
||||
Value: emuMessage1.ReactiveEnergyImport.Value,
|
||||
Unit: emuMessage2.ReactiveEnergyImport.Unit,
|
||||
Value: emuMessage2.ReactiveEnergyImport.Value,
|
||||
}
|
||||
return variables, nil
|
||||
default:
|
||||
|
Reference in New Issue
Block a user