Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
0f6590c720
|
|||
|
42ff8b51ed
|
|||
|
f63c22912a
|
|||
|
c37420a993
|
|||
|
ac0c417a48
|
|||
|
dc965aeba6
|
@@ -105,6 +105,7 @@
|
|||||||
"devicePart": "3",
|
"devicePart": "3",
|
||||||
"valueFrom": "payload",
|
"valueFrom": "payload",
|
||||||
"valuePart": "1",
|
"valuePart": "1",
|
||||||
|
"valueType": "float",
|
||||||
"unitFrom": "payload",
|
"unitFrom": "payload",
|
||||||
"unitPart": "3"
|
"unitPart": "3"
|
||||||
}
|
}
|
||||||
@@ -185,7 +186,7 @@
|
|||||||
"attributes": {
|
"attributes": {
|
||||||
"application": "Shellies Sensor Temperature",
|
"application": "Shellies Sensor Temperature",
|
||||||
"deviceSelector": "T:2",
|
"deviceSelector": "T:2",
|
||||||
"valueSelector": "j:$.tC",
|
"valueSelector": "J:$.tC",
|
||||||
"unitSelector": "C:°C"
|
"unitSelector": "C:°C"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,7 +200,7 @@
|
|||||||
"attributes": {
|
"attributes": {
|
||||||
"application": "Shellies Sensor Humidity",
|
"application": "Shellies Sensor Humidity",
|
||||||
"deviceSelector": "T:2",
|
"deviceSelector": "T:2",
|
||||||
"valueSelector": "j:$.rh",
|
"valueSelector": "J:$.rh",
|
||||||
"unitSelector": "C:%"
|
"unitSelector": "C:%"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -213,7 +214,7 @@
|
|||||||
"attributes": {
|
"attributes": {
|
||||||
"application": "Shellies Sensor Power",
|
"application": "Shellies Sensor Power",
|
||||||
"deviceSelector": "T:2",
|
"deviceSelector": "T:2",
|
||||||
"valueSelector": "j:$.battery.percent",
|
"valueSelector": "J:$.battery.percent",
|
||||||
"unitSelector": "C:%"
|
"unitSelector": "C:%"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,13 @@ package dt1t
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"udi/handlers/handler"
|
"time"
|
||||||
"udi/database"
|
|
||||||
"udi/config"
|
"udi/config"
|
||||||
|
"udi/database"
|
||||||
|
"udi/handlers/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Dt1tHandler struct {
|
type Dt1tHandler struct {
|
||||||
handler.CommonHandler
|
handler.CommonHandler
|
||||||
ready bool
|
ready bool
|
||||||
@@ -18,12 +16,10 @@ type Dt1tHandler struct {
|
|||||||
dbh *database.DatabaseHandle
|
dbh *database.DatabaseHandle
|
||||||
application string
|
application string
|
||||||
device string
|
device string
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
t := &Dt1tHandler {
|
t := &Dt1tHandler{}
|
||||||
}
|
|
||||||
|
|
||||||
if config.Attributes["Application"] == "" {
|
if config.Attributes["Application"] == "" {
|
||||||
log.Println("Error: application not configured")
|
log.Println("Error: application not configured")
|
||||||
@@ -44,7 +40,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
||||||
if ! self.ready {
|
if !self.ready {
|
||||||
self.Lost("Handler is not marked as ready", nil, message)
|
self.Lost("Handler is not marked as ready", nil, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -55,7 +51,7 @@ func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
|||||||
self.Lost("Invalid raw value", err, message)
|
self.Lost("Invalid raw value", err, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if temperature & 0x8000 != 0{
|
if temperature&0x8000 != 0 {
|
||||||
temperature = ((temperature - 1) ^ 0xffff) * -1
|
temperature = ((temperature - 1) ^ 0xffff) * -1
|
||||||
}
|
}
|
||||||
temperatureF := float32(temperature) / 10.0
|
temperatureF := float32(temperature) / 10.0
|
||||||
@@ -69,7 +65,7 @@ func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
|||||||
variable.Label = "Temperature"
|
variable.Label = "Temperature"
|
||||||
variable.Variable = ""
|
variable.Variable = ""
|
||||||
variable.Unit = "°C"
|
variable.Unit = "°C"
|
||||||
variable.Value = fmt.Sprintf("%f", temperatureF)
|
variable.Value = temperatureF
|
||||||
measurement.Values = make(map[string]database.VariableType)
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
measurement.Values["Value"] = variable
|
measurement.Values["Value"] = variable
|
||||||
|
|
||||||
@@ -77,5 +73,3 @@ func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
|||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
self.S()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
package prepared
|
package prepared
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
"log"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
|
||||||
"udi/database"
|
"udi/database"
|
||||||
|
"udi/handlers/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type PreparedHandler struct {
|
type PreparedHandler struct {
|
||||||
handler.CommonHandler
|
handler.CommonHandler
|
||||||
dbh *database.DatabaseHandle
|
dbh *database.DatabaseHandle
|
||||||
@@ -18,7 +17,7 @@ type PreparedHandler struct {
|
|||||||
type endpoint_t struct {
|
type endpoint_t struct {
|
||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
Variable string `json:"variable"`
|
Variable string `json:"variable"`
|
||||||
Value string `json:"value"`
|
Value interface{} `json:"value"`
|
||||||
Unit string `json:"unit"`
|
Unit string `json:"unit"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
}
|
}
|
||||||
@@ -29,10 +28,8 @@ type observation_t struct {
|
|||||||
Variables map[string]endpoint_t `json:"variables"`
|
Variables map[string]endpoint_t `json:"variables"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
t := &PreparedHandler {
|
t := &PreparedHandler{}
|
||||||
}
|
|
||||||
t.Id = id
|
t.Id = id
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
log.Printf("Handler Prepared %d initialized", id)
|
log.Printf("Handler Prepared %d initialized", id)
|
||||||
@@ -55,13 +52,13 @@ func (self *PreparedHandler) Handle(message handler.MessageT) {
|
|||||||
measurement.Application = self.Id
|
measurement.Application = self.Id
|
||||||
measurement.Device = observation.Device
|
measurement.Device = observation.Device
|
||||||
|
|
||||||
measurement.Attributes = map[string]interface{} {
|
measurement.Attributes = map[string]interface{}{
|
||||||
"Label": observation.Label,
|
"Label": observation.Label,
|
||||||
}
|
}
|
||||||
|
|
||||||
measurement.Values = make(map[string]database.VariableType)
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
for k, v := range observation.Variables {
|
for k, v := range observation.Variables {
|
||||||
measurement.Values[k] = database.VariableType {
|
measurement.Values[k] = database.VariableType{
|
||||||
Label: v.Label,
|
Label: v.Label,
|
||||||
Variable: v.Variable,
|
Variable: v.Variable,
|
||||||
Unit: v.Unit,
|
Unit: v.Unit,
|
||||||
@@ -75,5 +72,3 @@ func (self *PreparedHandler) Handle(message handler.MessageT) {
|
|||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
self.S()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
t.application = config.Attributes["application"]
|
t.application = config.Attributes["application"]
|
||||||
|
|
||||||
t.deviceSelector = config.Attributes["deviceSelector"]
|
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:])
|
jp, err := jsonpath.Compile(t.deviceSelector[2:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Unable to compile deviceJsonpath: %s, %s", t.deviceSelector[2:], err)
|
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.deviceJsonpath = jp
|
||||||
}
|
}
|
||||||
t.valueSelector = config.Attributes["valueSelector"]
|
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:])
|
jp, err := jsonpath.Compile(t.valueSelector[2:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Unable to compile valueJsonpath: %s, %s", t.valueSelector[2:], err)
|
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.valueJsonpath = jp
|
||||||
}
|
}
|
||||||
t.unitSelector = config.Attributes["unitSelector"]
|
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:])
|
jp, err := jsonpath.Compile(t.unitSelector[2:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Unable to compile unitJsonpath: %s, %s", t.unitSelector[2:], err)
|
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) {
|
func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (interface{}, error) {
|
||||||
var res interface{}
|
var res interface{}
|
||||||
switch selector[:2] {
|
switch selector[:2] {
|
||||||
case "J:", "j:":
|
case "J:":
|
||||||
// extract using jsonpath from payload
|
// extract using jsonpath from payload
|
||||||
r, e := jp.Lookup(jPayload)
|
r, e := jp.Lookup(jPayload)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return "", fmt.Errorf("jp.Lookup failed with %s", e)
|
return "", fmt.Errorf("jp.Lookup failed with %s", e)
|
||||||
}
|
}
|
||||||
if selector[:2] == "j:" {
|
res = r
|
||||||
res = r.(float64)
|
|
||||||
} else {
|
|
||||||
res = fmt.Sprint(r)
|
|
||||||
}
|
|
||||||
case "T:":
|
case "T:":
|
||||||
// T: extract from topic
|
// T: extract from topic
|
||||||
i, e := strconv.Atoi(selector[2:])
|
i, e := strconv.Atoi(selector[2:])
|
||||||
@@ -152,12 +148,12 @@ func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
measurement.Device = device
|
measurement.Device = device.(string)
|
||||||
|
|
||||||
var variable database.VariableType
|
var variable database.VariableType
|
||||||
variable.Label = ""
|
variable.Label = ""
|
||||||
variable.Variable = ""
|
variable.Variable = ""
|
||||||
variable.Unit = unit
|
variable.Unit = unit.(string)
|
||||||
variable.Value = value
|
variable.Value = value
|
||||||
measurement.Values = make(map[string]database.VariableType)
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
measurement.Values["Value"] = variable
|
measurement.Values["Value"] = variable
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
package sver
|
package sver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"log"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"regexp"
|
"time"
|
||||||
"log"
|
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
|
||||||
"udi/database"
|
"udi/database"
|
||||||
|
"udi/handlers/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type SingleValueExtractorRegexHandler struct {
|
type SingleValueExtractorRegexHandler struct {
|
||||||
handler.CommonHandler
|
handler.CommonHandler
|
||||||
ready bool
|
ready bool
|
||||||
@@ -32,14 +31,14 @@ type localConfig struct {
|
|||||||
device string
|
device string
|
||||||
valueFrom string
|
valueFrom string
|
||||||
valuePart int
|
valuePart int
|
||||||
|
valueType string
|
||||||
unitFrom string
|
unitFrom string
|
||||||
unitPart int
|
unitPart int
|
||||||
unit string
|
unit string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
t := &SingleValueExtractorRegexHandler {
|
t := &SingleValueExtractorRegexHandler{
|
||||||
ready: false,
|
ready: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +87,12 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
localConfig.valuePart = valuePart
|
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 {
|
if config.Attributes["unitFrom"] != PAYLOAD_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
|
||||||
@@ -116,7 +121,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||||
if ! self.ready {
|
if !self.ready {
|
||||||
self.Lost("Handler is not marked as ready", nil, message)
|
self.Lost("Handler is not marked as ready", nil, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -161,6 +166,7 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
variable.Label = ""
|
variable.Label = ""
|
||||||
variable.Variable = ""
|
variable.Variable = ""
|
||||||
|
|
||||||
|
var value string
|
||||||
switch self.config.valueFrom {
|
switch self.config.valueFrom {
|
||||||
case PAYLOAD_SEL:
|
case PAYLOAD_SEL:
|
||||||
if self.payloadRegex == nil {
|
if self.payloadRegex == nil {
|
||||||
@@ -171,9 +177,19 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
self.Lost("valuePart out of range", nil, message)
|
self.Lost("valuePart out of range", nil, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
variable.Value = payloadMatches[self.config.valuePart]
|
value = payloadMatches[self.config.valuePart]
|
||||||
case PAYLOAD_FULL_SEL:
|
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 {
|
switch self.config.unitFrom {
|
||||||
@@ -197,4 +213,3 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
self.S()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user