@alwaysai/device-agent 1.2.0 → 1.3.0-1
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.
- package/lib/application-control/environment-variables.d.ts +1 -1
- package/lib/application-control/environment-variables.d.ts.map +1 -1
- package/lib/cloud-connection/device-agent-cloud-connection.d.ts +3 -2
- package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
- package/lib/cloud-connection/device-agent-cloud-connection.js +80 -59
- package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
- package/lib/cloud-connection/live-updates-handler.d.ts.map +1 -1
- package/lib/cloud-connection/live-updates-handler.js +2 -4
- package/lib/cloud-connection/live-updates-handler.js.map +1 -1
- package/lib/cloud-connection/live-updates-handler.test.js +1 -1
- package/lib/cloud-connection/live-updates-handler.test.js.map +1 -1
- package/lib/cloud-connection/passthrough-handler.d.ts.map +1 -1
- package/lib/cloud-connection/passthrough-handler.js +3 -6
- package/lib/cloud-connection/passthrough-handler.js.map +1 -1
- package/lib/cloud-connection/publisher.d.ts +5 -4
- package/lib/cloud-connection/publisher.d.ts.map +1 -1
- package/lib/cloud-connection/publisher.js +9 -8
- package/lib/cloud-connection/publisher.js.map +1 -1
- package/lib/cloud-connection/shadow-handler.d.ts +1 -0
- package/lib/cloud-connection/shadow-handler.d.ts.map +1 -1
- package/lib/cloud-connection/shadow-handler.js +5 -6
- package/lib/cloud-connection/shadow-handler.js.map +1 -1
- package/lib/cloud-connection/shadow-handler.test.js +9 -0
- package/lib/cloud-connection/shadow-handler.test.js.map +1 -1
- package/lib/cloud-connection/transaction-manager.d.ts +14 -0
- package/lib/cloud-connection/transaction-manager.d.ts.map +1 -0
- package/lib/cloud-connection/transaction-manager.js +53 -0
- package/lib/cloud-connection/transaction-manager.js.map +1 -0
- package/lib/cloud-connection/transaction-manager.test.d.ts +2 -0
- package/lib/cloud-connection/transaction-manager.test.d.ts.map +1 -0
- package/lib/cloud-connection/transaction-manager.test.js +82 -0
- package/lib/cloud-connection/transaction-manager.test.js.map +1 -0
- package/lib/device-control/device-control.d.ts +35 -24
- package/lib/device-control/device-control.d.ts.map +1 -1
- package/lib/device-control/device-control.js +46 -11
- package/lib/device-control/device-control.js.map +1 -1
- package/lib/subcommands/app/env-vars.d.ts.map +1 -1
- package/lib/subcommands/app/env-vars.js +15 -9
- package/lib/subcommands/app/env-vars.js.map +1 -1
- package/lib/subcommands/device/clean.d.ts.map +1 -1
- package/lib/subcommands/device/clean.js +14 -12
- package/lib/subcommands/device/clean.js.map +1 -1
- package/lib/subcommands/device/device.d.ts +1 -0
- package/lib/subcommands/device/device.d.ts.map +1 -1
- package/lib/subcommands/device/device.js +23 -16
- package/lib/subcommands/device/device.js.map +1 -1
- package/lib/subcommands/device/index.js +1 -1
- package/lib/subcommands/device/index.js.map +1 -1
- package/lib/util/logger.d.ts.map +1 -1
- package/lib/util/logger.js +1 -1
- package/lib/util/logger.js.map +1 -1
- package/lib/util/safe-rimraf.d.ts +2 -0
- package/lib/util/safe-rimraf.d.ts.map +1 -0
- package/lib/util/safe-rimraf.js +16 -0
- package/lib/util/safe-rimraf.js.map +1 -0
- package/package.json +4 -3
- package/readme.md +1 -0
- package/src/application-control/environment-variables.ts +1 -1
- package/src/cloud-connection/device-agent-cloud-connection.ts +124 -80
- package/src/cloud-connection/live-updates-handler.test.ts +2 -2
- package/src/cloud-connection/live-updates-handler.ts +2 -8
- package/src/cloud-connection/passthrough-handler.ts +8 -7
- package/src/cloud-connection/publisher.ts +27 -10
- package/src/cloud-connection/shadow-handler.test.ts +9 -0
- package/src/cloud-connection/shadow-handler.ts +6 -14
- package/src/cloud-connection/transaction-manager.test.ts +92 -0
- package/src/cloud-connection/transaction-manager.ts +60 -0
- package/src/device-control/device-control.ts +50 -12
- package/src/subcommands/app/env-vars.ts +17 -10
- package/src/subcommands/device/clean.ts +22 -13
- package/src/subcommands/device/device.ts +31 -20
- package/src/subcommands/device/index.ts +2 -2
- package/src/util/logger.ts +15 -10
- package/src/util/safe-rimraf.ts +14 -0
- package/lib/cloud-connection/transaction-queue.d.ts +0 -12
- package/lib/cloud-connection/transaction-queue.d.ts.map +0 -1
- package/lib/cloud-connection/transaction-queue.js +0 -38
- package/lib/cloud-connection/transaction-queue.js.map +0 -1
- package/lib/cloud-connection/transaction-queue.test.d.ts +0 -2
- package/lib/cloud-connection/transaction-queue.test.d.ts.map +0 -1
- package/lib/cloud-connection/transaction-queue.test.js +0 -46
- package/lib/cloud-connection/transaction-queue.test.js.map +0 -1
- package/src/cloud-connection/transaction-queue.test.ts +0 -55
- 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 {
|
|
53
|
+
import { TransactionManager } from './transaction-manager';
|
|
53
54
|
import {
|
|
54
55
|
buildSignedUrlsRequestMessage,
|
|
55
56
|
buildStatusResponseMessage
|
|
56
57
|
} from './message-builder';
|
|
57
|
-
import {
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
315
|
-
|
|
331
|
+
private handleAppConfigUpdate = async (
|
|
332
|
+
update: ShadowUpdate,
|
|
316
333
|
txId: string
|
|
317
334
|
): Promise<boolean> => {
|
|
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
|
-
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
|
-
|
|
347
|
-
|
|
348
|
-
|
|
362
|
+
this.appCfgUpdateQueue.push(update);
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
349
365
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
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.
|
|
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.
|
|
468
|
+
this.txnMgr.addTransaction(txId, projectId, payload.baseCommand);
|
|
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.
|
|
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.
|
|
495
|
+
this.txnMgr.addTransaction(txId, projectId, payload.baseCommand);
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
try {
|
|
619
|
+
for (const shadowUpdate of shadowUpdates) {
|
|
620
|
+
const projectId = shadowUpdate.projectId;
|
|
621
|
+
const txId = shadowUpdate.txId;
|
|
576
622
|
try {
|
|
577
|
-
this.
|
|
623
|
+
this.txnMgr.addTransaction(txId, projectId, topic);
|
|
578
624
|
const completed = await this.atomicCmd({
|
|
579
|
-
func: this.
|
|
580
|
-
args: [
|
|
625
|
+
func: this.handleAppConfigUpdate,
|
|
626
|
+
args: [shadowUpdate, txId],
|
|
581
627
|
projectId,
|
|
582
628
|
txId
|
|
583
629
|
});
|
|
584
630
|
if (completed) {
|
|
585
|
-
this.
|
|
631
|
+
this.txnMgr.completeTransaction(txId);
|
|
586
632
|
}
|
|
587
633
|
} catch (e) {
|
|
588
|
-
logger.error(`Error
|
|
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;
|
|
@@ -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.
|
|
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}`
|
|
@@ -92,22 +92,23 @@ function processPublish(passthroughHandler: PassthroughHandler) {
|
|
|
92
92
|
);
|
|
93
93
|
break;
|
|
94
94
|
case 'heartbeat':
|
|
95
|
-
logger.debug(`Recieved heartbeat packet: ${packet}`);
|
|
96
95
|
passthroughHandler.channel.ack(msg);
|
|
97
|
-
logger.debug(
|
|
96
|
+
logger.debug(
|
|
97
|
+
`Heartbeat package received & acknowledged: ${packet}`
|
|
98
|
+
);
|
|
98
99
|
break;
|
|
99
100
|
default:
|
|
100
|
-
logger.debug(`Recieved unknown 'action' packet: ${packet}`);
|
|
101
101
|
passthroughHandler.channel.ack(msg);
|
|
102
|
-
logger.debug(
|
|
102
|
+
logger.debug(
|
|
103
|
+
`Unknown 'action' package received & acknowledged: ${packet}`
|
|
104
|
+
);
|
|
103
105
|
break;
|
|
104
106
|
}
|
|
105
107
|
} else {
|
|
108
|
+
passthroughHandler.channel.ack(msg);
|
|
106
109
|
logger.debug(
|
|
107
|
-
`Received a RabbitMQ
|
|
110
|
+
`Received & acknowledged a RabbitMQ Package of unknown structure: ${parsedPacket}`
|
|
108
111
|
);
|
|
109
|
-
passthroughHandler.channel.ack(msg);
|
|
110
|
-
logger.debug(`Package of unknown structure acknowledged`);
|
|
111
112
|
}
|
|
112
113
|
} catch (e) {
|
|
113
114
|
logger.error(`There was a problem parsing RabbitMQ packet ${e}`);
|
|
@@ -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(
|
|
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
|
-
|
|
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(
|
|
65
|
-
|
|
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(
|
|
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
|
}
|
|
@@ -7,6 +7,7 @@ import { logger } from '../util/logger';
|
|
|
7
7
|
import { Publisher } from './publisher';
|
|
8
8
|
import { AppConfigModels, getAppCfgModelsDiff } from './shadow';
|
|
9
9
|
import { getSystemInformation } from '../device-control/device-control';
|
|
10
|
+
import { generateTxId } from '@alwaysai/device-agent-schemas';
|
|
10
11
|
|
|
11
12
|
export interface ShadowTopics {
|
|
12
13
|
projects: {
|
|
@@ -35,6 +36,7 @@ export type EnvVarUpdate = {
|
|
|
35
36
|
|
|
36
37
|
export type ShadowUpdate = {
|
|
37
38
|
projectId: string;
|
|
39
|
+
txId: string;
|
|
38
40
|
appCfgUpdate?: AppConfigUpdate;
|
|
39
41
|
envVarUpdate?: EnvVarUpdate;
|
|
40
42
|
};
|
|
@@ -77,7 +79,9 @@ export class ShadowHandler {
|
|
|
77
79
|
|
|
78
80
|
for (const projectId of deltaKeys) {
|
|
79
81
|
const projectShadow = delta[projectId];
|
|
80
|
-
|
|
82
|
+
// For incoming shadow updates, there will be no TxID, so it needs to be generated here.
|
|
83
|
+
const txId = generateTxId();
|
|
84
|
+
const shadowUpdate: ShadowUpdate = { projectId, txId };
|
|
81
85
|
|
|
82
86
|
if (projectShadow.appConfig) {
|
|
83
87
|
const newAppCfg = JSON.parse(projectShadow.appConfig);
|
|
@@ -140,7 +144,7 @@ export class ShadowHandler {
|
|
|
140
144
|
case this.shadowTopics.projects.updateDelta:
|
|
141
145
|
if (clientToken === this.clientId) {
|
|
142
146
|
logger.debug(
|
|
143
|
-
`Ignoring
|
|
147
|
+
`Ignoring delta caused by Device Agent: ${JSON.stringify(
|
|
144
148
|
{ topic, payload },
|
|
145
149
|
null,
|
|
146
150
|
2
|
|
@@ -179,9 +183,6 @@ export class ShadowHandler {
|
|
|
179
183
|
clientToken: this.clientId
|
|
180
184
|
};
|
|
181
185
|
const topic = this.shadowTopics.systemInfo.update;
|
|
182
|
-
logger.debug(
|
|
183
|
-
`Publishing message:\n${JSON.stringify({ topic, packet }, null, 2)}`
|
|
184
|
-
);
|
|
185
186
|
this.publisher.publish(topic, JSON.stringify(packet));
|
|
186
187
|
}
|
|
187
188
|
|
|
@@ -200,9 +201,6 @@ export class ShadowHandler {
|
|
|
200
201
|
clientToken: this.clientId
|
|
201
202
|
};
|
|
202
203
|
const topic = this.shadowTopics.projects.update;
|
|
203
|
-
logger.debug(
|
|
204
|
-
`Publishing message:\n${JSON.stringify({ topic, packet }, null, 2)}`
|
|
205
|
-
);
|
|
206
204
|
this.publisher.publish(topic, JSON.stringify(packet));
|
|
207
205
|
}
|
|
208
206
|
|
|
@@ -211,9 +209,6 @@ export class ShadowHandler {
|
|
|
211
209
|
const packet = {
|
|
212
210
|
clientToken: this.clientId
|
|
213
211
|
};
|
|
214
|
-
logger.debug(
|
|
215
|
-
`Publishing message:\n${JSON.stringify({ topic, packet }, null, 2)}`
|
|
216
|
-
);
|
|
217
212
|
this.publisher.publish(topic, JSON.stringify(packet));
|
|
218
213
|
}
|
|
219
214
|
|
|
@@ -232,9 +227,6 @@ export class ShadowHandler {
|
|
|
232
227
|
},
|
|
233
228
|
clientToken: this.clientId
|
|
234
229
|
};
|
|
235
|
-
logger.debug(
|
|
236
|
-
`Publishing message:\n${JSON.stringify({ topic, packet }, null, 2)}`
|
|
237
|
-
);
|
|
238
230
|
this.publisher.publish(topic, JSON.stringify(packet));
|
|
239
231
|
}
|
|
240
232
|
}
|