-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
423 lines (361 loc) · 11.4 KB
/
service.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
package main
import (
"fmt"
"sync"
"time"
"github.com/openchirp/framework"
"github.com/openchirp/framework/pubsub"
"github.com/openchirp/lorawan-service/lorawan/appserver"
. "github.com/openchirp/lorawan-service/lorawan/deviceconfig"
"github.com/openchirp/lorawan-service/lorawan/pubsubmanager"
"github.com/sirupsen/logrus"
)
const LorawanServiceModName = "LorawanService"
type LorawanService struct {
AppGRPCServer string
AppGRPCUser string
AppGRPCPass string
AppID int64
OrganizationID int64
NetworkServerID int64
AppMQTTBroker string
AppMQTTUser string
AppMQTTPass string
OCServer string
OCMQTTBroker string
OCID string
OCToken string
Log *logrus.Logger
client *framework.ServiceClient
updates <-chan framework.DeviceUpdate
app *appserver.AppServer
appMqtt *appserver.AppServerMQTT
configs map[string]DeviceConfig // OCID --> DeviceConfig
ocpubsub *pubsub.PubSub
pubsubman *pubsubmanager.PubSubManager
fatalerror chan error
stopruntime chan bool
runtimewait sync.WaitGroup
}
func statusErrMsg(err error) string {
return fmt.Sprintf("Error - %v", err)
}
func makelogitem(log *logrus.Logger, config DeviceConfig) *logrus.Entry {
return log.WithFields(logrus.Fields{
"Module": LorawanServiceModName,
"OCID": config.ID,
"OCName": config.Name,
"OCOwner": config.OwnerString(),
})
}
func (s *LorawanService) startOC() error {
logitem := s.Log.WithField("Module", LorawanServiceModName)
/* Start framework service client */
c, err := framework.StartServiceClientStatus(
s.OCServer,
s.OCMQTTBroker,
s.OCID,
s.OCToken,
"Unexpected disconnect!")
if err != nil {
logitem.Error("Failed to StartServiceClient: ", err)
return err
}
logitem.Debug("Started service")
/* Post service status indicating I am starting */
err = c.SetStatus("Starting")
if err != nil {
logitem.Error("Failed to publish service status: ", err)
return err
}
logitem.Debug("Published Service Status")
s.client = c
return nil
}
func (s *LorawanService) startAppserver() error {
logitem := s.Log.WithField("Module", LorawanServiceModName)
/* Connect to App Server's MQTT Broker */
// if ctx.Uint("app-mqtt-qos") < 0 || 2 < ctx.Uint("app-mqtt-qos") {
// log.Fatal("App Server QoS out of valid range")
// return err
// }
appMqtt, err := appserver.NewAppServerMqtt(
s.AppMQTTBroker,
s.AppMQTTUser,
s.AppMQTTPass,
s.AppID,
s.Log)
if err != nil {
logitem.Fatal("Failed to connect to App Server's MQTT Broker: ", err)
return err
}
s.appMqtt = appMqtt
/* Launch LoRaWAN Service */
s.configs = make(map[string]DeviceConfig)
s.app = appserver.NewAppServer(
s.AppGRPCServer,
s.AppID,
s.OrganizationID,
s.NetworkServerID,
s.Log)
logitem.Debugf("Connecting to lora app server as %s:%s", s.AppGRPCUser, s.AppGRPCPass)
if err := s.app.Login(s.AppGRPCUser, s.AppGRPCPass); err != nil {
logitem.Fatalf("Failed Login to App Server: %v\n", err)
}
if err := s.app.Connect(); err != nil {
logitem.Fatalf("Failed Connect to App Server: %v\n", err)
}
return nil
}
func (s *LorawanService) startPubsubManager() error {
s.pubsubman = pubsubmanager.NewPubSubManager(s.client, s.appMqtt, s.Log)
return nil
}
func (s *LorawanService) startOCUpdateStream() error {
logitem := s.Log.WithField("Module", LorawanServiceModName)
logitem.Info("Starting device updates stream")
updates, err := s.client.StartDeviceUpdates()
if err != nil {
logitem.Error("Failed to start device updates stream: ", err)
return err
}
s.updates = updates
return nil
}
func (s *LorawanService) syncConfigs() error {
logitem := s.Log.WithField("Module", LorawanServiceModName)
// fetch initial service configs
logitem.Info("Fetching framework initial device configs")
configUpdates, err := s.client.FetchDeviceConfigsAsUpdates()
if err != nil {
logitem.Error("Failed to fetch initial device configs: ", err)
return err
}
// sync
// Since we could fail to fetch info for a device, we need to leave the
// possibility of less DeviceConfig being created
configs := make([]DeviceConfig, 0, len(configUpdates))
for i := range configUpdates {
devconfig, err := DeviceUpdateAdapter{configUpdates[i]}.GetDeviceConfig(s.client)
if err != nil {
// Had problem fetching device info
logitem.Info(err)
continue
}
configs = append(configs, devconfig)
// Local logitem
logitem := logitem.WithFields(devconfig.OCDeviceInfo.LogrusFields())
logitem = logitem.WithField("DevEUI", devconfig.DevEUI)
logitem.Debug("Received DevConfig")
}
err = s.client.SetStatus("Synchronizing initial registered devices and app server")
if err != nil {
logitem.Fatal("Failed to publish service status: ", err)
return err
}
logitem.Debug("Synchronizing initial registered devices and app server")
cerrors, err := s.app.DeviceRegistrationSync(configs)
if err != nil {
logitem.Fatal(err)
return err
}
// report any config sync errors -- add DeviceConfigs that succeed
for i, e := range cerrors {
devConfig := configs[i]
logitem := makelogitem(s.Log, devConfig)
if e != nil {
logitem.Infof("Failed to add or update config: %v", e)
if err := s.client.SetDeviceStatus(devConfig.ID, statusErrMsg(e)); err != nil {
logitem.Errorf("Failed to post device status: %v", err)
}
} else {
s.configs[devConfig.ID] = devConfig
s.pubsubman.Add(devConfig)
if err := s.client.SetDeviceStatus(devConfig.ID, deviceStatusSuccess); err != nil {
logitem.Errorf("Failed to post device status: %v", err)
}
}
}
return nil
}
func (s *LorawanService) runtime() {
defer s.runtimewait.Done()
logitem := s.Log.WithField("Module", LorawanServiceModName)
/* Start runtime event loop */
for {
select {
case <-s.stopruntime:
return
case update := <-s.updates:
if err := s.processUpdate(update); err != nil {
s.fatalerror <- err
<-s.stopruntime
return
}
case <-time.After(appServerJWTRefreshTime):
logitem.Debug("Reconnecting to app server")
if err := s.app.ReLogin(); err != nil {
logitem.Fatalf("Failed to relogin the app server: %v\n", err)
err = s.client.SetStatus("Failed to relogin to app server: ", err)
if err != nil {
logitem.Error("Failed to publish service status: ", err)
}
}
}
}
}
func (s *LorawanService) processUpdate(update framework.DeviceUpdate) error {
logitem := s.Log.WithFields(logrus.Fields{
"Module": LorawanServiceModName,
"UpdateType": update.Type,
"OCID": update.Id,
})
/* If runningStatus is set, post a service status as an alive msg */
if runningStatus {
if err := s.client.SetStatus("Running"); err != nil {
logitem.Error("Failed to publish service status: ", err)
return err
}
logitem.Debug("Published Service Status")
}
logitem.Debug("Fetching device info")
devconfig, err := DeviceUpdateAdapter{update}.GetDeviceConfig(s.client)
if err != nil {
// Had problem fetching device info
logitem.Errorf("Failed to fetch OC info: %v", err)
return nil
}
logitem = logitem.WithFields(devconfig.LogrusFields())
processadd := func() error {
logitem.Info("Process Add")
if err := s.app.DeviceRegister(devconfig); err != nil {
logitem.Infof("Failed to add device config: %v", err)
errMsg := statusErrMsg(err)
if e := s.client.SetDeviceStatus(devconfig.ID, errMsg); e != nil {
logitem.Errorf("Failed to post device status: %v", e)
}
return nil
}
s.configs[devconfig.ID] = devconfig
if err := s.pubsubman.Add(devconfig); err != nil {
logitem.Errorf("Failed to add device to pubsubmanager: %v", err)
}
if err := s.client.SetDeviceStatus(devconfig.ID, deviceStatusSuccess); err != nil {
logitem.Errorf("Failed to post device status: %v", err)
}
return nil
}
processremove := func() error {
logitem.Info("Process Remove")
defer delete(s.configs, devconfig.ID)
oldconfig := s.configs[devconfig.ID]
if err := s.pubsubman.Remove(oldconfig); err != nil {
logitem.Errorf("Failed to remove device from pubsubmanager: %v", err)
}
if err := s.app.DeviceDeregister(oldconfig); err != nil {
logitem.Infof("Failed to deregister device config: %v", err)
return nil
}
return nil
}
processupdate := func() error {
logitem.Info("Process Update")
if oldconfig, ok := s.configs[devconfig.ID]; ok {
if err := s.app.DeviceUpdate(oldconfig, devconfig); err != nil {
logitem.Infof("Failed to update device config: %v", err)
/* This update failed, so remove all references to this device */
if err := s.pubsubman.Remove(oldconfig); err != nil {
logitem.Errorf("Failed to remove device from pubsubmanager: %v", err)
}
delete(s.configs, devconfig.ID)
errMsg := statusErrMsg(err)
if e := s.client.SetDeviceStatus(devconfig.ID, errMsg); e != nil {
logitem.Errorf("Failed to post device status: %v", e)
}
return nil
}
if err := s.pubsubman.Update(oldconfig, devconfig); err != nil {
logitem.Errorf("Failed to update device in pubsubmanager: %v", err)
}
s.configs[devconfig.ID] = devconfig
if err := s.client.SetDeviceStatus(devconfig.ID, deviceStatusSuccess); err != nil {
logitem.Errorf("Failed to post device status: %v", err)
}
} else {
logitem.Info("Diverting to Add process")
return processadd()
}
return nil
}
switch update.Type {
case framework.DeviceUpdateTypeAdd:
return processadd()
case framework.DeviceUpdateTypeRem:
return processremove()
case framework.DeviceUpdateTypeUpd:
return processupdate()
case framework.DeviceUpdateTypeErr:
logitem.Errorf(update.Error())
}
return nil
}
func (s *LorawanService) Start() error {
logitem := s.Log.WithField("Module", LorawanServiceModName)
if err := s.startOC(); err != nil {
return fmt.Errorf("Failed to start OC connection: %v", err)
}
if err := s.startAppserver(); err != nil {
return fmt.Errorf("Failed to start app server connection: %v", err)
}
if err := s.startPubsubManager(); err != nil {
return fmt.Errorf("Failed to start pubsub manager(bridge): %v", err)
}
if err := s.startOCUpdateStream(); err != nil {
return fmt.Errorf("Failed to start OC device update stream: %v", err)
}
if err := s.syncConfigs(); err != nil {
return fmt.Errorf("Failed to sync configs: %v", err)
}
/* Post service status indicating I started */
if err := s.client.SetStatus("Started"); err != nil {
return fmt.Errorf("Failed to publish service status: %v", err)
}
logitem.Debug("Published Service Status")
s.stopruntime = make(chan bool)
s.runtimewait.Add(1)
go s.runtime()
return nil
}
func (s *LorawanService) Stop() error {
logitem := s.Log.WithField("Module", LorawanServiceModName)
logitem.Warning("Shutting down")
if err := s.client.SetStatus("Shutting down"); err != nil {
logitem.Error("Failed to publish service status: ", err)
}
logitem.Info("Published service status")
s.stopruntime <- true
close(s.stopruntime)
s.client.StopDeviceUpdates()
s.client.StopClient()
s.appMqtt.Disconnect()
err := s.app.Disconnect()
s.runtimewait.Wait()
return err
}
func (s *LorawanService) FatalError() <-chan error {
return s.fatalerror
}
func (s *LorawanService) DebugDump() {
// Temporarily change log level to info
originalLevel := s.Log.Level
s.Log.Level = logrus.DebugLevel
s.app.DebugDump()
s.pubsubman.DebugDump()
logitem := s.Log.WithField("Module", LorawanServiceModName).WithField("Debug", "dump")
logitem.Debugf("# Dumping ID to Config Maps")
for id, config := range s.configs {
logitem := logitem.WithFields(config.LogrusFields())
logitem.Debug("KEY: ID ", id)
}
s.Log.Level = originalLevel
}