From 8cf4562056ce773ee3d61c2f6a3a1fd2aa2134d7 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 5 Dec 2023 18:10:09 +0100 Subject: [PATCH] jsonpath --- src/udi/go.mod | 1 + src/udi/go.sum | 2 ++ src/udi/handlers/sve/sve.go | 53 ++++++++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/udi/go.mod b/src/udi/go.mod index c6b2572..fe51759 100644 --- a/src/udi/go.mod +++ b/src/udi/go.mod @@ -5,6 +5,7 @@ go 1.21.3 require ( github.com/eclipse/paho.mqtt.golang v1.4.3 github.com/google/uuid v1.4.0 + github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 gorm.io/driver/postgres v1.5.4 gorm.io/gorm v1.25.5 ) diff --git a/src/udi/go.sum b/src/udi/go.sum index 0a0dc0d..67dea6a 100644 --- a/src/udi/go.sum +++ b/src/udi/go.sum @@ -17,6 +17,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 h1:Yl0tPBa8QPjGmesFh1D0rDy+q1Twx6FyU7VWHi8wZbI= +github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852/go.mod h1:eqOVx5Vwu4gd2mmMZvVZsgIqNSaW3xxRThUJ0k/TPk4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/src/udi/handlers/sve/sve.go b/src/udi/handlers/sve/sve.go index fe64147..972e9be 100644 --- a/src/udi/handlers/sve/sve.go +++ b/src/udi/handlers/sve/sve.go @@ -7,6 +7,8 @@ import ( "strings" "regexp" "fmt" + "encoding/json" + "github.com/oliveagle/jsonpath" "udi/config" "udi/handlers/handler" "udi/database" @@ -19,11 +21,13 @@ type SingleValueExtractorHandler struct { ready bool config localConfig payloadRegex *regexp.Regexp + payloadJsonpath *jsonpath.Compiled dbh *database.DatabaseHandle } const TOPIC_SEL = "topic" -const PAYLOAD_SEL = "payload" +const PAYLOAD_REGEX_SEL = "payload-regex" +const PAYLOAD_JSONPATH_SEL = "payload-jsonpath" const PAYLOAD_FULL_SEL = "payload-full" const CONSTANT_SEL = "constant" @@ -60,8 +64,20 @@ func NewSveHandler(config config.HandlerConfigT) handler.Handler { } else { t.payloadRegex = nil } + payloadJsonpath := config.Attributes["payloadJsonpath"] + if payloadJsonpath != "" { + j, err := jsonpath.Compile(payloadJsonpath) + if err != nil { + log.Printf("Unable to compile jsonpath %s", payloadJsonpath) + t.payloadJsonpath = nil + } else { + t.payloadJsonpath = j + } + } else { + t.payloadJsonpath = nil + } - if config.Attributes["deviceFrom"] != TOPIC_SEL && config.Attributes["deviceFrom"] != PAYLOAD_SEL && config.Attributes["deviceFrom"] != CONSTANT_SEL { + if config.Attributes["deviceFrom"] != TOPIC_SEL && config.Attributes["deviceFrom"] != PAYLOAD_REGEX_SEL && config.Attributes["deviceFrom"] != CONSTANT_SEL { log.Printf("Error: invalid value %s for deviceFrom", config.Attributes["deviceFrom"]) return t } @@ -77,13 +93,13 @@ func NewSveHandler(config config.HandlerConfigT) handler.Handler { // empty device is valid localConfig.device = config.Attributes["device"] - if config.Attributes["valueFrom"] != TOPIC_SEL && config.Attributes["valueFrom"] != PAYLOAD_SEL && config.Attributes["valueFrom"] != PAYLOAD_FULL_SEL { + if config.Attributes["valueFrom"] != PAYLOAD_REGEX_SEL && config.Attributes["valueFrom"] != PAYLOAD_JSONPATH_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_FULL_SEL { + if config.Attributes["valueFrom"] == PAYLOAD_REGEX_SEL { valuePart, err2 := strconv.Atoi(config.Attributes["valuePart"]) if err2 != nil { log.Printf("Error: unable to convert valuePart to number: %s", err2) @@ -92,7 +108,7 @@ func NewSveHandler(config config.HandlerConfigT) handler.Handler { localConfig.valuePart = valuePart } - if config.Attributes["unitFrom"] != TOPIC_SEL && config.Attributes["unitFrom"] != PAYLOAD_SEL && config.Attributes["unitFrom"] != CONSTANT_SEL { + if config.Attributes["unitFrom"] != TOPIC_SEL && config.Attributes["unitFrom"] != PAYLOAD_REGEX_SEL && config.Attributes["unitFrom"] != CONSTANT_SEL { log.Printf("Error: invalid value %s for unitFrom", config.Attributes["unitFrom"]) return t } @@ -152,7 +168,7 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) { return } measurement.Device = subTopics[self.config.devicePart] - case PAYLOAD_SEL: + case PAYLOAD_REGEX_SEL: if self.payloadRegex == nil { lost("no payloadRegex defined, devicePart can't be used", message) return @@ -172,22 +188,29 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) { variable.Variable = "" switch self.config.valueFrom { - case TOPIC_SEL: - if self.config.valuePart >= len(subTopics) { - lost("valuePart out of range", message) - return - } - variable.Value = subTopics[self.config.valuePart] - case PAYLOAD_SEL: + case PAYLOAD_REGEX_SEL: if self.payloadRegex == nil { lost("no payloadRegex defined, valuePart can't be used", message) return } - if self.config.valuePart >= len(subTopics) { + if self.config.valuePart >= len(payloadMatches) { lost("valuePart out of range", message) 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 } @@ -199,7 +222,7 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) { return } variable.Unit = subTopics[self.config.unitPart] - case PAYLOAD_SEL: + case PAYLOAD_REGEX_SEL: if self.payloadRegex == nil { lost("no payloadRegex defined, unitPart can't be used", message) return