Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
c37420a993
|
|||
|
ac0c417a48
|
|||
|
dc965aeba6
|
@@ -105,6 +105,7 @@
|
||||
"devicePart": "3",
|
||||
"valueFrom": "payload",
|
||||
"valuePart": "1",
|
||||
"valueType": "float",
|
||||
"unitFrom": "payload",
|
||||
"unitPart": "3"
|
||||
}
|
||||
@@ -185,7 +186,7 @@
|
||||
"attributes": {
|
||||
"application": "Shellies Sensor Temperature",
|
||||
"deviceSelector": "T:2",
|
||||
"valueSelector": "j:$.tC",
|
||||
"valueSelector": "J:$.tC",
|
||||
"unitSelector": "C:°C"
|
||||
}
|
||||
}
|
||||
@@ -199,7 +200,7 @@
|
||||
"attributes": {
|
||||
"application": "Shellies Sensor Humidity",
|
||||
"deviceSelector": "T:2",
|
||||
"valueSelector": "j:$.rh",
|
||||
"valueSelector": "J:$.rh",
|
||||
"unitSelector": "C:%"
|
||||
}
|
||||
}
|
||||
@@ -213,7 +214,7 @@
|
||||
"attributes": {
|
||||
"application": "Shellies Sensor Power",
|
||||
"deviceSelector": "T:2",
|
||||
"valueSelector": "j:$.battery.percent",
|
||||
"valueSelector": "J:$.battery.percent",
|
||||
"unitSelector": "C:%"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t.application = config.Attributes["application"]
|
||||
|
||||
t.deviceSelector = config.Attributes["deviceSelector"]
|
||||
if t.deviceSelector[:2] == "J:" || t.deviceSelector[:2] == "j:" {
|
||||
if t.deviceSelector[:2] == "J:" {
|
||||
jp, err := jsonpath.Compile(t.deviceSelector[2:])
|
||||
if err != nil {
|
||||
log.Printf("Unable to compile deviceJsonpath: %s, %s", t.deviceSelector[2:], err)
|
||||
@@ -55,7 +55,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t.deviceJsonpath = jp
|
||||
}
|
||||
t.valueSelector = config.Attributes["valueSelector"]
|
||||
if t.valueSelector[:2] == "J:" || t.valueSelector[:2] == "j:" {
|
||||
if t.valueSelector[:2] == "J:" {
|
||||
jp, err := jsonpath.Compile(t.valueSelector[2:])
|
||||
if err != nil {
|
||||
log.Printf("Unable to compile valueJsonpath: %s, %s", t.valueSelector[2:], err)
|
||||
@@ -64,7 +64,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t.valueJsonpath = jp
|
||||
}
|
||||
t.unitSelector = config.Attributes["unitSelector"]
|
||||
if t.unitSelector[:2] == "J:" || t.unitSelector[:2] == "j:" {
|
||||
if t.unitSelector[:2] == "J:" {
|
||||
jp, err := jsonpath.Compile(t.unitSelector[2:])
|
||||
if err != nil {
|
||||
log.Printf("Unable to compile unitJsonpath: %s, %s", t.unitSelector[2:], err)
|
||||
@@ -83,17 +83,13 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (interface{}, error) {
|
||||
var res interface{}
|
||||
switch selector[:2] {
|
||||
case "J:", "j:":
|
||||
case "J:":
|
||||
// extract using jsonpath from payload
|
||||
r, e := jp.Lookup(jPayload)
|
||||
if e != nil {
|
||||
return "", fmt.Errorf("jp.Lookup failed with %s", e)
|
||||
}
|
||||
if selector[:2] == "j:" {
|
||||
res = r.(float64)
|
||||
} else {
|
||||
res = fmt.Sprint(r)
|
||||
}
|
||||
res = r
|
||||
case "T:":
|
||||
// T: extract from topic
|
||||
i, e := strconv.Atoi(selector[2:])
|
||||
@@ -152,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
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
package sver
|
||||
|
||||
import (
|
||||
"time"
|
||||
"log"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"regexp"
|
||||
"log"
|
||||
"time"
|
||||
"udi/config"
|
||||
"udi/handlers/handler"
|
||||
"udi/database"
|
||||
"udi/handlers/handler"
|
||||
)
|
||||
|
||||
|
||||
type SingleValueExtractorRegexHandler struct {
|
||||
handler.CommonHandler
|
||||
ready bool
|
||||
@@ -32,12 +31,12 @@ type localConfig struct {
|
||||
device string
|
||||
valueFrom string
|
||||
valuePart int
|
||||
valueType string
|
||||
unitFrom string
|
||||
unitPart int
|
||||
unit string
|
||||
}
|
||||
|
||||
|
||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t := &SingleValueExtractorRegexHandler{
|
||||
ready: false,
|
||||
@@ -88,6 +87,12 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
localConfig.valuePart = valuePart
|
||||
}
|
||||
|
||||
if config.Attributes["valueType"] != "float" && config.Attributes["valueType"] != "string" {
|
||||
log.Printf("Error: invalid value %s for valueType", config.Attributes["valueType"])
|
||||
return t
|
||||
}
|
||||
localConfig.valueType = config.Attributes["valueType"]
|
||||
|
||||
if config.Attributes["unitFrom"] != PAYLOAD_SEL && config.Attributes["unitFrom"] != CONSTANT_SEL {
|
||||
log.Printf("Error: invalid value %s for unitFrom", config.Attributes["unitFrom"])
|
||||
return t
|
||||
@@ -161,6 +166,7 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||
variable.Label = ""
|
||||
variable.Variable = ""
|
||||
|
||||
var value string
|
||||
switch self.config.valueFrom {
|
||||
case PAYLOAD_SEL:
|
||||
if self.payloadRegex == nil {
|
||||
@@ -171,9 +177,19 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||
self.Lost("valuePart out of range", nil, message)
|
||||
return
|
||||
}
|
||||
variable.Value = payloadMatches[self.config.valuePart]
|
||||
value = payloadMatches[self.config.valuePart]
|
||||
case PAYLOAD_FULL_SEL:
|
||||
variable.Value = message.Payload
|
||||
value = message.Payload
|
||||
}
|
||||
if self.config.valueType == "float" {
|
||||
fValue, err := strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
self.Lost("Unable to convert value to float", err, message)
|
||||
return
|
||||
}
|
||||
variable.Value = fValue
|
||||
} else {
|
||||
variable.Value = value
|
||||
}
|
||||
|
||||
switch self.config.unitFrom {
|
||||
@@ -197,4 +213,3 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||
self.dbh.StoreMeasurement(&measurement)
|
||||
self.S()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user