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