Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
ac0c417a48
|
|||
|
dc965aeba6
|
|||
|
b940f715c0
|
|||
|
8c5626942f
|
|||
|
6d0dc12ac1
|
|||
|
6a4aac4140
|
|||
|
77d23e39cf
|
|||
|
e28042f3be
|
@@ -165,7 +165,7 @@
|
||||
{
|
||||
"topics": [ "shellyplusht/+/status/humidity:0" ],
|
||||
"handler": "SVEJ",
|
||||
"id": "SVE4",
|
||||
"id": "SVEJ3",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
@@ -175,6 +175,48 @@
|
||||
"unitSelector": "C:%"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "shellies/sensor/+/status/temperature:0" ],
|
||||
"handler": "SVEJ",
|
||||
"id": "SVEJ4",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
"application": "Shellies Sensor Temperature",
|
||||
"deviceSelector": "T:2",
|
||||
"valueSelector": "J:$.tC",
|
||||
"unitSelector": "C:°C"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "shellies/sensor/+/status/humidity:0" ],
|
||||
"handler": "SVEJ",
|
||||
"id": "SVEJ5",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
"application": "Shellies Sensor Humidity",
|
||||
"deviceSelector": "T:2",
|
||||
"valueSelector": "J:$.rh",
|
||||
"unitSelector": "C:%"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "shellies/sensor/+/status/devicepower:0" ],
|
||||
"handler": "SVEJ",
|
||||
"id": "SVEJ6",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
"application": "Shellies Sensor Power",
|
||||
"deviceSelector": "T:2",
|
||||
"valueSelector": "J:$.battery.percent",
|
||||
"unitSelector": "C:%"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"archiver": {
|
||||
|
||||
@@ -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,9 +34,8 @@ T:TopicPartIndex
|
||||
C:ConstantValue
|
||||
*/
|
||||
|
||||
|
||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t := &SingleValueExtractorJsonpathHandler {
|
||||
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,9 +109,8 @@ func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []st
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT) {
|
||||
if ! self.ready {
|
||||
if !self.ready {
|
||||
self.Lost("Handler is not marked as ready", nil, message)
|
||||
return
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user