not working jsonpath stuff
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2023-12-06 12:21:36 +01:00
parent 8cf4562056
commit 22b1203ea8

View File

@ -7,6 +7,7 @@ import (
"strings"
"regexp"
"fmt"
"reflect"
"encoding/json"
"github.com/oliveagle/jsonpath"
"udi/config"
@ -26,8 +27,7 @@ type SingleValueExtractorHandler struct {
}
const TOPIC_SEL = "topic"
const PAYLOAD_REGEX_SEL = "payload-regex"
const PAYLOAD_JSONPATH_SEL = "payload-jsonpath"
const PAYLOAD_SEL = "payload"
const PAYLOAD_FULL_SEL = "payload-full"
const CONSTANT_SEL = "constant"
@ -77,7 +77,7 @@ func NewSveHandler(config config.HandlerConfigT) handler.Handler {
t.payloadJsonpath = nil
}
if config.Attributes["deviceFrom"] != TOPIC_SEL && config.Attributes["deviceFrom"] != PAYLOAD_REGEX_SEL && config.Attributes["deviceFrom"] != CONSTANT_SEL {
if config.Attributes["deviceFrom"] != TOPIC_SEL && config.Attributes["deviceFrom"] != PAYLOAD_SEL && config.Attributes["deviceFrom"] != CONSTANT_SEL {
log.Printf("Error: invalid value %s for deviceFrom", config.Attributes["deviceFrom"])
return t
}
@ -93,13 +93,13 @@ func NewSveHandler(config config.HandlerConfigT) handler.Handler {
// empty device is valid
localConfig.device = config.Attributes["device"]
if config.Attributes["valueFrom"] != PAYLOAD_REGEX_SEL && config.Attributes["valueFrom"] != PAYLOAD_JSONPATH_SEL && config.Attributes["valueFrom"] != PAYLOAD_FULL_SEL {
if config.Attributes["valueFrom"] != PAYLOAD_SEL && config.Attributes["valueFrom"] != PAYLOAD_FULL_SEL {
log.Printf("Error: invalid value %s for valueFrom", config.Attributes["valueFrom"])
return t
}
localConfig.valueFrom = config.Attributes["valueFrom"]
if config.Attributes["valueFrom"] == PAYLOAD_REGEX_SEL {
if config.Attributes["valueFrom"] == PAYLOAD_SEL {
valuePart, err2 := strconv.Atoi(config.Attributes["valuePart"])
if err2 != nil {
log.Printf("Error: unable to convert valuePart to number: %s", err2)
@ -108,13 +108,13 @@ func NewSveHandler(config config.HandlerConfigT) handler.Handler {
localConfig.valuePart = valuePart
}
if config.Attributes["unitFrom"] != TOPIC_SEL && config.Attributes["unitFrom"] != PAYLOAD_REGEX_SEL && config.Attributes["unitFrom"] != CONSTANT_SEL {
if config.Attributes["unitFrom"] != PAYLOAD_SEL && config.Attributes["unitFrom"] != CONSTANT_SEL {
log.Printf("Error: invalid value %s for unitFrom", config.Attributes["unitFrom"])
return t
}
localConfig.unitFrom = config.Attributes["unitFrom"]
if config.Attributes["unitFrom"] != CONSTANT_SEL {
if config.Attributes["unitFrom"] == PAYLOAD_SEL {
unitPart, err3 := strconv.Atoi(config.Attributes["unitPart"])
if err3 != nil {
log.Printf("Error: unable to convert unitPart to number: %s", err3)
@ -160,6 +160,16 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) {
payloadMatches = self.payloadRegex.FindStringSubmatch(message.Payload)
//log.Printf("Matches: %s", strings.Join(payloadMatches, ", "))
}
if self.payloadJsonpath != nil {
var jsonData interface{}
json.Unmarshal([]byte(message.Payload), &jsonData)
p, err := self.payloadJsonpath.Lookup(jsonData)
if err != nil {
lost(fmt.Sprintf("jsonpath error: %s", err), message)
return
}
log.Printf("XXXX: %s", reflect.TypeOf(p))
}
switch self.config.deviceFrom {
case TOPIC_SEL:
@ -168,12 +178,12 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) {
return
}
measurement.Device = subTopics[self.config.devicePart]
case PAYLOAD_REGEX_SEL:
if self.payloadRegex == nil {
lost("no payloadRegex defined, devicePart can't be used", message)
case PAYLOAD_SEL:
if self.payloadRegex == nil && self.payloadJsonpath == nil {
lost("no payloadRegex or payloadJsonpath defined, devicePart can't be used", message)
return
}
if self.config.devicePart >= len(subTopics) {
if self.config.devicePart >= len(payloadMatches) {
lost("devicePart out of range", message)
return
}
@ -188,9 +198,9 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) {
variable.Variable = ""
switch self.config.valueFrom {
case PAYLOAD_REGEX_SEL:
if self.payloadRegex == nil {
lost("no payloadRegex defined, valuePart can't be used", message)
case PAYLOAD_SEL:
if self.payloadRegex == nil && self.payloadJsonpath == nil {
lost("no payloadRegex or payloadJsonpath defined, valuePart can't be used", message)
return
}
if self.config.valuePart >= len(payloadMatches) {
@ -198,36 +208,17 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) {
return
}
variable.Value = payloadMatches[self.config.valuePart]
case PAYLOAD_JSONPATH_SEL:
if self.payloadJsonpath == nil {
lost("no payloadJsonpath defined, valuePart can't be used", message)
return
}
var jsonData interface{}
json.Unmarshal([]byte(message.Payload), &jsonData)
result, err := self.payloadJsonpath.Lookup(jsonData)
if err != nil {
lost(fmt.Sprintf("jsonpath error: %s", err), message)
return
}
variable.Value = result
case PAYLOAD_FULL_SEL:
variable.Value = message.Payload
}
switch self.config.unitFrom {
case TOPIC_SEL:
if self.config.unitPart >= len(subTopics) {
lost("unitPart out of range", message)
case PAYLOAD_SEL:
if self.payloadRegex == nil && self.payloadJsonpath == nil {
lost("no payloadRegex or payloadJsonpath defined, unitPart can't be used", message)
return
}
variable.Unit = subTopics[self.config.unitPart]
case PAYLOAD_REGEX_SEL:
if self.payloadRegex == nil {
lost("no payloadRegex defined, unitPart can't be used", message)
return
}
if self.config.unitPart >= len(subTopics) {
if self.config.unitPart >= len(payloadMatches) {
lost("unitPart out of range", message)
return
}