dt1t and counter and refactoring using embedded interfaces
This commit is contained in:
@ -1,28 +1,22 @@
|
||||
package sver
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
"strconv"
|
||||
"strings"
|
||||
"regexp"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"encoding/json"
|
||||
"github.com/oliveagle/jsonpath"
|
||||
"log"
|
||||
"udi/config"
|
||||
"udi/handlers/handler"
|
||||
"udi/database"
|
||||
)
|
||||
|
||||
var idSeq int = 0
|
||||
|
||||
type SingleValueExtractorRegexHandler struct {
|
||||
id int
|
||||
handler.CommonHandler
|
||||
ready bool
|
||||
config localConfig
|
||||
payloadRegex *regexp.Regexp
|
||||
payloadJsonpath *jsonpath.Compiled
|
||||
dbh *database.DatabaseHandle
|
||||
}
|
||||
|
||||
@ -44,12 +38,10 @@ type localConfig struct {
|
||||
}
|
||||
|
||||
|
||||
func NewSverHandler(config config.HandlerConfigT) handler.Handler {
|
||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t := &SingleValueExtractorRegexHandler {
|
||||
id: idSeq,
|
||||
ready: false,
|
||||
}
|
||||
idSeq += 1
|
||||
|
||||
var localConfig localConfig
|
||||
if config.Attributes["application"] == "" {
|
||||
@ -64,18 +56,6 @@ 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"])
|
||||
@ -128,22 +108,15 @@ func NewSverHandler(config config.HandlerConfigT) handler.Handler {
|
||||
|
||||
t.config = localConfig
|
||||
|
||||
t.Id = id
|
||||
t.ready = true
|
||||
t.dbh = database.NewDatabaseHandle()
|
||||
return t
|
||||
}
|
||||
|
||||
func (self *SingleValueExtractorRegexHandler) GetId() string {
|
||||
return fmt.Sprintf("SVE%d", self.id)
|
||||
}
|
||||
|
||||
func lost(msg string, message handler.MessageT) {
|
||||
log.Printf("Error: %s, message %s is lost", msg, message)
|
||||
}
|
||||
|
||||
func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||
if ! self.ready {
|
||||
log.Println("Handler is not marked as ready, message %s is lost", message)
|
||||
self.Lost("Handler is not marked as ready", nil, message)
|
||||
return
|
||||
}
|
||||
//log.Printf("Handler SingleValueExtractor %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||
@ -160,31 +133,21 @@ 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:
|
||||
if self.config.devicePart >= len(subTopics) {
|
||||
lost("devicePart out of range", message)
|
||||
self.Lost("devicePart out of range", nil, message)
|
||||
return
|
||||
}
|
||||
measurement.Device = subTopics[self.config.devicePart]
|
||||
case PAYLOAD_SEL:
|
||||
if self.payloadRegex == nil && self.payloadJsonpath == nil {
|
||||
lost("no payloadRegex or payloadJsonpath defined, devicePart can't be used", message)
|
||||
if self.payloadRegex == nil {
|
||||
self.Lost("no payloadRegex defined, devicePart can't be used", nil, message)
|
||||
return
|
||||
}
|
||||
if self.config.devicePart >= len(payloadMatches) {
|
||||
lost("devicePart out of range", message)
|
||||
self.Lost("devicePart out of range", nil, message)
|
||||
return
|
||||
}
|
||||
measurement.Device = payloadMatches[self.config.devicePart]
|
||||
@ -199,12 +162,12 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||
|
||||
switch self.config.valueFrom {
|
||||
case PAYLOAD_SEL:
|
||||
if self.payloadRegex == nil && self.payloadJsonpath == nil {
|
||||
lost("no payloadRegex or payloadJsonpath defined, valuePart can't be used", message)
|
||||
if self.payloadRegex == nil {
|
||||
self.Lost("no payloadRegex defined, valuePart can't be used", nil, message)
|
||||
return
|
||||
}
|
||||
if self.config.valuePart >= len(payloadMatches) {
|
||||
lost("valuePart out of range", message)
|
||||
self.Lost("valuePart out of range", nil, message)
|
||||
return
|
||||
}
|
||||
variable.Value = payloadMatches[self.config.valuePart]
|
||||
@ -214,12 +177,12 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||
|
||||
switch self.config.unitFrom {
|
||||
case PAYLOAD_SEL:
|
||||
if self.payloadRegex == nil && self.payloadJsonpath == nil {
|
||||
lost("no payloadRegex or payloadJsonpath defined, unitPart can't be used", message)
|
||||
if self.payloadRegex == nil {
|
||||
self.Lost("no payloadRegex defined, unitPart can't be used", nil, message)
|
||||
return
|
||||
}
|
||||
if self.config.unitPart >= len(payloadMatches) {
|
||||
lost("unitPart out of range", message)
|
||||
self.Lost("unitPart out of range", nil, message)
|
||||
return
|
||||
}
|
||||
variable.Unit = payloadMatches[self.config.unitPart]
|
||||
@ -231,5 +194,6 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||
|
||||
//log.Printf("Prepared measurement item: %s", measurement)
|
||||
self.dbh.StoreMeasurement(&measurement)
|
||||
self.S()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user