diff --git a/.gitignore b/.gitignore index e69de29..06a0374 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,2 @@ +config.json +src/udi/udi diff --git a/src/udi/config/config.go b/src/udi/config/config.go new file mode 100644 index 0000000..efbd31c --- /dev/null +++ b/src/udi/config/config.go @@ -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) + } +} + diff --git a/src/udi/main.go b/src/udi/main.go index 1fa815f..a9f18fc 100644 --- a/src/udi/main.go +++ b/src/udi/main.go @@ -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() diff --git a/src/udi/mqtt/mqtt.go b/src/udi/mqtt/mqtt.go index a6d21b1..77ffc3e 100644 --- a/src/udi/mqtt/mqtt.go +++ b/src/udi/mqtt/mqtt.go @@ -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, } diff --git a/src/udi/udi b/src/udi/udi index 6d762d7..f0a1783 100755 Binary files a/src/udi/udi and b/src/udi/udi differ