z2m working again
This commit is contained in:
@@ -100,6 +100,16 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "zigbee2mqtt/+" ],
|
||||
"handler": "Z2M",
|
||||
"id": "Z2M",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "shellyplusht/+/status/temperature:0" ],
|
||||
"handler": "SVEJ",
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
"udi/handlers/sver"
|
||||
|
||||
// "udi/handlers/ttn"
|
||||
// "udi/handlers/z2m"
|
||||
"udi/handlers/z2m"
|
||||
"udi/mqtt"
|
||||
)
|
||||
|
||||
@@ -54,8 +54,8 @@ func InitDispatcher() {
|
||||
factory = locative.New
|
||||
case "PREP":
|
||||
factory = prepared.New
|
||||
// case "Z2M":
|
||||
// factory = z2m.New
|
||||
case "Z2M":
|
||||
factory = z2m.New
|
||||
case "Car":
|
||||
factory = car.New
|
||||
default:
|
||||
|
||||
80
src/udi/handlers/z2m/z2m.go
Normal file
80
src/udi/handlers/z2m/z2m.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package z2m
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
"udi/config"
|
||||
"udi/database"
|
||||
"udi/handlers/handler"
|
||||
)
|
||||
|
||||
type Z2MHandler struct {
|
||||
handler.CommonHandler
|
||||
dbh *database.DatabaseHandle
|
||||
}
|
||||
|
||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t := &Z2MHandler{}
|
||||
t.Id = id
|
||||
t.dbh = database.NewDatabaseHandle()
|
||||
log.Printf("Handler Z2M %d initialized", id)
|
||||
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)
|
||||
|
||||
measurement.Device = deviceId
|
||||
|
||||
// Parse JSON direkt in eine map
|
||||
var jsonData map[string]interface{}
|
||||
err := json.Unmarshal([]byte(message.Payload), &jsonData)
|
||||
if err != nil {
|
||||
self.Lost("Failed to parse JSON payload", err, message)
|
||||
return
|
||||
}
|
||||
|
||||
measurement.Attributes = make(map[string]interface{})
|
||||
measurement.Values = make(map[string]database.VariableType)
|
||||
|
||||
// Extract device info for application naming
|
||||
if deviceData, ok := jsonData["device"]; ok {
|
||||
if deviceMap, ok := deviceData.(map[string]any); ok {
|
||||
manufacturerId, hasManufacturer := deviceMap["manufacturerID"]
|
||||
model, hasModel := deviceMap["model"]
|
||||
|
||||
if !hasManufacturer || !hasModel {
|
||||
self.Lost("Missing manufacturerID or model in device data", fmt.Errorf("manufacturerID: %v, model: %v", hasManufacturer, hasModel), handler.MessageT{})
|
||||
return
|
||||
}
|
||||
|
||||
measurement.Application = "z2m_" + fmt.Sprintf("%v", manufacturerId) + "_" + model.(string)
|
||||
}
|
||||
delete(jsonData, "device")
|
||||
}
|
||||
|
||||
// Konvertiere die restlichen Elemente in VariableType-Map
|
||||
for key, value := range jsonData {
|
||||
measurement.Values[key] = database.VariableType{
|
||||
Label: key,
|
||||
Variable: "",
|
||||
Unit: "",
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
measurement.Attributes["Status"] = "ok"
|
||||
log.Printf("Prepared measurement item: %s", measurement)
|
||||
self.dbh.StoreMeasurement(&measurement)
|
||||
self.S()
|
||||
}
|
||||
Reference in New Issue
Block a user