@alwaysai/device-agent 1.0.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/lib/application-control/environment-variables.d.ts +1 -1
  2. package/lib/application-control/environment-variables.d.ts.map +1 -1
  3. package/lib/cloud-connection/device-agent-cloud-connection.d.ts +3 -2
  4. package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
  5. package/lib/cloud-connection/device-agent-cloud-connection.js +81 -59
  6. package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
  7. package/lib/cloud-connection/live-updates-handler.d.ts.map +1 -1
  8. package/lib/cloud-connection/live-updates-handler.js +2 -4
  9. package/lib/cloud-connection/live-updates-handler.js.map +1 -1
  10. package/lib/cloud-connection/live-updates-handler.test.js +1 -1
  11. package/lib/cloud-connection/live-updates-handler.test.js.map +1 -1
  12. package/lib/cloud-connection/messages.js +6 -6
  13. package/lib/cloud-connection/messages.js.map +1 -1
  14. package/lib/cloud-connection/passthrough-handler.d.ts.map +1 -1
  15. package/lib/cloud-connection/passthrough-handler.js +37 -11
  16. package/lib/cloud-connection/passthrough-handler.js.map +1 -1
  17. package/lib/cloud-connection/publisher.d.ts +5 -4
  18. package/lib/cloud-connection/publisher.d.ts.map +1 -1
  19. package/lib/cloud-connection/publisher.js +9 -8
  20. package/lib/cloud-connection/publisher.js.map +1 -1
  21. package/lib/cloud-connection/shadow-handler.d.ts +5 -0
  22. package/lib/cloud-connection/shadow-handler.d.ts.map +1 -1
  23. package/lib/cloud-connection/shadow-handler.js +20 -5
  24. package/lib/cloud-connection/shadow-handler.js.map +1 -1
  25. package/lib/cloud-connection/shadow-handler.test.js +9 -0
  26. package/lib/cloud-connection/shadow-handler.test.js.map +1 -1
  27. package/lib/cloud-connection/transaction-manager.d.ts +10 -0
  28. package/lib/cloud-connection/transaction-manager.d.ts.map +1 -0
  29. package/lib/cloud-connection/transaction-manager.js +41 -0
  30. package/lib/cloud-connection/transaction-manager.js.map +1 -0
  31. package/lib/cloud-connection/transaction-manager.test.d.ts +2 -0
  32. package/lib/cloud-connection/transaction-manager.test.d.ts.map +1 -0
  33. package/lib/cloud-connection/transaction-manager.test.js +63 -0
  34. package/lib/cloud-connection/transaction-manager.test.js.map +1 -0
  35. package/lib/device-control/device-control.d.ts +58 -3
  36. package/lib/device-control/device-control.d.ts.map +1 -1
  37. package/lib/device-control/device-control.js +141 -14
  38. package/lib/device-control/device-control.js.map +1 -1
  39. package/lib/subcommands/app/env-vars.d.ts.map +1 -1
  40. package/lib/subcommands/app/env-vars.js +15 -9
  41. package/lib/subcommands/app/env-vars.js.map +1 -1
  42. package/lib/subcommands/device/clean.d.ts.map +1 -1
  43. package/lib/subcommands/device/clean.js +14 -12
  44. package/lib/subcommands/device/clean.js.map +1 -1
  45. package/lib/subcommands/device/device.d.ts +1 -0
  46. package/lib/subcommands/device/device.d.ts.map +1 -1
  47. package/lib/subcommands/device/device.js +26 -7
  48. package/lib/subcommands/device/device.js.map +1 -1
  49. package/lib/subcommands/device/index.js +1 -1
  50. package/lib/subcommands/device/index.js.map +1 -1
  51. package/lib/util/logger.d.ts.map +1 -1
  52. package/lib/util/logger.js +1 -1
  53. package/lib/util/logger.js.map +1 -1
  54. package/lib/util/safe-rimraf.d.ts +2 -0
  55. package/lib/util/safe-rimraf.d.ts.map +1 -0
  56. package/lib/util/safe-rimraf.js +16 -0
  57. package/lib/util/safe-rimraf.js.map +1 -0
  58. package/package.json +4 -2
  59. package/readme.md +1 -0
  60. package/src/application-control/environment-variables.ts +1 -1
  61. package/src/cloud-connection/device-agent-cloud-connection.ts +125 -80
  62. package/src/cloud-connection/live-updates-handler.test.ts +2 -2
  63. package/src/cloud-connection/live-updates-handler.ts +2 -8
  64. package/src/cloud-connection/messages.ts +9 -9
  65. package/src/cloud-connection/passthrough-handler.ts +43 -10
  66. package/src/cloud-connection/publisher.ts +27 -10
  67. package/src/cloud-connection/shadow-handler.test.ts +9 -0
  68. package/src/cloud-connection/shadow-handler.ts +25 -11
  69. package/src/cloud-connection/transaction-manager.test.ts +73 -0
  70. package/src/cloud-connection/transaction-manager.ts +43 -0
  71. package/src/device-control/device-control.ts +142 -9
  72. package/src/subcommands/app/env-vars.ts +17 -10
  73. package/src/subcommands/device/clean.ts +22 -13
  74. package/src/subcommands/device/device.ts +36 -9
  75. package/src/subcommands/device/index.ts +2 -2
  76. package/src/util/logger.ts +15 -10
  77. package/src/util/safe-rimraf.ts +14 -0
  78. package/lib/cloud-connection/transaction-queue.d.ts +0 -12
  79. package/lib/cloud-connection/transaction-queue.d.ts.map +0 -1
  80. package/lib/cloud-connection/transaction-queue.js +0 -38
  81. package/lib/cloud-connection/transaction-queue.js.map +0 -1
  82. package/lib/cloud-connection/transaction-queue.test.d.ts +0 -2
  83. package/lib/cloud-connection/transaction-queue.test.d.ts.map +0 -1
  84. package/lib/cloud-connection/transaction-queue.test.js +0 -46
  85. package/lib/cloud-connection/transaction-queue.test.js.map +0 -1
  86. package/src/cloud-connection/transaction-queue.test.ts +0 -55
  87. package/src/cloud-connection/transaction-queue.ts +0 -40
