-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.go
64 lines (58 loc) · 1.56 KB
/
mqtt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package mqtt
import (
"log"
"time"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
type Config struct {
// Address for MQTT broker (e.g. "tcp://foobar.com:1883")
BrokerAddr string
// Username for MQTT broker (ignored if empty)
Username string
// Password for MQTT broker (ignored if empty)
Password string
// Client ID for MQTT broker (ignored if empty)
ClientID string
// MQTT topic to which we'll publish sensor readings
TopicSensor string
// MQTT topic to which we'll publish badge scans
TopicBadge string
}
func NewClient(c Config) MQTT.Client {
opts := MQTT.NewClientOptions()
opts.AddBroker(c.BrokerAddr)
opts.SetClientID(c.ClientID)
opts.SetUsername(c.Username)
opts.SetPassword(c.Password)
opts.SetDefaultPublishHandler(
func(client MQTT.Client, msg MQTT.Message) {
log.Printf("MQTT: recv topic %s: %s", msg.Topic(), msg.Payload())
})
opts.SetOnConnectHandler(
func(client MQTT.Client) {
log.Printf("MQTT: connected")
})
opts.SetConnectionLostHandler(
func(client MQTT.Client, err error) {
log.Printf("MQTT: connection lost: %v", err)
})
opts.SetReconnectingHandler(
func(client MQTT.Client, options *MQTT.ClientOptions) {
log.Printf("MQTT: reconnecting")
})
opts.SetAutoReconnect(true)
opts.SetMaxReconnectInterval(10 * time.Second)
client := MQTT.NewClient(opts)
go func(client MQTT.Client) {
for {
token := client.Connect()
if token.Wait() && token.Error() != nil {
log.Printf("MQTT: unable to connect, %s", token.Error())
<-time.After(10 * time.Second)
} else {
break
}
}
}(client)
return client
}