This commit is contained in:
@ -6,48 +6,85 @@ package emuProfIILoRaCfg1
|
||||
|
||||
|
||||
import (
|
||||
"log"
|
||||
"fmt"
|
||||
//"encoding/json"
|
||||
"encoding/json"
|
||||
"udi/database"
|
||||
)
|
||||
|
||||
/*
|
||||
const nameMapping = map[string]string {
|
||||
"Active Energy Export T1 64bit": "ActiveEnergyExport",
|
||||
"Reactive Energy Export T1 64bit": "ReactiveEnergyExport",
|
||||
"Active Energy Import T1 64bit": "ActiveEnergyImport",
|
||||
"Reactive Energy Import T1 64bit": "ReactiveEnergyImport",
|
||||
}
|
||||
*/
|
||||
|
||||
type emuVariable struct {
|
||||
Value interface{} `json:"value"`
|
||||
Unit string `json:"unit"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
func i2m(i interface{}) (map[string]interface{}, error) {
|
||||
o, ok := i.(map[string]interface{})
|
||||
if ! ok {
|
||||
return nil, fmt.Errorf("Unable to transform payload, outer stage")
|
||||
}
|
||||
return o, nil
|
||||
/*
|
||||
{
|
||||
"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 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"`
|
||||
Medium struct {
|
||||
Desc string `json:"desc"`
|
||||
Type string `json:"type"`
|
||||
} `json:"medium"`
|
||||
Timestamp int `json:"timestamp"`
|
||||
}
|
||||
|
||||
func Parse(fPort int, decodedPayload interface{}) (map[string]database.VariableType, error) {
|
||||
dp, err := i2m(decodedPayload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func Parse(fPort int, decodedPayload map[string]interface{}) (map[string]database.VariableType, error) {
|
||||
switch fPort {
|
||||
case 1:
|
||||
var emuMessage1 emuMessage1
|
||||
err := json.Unmarshal([]byte(decodedPayload), &emuMessage1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to parse payload, fPort %d", fPort)
|
||||
}
|
||||
variables := make(map[string]database.VariableType)
|
||||
variables["ActiveEnergyExport"] = database.VariableType {
|
||||
Variable: "ActiveEnergyExport",
|
||||
Unit: emuMessage1.ActiveEnergyExport.Unit,
|
||||
Value: emuMessage1.ActiveEnergyExport.Value,
|
||||
}
|
||||
variables["ActiveEnergyImport"] = database.VariableType {
|
||||
Variable: "ActiveEnergyImport",
|
||||
Unit: emuMessage1.ActiveEnergyImport.Unit,
|
||||
Value: emuMessage1.ActiveEnergyImport.Value,
|
||||
}
|
||||
variables["ReactiveEnergyExport"] = database.VariableType {
|
||||
Variable: "ReactiveEnergyExport",
|
||||
Unit: emuMessage1.ReactiveEnergyExport.Unit,
|
||||
Value: emuMessage1.ReactiveEnergyExport.Value,
|
||||
}
|
||||
variables["ReactiveEnergyImport"] = database.VariableType {
|
||||
Variable: "ReactiveEnergyImport",
|
||||
Unit: emuMessage1.ReactiveEnergyImport.Unit,
|
||||
Value: emuMessage1.ReactiveEnergyImport.Value,
|
||||
}
|
||||
return variables, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Unexpected fPort %d", fPort)
|
||||
}
|
||||
|
||||
variables := make(map[string]database.VariableType)
|
||||
for key, item := range dp {
|
||||
log.Printf("Key: %s, Item: %s", key, item)
|
||||
variables[key] = database.VariableType{ Variable: key, Unit: item.(map[string]interface{})["unit"], Value: item.(map[string]interface{})["value"] }
|
||||
}
|
||||
|
||||
return variables, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user