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