Compare commits

...

3 Commits

Author SHA1 Message Date
65909becd6 Merge branch 'regex-jsonpath-separation'
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2023-12-06 14:33:18 +01:00
22b1203ea8 not working jsonpath stuff
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-12-06 12:21:36 +01:00
8cf4562056 jsonpath
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2023-12-05 18:10:09 +01:00

View File

@ -7,6 +7,9 @@ import (
"strings"
"regexp"
"fmt"
"reflect"
"encoding/json"
"github.com/oliveagle/jsonpath"
"udi/config"
"udi/handlers/handler"
"udi/database"
@ -19,6 +22,7 @@ type SingleValueExtractorRegexHandler struct {
ready bool
config localConfig
payloadRegex *regexp.Regexp
payloadJsonpath *jsonpath.Compiled
dbh *database.DatabaseHandle
}
@ -60,6 +64,18 @@ func NewSverHandler(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 {
log.Printf("Error: invalid value %s for deviceFrom", config.Attributes["deviceFrom"])
@ -77,13 +93,13 @@ func NewSverHandler(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_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_SEL {
valuePart, err2 := strconv.Atoi(config.Attributes["valuePart"])
if err2 != nil {
log.Printf("Error: unable to convert valuePart to number: %s", err2)
@ -92,13 +108,13 @@ func NewSverHandler(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"] != 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)
@ -144,6 +160,16 @@ func (self *SingleValueExtractorRegexHandler) 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:
@ -153,8 +179,8 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
}
measurement.Device = subTopics[self.config.devicePart]
case PAYLOAD_SEL:
if self.payloadRegex == nil {
lost("no payloadRegex defined, devicePart can't be used", message)
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(payloadMatches) {
@ -172,15 +198,9 @@ func (self *SingleValueExtractorRegexHandler) 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:
if self.payloadRegex == nil {
lost("no payloadRegex defined, valuePart can't be used", message)
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) {
@ -193,15 +213,9 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
}
switch self.config.unitFrom {
case TOPIC_SEL:
if self.config.unitPart >= len(subTopics) {
lost("unitPart out of range", message)
return
}
variable.Unit = subTopics[self.config.unitPart]
case PAYLOAD_SEL:
if self.payloadRegex == nil {
lost("no payloadRegex defined, unitPart can't be used", message)
if self.payloadRegex == nil && self.payloadJsonpath == nil {
lost("no payloadRegex or payloadJsonpath defined, unitPart can't be used", message)
return
}
if self.config.unitPart >= len(payloadMatches) {