|
|
|
|
@@ -1,16 +1,17 @@
|
|
|
|
|
package svej
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"time"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/oliveagle/jsonpath"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"time"
|
|
|
|
|
"udi/config"
|
|
|
|
|
"udi/handlers/handler"
|
|
|
|
|
"udi/database"
|
|
|
|
|
"udi/handlers/handler"
|
|
|
|
|
|
|
|
|
|
"github.com/oliveagle/jsonpath"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SingleValueExtractorJsonpathHandler struct {
|
|
|
|
|
@@ -33,7 +34,6 @@ T:TopicPartIndex
|
|
|
|
|
C:ConstantValue
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
|
|
|
|
t := &SingleValueExtractorJsonpathHandler{
|
|
|
|
|
ready: false,
|
|
|
|
|
@@ -76,12 +76,12 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|
|
|
|
t.Id = id
|
|
|
|
|
t.ready = true
|
|
|
|
|
t.dbh = database.NewDatabaseHandle()
|
|
|
|
|
log.Printf("Handler SVEJ %d initialized", id)
|
|
|
|
|
log.Printf("Handler SVEJ %s initialized", id)
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (string, error) {
|
|
|
|
|
var res string
|
|
|
|
|
func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (interface{}, error) {
|
|
|
|
|
var res interface{}
|
|
|
|
|
switch selector[:2] {
|
|
|
|
|
case "J:":
|
|
|
|
|
// extract using jsonpath from payload
|
|
|
|
|
@@ -89,7 +89,7 @@ func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []st
|
|
|
|
|
if e != nil {
|
|
|
|
|
return "", fmt.Errorf("jp.Lookup failed with %s", e)
|
|
|
|
|
}
|
|
|
|
|
res = fmt.Sprint(r)
|
|
|
|
|
res = r
|
|
|
|
|
case "T:":
|
|
|
|
|
// T: extract from topic
|
|
|
|
|
i, e := strconv.Atoi(selector[2:])
|
|
|
|
|
@@ -109,7 +109,6 @@ func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []st
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT) {
|
|
|
|
|
if !self.ready {
|
|
|
|
|
self.Lost("Handler is not marked as ready", nil, message)
|
|
|
|
|
@@ -149,12 +148,12 @@ func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
measurement.Device = device
|
|
|
|
|
measurement.Device = device.(string)
|
|
|
|
|
|
|
|
|
|
var variable database.VariableType
|
|
|
|
|
variable.Label = ""
|
|
|
|
|
variable.Variable = ""
|
|
|
|
|
variable.Unit = unit
|
|
|
|
|
variable.Unit = unit.(string)
|
|
|
|
|
variable.Value = value
|
|
|
|
|
measurement.Values = make(map[string]database.VariableType)
|
|
|
|
|
measurement.Values["Value"] = variable
|
|
|
|
|
@@ -163,4 +162,3 @@ func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT
|
|
|
|
|
self.dbh.StoreMeasurement(&measurement)
|
|
|
|
|
self.S()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|