allow unregistered sensors in config
This commit is contained in:
parent
9cc656e937
commit
c0d315a409
3 changed files with 17 additions and 2 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue