mqtt authentication
This commit is contained in:
parent
a53d1eabe5
commit
c1412dffb2
4 changed files with 57 additions and 12 deletions
|
@ -16,6 +16,9 @@ const influxURL = "https://influx.default-address.com"
|
|||
const mqttURL = "tcp://default-address.com:1883"
|
||||
const mqttTopic = "sensor/#"
|
||||
const defaultLocation = "default-location"
|
||||
const mqttUser = "weather-api"
|
||||
const mqttPassword = "weather-api"
|
||||
const useAnonymousMqttAuthentication = false
|
||||
const mqttPublishInterval = time.Second
|
||||
const mqttMinDistToLastValue = 250 * time.Millisecond
|
||||
|
||||
|
@ -48,6 +51,18 @@ func GetMqttTopic() string {
|
|||
return getVariableWithDefault("WEATHER-API-MQTT_TOPIC", mqttTopic)
|
||||
}
|
||||
|
||||
func GetMqttUser() string {
|
||||
return getVariableWithDefault("WEATHER-API-MQTT_USER", mqttTopic)
|
||||
}
|
||||
|
||||
func GetMqttPassword() string {
|
||||
return getVariableWithDefault("WEATHER-API-MQTT_PASSWORD", mqttTopic)
|
||||
}
|
||||
|
||||
func UseAnonymousMqttAuthentication() bool {
|
||||
return getVariableWithDefaultBool("WEATHER-API-ANONYMOUS_MQTT_AUTHENTICATION", useAnonymousMqttAuthentication)
|
||||
}
|
||||
|
||||
func MqttPublishInterval() time.Duration {
|
||||
interval, err := strconv.ParseInt(os.Getenv("WEATHER-API-MQTT_PUBLISH_INTERVAL"), 10, 64)
|
||||
if err != nil {
|
||||
|
@ -66,11 +81,7 @@ func MqttMinDistToLastValue() time.Duration {
|
|||
|
||||
//common config
|
||||
func AllowUnregisteredSensors() bool {
|
||||
allow, err := strconv.ParseBool(os.Getenv("WEATHER-API-ALLOW_UNREGISTERED_SENSORS"))
|
||||
if err != nil {
|
||||
return allowUnregisteredSensors
|
||||
}
|
||||
return allow
|
||||
return getVariableWithDefaultBool("WEATHER-API-ALLOW_UNREGISTERED_SENSORS", allowUnregisteredSensors)
|
||||
}
|
||||
|
||||
//helper
|
||||
|
@ -81,3 +92,11 @@ func getVariableWithDefault(variableKey, defaultValue string) string {
|
|||
}
|
||||
return variable
|
||||
}
|
||||
|
||||
func getVariableWithDefaultBool(variableKey string, defaultValue bool) bool {
|
||||
ok, err := strconv.ParseBool(os.Getenv(variableKey))
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
|
18
main.go
18
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"weather-data/api"
|
||||
"weather-data/config"
|
||||
|
@ -33,13 +34,22 @@ func main() {
|
|||
defer weatherStorage.Close()
|
||||
|
||||
//setup new weatherData source -> mqtt
|
||||
weatherSource, err = weathersource.NewMqttSource(
|
||||
config.GetMqttUrl(),
|
||||
config.GetMqttTopic())
|
||||
|
||||
if config.UseAnonymousMqttAuthentication() {
|
||||
weatherSource, err = weathersource.NewAnonymousMqttSource(
|
||||
config.GetMqttUrl(),
|
||||
config.GetMqttTopic())
|
||||
} else {
|
||||
weatherSource, err = weathersource.NewMqttSource(
|
||||
config.GetMqttUrl(),
|
||||
config.GetMqttTopic(),
|
||||
config.GetMqttUser(),
|
||||
config.GetMqttPassword())
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println("Could not connect to mqtt:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
defer weatherSource.Close()
|
||||
weatherSource.AddNewWeatherDataCallback(handleNewWeatherData)
|
||||
|
||||
|
|
|
@ -9,6 +9,9 @@ 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-MQTT_USER" -Value "weather-api"
|
||||
Set-Item -Path "Env:WEATHER-API-MQTT_PASSWORD" -Value "weather-api"
|
||||
Set-Item -Path "Env:WEATHER-API-ANONYMOUS_MQTT_AUTHENTICATION" -Value "false"
|
||||
Set-Item -Path "Env:WEATHER-API-MQTT_PUBLISH_INTERVAL" -Value "2500"
|
||||
Set-Item -Path "Env:WEATHER-API-MQTT_MIN_DIST_TO_LAST_VALUE" -Value "250"
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package weathersource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -30,8 +29,18 @@ func (source *mqttWeatherSource) Close() {
|
|||
source.mqttClient.Disconnect(2)
|
||||
}
|
||||
|
||||
//NewAnonymousMqttSource Factory function for mqttWeatherSource with anonymous authentication
|
||||
func NewAnonymousMqttSource(url, topic string) (*mqttWeatherSource, error) {
|
||||
return newMqttSource(url, topic, "", "", true)
|
||||
}
|
||||
|
||||
//NewMqttSource Factory function for mqttWeatherSource with authentication
|
||||
func NewMqttSource(url, topic, user, password string) (*mqttWeatherSource, error) {
|
||||
return newMqttSource(url, topic, user, password, false)
|
||||
}
|
||||
|
||||
//NewMqttSource Factory function for mqttWeatherSource
|
||||
func NewMqttSource(url, topic string) (*mqttWeatherSource, error) {
|
||||
func newMqttSource(url, topic, user, password string, anonymous bool) (*mqttWeatherSource, error) {
|
||||
source := new(mqttWeatherSource)
|
||||
source.url = url
|
||||
|
||||
|
@ -42,6 +51,11 @@ func NewMqttSource(url, topic string) (*mqttWeatherSource, error) {
|
|||
opts.SetDefaultPublishHandler(source.mqttMessageHandler())
|
||||
opts.SetPingTimeout(1 * time.Second)
|
||||
|
||||
if !anonymous {
|
||||
opts.Username = user
|
||||
opts.Password = password
|
||||
}
|
||||
|
||||
source.mqttClient = mqtt.NewClient(opts)
|
||||
|
||||
if token := source.mqttClient.Connect(); token.Wait() && token.Error() != nil {
|
||||
|
@ -69,7 +83,6 @@ func (source *mqttWeatherSource) mqttMessageHandler() mqtt.MessageHandler {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println(sensorId)
|
||||
|
||||
lastWeatherData, found := source.getUnwrittenDatapoints(sensorId)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue