@@ -9,6 +9,13 @@ import (
99 mqtt "github.com/eclipse/paho.mqtt.golang"
1010)
1111
12+ type MqttConfig struct {
13+ Url string `yaml:"url"`
14+ User string `yaml:"user"`
15+ Password string `yaml:"password"`
16+ Prefix string `yaml:"prefix"`
17+ }
18+
1219type MosquittoConnection struct {
1320 client mqtt.Client
1421 prefix string
@@ -22,16 +29,24 @@ var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err
2229 log .Printf ("Connect lost: %v" , err )
2330}
2431
25- func New (url string , prefix string ) (* MosquittoConnection , error ) {
32+ func New (config * MqttConfig ) (* MosquittoConnection , error ) {
2633 opts := mqtt .NewClientOptions ()
27- opts .AddBroker (url )
34+ opts .AddBroker (config . Url )
2835 opts .SetClientID ("sofar" )
2936 opts .OnConnect = connectHandler
3037 opts .OnConnectionLost = connectLostHandler
3138
39+ if config .User != "" {
40+ opts .SetUsername (config .User )
41+ }
42+
43+ if config .Password != "" {
44+ opts .SetPassword (config .Password )
45+ }
46+
3247 conn := & MosquittoConnection {}
3348 conn .client = mqtt .NewClient (opts )
34- conn .prefix = prefix
49+ conn .prefix = config . Prefix
3550 if token := conn .client .Connect (); token .Wait () && token .Error () != nil {
3651 return nil , token .Error ()
3752 }
0 commit comments