This commit is contained in:
2026-02-09 15:36:35 +01:00
parent 5b46ecb0b1
commit 47116904fc
2 changed files with 215 additions and 86 deletions

View File

@@ -1,113 +1,107 @@
package z2m package z2m
import ( import (
"fmt" "encoding/json"
"log" "fmt"
"time" "log"
"strings" "reflect"
"reflect" "strings"
"encoding/json" "time"
"udi/config" "udi/config"
"udi/handlers/handler" "udi/database"
"udi/database" "udi/handlers/handler"
"udi/handlers/z2m/models/wsdcgq11lm" "udi/handlers/z2m/models/gs361ah04"
"udi/handlers/z2m/models/wsdcgq01lm" "udi/handlers/z2m/models/wsdcgq01lm"
"udi/handlers/z2m/models/gs361ah04" "udi/handlers/z2m/models/wsdcgq11lm"
) )
type Z2MHandler struct { type Z2MHandler struct {
handler.CommonHandler handler.CommonHandler
dbh *database.DatabaseHandle dbh *database.DatabaseHandle
} }
func parse(T any, payload string, variables *map[string]database.VariableType) error { func parse(T any, payload string, variables *map[string]database.VariableType) error {
observationType := reflect.TypeOf(T) observationType := reflect.TypeOf(T)
observation := reflect.New(observationType).Interface() observation := reflect.New(observationType).Interface()
err := json.Unmarshal([]byte(payload), observation) err := json.Unmarshal([]byte(payload), observation)
if err != nil { if err != nil {
return fmt.Errorf("Unable to parse payload into Observation struct: %v, %s", err, payload) return fmt.Errorf("Unable to parse payload into Observation struct: %v, %s", err, payload)
} }
observationValue := reflect.ValueOf(observation).Elem() observationValue := reflect.ValueOf(observation).Elem()
for i := 0; i < observationType.NumField(); i++ { for i := 0; i < observationType.NumField(); i++ {
field := observationType.Field(i) field := observationType.Field(i)
name := field.Name name := field.Name
unit := field.Tag.Get("unit") unit := field.Tag.Get("unit")
value := observationValue.Field(i).Interface() value := observationValue.Field(i).Interface()
(*variables)[name] = database.VariableType { (*variables)[name] = database.VariableType{
Label: name, Label: name,
Variable: "y", Variable: "y",
Unit: unit, Unit: unit,
Value: value, Value: value,
} }
} }
return nil
return nil
} }
func New(id string, config config.HandlerConfigT) handler.Handler { func New(id string, config config.HandlerConfigT) handler.Handler {
t := &Z2MHandler { t := &Z2MHandler{}
} t.Id = id
t.Id = id t.dbh = database.NewDatabaseHandle()
t.dbh = database.NewDatabaseHandle() log.Printf("Handler Z2M %d initialized", id)
log.Printf("Handler Z2M %d initialized", id) return t
return t
} }
func (self *Z2MHandler) Handle(message handler.MessageT) { func (self *Z2MHandler) Handle(message handler.MessageT) {
log.Printf("Handler Z2M %d processing %s -> %s", self.Id, message.Topic, message.Payload) log.Printf("Handler Z2M %d processing %s -> %s", self.Id, message.Topic, message.Payload)
var measurement database.Measurement var measurement database.Measurement
measurement.Time = time.Now() measurement.Time = time.Now()
subTopics := strings.Split(message.Topic, "/")
deviceId := subTopics[1]
log.Printf("DeviceId: %s", deviceId)
device, err1 := self.dbh.GetDeviceByLabel(deviceId)
if err1 != nil {
self.Lost("Error when loading device", err1, message)
return
}
log.Printf("Device: %s", device)
subTopics := strings.Split(message.Topic, "/") measurement.Application = device.Application.Label
deviceId := subTopics[1] measurement.Device = device.Attributes["Label"].(string)
log.Printf("DeviceId: %s", deviceId)
device, err1 := self.dbh.GetDeviceByLabel(deviceId)
if err1 != nil {
self.Lost("Error when loading device", err1, message)
return
}
log.Printf("Device: %s", device)
measurement.Application = device.Application.Label var T any
measurement.Device = device.Attributes["Label"].(string) switch device.DeviceType.ModelIdentifier {
case "WSDCGQ11LM":
T = wsdcgq11lm.Observation{}
case "WSDCGQ01LM":
T = wsdcgq01lm.Observation{}
case "GS361A-H04":
T = gs361ah04.Observation{}
default:
self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message)
return
}
var T any measurement.Values = make(map[string]database.VariableType)
switch device.DeviceType.ModelIdentifier { measurement.Attributes = make(map[string]interface{})
case "WSDCGQ11LM": err3 := parse(T,
T = wsdcgq11lm.Observation{} message.Payload,
case "WSDCGQ01LM": &(measurement.Values))
T = wsdcgq01lm.Observation{} if err3 != nil {
case "GS361A-H04": self.Lost("Model parser failed", err3, message)
T = gs361ah04.Observation{} return
default: }
self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message)
return
}
measurement.Values = make(map[string]database.VariableType) measurement.Attributes["Status"] = "ok"
measurement.Attributes = make(map[string]interface{}) measurement.Attributes["DeviceId"] = deviceId
err3 := parse(T, measurement.Attributes["DeviceModel"] = device.DeviceType.ModelIdentifier
message.Payload, log.Printf("Prepared measurement item: %s", measurement)
&(measurement.Values)) self.dbh.StoreMeasurement(&measurement)
if err3 != nil { self.S()
self.Lost("Model parser failed", err3, message)
return
}
measurement.Attributes["Status"] = "ok"
measurement.Attributes["DeviceId"] = deviceId
measurement.Attributes["DeviceModel"] = device.DeviceType.ModelIdentifier
log.Printf("Prepared measurement item: %s", measurement)
self.dbh.StoreMeasurement(&measurement)
self.S()
} }

