|
|
|
@ -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
|
|
|
|
|