Compare commits

...

5 Commits

Author SHA1 Message Date
b6a7447cb3 fix
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2024-11-11 16:13:04 +01:00
58d86101f3 Merge branch 'main' of gitea.hottis.de:wn/universal-data-ingest 2024-11-11 16:11:47 +01:00
81e067e672 seems to work now 2024-11-11 16:11:32 +01:00
45e4cd793b static parse function 2024-11-11 15:00:04 +01:00
830596f211 saerbeck query 2024-11-11 12:45:47 +01:00
6 changed files with 121 additions and 24 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
src/udi/udi
src/udi/main
src/udi/migrate_schema
tmp/
ENVDB

View File

@ -20,15 +20,11 @@
},
{
"topics": [ "zigbee2mqtt/+" ],
"handler": "SVEJ",
"id": "SVEJ1",
"handler": "Z2M",
"id": "Z2M",
"config": {
"databaseConnStr": "",
"attributes": {
"application": "Temperature Multisensor",
"deviceSelector": "L:1",
"valueSelector": "J:$.temperature",
"unitSelector": "C:°C"
}
}
}

View File

@ -18,6 +18,7 @@ import "udi/handlers/svej"
import "udi/handlers/dt1t"
import "udi/handlers/locative"
import "udi/handlers/snmp"
import "udi/handlers/z2m"
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
@ -50,6 +51,8 @@ func InitDispatcher() {
factory = locative.New
case "SNMP":
factory = snmp.New
case "Z2M":
factory = z2m.New
default:
factory = nil
log.Printf("No handler %s found, ignore mapping", mapping.Handler)

View File

@ -99,24 +99,6 @@ func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []st
return "", fmt.Errorf("not enough subtopics")
}
res = subTopics[i]
case "L:":
// L: extract from topic and later match against devices table in database
i, e := strconv.Atoi(selector[2:])
if e != nil {
return "", fmt.Errorf("Atoi failed with %s", e)
}
if i >= len(subTopics) {
return "", fmt.Errorf("not enough subtopics")
}
ext := subTopics[i]
lookup, err1b := self.dbh.GetDeviceByLabel(ext)
if err1b != nil {
log.Printf("ext lookup %s failed: %v", ext, err1b)
res = ext
} else {
log.Printf("ext: %s", lookup)
res = ext
}
case "C:":
// use constant value
res = selector[2:]

View File

@ -0,0 +1,11 @@
package wsdcgq11lm
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"`
}

104
src/udi/handlers/z2m/z2m.go Normal file
View File

@ -0,0 +1,104 @@
package z2m
import (
"fmt"
"log"
"time"
"strings"
"reflect"
"encoding/json"
"udi/config"
"udi/handlers/handler"
"udi/database"
"udi/handlers/z2m/models/wsdcgq11lm"
)
type Z2MHandler struct {
handler.CommonHandler
dbh *database.DatabaseHandle
}
func parse(T any, payload string, variables *map[string]database.VariableType, attributes *map[string]interface{}) error {
observationType := reflect.TypeOf(T)
observation := reflect.New(observationType).Interface()
err := json.Unmarshal([]byte(payload), observation)
if err != nil {
return fmt.Errorf("Unable to parse payload into Observation struct: %v, %s", err, payload)
}
observationValue := reflect.ValueOf(observation).Elem()
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
}
func New(id string, config config.HandlerConfigT) handler.Handler {
t := &Z2MHandler {
}
t.Id = id
t.dbh = database.NewDatabaseHandle()
return t
}
func (self *Z2MHandler) Handle(message handler.MessageT) {
log.Printf("Handler Z2M %d processing %s -> %s", self.Id, message.Topic, message.Payload)
var measurement database.Measurement
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)
measurement.Application = device.Application.Label
measurement.Device = device.Attributes["Label"].(string)
var T any
switch device.DeviceType.ModelIdentifier {
case "WSDCGQ11LM":
T = wsdcgq11lm.Observation{}
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 = make(map[string]interface{})
err3 := parse(T,
message.Payload,
&(measurement.Values),
&(measurement.Attributes))
if err3 != nil {
self.Lost("Model parser failed", err3, message)
return
}
log.Printf("Prepared measurement item: %s", measurement)
self.dbh.StoreMeasurement(&measurement)
self.S()
}