Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
ff659b648c
|
|||
a5209dad8f
|
|||
00a9eceea8
|
|||
1d947b7676
|
|||
57ebd76849
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ src/udi/udi
|
|||||||
src/udi/migrate_schema
|
src/udi/migrate_schema
|
||||||
tmp/
|
tmp/
|
||||||
ENVDB
|
ENVDB
|
||||||
|
ENVDB.cluster
|
||||||
|
@ -1,3 +1,16 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: PersistentVolumeClaim
|
||||||
|
metadata:
|
||||||
|
name: udi-archive
|
||||||
|
namespace: udi
|
||||||
|
spec:
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteOnce
|
||||||
|
storageClassName: nfs-client
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: 50Gi
|
||||||
|
---
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
@ -5,6 +18,8 @@ metadata:
|
|||||||
namespace: udi
|
namespace: udi
|
||||||
labels:
|
labels:
|
||||||
app: udi
|
app: udi
|
||||||
|
annotations:
|
||||||
|
secret.reloader.stakater.com/reload: "udi-conf,udi-db-cred"
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
selector:
|
selector:
|
||||||
@ -24,3 +39,14 @@ spec:
|
|||||||
secretKeyRef:
|
secretKeyRef:
|
||||||
name: udi-conf
|
name: udi-conf
|
||||||
key: UDI_CONF
|
key: UDI_CONF
|
||||||
|
envFrom:
|
||||||
|
- secretRef:
|
||||||
|
name: udi-db-cred
|
||||||
|
volumeMounts:
|
||||||
|
- mountPath: /archive
|
||||||
|
name: udi-archive
|
||||||
|
volumes:
|
||||||
|
- name: udi-archive
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: udi-archive
|
||||||
|
|
||||||
|
47
deployment/load-config.sh
Executable file
47
deployment/load-config.sh
Executable file
@ -0,0 +1,47 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
FILE=$1
|
||||||
|
if [ "$FILE" = "" ]; then
|
||||||
|
echo "give config file to load as first argument"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
SECRET_NAME=$2
|
||||||
|
if [ "$SECRET_NAME" = "" ]; then
|
||||||
|
echo "give secret name to create/modify as second argument"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
NAMESPACE=$3
|
||||||
|
if [ "$NAMESPACE" = "" ]; then
|
||||||
|
echo "give namespace as third argument"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
kubectl create secret generic $SECRET_NAME \
|
||||||
|
--from-literal=UDI_CONF="`cat $FILE`" \
|
||||||
|
-n $NAMESPACE \
|
||||||
|
--dry-run=client \
|
||||||
|
-o yaml \
|
||||||
|
--save-config | \
|
||||||
|
kubectl apply -f -
|
||||||
|
|
||||||
|
. ~/Workspace/MyKubernetesEnv/ENVDB
|
||||||
|
DATABASE=udi
|
||||||
|
LOGIN=udi
|
||||||
|
PASSWORD=`openssl rand -base64 24`
|
||||||
|
psql <<EOF
|
||||||
|
ALTER USER $LOGIN WITH PASSWORD '$PASSWORD';
|
||||||
|
GRANT ALL PRIVILEGES ON DATABASE $DATABASE TO $LOGIN;
|
||||||
|
COMMIT;
|
||||||
|
EOF
|
||||||
|
|
||||||
|
kubectl create secret generic udi-db-cred \
|
||||||
|
--dry-run=client \
|
||||||
|
-o yaml \
|
||||||
|
--save-config \
|
||||||
|
--from-literal=PGUSER="$LOGIN" \
|
||||||
|
--from-literal=PGHOST="timescaledb.database.svc.cluster.local" \
|
||||||
|
--from-literal=PGPASSWORD="$PASSWORD" \
|
||||||
|
--from-literal=PGSSLMODE="require" \
|
||||||
|
--from-literal=PGDATABASE="$DATABASE" | \
|
||||||
|
kubectl apply -f - -n $NAMESPACE
|
||||||
|
|
39
queries/hottis.sql
Normal file
39
queries/hottis.sql
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
create or replace view pv_power_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'PowerActive'->>'value' as float) as power,
|
||||||
|
values->'Status'->>'status' as status,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'PV';
|
||||||
|
|
||||||
|
create or replace view pv_total_import_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Power'->>'value' as float) as power,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'Power' and
|
||||||
|
device = 'Total' and
|
||||||
|
attributes->>'Status' = 'Ok';
|
||||||
|
|
||||||
|
create or replace view power_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Power'->>'value' as float) as power,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'Power' and
|
||||||
|
attributes->>'Status' = 'Ok';
|
||||||
|
|
||||||
|
create or replace view temperature_heating_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Value'->>'value' as float) as temperature,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'Temperature Heating';
|
||||||
|
|
||||||
|
create or replace view gas_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Volume'->>'value' as float) as volume,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'Gas' and
|
||||||
|
attributes->>'Status' = 'Ok';
|
@ -17,10 +17,11 @@ type ConfigT struct {
|
|||||||
TlsEnable string `json:"tlsEnable"`
|
TlsEnable string `json:"tlsEnable"`
|
||||||
} `json:"mqtt"`
|
} `json:"mqtt"`
|
||||||
TopicMappings []struct {
|
TopicMappings []struct {
|
||||||
|
Id string `json:"id"`
|
||||||
Topics []string `json:"topics"`
|
Topics []string `json:"topics"`
|
||||||
Handler string `json:"handler"`
|
Handler string `json:"handler"`
|
||||||
|
Config HandlerConfigT `json:"config"`
|
||||||
} `json:"topicMappings"`
|
} `json:"topicMappings"`
|
||||||
Handlers map[string]HandlerConfigT `json:"handlers"`
|
|
||||||
Archiver struct {
|
Archiver struct {
|
||||||
Dir string `json:"dir"`
|
Dir string `json:"dir"`
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import "udi/handlers/ttn"
|
|||||||
import "udi/handlers/iot"
|
import "udi/handlers/iot"
|
||||||
import "udi/handlers/pv"
|
import "udi/handlers/pv"
|
||||||
import "udi/handlers/mbgw3"
|
import "udi/handlers/mbgw3"
|
||||||
|
import "udi/handlers/sve"
|
||||||
|
|
||||||
|
|
||||||
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
||||||
@ -20,10 +21,37 @@ var archiverChannel chan handler.MessageT = make(chan handler.MessageT, 100)
|
|||||||
func InitDispatcher() {
|
func InitDispatcher() {
|
||||||
log.Printf("Initializing dispatcher")
|
log.Printf("Initializing dispatcher")
|
||||||
go archiver()
|
go archiver()
|
||||||
handlerMap["TTN"] = ttn.NewTTNHandler()
|
|
||||||
handlerMap["IoT"] = iot.NewIoTHandler()
|
for _, mapping := range config.Config.TopicMappings {
|
||||||
handlerMap["PV"] = pv.NewPvHandler(config.Config.Handlers["PV"])
|
log.Printf("Trying to initialize %s", mapping)
|
||||||
handlerMap["MBGW3"] = mbgw3.NewMbgw3Handler(config.Config.Handlers["MBGW3"])
|
|
||||||
|
var factory interface{}
|
||||||
|
switch mapping.Handler {
|
||||||
|
case "TTN":
|
||||||
|
factory = ttn.NewTTNHandler
|
||||||
|
case "IoT":
|
||||||
|
factory = iot.NewIoTHandler
|
||||||
|
case "PV":
|
||||||
|
factory = pv.NewPvHandler
|
||||||
|
case "MBGW3":
|
||||||
|
factory = mbgw3.NewMbgw3Handler
|
||||||
|
case "SVE":
|
||||||
|
factory = sve.NewSveHandler
|
||||||
|
default:
|
||||||
|
factory = nil
|
||||||
|
log.Printf("No handler %s found, ignore mapping", mapping.Handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn, ok := factory.(func(config.HandlerConfigT) handler.Handler)
|
||||||
|
if ! ok {
|
||||||
|
log.Println("Typ Assertion failed")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
handler := fn(mapping.Config)
|
||||||
|
handlerMap[mapping.Id] = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("handlerMap: %s", handlerMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
func storeMessage(filename string, item handler.MessageT) {
|
func storeMessage(filename string, item handler.MessageT) {
|
||||||
@ -83,13 +111,13 @@ func handleMessage(message handler.MessageT) {
|
|||||||
for _, subscribedTopic := range mapping.Topics {
|
for _, subscribedTopic := range mapping.Topics {
|
||||||
// log.Printf("Testing %s in %s", message.Topic, subscribedTopic)
|
// log.Printf("Testing %s in %s", message.Topic, subscribedTopic)
|
||||||
if mqtt.TopicMatchesSubscription(message.Topic, subscribedTopic) {
|
if mqtt.TopicMatchesSubscription(message.Topic, subscribedTopic) {
|
||||||
log.Printf("Handle message in handler %s", mapping.Handler)
|
log.Printf("Handle message in handler %s", mapping.Id)
|
||||||
handler, exists := handlerMap[mapping.Handler]
|
handler, exists := handlerMap[mapping.Id]
|
||||||
if exists {
|
if exists {
|
||||||
handler.Handle(message)
|
handler.Handle(message)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Handler %s not found, message %s is lost", mapping.Handler, message)
|
log.Printf("Handler %s not found, message %s is lost", mapping.Id, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ type MessageT struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
|
GetId() string
|
||||||
Handle(MessageT)
|
Handle(MessageT)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package iot
|
package iot
|
||||||
|
|
||||||
import "log"
|
import "log"
|
||||||
|
import "fmt"
|
||||||
import "udi/handlers/handler"
|
import "udi/handlers/handler"
|
||||||
|
|
||||||
var idSeq int = 0
|
var idSeq int = 0
|
||||||
@ -9,7 +10,7 @@ type IoTHandler struct {
|
|||||||
id int
|
id int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIoTHandler() *IoTHandler {
|
func NewIoTHandler() handler.Handler {
|
||||||
t := &IoTHandler {
|
t := &IoTHandler {
|
||||||
id: idSeq,
|
id: idSeq,
|
||||||
}
|
}
|
||||||
@ -17,6 +18,10 @@ func NewIoTHandler() *IoTHandler {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *IoTHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("IoT%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *IoTHandler) Handle(message handler.MessageT) {
|
func (self *IoTHandler) Handle(message handler.MessageT) {
|
||||||
log.Printf("Handler IoT %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
log.Printf("Handler IoT %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
//"reflect"
|
//"reflect"
|
||||||
"time"
|
"time"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"fmt"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
"udi/handlers/handler"
|
||||||
@ -30,7 +31,7 @@ type Observation struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func NewMbgw3Handler(config config.HandlerConfigT) *Mbgw3Handler {
|
func NewMbgw3Handler(config config.HandlerConfigT) handler.Handler {
|
||||||
t := &Mbgw3Handler {
|
t := &Mbgw3Handler {
|
||||||
id: idSeq,
|
id: idSeq,
|
||||||
}
|
}
|
||||||
@ -39,6 +40,10 @@ func NewMbgw3Handler(config config.HandlerConfigT) *Mbgw3Handler {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *Mbgw3Handler) GetId() string {
|
||||||
|
return fmt.Sprintf("MBGW3%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *Mbgw3Handler) Handle(message handler.MessageT) {
|
func (self *Mbgw3Handler) Handle(message handler.MessageT) {
|
||||||
// log.Printf("Handler MBGW3 %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
// log.Printf("Handler MBGW3 %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
"fmt"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
"udi/handlers/handler"
|
||||||
@ -39,7 +40,7 @@ type PvValue struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func NewPvHandler(config config.HandlerConfigT) *PvHandler {
|
func NewPvHandler(config config.HandlerConfigT) handler.Handler {
|
||||||
t := &PvHandler {
|
t := &PvHandler {
|
||||||
id: idSeq,
|
id: idSeq,
|
||||||
}
|
}
|
||||||
@ -48,6 +49,10 @@ func NewPvHandler(config config.HandlerConfigT) *PvHandler {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *PvHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("PV%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *PvHandler) Handle(message handler.MessageT) {
|
func (self *PvHandler) Handle(message handler.MessageT) {
|
||||||
//log.Printf("Handler PV %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
//log.Printf("Handler PV %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
|
|
||||||
|
221
src/udi/handlers/sve/sve.go
Normal file
221
src/udi/handlers/sve/sve.go
Normal file
@ -0,0 +1,221 @@
|
|||||||
|
package sve
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"regexp"
|
||||||
|
"fmt"
|
||||||
|
"udi/config"
|
||||||
|
"udi/handlers/handler"
|
||||||
|
"udi/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
var idSeq int = 0
|
||||||
|
|
||||||
|
type SingleValueExtractorHandler struct {
|
||||||
|
id int
|
||||||
|
ready bool
|
||||||
|
config localConfig
|
||||||
|
payloadRegex *regexp.Regexp
|
||||||
|
dbh *database.DatabaseHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
const TOPIC_SEL = "topic"
|
||||||
|
const PAYLOAD_SEL = "payload"
|
||||||
|
const PAYLOAD_FULL_SEL = "payload-full"
|
||||||
|
const CONSTANT_SEL = "constant"
|
||||||
|
|
||||||
|
type localConfig struct {
|
||||||
|
application string
|
||||||
|
deviceFrom string
|
||||||
|
devicePart int
|
||||||
|
device string
|
||||||
|
valueFrom string
|
||||||
|
valuePart int
|
||||||
|
unitFrom string
|
||||||
|
unitPart int
|
||||||
|
unit string
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func NewSveHandler(config config.HandlerConfigT) handler.Handler {
|
||||||
|
t := &SingleValueExtractorHandler {
|
||||||
|
id: idSeq,
|
||||||
|
ready: false,
|
||||||
|
}
|
||||||
|
idSeq += 1
|
||||||
|
|
||||||
|
var localConfig localConfig
|
||||||
|
if config.Attributes["application"] == "" {
|
||||||
|
log.Println("Error: application not configured")
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
localConfig.application = config.Attributes["application"]
|
||||||
|
|
||||||
|
payloadRegex := config.Attributes["payloadRegex"]
|
||||||
|
if payloadRegex != "" {
|
||||||
|
t.payloadRegex = regexp.MustCompile(payloadRegex)
|
||||||
|
} else {
|
||||||
|
t.payloadRegex = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Attributes["deviceFrom"] != TOPIC_SEL && config.Attributes["deviceFrom"] != PAYLOAD_SEL && config.Attributes["deviceFrom"] != CONSTANT_SEL {
|
||||||
|
log.Printf("Error: invalid value %s for deviceFrom", config.Attributes["deviceFrom"])
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
localConfig.deviceFrom = config.Attributes["deviceFrom"]
|
||||||
|
|
||||||
|
devicePart, err1 := strconv.Atoi(config.Attributes["devicePart"])
|
||||||
|
if err1 != nil {
|
||||||
|
log.Printf("Error: unable to convert devicePart to number: %s", err1)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
localConfig.devicePart = devicePart
|
||||||
|
|
||||||
|
// empty device is valid
|
||||||
|
localConfig.device = config.Attributes["device"]
|
||||||
|
|
||||||
|
if config.Attributes["valueFrom"] != TOPIC_SEL && config.Attributes["valueFrom"] != PAYLOAD_SEL && config.Attributes["valueFrom"] != PAYLOAD_FULL_SEL {
|
||||||
|
log.Printf("Error: invalid value %s for valueFrom", config.Attributes["valueFrom"])
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
localConfig.valueFrom = config.Attributes["valueFrom"]
|
||||||
|
|
||||||
|
if config.Attributes["valueFrom"] != PAYLOAD_FULL_SEL {
|
||||||
|
valuePart, err2 := strconv.Atoi(config.Attributes["valuePart"])
|
||||||
|
if err2 != nil {
|
||||||
|
log.Printf("Error: unable to convert valuePart to number: %s", err2)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
localConfig.valuePart = valuePart
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.Attributes["unitFrom"] != TOPIC_SEL && config.Attributes["unitFrom"] != PAYLOAD_SEL && config.Attributes["unitFrom"] != CONSTANT_SEL {
|
||||||
|
log.Printf("Error: invalid value %s for unitFrom", config.Attributes["unitFrom"])
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
localConfig.unitFrom = config.Attributes["unitFrom"]
|
||||||
|
|
||||||
|
if config.Attributes["unitFrom"] != CONSTANT_SEL {
|
||||||
|
unitPart, err3 := strconv.Atoi(config.Attributes["unitPart"])
|
||||||
|
if err3 != nil {
|
||||||
|
log.Printf("Error: unable to convert unitPart to number: %s", err3)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
localConfig.unitPart = unitPart
|
||||||
|
}
|
||||||
|
|
||||||
|
// empty unit is valid
|
||||||
|
localConfig.unit = config.Attributes["unit"]
|
||||||
|
|
||||||
|
t.config = localConfig
|
||||||
|
|
||||||
|
t.ready = true
|
||||||
|
t.dbh = database.NewDatabaseHandle(config.DatabaseConnStr)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *SingleValueExtractorHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("SVE%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func lost(msg string, message handler.MessageT) {
|
||||||
|
log.Printf("Error: %s, message %s is lost", msg, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *SingleValueExtractorHandler) Handle(message handler.MessageT) {
|
||||||
|
if ! self.ready {
|
||||||
|
log.Println("Handler is not marked as ready, message %s is lost", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//log.Printf("Handler SingleValueExtractor %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
|
|
||||||
|
var measurement database.Measurement
|
||||||
|
measurement.Time = time.Now()
|
||||||
|
measurement.Application = self.config.application
|
||||||
|
|
||||||
|
subTopics := strings.Split(message.Topic, "/")
|
||||||
|
//log.Printf("Subtopics: %s", strings.Join(subTopics, ", "))
|
||||||
|
|
||||||
|
var payloadMatches []string
|
||||||
|
if self.payloadRegex != nil {
|
||||||
|
payloadMatches = self.payloadRegex.FindStringSubmatch(message.Payload)
|
||||||
|
//log.Printf("Matches: %s", strings.Join(payloadMatches, ", "))
|
||||||
|
}
|
||||||
|
|
||||||
|
switch self.config.deviceFrom {
|
||||||
|
case TOPIC_SEL:
|
||||||
|
if self.config.devicePart >= len(subTopics) {
|
||||||
|
lost("devicePart out of range", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
measurement.Device = subTopics[self.config.devicePart]
|
||||||
|
case PAYLOAD_SEL:
|
||||||
|
if self.payloadRegex == nil {
|
||||||
|
lost("no payloadRegex defined, devicePart can't be used", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if self.config.devicePart >= len(subTopics) {
|
||||||
|
lost("devicePart out of range", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
measurement.Device = payloadMatches[self.config.devicePart]
|
||||||
|
case CONSTANT_SEL:
|
||||||
|
measurement.Device = self.config.device
|
||||||
|
}
|
||||||
|
|
||||||
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
|
var variable database.VariableType
|
||||||
|
variable.Label = ""
|
||||||
|
variable.Variable = ""
|
||||||
|
|
||||||
|
switch self.config.valueFrom {
|
||||||
|
case TOPIC_SEL:
|
||||||
|
if self.config.valuePart >= len(subTopics) {
|
||||||
|
lost("valuePart out of range", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
variable.Value = subTopics[self.config.valuePart]
|
||||||
|
case PAYLOAD_SEL:
|
||||||
|
if self.payloadRegex == nil {
|
||||||
|
lost("no payloadRegex defined, valuePart can't be used", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if self.config.valuePart >= len(subTopics) {
|
||||||
|
lost("valuePart out of range", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
variable.Value = payloadMatches[self.config.valuePart]
|
||||||
|
case PAYLOAD_FULL_SEL:
|
||||||
|
variable.Value = message.Payload
|
||||||
|
}
|
||||||
|
|
||||||
|
switch self.config.unitFrom {
|
||||||
|
case TOPIC_SEL:
|
||||||
|
if self.config.unitPart >= len(subTopics) {
|
||||||
|
lost("unitPart out of range", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
variable.Unit = subTopics[self.config.unitPart]
|
||||||
|
case PAYLOAD_SEL:
|
||||||
|
if self.payloadRegex == nil {
|
||||||
|
lost("no payloadRegex defined, unitPart can't be used", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if self.config.unitPart >= len(subTopics) {
|
||||||
|
lost("unitPart out of range", message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
variable.Unit = payloadMatches[self.config.unitPart]
|
||||||
|
case CONSTANT_SEL:
|
||||||
|
variable.Unit = self.config.unit
|
||||||
|
}
|
||||||
|
|
||||||
|
measurement.Values["Value"] = variable
|
||||||
|
|
||||||
|
//log.Printf("Prepared measurement item: %s", measurement)
|
||||||
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
|||||||
package ttn
|
package ttn
|
||||||
|
|
||||||
import "log"
|
import "log"
|
||||||
|
import "fmt"
|
||||||
import "udi/handlers/handler"
|
import "udi/handlers/handler"
|
||||||
|
|
||||||
var idSeq int = 0
|
var idSeq int = 0
|
||||||
@ -9,7 +10,7 @@ type TTNHandler struct {
|
|||||||
id int
|
id int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTTNHandler() *TTNHandler {
|
func NewTTNHandler() handler.Handler {
|
||||||
t := &TTNHandler {
|
t := &TTNHandler {
|
||||||
id: idSeq,
|
id: idSeq,
|
||||||
}
|
}
|
||||||
@ -17,6 +18,10 @@ func NewTTNHandler() *TTNHandler {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *TTNHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("TTN%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *TTNHandler) Handle(message handler.MessageT) {
|
func (self *TTNHandler) Handle(message handler.MessageT) {
|
||||||
log.Printf("Handler TTN %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
log.Printf("Handler TTN %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user