-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
695 lines (626 loc) · 21.7 KB
/
main.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
// The order the loraserverMQTT arguments are picked up is as follows:
// 1) If lsbroker is specified on commandline, use all cmdline parameters
// 2) If lsbroker is specified as a service property, use all service props
// 3) If broker was unset in 1 or 2, set Broker, User, and Pass from framework args
package main
import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
"syscall"
"github.com/brocaar/loraserver/models"
"github.com/brocaar/lorawan"
"github.com/coreos/go-systemd/daemon"
"github.com/openchirp/framework"
"github.com/openchirp/framework/pubsub"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/wercker/journalhook"
)
const (
version string = "1.1"
)
const (
// Set this value to true to have the service publish a service status of
// "Running" each time it receives a device update event
//
// This could be used as a service alive pulse if enabled
// Otherwise, the service status will indicate "Started" at the time the
// service "Started" the client
runningStatus = true
// Set this to true to allow gateways to interact in the
// <devid>/{tx, rx, stats} mqtt domain in addition to
// the <devid>/gateway/<gwid>/{tx, rx, stats} prefix domain
supportTransducerGateway = false
// Set this to true to have the service decode RX packets and publish
// on packets it receives
supportRXPacketStats = true
// Set this to true to have the service decode TX packets and publish
// on packets it receives
supportTXPacketStats = true
defaultFrameworkURI = "http://localhost:7000"
defaultBrokerURI = "tcp://localhost:1883"
defaultServiceID = ""
defaultServiceToken = ""
defaultLsBroker = "<framework_broker>"
defaultLsQoS = uint(2)
defaultLsUser = "<framework_id>"
defaultLsPass = "<framework_pass>"
)
const (
gatewayIDKey = "Gateway ID"
gatewayIDLength = len("D00D8BADF00D0001")
)
const (
topicLat = "latitude"
topicLon = "longitude"
topicAlt = "altitude"
topicPktRecv = "packets_received"
topicPktRecvOk = "packets_received_ok"
)
const (
topicFrequency = "rx_frequency"
topicRSSI = "rx_rssi"
topicLoRaSNR = "rx_lorasnr"
topicSpreadingFactor = "rx_spreadingfactor"
topicBandwidth = "rx_bandwidth"
topicDevAddr = "rx_devaddr"
topicNetworkID = "rx_networkid"
topicCRCStatus = "rx_crcstatus"
topicTimestamp = "rx_timestamp"
)
const (
topicTxPower = "tx_power"
topicTxFrequency = "tx_frequency"
topicTxSpreadingFactor = "tx_spreadingfactor"
topicTxBandwidth = "tx_bandwidth"
topicTxTimestamp = "tx_timestamp"
)
func isHexCharacters(id string) bool {
for _, c := range id {
if !(('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) {
return false
}
}
return true
}
/*
{
"mac":"d00d8badf00d0003",
"time":"2017-09-11T02:16:14Z",
"latitude":40.44127,
"longitude":-79.94218,
"altitude":317,
"rxPacketsReceived":0,
"rxPacketsReceivedOK":0
}
*/
type StatsPacket struct {
Mac string `json:"mac"`
Time string `json:"time"`
Latitude float64 `json:"Latitude"`
Longitude float64 `json:"Longitude"`
Altitude int `json:"altitude"`
RxReceived uint `json:"rxPacketsReceived"`
RxReceivedOk uint `json:"rxPacketsReceivedOK"`
}
func ProcessStatsHandler(log *logrus.Logger, c *framework.ServiceClient, lsMQTT *pubsub.MQTTClient, devid, endpoint, gwid string) func(topic string, payload []byte) {
// OC Device Transducer Gateway Topic
devTopic := endpoint
// OC Device Root Gateway Topic
// devGwTopic := endpoint + "/gateway/" + gwid
// Lora Server Gateway Topic
lsTopic := "gateway/" + gwid
return func(topic string, payload []byte) {
var stats StatsPacket
loglocal := log.WithFields(logrus.Fields{"devid": devid, "gwid": gwid, "pkt": "stat"})
// forward first
err := lsMQTT.Publish(lsTopic+"/stats", payload)
if err != nil {
loglocal.Warnf("Failed forward stats to %s", lsTopic+"/stats")
}
// then parse
err = json.Unmarshal(payload, &stats)
if err != nil {
loglocal.Warnf("Failed to Unmarshal stats JSON")
return
}
loglocal.Debug("Received stats: ", stats)
err = c.Publish(devTopic+"/"+topicLat, fmt.Sprint(stats.Latitude))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicLat, devid)
}
err = c.Publish(devTopic+"/"+topicLon, fmt.Sprint(stats.Longitude))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicLon, devid)
}
err = c.Publish(devTopic+"/"+topicAlt, fmt.Sprint(stats.Altitude))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicAlt, devid)
}
err = c.Publish(devTopic+"/"+topicPktRecv, fmt.Sprint(stats.RxReceived))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicPktRecv, devid)
}
err = c.Publish(devTopic+"/"+topicPktRecvOk, fmt.Sprint(stats.RxReceivedOk))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicPktRecvOk, devid)
}
}
}
func ProcessRXHandler(log *logrus.Logger, c *framework.ServiceClient, lsMQTT *pubsub.MQTTClient, devid, endpoint, gwid string) func(topic string, payload []byte) {
// OC Device Transducer Gateway Topic
devTopic := endpoint
// OC Device Root Gateway Topic
// devGwTopic := endpoint + "/gateway/" + gwid
// Lora Server Gateway Topic
lsTopic := "gateway/" + gwid
return func(topic string, payload []byte) {
loglocal := log.WithFields(logrus.Fields{"devid": devid, "gwid": gwid, "pkt": "rx"})
// forward first
err := lsMQTT.Publish(lsTopic+"/rx", payload)
if err != nil {
loglocal.Warnf("Failed forward rx to %s", lsTopic+"/rx")
}
// then parse
if supportRXPacketStats {
var rx models.RXPacket
err = json.Unmarshal(payload, &rx)
if err != nil {
loglocal.Warnf("Failed to Unmarshal rx JSON")
return
}
loglocal.Debug("Received rx: ", rx)
err = c.Publish(devTopic+"/"+topicCRCStatus, fmt.Sprint(rx.RXInfo.CRCStatus))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicCRCStatus, devid)
}
err = c.Publish(devTopic+"/"+topicFrequency, fmt.Sprint(rx.RXInfo.Frequency))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicFrequency, devid)
}
err = c.Publish(devTopic+"/"+topicRSSI, fmt.Sprint(rx.RXInfo.RSSI))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicRSSI, devid)
}
err = c.Publish(devTopic+"/"+topicLoRaSNR, fmt.Sprint(rx.RXInfo.LoRaSNR))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicLoRaSNR, devid)
}
err = c.Publish(devTopic+"/"+topicSpreadingFactor, fmt.Sprint(rx.RXInfo.DataRate.SpreadFactor))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicSpreadingFactor, devid)
}
err = c.Publish(devTopic+"/"+topicBandwidth, fmt.Sprint(rx.RXInfo.DataRate.Bandwidth))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicBandwidth, devid)
}
err = c.Publish(devTopic+"/"+topicTimestamp, fmt.Sprint(rx.RXInfo.Timestamp))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicTimestamp, devid)
}
if rx.PHYPayload.MHDR.MType == lorawan.UnconfirmedDataUp ||
rx.PHYPayload.MHDR.MType == lorawan.ConfirmedDataUp {
devAddrBuf, _ := rx.PHYPayload.MACPayload.(*lorawan.MACPayload).FHDR.DevAddr.MarshalBinary()
// devAddrBuf[3] = devAddrBuf[3] & 0x01
devAddr := binary.LittleEndian.Uint32(devAddrBuf)
// nwkID := rx.PHYPayload.MACPayload.(*lorawan.MACPayload).FHDR.DevAddr.NwkID()
// err = c.Publish(devTopic+"/"+topicNetworkID, fmt.Sprint(uint(binary.LittleEndian.Uint32(nwkID))))
// if err != nil {
// loglocal.Errorf("Failed to publish %s for deviceid %s", topicNetworkID, devid)
// }
err = c.Publish(devTopic+"/"+topicDevAddr, fmt.Sprint(devAddr))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicDevAddr, devid)
}
}
}
}
}
func ProcessTXHandler(log *logrus.Logger, c *framework.ServiceClient, lsMQTT *pubsub.MQTTClient, devid, endpoint, gwid string) func(topic string, payload []byte) {
// OC Device Transducer Gateway Topic
devTopic := endpoint
// OC Device Root Gateway Topic
devGwTopic := endpoint + "/gateway/" + gwid
// Lora Server Gateway Topic
// lsTopic := "gateway/" + gwid
return func(topic string, payload []byte) {
loglocal := log.WithFields(logrus.Fields{"devid": devid, "gwid": gwid, "pkt": "tx"})
// forward first
if supportTransducerGateway {
err := c.Publish(devTopic+"/tx", payload)
if err != nil {
loglocal.Warnf("Failed forward tx to %s", devTopic+"/tx")
}
}
err := c.Publish(devGwTopic+"/tx", payload)
if err != nil {
loglocal.Warnf("Failed forward tx to %s", devGwTopic+"/tx")
}
// then parse
if supportTXPacketStats {
var tx models.TXPacket
err = json.Unmarshal(payload, &tx)
if err != nil {
loglocal.Warnf("Failed to Unmarshal rx JSON")
return
}
loglocal.Debug("Received tx: ", tx)
err = c.Publish(devTopic+"/"+topicTxPower, fmt.Sprint(tx.TXInfo.Power))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicTxPower, devid)
}
err = c.Publish(devTopic+"/"+topicTxFrequency, fmt.Sprint(tx.TXInfo.Frequency))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicTxFrequency, devid)
}
err = c.Publish(devTopic+"/"+topicTxSpreadingFactor, fmt.Sprint(tx.TXInfo.DataRate.SpreadFactor))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicTxSpreadingFactor, devid)
}
err = c.Publish(devTopic+"/"+topicTxBandwidth, fmt.Sprint(tx.TXInfo.DataRate.Bandwidth))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicTxBandwidth, devid)
}
if tx.TXInfo.Immediately {
// If send immediately, publish a -1
err = c.Publish(devTopic+"/"+topicTxTimestamp, fmt.Sprint(-1))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicTxTimestamp, devid)
}
} else {
err = c.Publish(devTopic+"/"+topicTxTimestamp, fmt.Sprint(tx.TXInfo.Timestamp))
if err != nil {
loglocal.Errorf("Failed to publish %s for deviceid %s", topicTxTimestamp, devid)
}
}
}
}
}
func run(ctx *cli.Context) error {
systemdIntegration := ctx.Bool("systemd")
/* Set logging level */
log := logrus.New()
log.SetLevel(logrus.Level(uint32(ctx.Int("log-level"))))
if systemdIntegration {
log.AddHook(&journalhook.JournalHook{})
log.Out = ioutil.Discard
}
/* Options to be filled in by arguments */
var frameworkURI = ctx.String("framework-server")
var brokerURI = ctx.String("mqtt-server")
var serviceID = ctx.String("service-id")
var serviceToken = ctx.String("service-token")
var loraserverMQTTBroker = ctx.String("ls-mqtt-server")
var loraserverMQTTQoS = fmt.Sprint(ctx.Uint("ls-mqtt-qos"))
var loraserverMQTTUser = ctx.String("ls-mqtt-user")
var loraserverMQTTPass = ctx.String("ls-mqtt-pass")
log.Info("Starting LoRaWAN Gateways Service")
c, err := framework.StartServiceClientStatus(
frameworkURI,
brokerURI,
serviceID,
serviceToken,
"Unexpected disconnect!",
)
if err != nil {
log.Error("Failed to StartServiceClient: ", err)
return cli.NewExitError(nil, 1)
}
defer c.StopClient()
log.Info("Started LoRaWAN Gateways Service")
err = c.SetStatus("Starting")
if err != nil {
log.Error("Failed to publish service status: ", err)
return cli.NewExitError(nil, 1)
}
log.Info("Published Service Status")
/* Hunt down the loraserver MQTT parameters */
if loraserverMQTTBroker == defaultLsBroker {
// Try setting loraserverMQTT parameters from service properties
loraserverMQTTBroker = c.GetProperty("MQTTBroker")
loraserverMQTTQoS = c.GetProperty("MQTTQos")
loraserverMQTTUser = c.GetProperty("MQTTUser")
loraserverMQTTPass = c.GetProperty("MQTTPass")
if loraserverMQTTBroker == "" {
// Set to framework mqtt parameters
loraserverMQTTBroker = brokerURI
loraserverMQTTQoS = fmt.Sprint(defaultLsQoS)
loraserverMQTTUser = serviceID
loraserverMQTTPass = serviceToken
logitem := log.WithFields(logrus.Fields{"user": loraserverMQTTUser, "broker": loraserverMQTTBroker})
logitem.Info("Used loraserver's MQTT broker parameters from framework broker settings")
} else {
// Using all service properties parameters
logitem := log.WithFields(logrus.Fields{"user": loraserverMQTTUser, "broker": loraserverMQTTBroker})
logitem.Info("Used loraserver's MQTT broker parameters from service properties")
}
} else {
// Using all commandline parameters
if loraserverMQTTUser == defaultLsUser {
loraserverMQTTUser = ""
}
if loraserverMQTTPass == defaultLsPass {
loraserverMQTTPass = ""
}
logitem := log.WithFields(logrus.Fields{"user": loraserverMQTTUser, "broker": loraserverMQTTBroker})
logitem.Info("Used loraserver's MQTT broker parameters from commandline")
}
// Set QoS if not specified
if loraserverMQTTQoS == "" {
loraserverMQTTQoS = fmt.Sprint(defaultLsQoS)
}
/* Start the loraserver interface MQTT client */
log.Info("Starting loraserver MQTT client")
lsMQTT, err := pubsub.NewMQTTClient(
loraserverMQTTBroker,
loraserverMQTTUser,
loraserverMQTTPass,
pubsub.ParseMQTTQoS(loraserverMQTTQoS),
false,
)
if err != nil {
log.Error("Failed to start loraserver MQTT client: ", err)
return cli.NewExitError(nil, 1)
}
defer lsMQTT.Disconnect()
/* Create the MQTT bridge manager */
mqttBridge := NewBridgeService(c, lsMQTT, log)
/* Create gateway id --> device id table */
gwidDevice := make(map[string]string)
deviceGwid := make(map[string]string)
log.Info("Starting Device Updates Stream")
updates, err := c.StartDeviceUpdatesSimple()
if err != nil {
log.Error("Failed to start device updates stream: ", err)
return cli.NewExitError(nil, 1)
}
defer c.StopDeviceUpdates()
log.Info("Processing device updates")
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
err = c.SetStatus("Started")
if err != nil {
log.Error("Failed to publish service status: ", err)
return cli.NewExitError(nil, 1)
}
log.Info("Published Service Status")
if systemdIntegration {
daemon.SdNotify(false, daemon.SdNotifyReady)
}
for {
select {
case update := <-updates:
if runningStatus {
err = c.SetStatus("Running")
if err != nil {
log.Error("Failed to publish service status: ", err)
return cli.NewExitError(nil, 1)
}
log.Info("Published Service Status")
}
logitem := log.WithFields(logrus.Fields{"type": update.Type, "devid": update.Id, "gwid": update.Config[gatewayIDKey]})
switch update.Type {
case framework.DeviceUpdateTypeErr:
log.Errorf("Event Error: %v", error(update))
case framework.DeviceUpdateTypeRem:
logitem.Info("Removing links")
if gwid, ok := deviceGwid[update.Id]; ok {
devTopic := update.Topic
devGwTopic := update.Topic + "/gateway/" + gwid
c.Unsubscribe(devGwTopic + "/stats")
if supportTransducerGateway {
c.Unsubscribe(devTopic + "/stats")
}
c.Unsubscribe(devGwTopic + "/rx")
if supportTransducerGateway {
c.Unsubscribe(devTopic + "/rx")
}
mqttBridge.RemoveLinksAll(update.Id)
delete(gwidDevice, deviceGwid[update.Id])
delete(deviceGwid, update.Id)
}
case framework.DeviceUpdateTypeUpd:
logitem.Info("Removing links for update")
if gwid, ok := deviceGwid[update.Id]; ok {
devTopic := update.Topic
devGwTopic := update.Topic + "/gateway/" + gwid
c.Unsubscribe(devGwTopic + "/stats")
if supportTransducerGateway {
c.Unsubscribe(devTopic + "/stats")
}
c.Unsubscribe(devGwTopic + "/rx")
if supportTransducerGateway {
c.Unsubscribe(devTopic + "/rx")
}
mqttBridge.RemoveLinksAll(update.Id)
delete(gwidDevice, deviceGwid[update.Id])
delete(deviceGwid, update.Id)
}
fallthrough
case framework.DeviceUpdateTypeAdd:
logitem.Info("Adding links")
gwid, ok := update.Config[gatewayIDKey]
/* Check that the Gateway ID parameter was specified in the config */
if !ok {
logitem.Warn("No \"", gatewayIDKey, "\" key specified in config")
c.SetDeviceStatus(update.Id, "No Gateway ID in config")
continue
}
/* Check the length of the gateway id */
if len(gwid) != gatewayIDLength {
logitem.Warn("Gateway ID \"", gwid, "\" contains invalid hex characters")
c.SetDeviceStatus(update.Id, "Gateway ID has invalid length (", len(gwid), "!=", gatewayIDLength, ")")
continue
}
/* Check that all characters are valid hex characters */
if !isHexCharacters(gwid) {
logitem.Warn("Gateway ID \"", gwid, "\" contains non-hex characters")
c.SetDeviceStatus(update.Id, "Gateway ID contains non-hex chars")
continue
}
/* Change case to lowercase */
gwid = strings.ToLower(gwid)
/* Check that the gateway id is not already mapped */
if devid, ok := gwidDevice[gwid]; ok {
logitem.Warn("Gateway ID \"", gwid, "\" already owned by device id ", devid)
c.SetDeviceStatus(update.Id, "Gateway ID already in use by device id ", devid)
continue
}
gwidDevice[gwid] = update.Id
deviceGwid[update.Id] = gwid
c.SetDeviceStatus(update.Id, "Linking as gateway ", gwid)
// OC Device Transducer Gateway Topic
devTopic := update.Topic
// OC Device Root Gateway Topic
devGwTopic := update.Topic + "/gateway/" + gwid
// Lora Server Gateway Topic
lsTopic := "gateway/" + gwid
reportDeviceStatus := func(e error) {
if e != nil {
c.SetDeviceStatus(update.Id, "Error linking as \"", gwid, "\": ", e)
} else {
c.SetDeviceStatus(update.Id, "Linked as gateway ", gwid)
}
}
devid := update.Id
/* Add tx streams */
logitem.Debugf("Adding processor for %s", lsTopic+"/tx")
err = lsMQTT.Subscribe(lsTopic+"/tx", ProcessTXHandler(log, c, lsMQTT, devid, update.Topic, gwid))
if err != nil {
logitem.Error("Failed to link to device tx topic: ", err)
reportDeviceStatus(err)
mqttBridge.RemoveLinksAll(update.Id)
continue
}
/* Add rx streams */
if supportTransducerGateway {
logitem.Debugf("Adding processor for %s", devTopic+"/rx")
err = c.Subscribe(devTopic+"/rx", ProcessRXHandler(log, c, lsMQTT, devid, update.Topic, gwid))
if err != nil {
logitem.Error("Failed to link to device rx topic: ", err)
reportDeviceStatus(err)
mqttBridge.RemoveLinksAll(update.Id)
continue
}
}
logitem.Debugf("Adding processor for %s", devGwTopic+"/rx")
err = c.Subscribe(devGwTopic+"/rx", ProcessRXHandler(log, c, lsMQTT, devid, update.Topic, gwid))
if err != nil {
logitem.Error("Failed to link to device rx topic: ", err)
reportDeviceStatus(err)
mqttBridge.RemoveLinksAll(update.Id)
continue
}
/* Add stats streams */
if supportTransducerGateway {
logitem.Debugf("Adding processor for %s", devTopic+"/stats")
err = c.Subscribe(devTopic+"/stats", ProcessStatsHandler(log, c, lsMQTT, devid, update.Topic, gwid))
if err != nil {
logitem.Error("Failed to link to device status topic: ", err)
reportDeviceStatus(err)
mqttBridge.RemoveLinksAll(update.Id)
continue
}
}
logitem.Debugf("Adding processor for %s", devGwTopic+"/stats")
err = c.Subscribe(devGwTopic+"/stats", ProcessStatsHandler(log, c, lsMQTT, devid, update.Topic, gwid))
if err != nil {
logitem.Error("Failed to link to device status topic: ", err)
reportDeviceStatus(err)
mqttBridge.RemoveLinksAll(update.Id)
continue
}
reportDeviceStatus(nil)
}
case <-signals:
goto cleanup
}
}
cleanup:
log.Info("Shutting down")
err = c.SetStatus("Shutting down")
if err != nil {
log.Error("Failed to publish service status: ", err)
return cli.NewExitError(nil, 1)
}
log.Info("Published Service Status")
if systemdIntegration {
daemon.SdNotify(false, daemon.SdNotifyStopping)
}
return cli.NewExitError(nil, 0)
}
func main() {
app := cli.NewApp()
app.Name = "lorawangw-service"
app.Usage = ""
app.Copyright = "See https://github.com/openchirp/lorawangw-service for copyright information"
app.Version = version
app.Action = run
app.Flags = []cli.Flag{
/* Communication to OpenChirp Framework */
cli.StringFlag{
Name: "framework-server",
Usage: "OpenChirp framework server's URI",
Value: "http://localhost:7000",
EnvVar: "FRAMEWORK_SERVER",
},
cli.StringFlag{
Name: "mqtt-server",
Usage: "MQTT server's URI (e.g. scheme://host:port where scheme is tcp or tls)",
Value: "tls://localhost:1883",
EnvVar: "MQTT_SERVER",
},
cli.StringFlag{
Name: "service-id",
Usage: "OpenChirp service id",
EnvVar: "SERVICE_ID",
},
cli.StringFlag{
Name: "service-token",
Usage: "OpenChirp service token",
EnvVar: "SERVICE_TOKEN",
},
cli.IntFlag{
Name: "log-level",
Value: 4,
Usage: "debug=5, info=4, warning=3, error=2, fatal=1, panic=0",
EnvVar: "LOG_LEVEL",
},
cli.BoolFlag{
Name: "systemd",
Usage: "Indicates that this service can use systemd specific interfaces.",
EnvVar: "SYSTEMD",
},
/* Communication to LoRaServer */
cli.StringFlag{
Name: "ls-mqtt-server",
Usage: "LoRa Server MQTT server's URI (e.g. scheme://host:port where scheme is tcp or tls)",
Value: defaultLsBroker,
EnvVar: "LS_MQTT_SERVER",
},
cli.UintFlag{
Name: "ls-mqtt-qos",
Usage: "LoRa Server MQTT server's QoS (0, 1, or 2)",
Value: defaultLsQoS,
EnvVar: "LS_MQTT_QOS",
},
cli.StringFlag{
Name: "ls-mqtt-user",
Usage: "LoRa Server MQTT server's username",
Value: defaultLsUser,
EnvVar: "LS_MQTT_USER",
},
cli.StringFlag{
Name: "ls-mqtt-pass",
Usage: "LoRa Server MQTT server's password",
Value: defaultLsPass,
EnvVar: "LS_MQTT_PASS",
},
}
app.Run(os.Args)
}