From c0d315a409750b61a6711e6906f6af68b7485322 Mon Sep 17 00:00:00 2001 From: Joel Schmid Date: Sat, 20 Mar 2021 15:31:23 +0100 Subject: [PATCH] allow unregistered sensors in config --- config/config.go | 12 ++++++++++++ run_default.ps1 | 2 ++ weathersource/mqtt-source.go | 5 +++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index aa5ab69..78e39d9 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "os" + "strconv" ) // const influx stuff @@ -15,6 +16,9 @@ const mqttURL = "tcp://default-address.com:1883" const mqttTopic = "sensor/#" const defaultLocation = "default-location" +//other config stuff +const allowUnregisteredSensors = false + //influx config func GetInfluxUrl() string { return getVariableWithDefault("WEATHER-API-INFLUX_URL", influxURL) @@ -41,6 +45,14 @@ func GetMqttTopic() string { return getVariableWithDefault("WEATHER-API-MQTT_TOPIC", mqttTopic) } +func AllowUnregisteredSensors() bool { + allow, err := strconv.ParseBool(os.Getenv("WEATHER-API-ALLOW_UNREGISTERED_SENSORS")) + if err != nil { + return allowUnregisteredSensors + } + return allow +} + //helper func getVariableWithDefault(variableKey, defaultValue string) string { variable := os.Getenv(variableKey) diff --git a/run_default.ps1 b/run_default.ps1 index 6ef9ae9..492013a 100644 --- a/run_default.ps1 +++ b/run_default.ps1 @@ -10,5 +10,7 @@ Set-Item -Path "Env:WEATHER-API-INFLUX_BUCKET" -Value "default-bucket" Set-Item -Path "Env:WEATHER-API-MQTT_URL" -Value "tcp://default-address.com:1883" Set-Item -Path "Env:WEATHER-API-MQTT_TOPIC" -Value "sensor/#" +Set-Item -Path "Env:WEATHER-API-ALLOW_UNREGISTERED_SENSORS" -Value "true" + #start application Start-Process "main.exe" -Wait -NoNewWindow diff --git a/weathersource/mqtt-source.go b/weathersource/mqtt-source.go index fc022ab..829744e 100644 --- a/weathersource/mqtt-source.go +++ b/weathersource/mqtt-source.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" "time" + "weather-data/config" "weather-data/storage" mqtt "github.com/eclipse/paho.mqtt.golang" @@ -71,8 +72,8 @@ func (source *mqttWeatherSource) mqttMessageHandler() mqtt.MessageHandler { return } - if !source.sensorRegistry.ExistSensorId(sensorId) { - fmt.Println("sensor not registered") + if !config.AllowUnregisteredSensors() && !source.sensorRegistry.ExistSensorId(sensorId) { + fmt.Println("sensor have to be registered:", sensorId) return }