-
-
Notifications
You must be signed in to change notification settings - Fork 548
/
gcppubsub.go
344 lines (279 loc) · 9.76 KB
/
gcppubsub.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package gcppubsub
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"strings"
"sync"
"time"
"cloud.google.com/go/pubsub"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"google.golang.org/api/option"
"github.com/brocaar/loraserver/api/gw"
"github.com/brocaar/loraserver/internal/backend"
"github.com/brocaar/loraserver/internal/backend/gateway/marshaler"
"github.com/brocaar/loraserver/internal/helpers"
"github.com/brocaar/lorawan"
)
const uplinkSubscriptionTmpl = "%s-loraserver"
const (
marshalerV2JSON = iota
marshalerProtobuf
marshalerJSON
)
// Config holds the configuration for the GCP Pub/Sub backend.
type Config struct {
CredentialsFile string `mapstructure:"credentials_file"`
ProjectID string `mapstructure:"project_id"`
UplinkTopicName string `mapstructure:"uplink_topic_name"`
DownlinkTopicName string `mapstructure:"downlink_topic_name"`
UplinkRetentionDuration time.Duration `mapstructure:"uplink_retention_duration"`
}
// Backend implements a Google Cloud Pub/Sub backend.
type Backend struct {
sync.RWMutex
ctx context.Context
cancel context.CancelFunc
client *pubsub.Client
downlinkTopic *pubsub.Topic
uplinkTopic *pubsub.Topic
uplinkSubscription *pubsub.Subscription
uplinkFrameChan chan gw.UplinkFrame
gatewayStatsChan chan gw.GatewayStats
downlinkTXAckChan chan gw.DownlinkTXAck
gatewayMarshaler map[lorawan.EUI64]marshaler.Type
}
// NewBackend creates a new Backend.
func NewBackend(conf Config) (backend.Gateway, error) {
b := Backend{
gatewayMarshaler: make(map[lorawan.EUI64]marshaler.Type),
uplinkFrameChan: make(chan gw.UplinkFrame),
gatewayStatsChan: make(chan gw.GatewayStats),
downlinkTXAckChan: make(chan gw.DownlinkTXAck),
ctx: context.Background(),
}
var err error
var o []option.ClientOption
b.ctx, b.cancel = context.WithCancel(b.ctx)
if conf.CredentialsFile != "" {
o = append(o, option.WithCredentialsFile(conf.CredentialsFile))
}
log.Info("gateway/gcp_pub_sub: setting up client")
b.client, err = pubsub.NewClient(b.ctx, conf.ProjectID, o...)
if err != nil {
return nil, errors.Wrap(err, "gateway/gcp_pub_sub: new pubsub client error")
}
log.WithField("topic", conf.DownlinkTopicName).Info("gateway/gcp_pub_sub: setup downlink topic")
b.downlinkTopic = b.client.Topic(conf.DownlinkTopicName)
ok, err := b.downlinkTopic.Exists(b.ctx)
if err != nil {
return nil, errors.Wrap(err, "gateway/gcp_pub_sub: topic exists error")
}
if !ok {
return nil, fmt.Errorf("gateway/gcp_pub_sub: downlink topic '%s' does not exist", conf.DownlinkTopicName)
}
log.WithField("topic", conf.UplinkTopicName).Info("gateway/gcp_pub_sub: setup uplink topic")
b.uplinkTopic = b.client.Topic(conf.UplinkTopicName)
ok, err = b.uplinkTopic.Exists(b.ctx)
if err != nil {
return nil, errors.Wrap(err, "gateway/gcp_pub_sub: topic exists error")
}
if !ok {
return nil, fmt.Errorf("gateway/gcp_pub_sub: uplink topic '%s' does not exist", conf.UplinkTopicName)
}
upSubName := fmt.Sprintf(uplinkSubscriptionTmpl, conf.UplinkTopicName)
log.WithField("subscription", upSubName).Info("gateway/gcp_pub_sub: check if uplink subscription exists")
b.uplinkSubscription = b.client.Subscription(upSubName)
ok, err = b.uplinkSubscription.Exists(b.ctx)
if err != nil {
return nil, errors.Wrap(err, "gateway/gcp_pub_sub: subscription exists error")
}
// try to create the subscription if it doesn't exist
if !ok {
log.WithField("subscription", upSubName).Info("gateway/gcp_pub_sub: create uplink subscription")
b.uplinkSubscription, err = b.client.CreateSubscription(b.ctx, upSubName, pubsub.SubscriptionConfig{
Topic: b.uplinkTopic,
RetentionDuration: conf.UplinkRetentionDuration,
})
}
// consume uplink frames
go func() {
for {
err := b.uplinkSubscription.Receive(b.ctx, b.receiveFunc)
if err != nil {
log.WithError(err).Error("gateway/gcp_pub_sub: receive error")
time.Sleep(time.Second * 2)
continue
}
break
}
}()
return &b, nil
}
// SendTXPacket sends the given downlink frame to the gateway.
func (b *Backend) SendTXPacket(pl gw.DownlinkFrame) error {
if pl.TxInfo == nil {
return errors.New("tx_info must not be nil")
}
gatewayID := helpers.GetGatewayID(pl.TxInfo)
t := b.getGatewayMarshaler(gatewayID)
bb, err := marshaler.MarshalDownlinkFrame(t, pl)
if err != nil {
return errors.Wrap(err, "gateway/gcp_pub_sub: marshal downlink frame error")
}
return b.publishCommand(gatewayID, "down", bb)
}
// SendGatewayConfigPacket sends the given gateway configuration to the gateway.
func (b *Backend) SendGatewayConfigPacket(pl gw.GatewayConfiguration) error {
gatewayID := helpers.GetGatewayID(&pl)
t := b.getGatewayMarshaler(gatewayID)
bb, err := marshaler.MarshalGatewayConfiguration(t, pl)
if err != nil {
return errors.Wrap(err, "gateway/gcp_pub_sub: marshal gateway configuration error")
}
return b.publishCommand(gatewayID, "config", bb)
}
// RXPacketChan returns the channel to which uplink frames are published.
func (b *Backend) RXPacketChan() chan gw.UplinkFrame {
return b.uplinkFrameChan
}
// StatsPacketChan returns the channel to which gateway stats are published.
func (b *Backend) StatsPacketChan() chan gw.GatewayStats {
return b.gatewayStatsChan
}
// DownlinkTXAckChan returns the downlink tx ack channel.
func (b *Backend) DownlinkTXAckChan() chan gw.DownlinkTXAck {
return b.downlinkTXAckChan
}
// Close closes the backend.
func (b *Backend) Close() error {
log.Info("gateway/gcp_pub_sub: closing backend")
b.cancel()
close(b.uplinkFrameChan)
close(b.gatewayStatsChan)
close(b.downlinkTXAckChan)
return b.client.Close()
}
func (b *Backend) setGatewayMarshaler(gatewayID lorawan.EUI64, t marshaler.Type) {
b.Lock()
defer b.Unlock()
b.gatewayMarshaler[gatewayID] = t
}
func (b *Backend) getGatewayMarshaler(gatewayID lorawan.EUI64) marshaler.Type {
b.RLock()
defer b.RUnlock()
return b.gatewayMarshaler[gatewayID]
}
func (b *Backend) publishCommand(gatewayID lorawan.EUI64, command string, data []byte) error {
start := time.Now()
res := b.downlinkTopic.Publish(b.ctx, &pubsub.Message{
Data: data,
Attributes: map[string]string{
"deviceId": "gw-" + gatewayID.String(),
"subFolder": command,
},
})
if _, err := res.Get(b.ctx); err != nil {
return errors.Wrap(err, "get publish result error")
}
log.WithFields(log.Fields{
"duration": time.Now().Sub(start),
"gateway_id": gatewayID,
"command": command,
}).Info("gateway/gcp_pub_sub: message published")
return nil
}
func (b *Backend) receiveFunc(ctx context.Context, msg *pubsub.Message) {
msg.Ack()
var gatewayID lorawan.EUI64
gatewayIDStr, ok := msg.Attributes["deviceId"]
if !ok {
log.Error("gateway/gcp_pub_sub: received message does not contain 'deviceId' attribute")
}
typ, ok := msg.Attributes["subFolder"]
if !ok {
log.Error("gateway/gcp_pub_sub: received message does not contain 'subFolder' attribute")
}
gatewayIDStr = strings.Replace(gatewayIDStr, "gw-", "", 1)
if err := gatewayID.UnmarshalText([]byte(gatewayIDStr)); err != nil {
log.WithError(err).Error("gateway/gcp_pub_sub: unmarshal gateway id error")
}
log.WithFields(log.Fields{
"gateway_id": gatewayID,
"type": typ,
}).Info("gateway/gcp_pub_sub: message received")
var err error
switch typ {
case "up":
err = b.handleUplinkFrame(gatewayID, msg.Data)
case "stats":
err = b.handleGatewayStats(gatewayID, msg.Data)
case "ack":
err = b.handleDownlinkTXAck(gatewayID, msg.Data)
default:
log.WithFields(log.Fields{
"gateway_id": gatewayID,
"type": typ,
}).Warning("gateway/gcp_pub_sub: unexpected message type")
}
if err != nil {
log.WithError(err).WithFields(log.Fields{
"gateway_id": gatewayID,
"type": typ,
"data_base64": base64.StdEncoding.EncodeToString(msg.Data),
}).Error("gateway/gcp_pub_sub: handle received message error")
}
}
func (b *Backend) handleUplinkFrame(gatewayID lorawan.EUI64, data []byte) error {
var uplinkFrame gw.UplinkFrame
t, err := marshaler.UnmarshalUplinkFrame(data, &uplinkFrame)
if err != nil {
return errors.Wrap(err, "unmarshal error")
}
b.setGatewayMarshaler(gatewayID, t)
if uplinkFrame.RxInfo == nil {
return errors.New("rx_info must not be nil")
}
if uplinkFrame.TxInfo == nil {
return errors.New("tx_info must not be nil")
}
// make sure that the registered gateway is not using a different gateway_id
// than the ID used during the registration in Cloud IoT Core.
if !bytes.Equal(uplinkFrame.RxInfo.GatewayId, gatewayID[:]) {
return errors.New("gateway_id is not equal to expected gateway_id")
}
b.uplinkFrameChan <- uplinkFrame
return nil
}
func (b *Backend) handleGatewayStats(gatewayID lorawan.EUI64, data []byte) error {
var gatewayStats gw.GatewayStats
t, err := marshaler.UnmarshalGatewayStats(data, &gatewayStats)
if err != nil {
return errors.Wrap(err, "unmarshal error")
}
b.setGatewayMarshaler(gatewayID, t)
// make sure that the registered gateway is not using a different gateway_id
// than the ID used during the registration in Cloud IoT Core.
if !bytes.Equal(gatewayStats.GatewayId, gatewayID[:]) {
return errors.New("gateway_id is not equal to expected gateway_id")
}
b.gatewayStatsChan <- gatewayStats
return nil
}
func (b *Backend) handleDownlinkTXAck(gatewayID lorawan.EUI64, data []byte) error {
var ack gw.DownlinkTXAck
t, err := marshaler.UnmarshalDownlinkTXAck(data, &ack)
if err != nil {
return errors.Wrap(err, "unmarshal error")
}
b.setGatewayMarshaler(gatewayID, t)
// make sure that the registered gateway is not using a different gateway_id
// than the ID used during the registration in Cloud IoT Core.
if !bytes.Equal(ack.GatewayId, gatewayID[:]) {
return errors.New("gateway_id is not equal to expected gateway_id")
}
b.downlinkTXAckChan <- ack
return nil
}