static parse function
This commit is contained in:
48
src/udi/handlers/z2m/models/wsdcgq11lm/wsdcgq11lm.go
Normal file
48
src/udi/handlers/z2m/models/wsdcgq11lm/wsdcgq11lm.go
Normal file
@ -0,0 +1,48 @@
|
||||
package wsdcgq11lm
|
||||
|
||||
import (
|
||||
//"log"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"encoding/json"
|
||||
"udi/database"
|
||||
)
|
||||
|
||||
type Observation struct {
|
||||
LinkQuality uint8 `unit:"" json:"linkquality"`
|
||||
Battery uint8 `unit:"%" json:"battery"`
|
||||
Humidity float32 `unit:"%H" json:"humidity"`
|
||||
Pressure float32 `unit:"mbar" json:"pressure"`
|
||||
Temperature float32 `unit:"°C" json:"temperature"`
|
||||
Voltage uint16 `unit:"mV" json:"voltage"`
|
||||
}
|
||||
|
||||
|
||||
func Parse(payload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, _ *database.Device) error {
|
||||
var observation Observation
|
||||
err := json.Unmarshal([]byte(payload), &observation)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to parse payload into Observation struct: %v, %s", err, payload)
|
||||
}
|
||||
|
||||
observationType := reflect.TypeOf(observation)
|
||||
observationValue := reflect.ValueOf(observation)
|
||||
|
||||
for i := 0; i < observationType.NumField(); i++ {
|
||||
field := observationType.Field(i)
|
||||
name := field.Name
|
||||
unit := field.Tag.Get("unit")
|
||||
value := observationValue.Field(i).Interface()
|
||||
|
||||
(*variables)[name] = database.VariableType {
|
||||
Label: name,
|
||||
Variable: "y",
|
||||
Unit: unit,
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
(*attributes)["Status"] = "ok"
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user