Compare commits

..

2 Commits
0.0.5 ... 0.0.6

Author SHA1 Message Date
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
695f78a632 remove debug
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-12-05 17:13:27 +01:00
4 changed files with 42 additions and 16 deletions

View File

@ -23,7 +23,7 @@ func InitDispatcher() {
go archiver() go archiver()
for _, mapping := range config.Config.TopicMappings { for _, mapping := range config.Config.TopicMappings {
log.Printf("Trying to initialize %s", mapping) // log.Printf("Trying to initialize %s", mapping)
var factory interface{} var factory interface{}
switch mapping.Handler { switch mapping.Handler {

View File

@ -5,6 +5,7 @@ go 1.21.3
require ( require (
github.com/eclipse/paho.mqtt.golang v1.4.3 github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/google/uuid v1.4.0 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/driver/postgres v1.5.4
gorm.io/gorm v1.25.5 gorm.io/gorm v1.25.5
) )

View File

@ -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/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 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@ -7,6 +7,8 @@ import (
"strings" "strings"
"regexp" "regexp"
"fmt" "fmt"
"encoding/json"
"github.com/oliveagle/jsonpath"
"udi/config" "udi/config"
"udi/handlers/handler" "udi/handlers/handler"
"udi/database" "udi/database"
@ -19,11 +21,13 @@ type SingleValueExtractorHandler struct {
ready bool ready bool
config localConfig config localConfig
payloadRegex *regexp.Regexp payloadRegex *regexp.Regexp
payloadJsonpath *jsonpath.Compiled
dbh *database.DatabaseHandle dbh *database.DatabaseHandle
} }
const TOPIC_SEL = "topic" 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 PAYLOAD_FULL_SEL = "payload-full"
const CONSTANT_SEL = "constant" const CONSTANT_SEL = "constant"
@ -60,8 +64,20 @@ func NewSveHandler(config config.HandlerConfigT) handler.Handler {
} else { } else {
t.payloadRegex = nil 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"]) log.Printf("Error: invalid value %s for deviceFrom", config.Attributes["deviceFrom"])
return t return t
} }
@ -77,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"] != 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"]) 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_FULL_SEL { if config.Attributes["valueFrom"] == PAYLOAD_REGEX_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)
@ -92,7 +108,7 @@ func NewSveHandler(config config.HandlerConfigT) handler.Handler {
localConfig.valuePart = valuePart 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"]) log.Printf("Error: invalid value %s for unitFrom", config.Attributes["unitFrom"])
return t return t
} }
@ -152,7 +168,7 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) {
return return
} }
measurement.Device = subTopics[self.config.devicePart] measurement.Device = subTopics[self.config.devicePart]
case PAYLOAD_SEL: case PAYLOAD_REGEX_SEL:
if self.payloadRegex == nil { if self.payloadRegex == nil {
lost("no payloadRegex defined, devicePart can't be used", message) lost("no payloadRegex defined, devicePart can't be used", message)
return return
@ -172,22 +188,29 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) {
variable.Variable = "" variable.Variable = ""
switch self.config.valueFrom { switch self.config.valueFrom {
case TOPIC_SEL: case PAYLOAD_REGEX_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 { if self.payloadRegex == nil {
lost("no payloadRegex defined, valuePart can't be used", message) lost("no payloadRegex defined, valuePart can't be used", message)
return return
} }
if self.config.valuePart >= len(subTopics) { if self.config.valuePart >= len(payloadMatches) {
lost("valuePart out of range", message) lost("valuePart out of range", message)
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
} }
@ -199,7 +222,7 @@ func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) {
return return
} }
variable.Unit = subTopics[self.config.unitPart] variable.Unit = subTopics[self.config.unitPart]
case PAYLOAD_SEL: case PAYLOAD_REGEX_SEL:
if self.payloadRegex == nil { if self.payloadRegex == nil {
lost("no payloadRegex defined, unitPart can't be used", message) lost("no payloadRegex defined, unitPart can't be used", message)
return return