@@ -21,7 +21,8 @@ import {
21
21
  AppStateControlPayload,
22
22
  AppVersionControlInstallPayload,
23
23
  AppVersionControlUninstallPayload,
24
- ToClientMessage
24
+ ToClientMessage,
25
+ DeviceActionPayload
25
26
  } from '@alwaysai/device-agent-schemas';
26
27
  import { getDeviceUuid } from '../util/get-device-id';
27
28
  import { logger } from '../util/logger';
@@ -49,19 +50,19 @@ import { getStatusResponsePayload } from './messages';
49
50
  import { ModelsInstallResponsePayload } from '@alwaysai/device-agent-schemas';
50
51
  import sleep from '../util/sleep';
51
52
  import { createAppBackup, rollbackApp } from '../application-control/backup';
52
- import { TransactionQueue } from './transaction-queue';
53
+ import { TransactionManager } from './transaction-manager';
53
54
  import {
54
55
  buildSignedUrlsRequestMessage,
55
56
  buildStatusResponseMessage
56
57
  } from './message-builder';
57
- import { generateTxId } from '@alwaysai/device-agent-schemas';
58
+ import { reboot } from '../device-control/device-control';
58
59
 
59
60
  export class DeviceAgentCloudConnection {
60
61
  private shadowHandler: ShadowHandler;
61
62
  public publisher: Publisher;
62
63
  private cmdStatusMgr: CmdStatusManager;
63
64
  private liveUpdatesHandler: LiveUpdatesHandler;
64
- private txnQueue: TransactionQueue;
65
+ private txnMgr: TransactionManager;
65
66
  private device = awsIot.device;
66
67
 
67
68
  private clientId = getDeviceUuid();
@@ -185,6 +186,21 @@ export class DeviceAgentCloudConnection {
185
186
  return true;
186
187
  };
187
188
 
189
+ private async handleDeviceAction(payload: DeviceActionPayload) {
190
+ const { system_restart } = keyMirrors.deviceAction;
191
+ switch (payload.action) {
192
+ case system_restart: {
193
+ await reboot();
194
+ break;
195
+ }
196
+ default: {
197
+ logger.info(
198
+ `Unrecognized device action requested: '${payload.action}'.`
199
+ );
200
+ }
201
+ }
202
+ }
203
+
188
204
  private async publishCloudRequest(message: ToCloudMessage) {
189
205
  this.publisher.publishToCloud(message);
190
206
  }
@@ -223,6 +239,7 @@ export class DeviceAgentCloudConnection {
223
239
 
224
240
  try {
225
241
  const out: R = await func(...args);
242
+ this.shadowHandler.clearAppConfig(projectId);
226
243
  await this.shadowHandler.updateProjectShadow(projectId);
227
244
  return out;
228
245
  } catch (errorAppUpdate) {
@@ -274,13 +291,13 @@ export class DeviceAgentCloudConnection {
274
291
  await this.liveUpdatesHandler.disableTransactionStatus({
275
292
  txId
276
293
  });
277
- const sucessStatusResponsePayload = await getStatusResponsePayload(
294
+ const successStatusResponsePayload = await getStatusResponsePayload(
278
295
  keyMirrors.statusResponse.success,
279
296
  ''
280
297
  );
281
298
  // Send final status message
282
299
  const message = await buildStatusResponseMessage(
283
- sucessStatusResponsePayload,
300
+ successStatusResponsePayload,
284
301
  txId
285
302
  );
286
303
  this.publisher.publishToClient(message);
@@ -311,62 +328,60 @@ export class DeviceAgentCloudConnection {
311
328
  }
312
329
  }
313
330
 
314
- private handleAppConfigUpdates = async (
315
- updates: ShadowUpdate[],
331
+ private handleAppConfigUpdate = async (
332
+ update: ShadowUpdate,
316
333
  txId: string
317
334
  ): Promise<boolean> => {
318
- for (const update of updates) {
319
- const { projectId, appCfgUpdate, envVarUpdate } = update;
320
-
321
- if (
322
- appCfgUpdate &&
323
- appCfgUpdate.updatedModels &&
324
- Object.keys(appCfgUpdate.updatedModels).length
325
- ) {
326
- // When there are model updates request signed URLs and wait to apply config changes
327
- const { updatedModels } = appCfgUpdate;
328
-
329
- logger.debug(
330
- `Requesting presigned urls from cloud for model versions: ${JSON.stringify(
331
- updatedModels
332
- )}`
333
- );
334
- const modelsOnlyUrlsRequestPayload: SignedUrlsRequestPayload = {
335
- modelsOnlyUrlsRequest: {
336
- projectId,
337
- models: updatedModels
338
- }
339
- };
340
- const message = await buildSignedUrlsRequestMessage(
341
- modelsOnlyUrlsRequestPayload,
342
- txId
343
- );
344
- this.publisher.publishToCloud(message);
335
+ const { projectId, appCfgUpdate, envVarUpdate } = update;
336
+
337
+ if (
338
+ appCfgUpdate &&
339
+ appCfgUpdate.updatedModels &&
340
+ Object.keys(appCfgUpdate.updatedModels).length
341
+ ) {
342
+ // When there are model updates request signed URLs and wait to apply config changes
343
+ const { updatedModels } = appCfgUpdate;
344
+
345
+ logger.debug(
346
+ `Requesting presigned urls from cloud for model versions: ${JSON.stringify(
347
+ updatedModels
348
+ )}`
349
+ );
350
+ const modelsOnlyUrlsRequestPayload: SignedUrlsRequestPayload = {
351
+ modelsOnlyUrlsRequest: {
352
+ projectId,
353
+ models: updatedModels
354
+ }
355
+ };
356
+ const message = await buildSignedUrlsRequestMessage(
357
+ modelsOnlyUrlsRequestPayload,
358
+ txId
359
+ );
360
+ this.publisher.publishToCloud(message);
345
361
 
346
- this.appCfgUpdateQueue.push(update);
347
- return false;
348
- }
362
+ this.appCfgUpdateQueue.push(update);
363
+ return false;
364
+ }
349
365
 
350
- if (appCfgUpdate) {
351
- await this.atomicApplicationUpdate(
352
- updateAppCfg,
353
- [
354
- {
355
- projectId,
356
- newAppCfg: appCfgUpdate.newAppCfg
357
- }
358
- ],
359
- projectId
360
- );
361
- }
366
+ if (appCfgUpdate) {
367
+ await this.atomicApplicationUpdate(
368
+ updateAppCfg,
369
+ [
370
+ {
371
+ projectId,
372
+ newAppCfg: appCfgUpdate.newAppCfg
373
+ }
374
+ ],
375
+ projectId
376
+ );
377
+ }
362
378
 
363
- if (envVarUpdate) {
364
- await this.atomicApplicationUpdate(
365
- setEnv,
366
- [{ projectId, envVars: envVarUpdate.envVars }],
367
- projectId
368
- );
369
- }
379
+ if (envVarUpdate) {
380
+ await this.atomicApplicationUpdate(
381
+ setEnv,
382
+ [{ projectId, envVars: envVarUpdate.envVars }],
383
+ projectId
384
+ );
370
385
  }
371
386
  return true;
372
387
  };
@@ -392,7 +407,7 @@ export class DeviceAgentCloudConnection {
392
407
  this.publisher,
393
408
  this.clientId
394
409
  );
395
- this.txnQueue = new TransactionQueue();
410
+ this.txnMgr = new TransactionManager();
396
411
 
397
412
  this.subscribe(this.toDeviceTopic);
398
413
  this.subscribe(this.secureTunnelNotifyTopic);
@@ -450,7 +465,7 @@ export class DeviceAgentCloudConnection {
450
465
  const projectId = payload.projectId;
451
466
 
452
467
  try {
453
- this.txnQueue.addTxIdToQueue(txId, projectId);
468
+ this.txnMgr.addTransaction(txId, projectId);
454
469
  const completed = await this.atomicCmd({
455
470
  func: this.handleAppStateControl,
456
471
  args: [message.payload],
@@ -458,7 +473,7 @@ export class DeviceAgentCloudConnection {
458
473
  txId
459
474
  });
460
475
  if (completed) {
461
- this.txnQueue.completeTxn(txId);
476
+ this.txnMgr.completeTransaction(txId);
462
477
  }
463
478
  } catch (e) {
464
479
  logger.error(
@@ -477,7 +492,7 @@ export class DeviceAgentCloudConnection {
477
492
  ? payload.appReleaseHash
478
493
  : undefined;
479
494
  try {
480
- this.txnQueue.addTxIdToQueue(txId, projectId);
495
+ this.txnMgr.addTransaction(txId, projectId);
481
496
  const completed = await this.atomicCmd({
482
497
  func: this.handleAppVersionControl,
483
498
  args: [payload, txId],
@@ -485,7 +500,7 @@ export class DeviceAgentCloudConnection {
485
500
  txId
486
501
  });
487
502
  if (completed) {
488
- this.txnQueue.completeTxn(txId);
503
+ this.txnMgr.completeTransaction(txId);
489
504
  }
490
505
  } catch (e) {
491
506
  logger.error(`Error processing application install request: ${e}!`);
@@ -502,9 +517,11 @@ export class DeviceAgentCloudConnection {
502
517
  case keyMirrors.toDeviceAgentMessageType.app_install_response: {
503
518
  const payload = message.payload;
504
519
  const { projectId, appReleaseHash } = payload.appInstallResponse;
505
- if (txId !== this.txnQueue.getCurrentTxId()) {
520
+ if (txId !== this.txnMgr.getTransactionFromProject(projectId)) {
506
521
  throw new Error(
507
- `App install response received a message for a transaction ID ${txId} that is not currently underway (${this.txnQueue.getCurrentTxId()})!`
522
+ `App install response received a message for a transaction ID ${txId} that is not currently underway (${this.txnMgr.getTransactionFromProject(
523
+ projectId
524
+ )})!`
508
525
  );
509
526
  }
510
527
  const completed = await this.atomicCmd({
@@ -514,7 +531,7 @@ export class DeviceAgentCloudConnection {
514
531
  txId
515
532
  });
516
533
  if (completed) {
517
- this.txnQueue.completeTxn(txId);
534
+ this.txnMgr.completeTransaction(txId);
518
535
  }
519
536
 
520
537
  break;
@@ -524,9 +541,11 @@ export class DeviceAgentCloudConnection {
524
541
  // atomicCmd should be able to read it from the installed app
525
542
  const payload = message.payload;
526
543
  const { projectId } = payload.modelsInstallResponse;
527
- if (txId !== this.txnQueue.getCurrentTxId()) {
544
+ if (txId !== this.txnMgr.getTransactionFromProject(projectId)) {
528
545
  throw new Error(
529
- `Model install response received a message for a transaction ID ${txId} that is not currently underway (${this.txnQueue.getCurrentTxId()})!`
546
+ `Model install response received a message for a transaction ID ${txId} that is not currently underway (${this.txnMgr.getTransactionFromProject(
547
+ projectId
548
+ )})!`
530
549
  );
531
550
  }
532
551
  const completed = await this.atomicCmd({
@@ -536,11 +555,40 @@ export class DeviceAgentCloudConnection {
536
555
  txId
537
556
  });
538
557
  if (completed) {
539
- this.txnQueue.completeTxn(txId);
558
+ this.txnMgr.completeTransaction(txId);
540
559
  }
541
560
 
542
561
  break;
543
562
  }
563
+ case keyMirrors.toDeviceAgentMessageType.device_action: {
564
+ try {
565
+ const successStatusResponsePayload = await getStatusResponsePayload(
566
+ keyMirrors.statusResponse.success,
567
+ ''
568
+ );
569
+ const successStatusResponseMessage = await buildStatusResponseMessage(
570
+ successStatusResponsePayload,
571
+ txId
572
+ );
573
+ this.publisher.publishToClient(successStatusResponseMessage);
574
+
575
+ await this.handleDeviceAction(message.payload);
576
+ } catch (e) {
577
+ logger.error(
578
+ `There was a problem performing device action '${message.payload.action}': ${e.message}`
579
+ );
580
+ const failureStatusResponsePayload = await getStatusResponsePayload(
581
+ keyMirrors.statusResponse.failure,
582
+ e.message
583
+ );
584
+ const failureStatusResponseMessage = await buildStatusResponseMessage(
585
+ failureStatusResponsePayload,
586
+ txId
587
+ );
588
+ this.publisher.publishToClient(failureStatusResponseMessage);
589
+ }
590
+ break;
591
+ }
544
592
  default:
545
593
  logger.error(
546
594
  `Invalid client message: '${JSON.stringify(
@@ -559,7 +607,6 @@ export class DeviceAgentCloudConnection {
559
607
  logger.debug(
560
608
  `Received message: ${JSON.stringify({ topic, message }, null, 2)}`
561
609
  );
562
- const txId = message.txId || generateTxId(); // txId should be passed in for DeviceAgentMessage messages but shadow updates won't have this
563
610
  switch (topic) {
564
611
  case this.shadowHandler.shadowTopics.projects.getAccepted:
565
612
  case this.shadowHandler.shadowTopics.projects.updateDelta: {
@@ -569,26 +616,23 @@ export class DeviceAgentCloudConnection {
569
616
  clientToken: message.clientToken
570
617
  });
571
618
  if (shadowUpdates.length) {
572
- // FIXME: Take project ID of first shadow update. Most likely there will only be one update
573
- // so this should be sufficient for now.
574
- const projectId = shadowUpdates[0].projectId;
575
- try {
619
+ for (const shadowUpdate of shadowUpdates) {
620
+ const projectId = shadowUpdate.projectId;
621
+ const txId = shadowUpdate.txId;
576
622
  try {
577
- this.txnQueue.addTxIdToQueue(txId, projectId);
623
+ this.txnMgr.addTransaction(txId, projectId);
578
624
  const completed = await this.atomicCmd({
579
- func: this.handleAppConfigUpdates,
580
- args: [shadowUpdates, txId],
625
+ func: this.handleAppConfigUpdate,
626
+ args: [shadowUpdate, txId],
581
627
  projectId,
582
628
  txId
583
629
  });
584
630
  if (completed) {
585
- this.txnQueue.completeTxn(txId);
631
+ this.txnMgr.completeTransaction(txId);
586
632
  }
587
633
  } catch (e) {
588
- logger.error(`Error processing model update request: ${e}!`);
634
+ logger.error(`Error handling shadow message: ${e.message}`);
589
635
  }
590
- } catch (e) {
591
- logger.error(`Error handling shadow message: ${e.message}`);
592
636
  }
593
637
  }
594
638
  break;
@@ -619,6 +663,7 @@ export class DeviceAgentCloudConnection {
619
663
  // FIXME: EI-709 Skip this request for now to prevent kicking off another
620
664
  // shadow update process if IoT Core disconnect occurs during app config update
621
665
  //this.shadowHandler.getShadowUpdates();
666
+ void this.shadowHandler.updateSystemInfoShadow();
622
667
  });
623
668
 
624
669
  this.device.on('disconnect', () => {
@@ -1,6 +1,8 @@
1
1
  import { LiveUpdatesHandler } from './live-updates-handler';
2
2
  import { Publisher } from './publisher';
3
3
 
4
+ global.setTimeout = jest.fn() as unknown as typeof setTimeout;
5
+
4
6
  const testTrueToggles = {
5
7
  deviceStats: true,
6
8
  appState: true
@@ -15,8 +17,6 @@ const mockClient = jest.fn();
15
17
  const clientId = 'test-client';
16
18
  const emptyTxId = '';
17
19
 
18
- jest.spyOn(global, 'setTimeout');
19
-
20
20
  // NOTE: this was the way I found to mock private class functions
21
21
  const mockStartPublishingLiveUpdates = jest.spyOn(
22
22
  LiveUpdatesHandler.prototype as any,
@@ -69,7 +69,7 @@ export class LiveUpdatesHandler {
69
69
  logChunk: logStr
70
70
  };
71
71
  const message = await buildAppLogsMessage(payload, this.clientId);
72
- this.publisher.publishToClient(message);
72
+ this.publisher.publishToClient(message, logger.silly);
73
73
  });
74
74
 
75
75
  readable.on('error', (error) => {
@@ -153,14 +153,8 @@ export class LiveUpdatesHandler {
153
153
  const payload: ToClientMessagePayload = await payloadBuilderFunction(
154
154
  ...args
155
155
  );
156
- logger.debug(
157
- `payload returned from builder: ${JSON.stringify(payload)}`
158
- );
159
156
  const message = await messageBuilderFunction(payload, txId);
160
- logger.debug(
161
- `message returned from builder: ${JSON.stringify(message)}`
162
- );
163
- this.publisher.publishToClient(message);
157
+ this.publisher.publishToClient(message, logger.silly);
164
158
  } catch (e) {
165
159
  logger.error(
166
160
  `Error publishing live updates for ${messageType}: ${e.message}`
@@ -7,9 +7,9 @@ import {
7
7
  import { StatusResponseValue } from '@alwaysai/device-agent-schemas/lib/constants';
8
8
  import { getAppState } from '../application-control';
9
9
  import {
10
- getCpuUtil,
11
- getDiskUtil,
12
- getMemUtil
10
+ getCpuDetails,
11
+ getDiskDetails,
12
+ getMemDetails
13
13
  } from '../device-control/device-control';
14
14
  import { AgentConfigFile } from '../infrastructure/agent-config';
15
15
 
@@ -39,14 +39,14 @@ export async function getStatusResponsePayload(
39
39
  }
40
40
 
41
41
  export async function getDeviceStatsPayload(): Promise<DeviceStatsPayload> {
42
- const cpuUsage = await getCpuUtil();
43
- const diskUtil = await getDiskUtil();
44
- const memUtil = await getMemUtil();
42
+ const cpuDetails = await getCpuDetails();
43
+ const diskDetails = await getDiskDetails();
44
+ const memDetails = await getMemDetails();
45
45
 
46
46
  const deviceStatsPayload: DeviceStatsPayload = {
47
- cpuUsage,
48
- diskUtil,
49
- usedMemoryPercentage: memUtil
47
+ cpuDetails,
48
+ diskDetails,
49
+ memDetails
50
50
  };
51
51
  return deviceStatsPayload;
52
52
  }
@@ -70,18 +70,51 @@ function processPublish(passthroughHandler: PassthroughHandler) {
70
70
  while (messageQueue.length > 0) {
71
71
  const entry = messageQueue.shift();
72
72
  const { packet, msg } = entry;
73
- ackQueue.push(msg);
74
- // FIXME: put real topic here
75
- passthroughHandler.publisher.publishToCloudWithAck(packet, (errOrResp) => {
76
- while (ackQueue.length > 0) {
77
- const msg = ackQueue.shift();
78
- if (errOrResp === true) {
79
- passthroughHandler.channel.ack(msg); // acknowledge, allow queue to discard
80
- } else if (errOrResp === false) {
81
- passthroughHandler.channel.reject(msg, true); // reject and requeue
73
+ try {
74
+ const parsedPacket = JSON.parse(packet);
75
+ if (parsedPacket && parsedPacket['action']) {
76
+ switch (parsedPacket['action']) {
77
+ case 'analytics':
78
+ ackQueue.push(msg);
79
+ // FIXME: put real topic here
80
+ passthroughHandler.publisher.publishToCloudWithAck(
81
+ packet,
82
+ (errOrResp) => {
83
+ while (ackQueue.length > 0) {
84
+ const msg = ackQueue.shift();
85
+ if (errOrResp === true) {
86
+ passthroughHandler.channel.ack(msg); // acknowledge, allow queue to discard
87
+ } else if (errOrResp === false) {
88
+ passthroughHandler.channel.reject(msg, true); // reject and requeue
89
+ }
90
+ }
91
+ }
92
+ );
93
+ break;
94
+ case 'heartbeat':
95
+ passthroughHandler.channel.ack(msg);
96
+ logger.debug(
97
+ `Heartbeat package received & acknowledged: ${packet}`
98
+ );
99
+ break;
100
+ default:
101
+ passthroughHandler.channel.ack(msg);
102
+ logger.debug(
103
+ `Unknown 'action' package received & acknowledged: ${packet}`
104
+ );
105
+ break;
82
106
  }
107
+ } else {
108
+ passthroughHandler.channel.ack(msg);
109
+ logger.debug(
110
+ `Received & acknowledged a RabbitMQ Package of unknown structure: ${parsedPacket}`
111
+ );
83
112
  }
84
- });
113
+ } catch (e) {
114
+ logger.error(`There was a problem parsing RabbitMQ packet ${e}`);
115
+ passthroughHandler.channel.ack(msg);
116
+ logger.debug(`Problematic packet was acknowledged`);
117
+ }
85
118
  }
86
119
  }
87
120
 
@@ -5,6 +5,7 @@ import {
5
5
  getToClientTopic,
6
6
  getToCloudTopic
7
7
  } from '@alwaysai/device-agent-schemas';
8
+ import * as winston from 'winston';
8
9
 
9
10
  export class Publisher {
10
11
  private client: any;
@@ -19,8 +20,20 @@ export class Publisher {
19
20
  this.toCloudTopic = getToCloudTopic(this.clientId);
20
21
  }
21
22
 
22
- public publish(topic: string, payload: string) {
23
+ public publish(
24
+ topic: string,
25
+ payload: string,
26
+ customLogger: winston.LeveledLogMethod = logger.debug
27
+ ) {
23
28
  // TODO: topic validation
29
+ // By default, log the published message at debug level, unless otherwise specified
30
+ customLogger(
31
+ `Publishing message:\nTopic: ${topic}\nMessage: ${JSON.stringify(
32
+ JSON.parse(payload),
33
+ null,
34
+ 2
35
+ )}`
36
+ );
24
37
  this.client.publish(topic, payload, (err: any) => {
25
38
  if (err) {
26
39
  logger.error(
@@ -52,21 +65,25 @@ export class Publisher {
52
65
 
53
66
  public publishDeviceAgentMessage(
54
67
  topic: string,
55
- message: ToClientMessage | ToCloudMessage
68
+ message: ToClientMessage | ToCloudMessage,
69
+ logger?: winston.LeveledLogMethod
56
70
  ) {
57
71
  const messageStr = JSON.stringify(message);
58
- logger.debug(
59
- `Publishing message:\n${JSON.stringify({ topic, message }, null, 2)}`
60
- );
61
- this.publish(topic, messageStr);
72
+ this.publish(topic, messageStr, logger);
62
73
  }
63
74
 
64
- public publishToClient(message: ToClientMessage) {
65
- this.publishDeviceAgentMessage(this.toClientTopic, message);
75
+ public publishToClient(
76
+ message: ToClientMessage,
77
+ logger?: winston.LeveledLogMethod
78
+ ) {
79
+ this.publishDeviceAgentMessage(this.toClientTopic, message, logger);
66
80
  }
67
81
 
68
- public publishToCloud(message: ToCloudMessage) {
82
+ public publishToCloud(
83
+ message: ToCloudMessage,
84
+ logger?: winston.LeveledLogMethod
85
+ ) {
69
86
  // Can edit topic field in message here if we want
70
- this.publishDeviceAgentMessage(this.toCloudTopic, message);
87
+ this.publishDeviceAgentMessage(this.toCloudTopic, message, logger);
71
88
  }
72
89
  }
@@ -113,6 +113,7 @@ describe('Test Shadow Handler', () => {
113
113
  expect(updates.length).toBe(1);
114
114
  expect(updates[0]).toEqual({
115
115
  projectId: projectId1,
116
+ txId: expect.any(String),
116
117
  appCfgUpdate: {
117
118
  newAppCfg: appCfg1,
118
119
  updatedModels: {
@@ -157,6 +158,7 @@ describe('Test Shadow Handler', () => {
157
158
  expect(updates.length).toBe(1);
158
159
  expect(updates[0]).toEqual({
159
160
  projectId: projectId1,
161
+ txId: expect.any(String),
160
162
  appCfgUpdate: {
161
163
  newAppCfg: appCfg1,
162
164
  updatedModels: {
@@ -222,6 +224,7 @@ describe('Test Shadow Handler', () => {
222
224
  expect(updates.length).toBe(2);
223
225
  expect(updates[0]).toEqual({
224
226
  projectId: projectId1,
227
+ txId: expect.any(String),
225
228
  appCfgUpdate: {
226
229
  newAppCfg: appCfg1,
227
230
  updatedModels: {
@@ -232,6 +235,7 @@ describe('Test Shadow Handler', () => {
232
235
  });
233
236
  expect(updates[1]).toEqual({
234
237
  projectId: projectId2,
238
+ txId: expect.any(String),
235
239
  appCfgUpdate: {
236
240
  newAppCfg: appCfg2,
237
241
  updatedModels: {
@@ -274,6 +278,7 @@ describe('Test Shadow Handler', () => {
274
278
  expect(updates.length).toBe(1);
275
279
  expect(updates[0]).toEqual({
276
280
  projectId: projectId1,
281
+ txId: expect.any(String),
277
282
  appCfgUpdate: {
278
283
  newAppCfg: appCfg1
279
284
  }
@@ -337,6 +342,7 @@ describe('Test Shadow Handler', () => {
337
342
  expect(updates.length).toBe(1);
338
343
  expect(updates[0]).toEqual({
339
344
  projectId: projectId1,
345
+ txId: expect.any(String),
340
346
  envVarUpdate: {
341
347
  envVars: envVars1
342
348
  }
@@ -361,6 +367,7 @@ describe('Test Shadow Handler', () => {
361
367
  expect(updates.length).toBe(1);
362
368
  expect(updates[0]).toEqual({
363
369
  projectId: projectId1,
370
+ txId: expect.any(String),
364
371
  envVarUpdate: {
365
372
  envVars: envVars1
366
373
  }
@@ -391,12 +398,14 @@ describe('Test Shadow Handler', () => {
391
398
  expect(updates.length).toBe(2);
392
399
  expect(updates[0]).toEqual({
393
400
  projectId: projectId1,
401
+ txId: expect.any(String),
394
402
  envVarUpdate: {
395
403
  envVars: envVars1
396
404
  }
397
405
  });
398
406
  expect(updates[1]).toEqual({
399
407
  projectId: projectId2,
408
+ txId: expect.any(String),
400
409
  envVarUpdate: {
401
410
  envVars: envVars2
402
411
  }