This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
mqtt.go
328 lines (296 loc) · 9.71 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package mqtt
import (
"crypto/tls"
"fmt"
"sync"
"time"
"github.com/TheThingsNetwork/api/gateway"
"github.com/TheThingsNetwork/api/router"
"github.com/TheThingsNetwork/api/trace"
"github.com/TheThingsNetwork/gateway-connector-bridge/types"
"github.com/TheThingsNetwork/ttn/utils/random"
"github.com/apex/log"
paho "github.com/eclipse/paho.mqtt.golang"
"github.com/gogo/protobuf/proto"
)
// PublishTimeout is the timeout before returning from publish without checking error
var PublishTimeout = 50 * time.Millisecond
// New returns a new MQTT
func New(config Config, ctx log.Interface) (*MQTT, error) {
mqtt := new(MQTT)
mqtt.ctx = ctx.WithField("Connector", "MQTT")
mqttOpts := paho.NewClientOptions()
for _, broker := range config.Brokers {
mqttOpts.AddBroker(broker)
}
if config.TLSConfig != nil {
mqttOpts.SetTLSConfig(config.TLSConfig)
}
mqttOpts.SetClientID(fmt.Sprintf("bridge-%s", random.String(16)))
mqttOpts.SetUsername(config.Username)
mqttOpts.SetPassword(config.Password)
mqttOpts.SetKeepAlive(30 * time.Second)
mqttOpts.SetPingTimeout(10 * time.Second)
mqttOpts.SetCleanSession(true)
mqttOpts.SetDefaultPublishHandler(func(_ paho.Client, msg paho.Message) {
mqtt.ctx.Warnf("Received unhandled message on MQTT: %v", msg)
})
mqtt.subscriptions = make(map[string]subscription)
var reconnecting bool
mqttOpts.SetConnectionLostHandler(func(_ paho.Client, err error) {
mqtt.ctx.Warnf("Disconnected (%s). Reconnecting...", err.Error())
reconnecting = true
})
mqttOpts.SetOnConnectHandler(func(_ paho.Client) {
mqtt.ctx.Info("Connected")
if reconnecting {
mqtt.resubscribe()
reconnecting = false
}
})
mqtt.client = paho.NewClient(mqttOpts)
return mqtt, nil
}
// QoS indicates the MQTT Quality of Service level.
// 0: The broker/client will deliver the message once, with no confirmation.
// 1: The broker/client will deliver the message at least once, with confirmation required.
// 2: The broker/client will deliver the message exactly once by using a four step handshake.
var (
PublishQoS byte = 0x00
SubscribeQoS byte = 0x00
)
// BufferSize indicates the maximum number of MQTT messages that should be buffered
var BufferSize = 10
// Topic formats for connect, disconnect, uplink, downlink and status messages
var (
ConnectTopicFormat = "connect"
DisconnectTopicFormat = "disconnect"
UplinkTopicFormat = "%s/up"
DownlinkTopicFormat = "%s/down"
StatusTopicFormat = "%s/status"
)
// Config contains configuration for MQTT
type Config struct {
Brokers []string
Username string
Password string
TLSConfig *tls.Config
}
type subscription struct {
handler paho.MessageHandler
cancel func()
}
// MQTT side of the bridge
type MQTT struct {
ctx log.Interface
client paho.Client
subscriptions map[string]subscription
mu sync.Mutex
}
var (
// ConnectRetries says how many times the client should retry a failed connection
ConnectRetries = 10
// ConnectRetryDelay says how long the client should wait between retries
ConnectRetryDelay = time.Second
)
// Connect to MQTT
func (c *MQTT) Connect() error {
var err error
for retries := 0; retries < ConnectRetries; retries++ {
token := c.client.Connect()
finished := token.WaitTimeout(1 * time.Second)
if !finished {
c.ctx.Warn("MQTT connection took longer than expected...")
token.Wait()
}
err = token.Error()
if err == nil {
break
}
c.ctx.Warnf("Could not connect to MQTT (%s). Retrying...", err.Error())
<-time.After(ConnectRetryDelay)
}
if err != nil {
return fmt.Errorf("Could not connect to MQTT (%s)", err)
}
return err
}
// Disconnect from MQTT
func (c *MQTT) Disconnect() error {
c.client.Disconnect(100)
return nil
}
func (c *MQTT) publish(topic string, msg []byte) paho.Token {
return c.client.Publish(topic, PublishQoS, false, msg)
}
func (c *MQTT) subscribe(topic string, handler paho.MessageHandler, cancel func()) paho.Token {
c.mu.Lock()
defer c.mu.Unlock()
wrappedHandler := func(client paho.Client, msg paho.Message) {
if msg.Retained() {
c.ctx.WithField("Topic", msg.Topic()).Debug("Ignore retained message")
return
}
handler(client, msg)
}
c.subscriptions[topic] = subscription{wrappedHandler, cancel}
return c.client.Subscribe(topic, SubscribeQoS, wrappedHandler)
}
func (c *MQTT) resubscribe() {
c.mu.Lock()
defer c.mu.Unlock()
for topic, subscription := range c.subscriptions {
c.client.Subscribe(topic, SubscribeQoS, subscription.handler)
}
}
func (c *MQTT) unsubscribe(topic string) paho.Token {
c.mu.Lock()
defer c.mu.Unlock()
if subscription, ok := c.subscriptions[topic]; ok && subscription.cancel != nil {
subscription.cancel()
}
delete(c.subscriptions, topic)
return c.client.Unsubscribe(topic)
}
// SubscribeConnect subscribes to connect messages
func (c *MQTT) SubscribeConnect() (<-chan *types.ConnectMessage, error) {
messages := make(chan *types.ConnectMessage, BufferSize)
token := c.subscribe(ConnectTopicFormat, func(_ paho.Client, msg paho.Message) {
var connect types.ConnectMessage
if err := proto.Unmarshal(msg.Payload(), &connect); err != nil {
c.ctx.WithError(err).Warn("Could not unmarshal connect message")
return
}
ctx := c.ctx.WithField("GatewayID", connect.GatewayID)
select {
case messages <- &connect:
ctx.WithField("ProtoSize", len(msg.Payload())).Debug("Received connect message")
default:
ctx.Warn("Could not handle connect message: buffer full")
}
}, func() {
close(messages)
})
token.Wait()
return messages, token.Error()
}
// UnsubscribeConnect unsubscribes from connect messages
func (c *MQTT) UnsubscribeConnect() error {
token := c.unsubscribe(ConnectTopicFormat)
token.Wait()
return token.Error()
}
// SubscribeDisconnect subscribes to disconnect messages
func (c *MQTT) SubscribeDisconnect() (<-chan *types.DisconnectMessage, error) {
messages := make(chan *types.DisconnectMessage, BufferSize)
token := c.subscribe(DisconnectTopicFormat, func(_ paho.Client, msg paho.Message) {
var disconnect types.DisconnectMessage
if err := proto.Unmarshal(msg.Payload(), &disconnect); err != nil {
c.ctx.WithError(err).Warn("Could not unmarshal disconnect message")
return
}
ctx := c.ctx.WithField("GatewayID", disconnect.GatewayID)
select {
case messages <- &disconnect:
ctx.WithField("ProtoSize", len(msg.Payload())).Debug("Received disconnect message")
default:
ctx.Warn("Could not handle disconnect message: buffer full")
}
}, func() {
close(messages)
})
token.Wait()
return messages, token.Error()
}
// UnsubscribeDisconnect unsubscribes from disconnect messages
func (c *MQTT) UnsubscribeDisconnect() error {
token := c.unsubscribe(DisconnectTopicFormat)
token.Wait()
return token.Error()
}
// SubscribeUplink handles uplink messages for the given gateway ID
func (c *MQTT) SubscribeUplink(gatewayID string) (<-chan *types.UplinkMessage, error) {
ctx := c.ctx.WithField("GatewayID", gatewayID)
messages := make(chan *types.UplinkMessage, BufferSize)
token := c.subscribe(fmt.Sprintf(UplinkTopicFormat, gatewayID), func(_ paho.Client, msg paho.Message) {
uplink := types.UplinkMessage{
GatewayID: gatewayID,
Message: new(router.UplinkMessage),
}
if err := proto.Unmarshal(msg.Payload(), uplink.Message); err != nil {
ctx.WithError(err).Warn("Could not unmarshal uplink message")
return
}
uplink.Message.Trace = uplink.Message.Trace.WithEvent(trace.ReceiveEvent, "backend", "mqtt")
select {
case messages <- &uplink:
ctx.WithField("ProtoSize", len(msg.Payload())).Debug("Received uplink message")
default:
ctx.Warn("Could not handle uplink message: buffer full")
}
}, func() {
close(messages)
})
token.Wait()
return messages, token.Error()
}
// UnsubscribeUplink unsubscribes from uplink messages for the given gateway ID
func (c *MQTT) UnsubscribeUplink(gatewayID string) error {
token := c.unsubscribe(fmt.Sprintf(UplinkTopicFormat, gatewayID))
token.Wait()
return token.Error()
}
// SubscribeStatus handles status messages for the given gateway ID
func (c *MQTT) SubscribeStatus(gatewayID string) (<-chan *types.StatusMessage, error) {
ctx := c.ctx.WithField("GatewayID", gatewayID)
messages := make(chan *types.StatusMessage, BufferSize)
token := c.subscribe(fmt.Sprintf(StatusTopicFormat, gatewayID), func(_ paho.Client, msg paho.Message) {
status := types.StatusMessage{
Backend: "MQTT",
GatewayID: gatewayID,
Message: new(gateway.Status),
}
if err := proto.Unmarshal(msg.Payload(), status.Message); err != nil {
ctx.WithError(err).Warn("Could not unmarshal status message")
return
}
select {
case messages <- &status:
ctx.WithField("ProtoSize", len(msg.Payload())).Debug("Received status message")
default:
ctx.Warn("Could not handle status message: buffer full")
}
}, func() {
close(messages)
})
token.Wait()
return messages, token.Error()
}
// UnsubscribeStatus unsubscribes from status messages for the given gateway ID
func (c *MQTT) UnsubscribeStatus(gatewayID string) error {
token := c.unsubscribe(fmt.Sprintf(StatusTopicFormat, gatewayID))
token.Wait()
return token.Error()
}
// PublishDownlink publishes a downlink message
func (c *MQTT) PublishDownlink(message *types.DownlinkMessage) error {
ctx := c.ctx.WithField("GatewayID", message.GatewayID)
downlink := *message.Message
downlink.Trace = nil
msg, err := proto.Marshal(&downlink)
if err != nil {
return err
}
token := c.publish(fmt.Sprintf(DownlinkTopicFormat, message.GatewayID), msg)
go func() {
token.Wait()
if err := token.Error(); err != nil {
ctx.WithError(err).Warn("Could not publish downlink message")
return
}
ctx.WithField("ProtoSize", len(msg)).Debug("Published downlink message")
}()
return nil
}