This commit is contained in:
2023-11-27 11:56:00 +01:00
parent 72b1b246be
commit faef995b6f
5 changed files with 46 additions and 5 deletions

2
.gitignore vendored
View File

@ -0,0 +1,2 @@
config.json
src/udi/udi

35
src/udi/config/config.go Normal file
View File

@ -0,0 +1,35 @@
package config
import "encoding/json"
import "log"
import "os"
type ConfigT struct {
Mqtt struct {
Broker string
Username string
Password string
TlsEnable string
}
TopicMappings []struct {
Topics []string
Plugin string
}
Plugins []struct {
Name string
DatabaseConnStr string
Attributes map[string]interface{}
}
}
var Config ConfigT
func LoadConfiguration() {
err := json.Unmarshal([]byte(os.Getenv("UDI_CONF")), &Config)
if err != nil {
log.Fatalf("Unable to parse configuration: %s", err)
}
}

View File

@ -4,6 +4,7 @@ import "log"
import "os"
import "os/signal"
import um "udi/mqtt"
import "udi/config"
func inputDispatcher() {
@ -21,6 +22,8 @@ func main() {
log.Println("UDI starting")
config.LoadConfiguration()
go inputDispatcher()
um.StartMqttClient()

View File

@ -8,6 +8,7 @@ import "strings"
import MQTT "github.com/eclipse/paho.mqtt.golang"
import "github.com/google/uuid"
import "crypto/tls"
import "udi/config"
type Message struct {
Topic string
@ -71,7 +72,7 @@ func outputDispatcher(client MQTT.Client) {
}
func StartMqttClient() {
broker := os.Getenv("MQTT_BROKER")
broker := config.Config.Mqtt.Broker
if broker == "" {
log.Fatal("No broker given, set env var MQTT_BROKER")
}
@ -88,19 +89,19 @@ func StartMqttClient() {
SetReconnectingHandler(onReconnecting).
SetConnectRetry(true)
username := os.Getenv("MQTT_USERNAME")
username := config.Config.Mqtt.Username
if username != "" {
opts.SetUsername(username)
}
password := os.Getenv("MQTT_PASSWORD")
password := config.Config.Mqtt.Password
if password != "" {
opts.SetPassword(password)
}
enableTls := os.Getenv("MQTT_ENABLE_TLS")
enableTls := config.Config.Mqtt.TlsEnable
if enableTls == "true" {
log.Println("Enableing TLS connection")
log.Println("Enabling TLS connection")
tlsConfig := &tls.Config {
InsecureSkipVerify: true,
}

Binary file not shown.