fix configuration
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2023-12-05 16:21:30 +01:00
parent 00a9eceea8
commit a5209dad8f
11 changed files with 94 additions and 15 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ src/udi/udi
src/udi/migrate_schema
tmp/
ENVDB
ENVDB.cluster

View File

@ -37,6 +37,9 @@ spec:
secretKeyRef:
name: udi-conf
key: UDI_CONF
envFrom:
- secretRef:
name: udi-db-cred
volumeMounts:
- mountPath: /archive
name: udi-archive

View File

@ -23,3 +23,25 @@ kubectl create secret generic $SECRET_NAME \
-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

View File

@ -17,10 +17,11 @@ type ConfigT struct {
TlsEnable string `json:"tlsEnable"`
} `json:"mqtt"`
TopicMappings []struct {
Id string `json:"id"`
Topics []string `json:"topics"`
Handler string `json:"handler"`
Config HandlerConfigT `json:"config"`
} `json:"topicMappings"`
Handlers map[string]HandlerConfigT `json:"handlers"`
Archiver struct {
Dir string `json:"dir"`
}

View File

@ -21,11 +21,37 @@ var archiverChannel chan handler.MessageT = make(chan handler.MessageT, 100)
func InitDispatcher() {
log.Printf("Initializing dispatcher")
go archiver()
handlerMap["TTN"] = ttn.NewTTNHandler()
handlerMap["IoT"] = iot.NewIoTHandler()
handlerMap["PV"] = pv.NewPvHandler(config.Config.Handlers["PV"])
handlerMap["MBGW3"] = mbgw3.NewMbgw3Handler(config.Config.Handlers["MBGW3"])
handlerMap["SVE"] = sve.NewSveHandler(config.Config.Handlers["SVE"])
for _, mapping := range config.Config.TopicMappings {
log.Printf("Trying to initialize %s", mapping)
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) {
@ -85,13 +111,13 @@ func handleMessage(message handler.MessageT) {
for _, subscribedTopic := range mapping.Topics {
// log.Printf("Testing %s in %s", message.Topic, subscribedTopic)
if mqtt.TopicMatchesSubscription(message.Topic, subscribedTopic) {
log.Printf("Handle message in handler %s", mapping.Handler)
handler, exists := handlerMap[mapping.Handler]
log.Printf("Handle message in handler %s", mapping.Id)
handler, exists := handlerMap[mapping.Id]
if exists {
handler.Handle(message)
return
} 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)
}
}
}

View File

@ -9,6 +9,7 @@ type MessageT struct {
}
type Handler interface {
GetId() string
Handle(MessageT)
}

View File

@ -1,6 +1,7 @@
package iot
import "log"
import "fmt"
import "udi/handlers/handler"
var idSeq int = 0
@ -9,7 +10,7 @@ type IoTHandler struct {
id int
}
func NewIoTHandler() *IoTHandler {
func NewIoTHandler() handler.Handler {
t := &IoTHandler {
id: idSeq,
}
@ -17,6 +18,10 @@ func NewIoTHandler() *IoTHandler {
return t
}
func (self *IoTHandler) GetId() string {
return fmt.Sprintf("IoT%d", self.id)
}
func (self *IoTHandler) Handle(message handler.MessageT) {
log.Printf("Handler IoT %d processing %s -> %s", self.id, message.Topic, message.Payload)
}

View File

@ -5,6 +5,7 @@ import (
//"reflect"
"time"
"strconv"
"fmt"
"encoding/json"
"udi/config"
"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 {
id: idSeq,
}
@ -39,6 +40,10 @@ func NewMbgw3Handler(config config.HandlerConfigT) *Mbgw3Handler {
return t
}
func (self *Mbgw3Handler) GetId() string {
return fmt.Sprintf("MBGW3%d", self.id)
}
func (self *Mbgw3Handler) Handle(message handler.MessageT) {
// log.Printf("Handler MBGW3 %d processing %s -> %s", self.id, message.Topic, message.Payload)

View File

@ -4,6 +4,7 @@ import (
"log"
"reflect"
"time"
"fmt"
"encoding/json"
"udi/config"
"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 {
id: idSeq,
}
@ -48,6 +49,10 @@ func NewPvHandler(config config.HandlerConfigT) *PvHandler {
return t
}
func (self *PvHandler) GetId() string {
return fmt.Sprintf("PV%d", self.id)
}
func (self *PvHandler) Handle(message handler.MessageT) {
//log.Printf("Handler PV %d processing %s -> %s", self.id, message.Topic, message.Payload)

View File

@ -6,6 +6,7 @@ import (
"strconv"
"strings"
"regexp"
"fmt"
"udi/config"
"udi/handlers/handler"
"udi/database"
@ -38,7 +39,7 @@ type localConfig struct {
}
func NewSveHandler(config config.HandlerConfigT) *SingleValueExtractorHandler {
func NewSveHandler(config config.HandlerConfigT) handler.Handler {
t := &SingleValueExtractorHandler {
id: idSeq,
ready: false,
@ -111,6 +112,10 @@ func NewSveHandler(config config.HandlerConfigT) *SingleValueExtractorHandler {
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)
}

View File

@ -1,6 +1,7 @@
package ttn
import "log"
import "fmt"
import "udi/handlers/handler"
var idSeq int = 0
@ -9,7 +10,7 @@ type TTNHandler struct {
id int
}
func NewTTNHandler() *TTNHandler {
func NewTTNHandler() handler.Handler {
t := &TTNHandler {
id: idSeq,
}
@ -17,6 +18,10 @@ func NewTTNHandler() *TTNHandler {
return t
}
func (self *TTNHandler) GetId() string {
return fmt.Sprintf("TTN%d", self.id)
}
func (self *TTNHandler) Handle(message handler.MessageT) {
log.Printf("Handler TTN %d processing %s -> %s", self.id, message.Topic, message.Payload)
}