Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
ac0c417a48
|
|||
|
dc965aeba6
|
|||
|
b940f715c0
|
|||
|
8c5626942f
|
|||
|
6d0dc12ac1
|
|||
|
6a4aac4140
|
|||
|
77d23e39cf
|
|||
|
e28042f3be
|
|||
|
e1ad76f703
|
|||
|
6dac149a48
|
@@ -17,7 +17,7 @@ metadata:
|
||||
labels:
|
||||
app: udi
|
||||
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:
|
||||
replicas: 1
|
||||
selector:
|
||||
@@ -36,6 +36,8 @@ spec:
|
||||
name: %PRE%-udi-db-cred
|
||||
- secretRef:
|
||||
name: %PRE%-mqtt-password
|
||||
- secretRef:
|
||||
name: %PRE%-udi-influxdb-cred
|
||||
- configMapRef:
|
||||
name: %PRE%-udi-conf
|
||||
volumeMounts:
|
||||
|
||||
@@ -67,6 +67,7 @@ for NAMESPACE_DIR in `find $INSTANCES_DIR -type d -mindepth 1 -maxdepth 1`; do
|
||||
DATABASE_VARIABLE=$VARIABLE_PREFIX"_PGDATABASE"
|
||||
NEW_UDI_DB_DATABASE="${!DATABASE_VARIABLE}"
|
||||
NEW_UDI_DB_HOST=database.database1.svc.cluster.local
|
||||
INFLUXDB_URL=$VARIABLE_PREFIX"_INFLUXDB_URL"
|
||||
|
||||
kubectl create secret generic $INSTANCE-udi-db-cred \
|
||||
--dry-run=client \
|
||||
@@ -79,6 +80,13 @@ for NAMESPACE_DIR in `find $INSTANCES_DIR -type d -mindepth 1 -maxdepth 1`; do
|
||||
--from-literal=PGSSLMODE="require" | \
|
||||
kubectl apply -f - -n $NAMESPACE
|
||||
|
||||
kubectl create secret generic $INSTANCE-udi-influxdb-cred \
|
||||
--dry-run=client \
|
||||
-o yaml \
|
||||
--save-config \
|
||||
--from-literal=INFLUXDB_URL="${!INFLUXDB_URL}" | \
|
||||
kubectl apply -f - -n $NAMESPACE
|
||||
|
||||
# set configuration as configMap
|
||||
kubectl create configmap $INSTANCE-udi-conf \
|
||||
--from-literal=UDI_CONF="`cat $CONFIG_FILE`" \
|
||||
|
||||
@@ -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,7 +1,8 @@
|
||||
-----BEGIN PGP MESSAGE-----
|
||||
|
||||
jA0ECQMIOpibMyX5/pH90mMBMdpIUJC/lR3BeUddyWJjwixQcePZIAQ/QEV6VO71
|
||||
2cleQWnw8Ll5/bKtDv94UMLDZy86YdcNyeUXDP5cYPrGAFqWLaBmkLbpAJf7LRtb
|
||||
nckgwR1+++GPvn5vNYcqGt92pB0=
|
||||
=xy3G
|
||||
jA0ECQMIYUoTHR96Qfb90psBoxuk38UXPXTWPCmdW690bi2+w34S4NLHZvHfe3Ra
|
||||
nck319+PXvr0agfHGZ733hhTQv4sa8I2o6ICrgFqtKGfHmgnqL5kYNP9+NuV/IsF
|
||||
x3dxwjEejsZ5GYn/zk+CQceItQ8nyyJc2ms1KwTu2r4hMzuHmnVtvKxNCzPrw2N5
|
||||
SJIRhh41eequFkzELQqqXXu10raBFsttOemVhA==
|
||||
=TySu
|
||||
-----END PGP MESSAGE-----
|
||||
|
||||
@@ -40,7 +40,7 @@ func NewDatabaseHandle() *DatabaseHandle {
|
||||
})
|
||||
|
||||
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
|
||||
return &db
|
||||
}
|
||||
|
||||
@@ -1,29 +1,30 @@
|
||||
package svej
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
"strconv"
|
||||
"strings"
|
||||
"fmt"
|
||||
"github.com/oliveagle/jsonpath"
|
||||
"encoding/json"
|
||||
"udi/config"
|
||||
"udi/handlers/handler"
|
||||
"udi/database"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"udi/config"
|
||||
"udi/database"
|
||||
"udi/handlers/handler"
|
||||
|
||||
"github.com/oliveagle/jsonpath"
|
||||
)
|
||||
|
||||
type SingleValueExtractorJsonpathHandler struct {
|
||||
handler.CommonHandler
|
||||
ready bool
|
||||
application string
|
||||
deviceSelector string
|
||||
valueSelector string
|
||||
unitSelector string
|
||||
deviceJsonpath *jsonpath.Compiled
|
||||
valueJsonpath *jsonpath.Compiled
|
||||
unitJsonpath *jsonpath.Compiled
|
||||
dbh *database.DatabaseHandle
|
||||
handler.CommonHandler
|
||||
ready bool
|
||||
application string
|
||||
deviceSelector string
|
||||
valueSelector string
|
||||
unitSelector string
|
||||
deviceJsonpath *jsonpath.Compiled
|
||||
valueJsonpath *jsonpath.Compiled
|
||||
unitJsonpath *jsonpath.Compiled
|
||||
dbh *database.DatabaseHandle
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -33,134 +34,131 @@ T:TopicPartIndex
|
||||
C:ConstantValue
|
||||
*/
|
||||
|
||||
|
||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t := &SingleValueExtractorJsonpathHandler {
|
||||
ready: false,
|
||||
}
|
||||
t := &SingleValueExtractorJsonpathHandler{
|
||||
ready: false,
|
||||
}
|
||||
|
||||
if config.Attributes["application"] == "" {
|
||||
log.Println("Error: application not configured")
|
||||
return t
|
||||
}
|
||||
t.application = config.Attributes["application"]
|
||||
|
||||
t.deviceSelector = config.Attributes["deviceSelector"]
|
||||
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)
|
||||
return t
|
||||
}
|
||||
t.deviceJsonpath = jp
|
||||
}
|
||||
t.valueSelector = config.Attributes["valueSelector"]
|
||||
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)
|
||||
return t
|
||||
}
|
||||
t.valueJsonpath = jp
|
||||
}
|
||||
t.unitSelector = config.Attributes["unitSelector"]
|
||||
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)
|
||||
return t
|
||||
}
|
||||
t.unitJsonpath = jp
|
||||
}
|
||||
if config.Attributes["application"] == "" {
|
||||
log.Println("Error: application not configured")
|
||||
return t
|
||||
}
|
||||
t.application = config.Attributes["application"]
|
||||
|
||||
t.Id = id
|
||||
t.ready = true
|
||||
t.dbh = database.NewDatabaseHandle()
|
||||
log.Printf("Handler SVEJ %d initialized", id)
|
||||
return t
|
||||
t.deviceSelector = config.Attributes["deviceSelector"]
|
||||
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)
|
||||
return t
|
||||
}
|
||||
t.deviceJsonpath = jp
|
||||
}
|
||||
t.valueSelector = config.Attributes["valueSelector"]
|
||||
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)
|
||||
return t
|
||||
}
|
||||
t.valueJsonpath = jp
|
||||
}
|
||||
t.unitSelector = config.Attributes["unitSelector"]
|
||||
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)
|
||||
return t
|
||||
}
|
||||
t.unitJsonpath = jp
|
||||
}
|
||||
|
||||
t.Id = id
|
||||
t.ready = true
|
||||
t.dbh = database.NewDatabaseHandle()
|
||||
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
|
||||
switch selector[:2] {
|
||||
case "J:":
|
||||
// extract using jsonpath from payload
|
||||
r, e := jp.Lookup(jPayload)
|
||||
if e != nil {
|
||||
return "", fmt.Errorf("jp.Lookup failed with %s", e)
|
||||
}
|
||||
res = fmt.Sprint(r)
|
||||
case "T:":
|
||||
// T: extract from topic
|
||||
i, e := strconv.Atoi(selector[2:])
|
||||
if e != nil {
|
||||
return "", fmt.Errorf("Atoi failed with %s", e)
|
||||
}
|
||||
if i >= len(subTopics) {
|
||||
return "", fmt.Errorf("not enough subtopics")
|
||||
}
|
||||
res = subTopics[i]
|
||||
case "C:":
|
||||
// use constant value
|
||||
res = selector[2:]
|
||||
default:
|
||||
return "", fmt.Errorf("Invalid selector: %s", selector[:2])
|
||||
}
|
||||
return res, nil
|
||||
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
|
||||
r, e := jp.Lookup(jPayload)
|
||||
if e != nil {
|
||||
return "", fmt.Errorf("jp.Lookup failed with %s", e)
|
||||
}
|
||||
res = r
|
||||
case "T:":
|
||||
// T: extract from topic
|
||||
i, e := strconv.Atoi(selector[2:])
|
||||
if e != nil {
|
||||
return "", fmt.Errorf("Atoi failed with %s", e)
|
||||
}
|
||||
if i >= len(subTopics) {
|
||||
return "", fmt.Errorf("not enough subtopics")
|
||||
}
|
||||
res = subTopics[i]
|
||||
case "C:":
|
||||
// use constant value
|
||||
res = selector[2:]
|
||||
default:
|
||||
return "", fmt.Errorf("Invalid selector: %s", selector[:2])
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT) {
|
||||
if ! self.ready {
|
||||
self.Lost("Handler is not marked as ready", nil, message)
|
||||
return
|
||||
}
|
||||
log.Printf("Handler SingleValueExtractorJsonpath %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||
if !self.ready {
|
||||
self.Lost("Handler is not marked as ready", nil, message)
|
||||
return
|
||||
}
|
||||
log.Printf("Handler SingleValueExtractorJsonpath %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||
|
||||
var measurement database.Measurement
|
||||
measurement.Time = time.Now()
|
||||
measurement.Application = self.application
|
||||
|
||||
subTopics := strings.Split(message.Topic, "/")
|
||||
log.Printf("Subtopics: %s", strings.Join(subTopics, ", "))
|
||||
var jPayload interface{}
|
||||
err := json.Unmarshal([]byte(message.Payload), &jPayload)
|
||||
if err != nil {
|
||||
self.Lost("Unable to unmarshal payload", err, message)
|
||||
return
|
||||
}
|
||||
var measurement database.Measurement
|
||||
measurement.Time = time.Now()
|
||||
measurement.Application = self.application
|
||||
|
||||
device, err1 := self.ExtractionHelper(subTopics, jPayload, self.deviceSelector, self.deviceJsonpath)
|
||||
if err1 != nil {
|
||||
self.Lost("Device extraction failed", err1, message)
|
||||
return
|
||||
}
|
||||
log.Printf("device: %s", device)
|
||||
subTopics := strings.Split(message.Topic, "/")
|
||||
log.Printf("Subtopics: %s", strings.Join(subTopics, ", "))
|
||||
var jPayload interface{}
|
||||
err := json.Unmarshal([]byte(message.Payload), &jPayload)
|
||||
if err != nil {
|
||||
self.Lost("Unable to unmarshal payload", err, message)
|
||||
return
|
||||
}
|
||||
|
||||
value, err2 := self.ExtractionHelper(subTopics, jPayload, self.valueSelector, self.valueJsonpath)
|
||||
if err2 != nil {
|
||||
self.Lost("Value extraction failed", err2, message)
|
||||
return
|
||||
}
|
||||
device, err1 := self.ExtractionHelper(subTopics, jPayload, self.deviceSelector, self.deviceJsonpath)
|
||||
if err1 != nil {
|
||||
self.Lost("Device extraction failed", err1, message)
|
||||
return
|
||||
}
|
||||
log.Printf("device: %s", device)
|
||||
|
||||
unit, err3 := self.ExtractionHelper(subTopics, jPayload, self.unitSelector, self.unitJsonpath)
|
||||
if err3 != nil {
|
||||
self.Lost("Unit extraction failed", err3, message)
|
||||
return
|
||||
}
|
||||
value, err2 := self.ExtractionHelper(subTopics, jPayload, self.valueSelector, self.valueJsonpath)
|
||||
if err2 != nil {
|
||||
self.Lost("Value extraction failed", err2, message)
|
||||
return
|
||||
}
|
||||
|
||||
measurement.Device = device
|
||||
unit, err3 := self.ExtractionHelper(subTopics, jPayload, self.unitSelector, self.unitJsonpath)
|
||||
if err3 != nil {
|
||||
self.Lost("Unit extraction failed", err3, message)
|
||||
return
|
||||
}
|
||||
|
||||
var variable database.VariableType
|
||||
variable.Label = ""
|
||||
variable.Variable = ""
|
||||
variable.Unit = unit
|
||||
variable.Value = value
|
||||
measurement.Values = make(map[string]database.VariableType)
|
||||
measurement.Values["Value"] = variable
|
||||
measurement.Device = device.(string)
|
||||
|
||||
log.Printf("Prepared measurement item: %s", measurement)
|
||||
self.dbh.StoreMeasurement(&measurement)
|
||||
self.S()
|
||||
var variable database.VariableType
|
||||
variable.Label = ""
|
||||
variable.Variable = ""
|
||||
variable.Unit = unit.(string)
|
||||
variable.Value = value
|
||||
measurement.Values = make(map[string]database.VariableType)
|
||||
measurement.Values["Value"] = variable
|
||||
|
||||
log.Printf("Prepared measurement item: %s", measurement)
|
||||
self.dbh.StoreMeasurement(&measurement)
|
||||
self.S()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user