Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
b940f715c0
|
|||
|
8c5626942f
|
|||
|
6d0dc12ac1
|
|||
|
6a4aac4140
|
|||
|
77d23e39cf
|
|||
|
e28042f3be
|
|||
|
e1ad76f703
|
@@ -17,7 +17,7 @@ metadata:
|
|||||||
labels:
|
labels:
|
||||||
app: udi
|
app: udi
|
||||||
annotations:
|
annotations:
|
||||||
secret.reloader.stakater.com/reload: "%PRE%-udi-conf,%PRE%-udi-db-cred,%PRE%-mqtt-password"
|
secret.reloader.stakater.com/reload: "%PRE%-udi-conf,%PRE%-udi-db-cred,%PRE%-mqtt-password,%PRE%-udi-influxdb-cred"
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
selector:
|
selector:
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ for NAMESPACE_DIR in `find $INSTANCES_DIR -type d -mindepth 1 -maxdepth 1`; do
|
|||||||
--dry-run=client \
|
--dry-run=client \
|
||||||
-o yaml \
|
-o yaml \
|
||||||
--save-config \
|
--save-config \
|
||||||
--from-literal=INFLUXDB_URL="$INFLUXDB_URL" | \
|
--from-literal=INFLUXDB_URL="${!INFLUXDB_URL}" | \
|
||||||
kubectl apply -f - -n $NAMESPACE
|
kubectl apply -f - -n $NAMESPACE
|
||||||
|
|
||||||
# set configuration as configMap
|
# set configuration as configMap
|
||||||
|
|||||||
@@ -165,7 +165,7 @@
|
|||||||
{
|
{
|
||||||
"topics": [ "shellyplusht/+/status/humidity:0" ],
|
"topics": [ "shellyplusht/+/status/humidity:0" ],
|
||||||
"handler": "SVEJ",
|
"handler": "SVEJ",
|
||||||
"id": "SVE4",
|
"id": "SVEJ3",
|
||||||
"config": {
|
"config": {
|
||||||
"databaseConnStr": "",
|
"databaseConnStr": "",
|
||||||
"attributes": {
|
"attributes": {
|
||||||
@@ -175,6 +175,48 @@
|
|||||||
"unitSelector": "C:%"
|
"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": {
|
"archiver": {
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func NewDatabaseHandle() *DatabaseHandle {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Unable to create InfluxDB client: %s", err)
|
log.Printf("Unable to create InfluxDB client (config: URL: %s, Username: %s, Password: %s): %s", influxDB, username, password, err)
|
||||||
db.initialized = false
|
db.initialized = false
|
||||||
return &db
|
return &db
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
package svej
|
package svej
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"fmt"
|
"time"
|
||||||
"github.com/oliveagle/jsonpath"
|
|
||||||
"encoding/json"
|
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
|
||||||
"udi/database"
|
"udi/database"
|
||||||
|
"udi/handlers/handler"
|
||||||
|
|
||||||
|
"github.com/oliveagle/jsonpath"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SingleValueExtractorJsonpathHandler struct {
|
type SingleValueExtractorJsonpathHandler struct {
|
||||||
@@ -33,9 +34,8 @@ T:TopicPartIndex
|
|||||||
C:ConstantValue
|
C:ConstantValue
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
t := &SingleValueExtractorJsonpathHandler {
|
t := &SingleValueExtractorJsonpathHandler{
|
||||||
ready: false,
|
ready: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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:" {
|
if t.deviceSelector[:2] == "J:" || 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:" {
|
if t.valueSelector[:2] == "J:" || 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:" {
|
if t.unitSelector[:2] == "J:" || 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)
|
||||||
@@ -76,20 +76,24 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
t.Id = id
|
t.Id = id
|
||||||
t.ready = true
|
t.ready = true
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
log.Printf("Handler SVEJ %d initialized", id)
|
log.Printf("Handler SVEJ %s initialized", id)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (string, error) {
|
func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (interface{}, error) {
|
||||||
var res string
|
var res interface{}
|
||||||
switch selector[:2] {
|
switch selector[:2] {
|
||||||
case "J:":
|
case "J:", "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.(float64)
|
||||||
|
} else {
|
||||||
res = fmt.Sprint(r)
|
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:])
|
||||||
@@ -109,9 +113,8 @@ func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []st
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT) {
|
func (self *SingleValueExtractorJsonpathHandler) 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
|
||||||
}
|
}
|
||||||
@@ -163,4 +166,3 @@ func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT
|
|||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
self.S()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user