135
src/udi/config-test-2.json Normal file
View File

@@ -0,0 +1,135 @@
{
"mqtt": {
"broker": "mqtt://172.23.1.102:1883",
"tlsEnable": "false"
},
"topicMappings": [
{
"topics": [ "snmp" ],
"handler": "PREP",
"id": "SNMP",
"config": {
"attributes": {
}
}
},
{
"topics": [ "tsm" ],
"handler": "PREP",
"id": "TSM",
"config": {
"attributes": {
}
}
},
{
"topics": [ "dt1/ai/periodic/1" ],
"handler": "DT1T",
"id": "DT1T.0",
"config": {
"attributes": {
"Application": "Temperature Wago",
"Device": "Freezer",
"HardLow": "-273",
"SoftLow": "-50",
"SoftHigh": "20",
"HardHigh": "100"
}
}
},
{
"topics": [ "dt1/ai/periodic/3" ],
"handler": "DT1T",
"id": "DT1T.1",
"config": {
"attributes": {
"Application": "Temperature Wago",
"Device": "Outdoor",
"HardLow": "-273",
"SoftLow": "-60",
"SoftHigh": "60",
"HardHigh": "100"
}
}
},
{
"topics": [ "IoT/PV/Values" ],
"handler": "PV",
"id": "PV",
"config": {
"databaseConnStr": "",
"attributes": {
}
}
},
{
"topics": [ "IoT/Car/Values" ],
"handler": "Car",
"id": "Car",
"config": {
"databaseConnStr": "",
"attributes": {
}
}
},
{
"topics": [ "IoT/MBGW3/Measurement" ],
"handler": "MBGW3",
"id": "MBGW3",
"config": {
"databaseConnStr": "",
"attributes": {
}
}
},
{
"topics": [ "IoT/OneWireGW/Bus 1/#" ],
"handler": "SVER",
"id": "SVER0",
"config": {
"databaseConnStr": "",
"attributes": {
"application": "Temperature Heating",
"payloadRegex": "(\\d+(\\.\\d+)?)\\s*([^0-9\\s]\\S*)",
"deviceFrom": "topic",
"devicePart": "3",
"valueFrom": "payload",
"valuePart": "1",
"unitFrom": "payload",
"unitPart": "3"
}
}
},
{
"topics": [ "shellyplusht/+/status/temperature:0" ],
"handler": "SVEJ",
"id": "SVEJ2",
"config": {
"databaseConnStr": "",
"attributes": {
"application": "Temperature Shelly Plus HT",
"deviceSelector": "T:1",
"valueSelector": "J:$.tC",
"unitSelector": "C:°C"
}
}
},
{
"topics": [ "shellyplusht/+/status/humidity:0" ],
"handler": "SVEJ",
"id": "SVE4",
"config": {
"databaseConnStr": "",
"attributes": {
"application": "Humidity Shelly Plus HT",
"deviceSelector": "T:1",
"valueSelector": "J:$.rh",
"unitSelector": "C:%"
}
}
}
],
"archiver": {
"dir": "./tmp/udi"
}
}