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