From 22b1203ea8d6b881e73bcca6c585fd36a4a2980f Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 6 Dec 2023 12:21:36 +0100 Subject: [PATCH] not working jsonpath stuff --- src/udi/handlers/sve/sve.go | 65 ++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/src/udi/handlers/sve/sve.go b/src/udi/handlers/sve/sve.go index 972e9be..b329485 100644 --- a/src/udi/handlers/sve/sve.go +++ b/src/udi/handlers/sve/sve.go @@ -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 }