@alwaysai/device-agent 2.0.2 → 2.1.0-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 (146) hide show
  1. package/lib/application-control/config.js +1 -1
  2. package/lib/application-control/config.js.map +1 -1
  3. package/lib/application-control/install.d.ts.map +1 -1
  4. package/lib/application-control/install.js +2 -2
  5. package/lib/application-control/install.js.map +1 -1
  6. package/lib/cloud-connection/base-message-handler.d.ts.map +1 -1
  7. package/lib/cloud-connection/base-message-handler.js +5 -4
  8. package/lib/cloud-connection/base-message-handler.js.map +1 -1
  9. package/lib/cloud-connection/bootstrap-agent.d.ts +16 -0
  10. package/lib/cloud-connection/bootstrap-agent.d.ts.map +1 -0
  11. package/lib/cloud-connection/{device-agent.js → bootstrap-agent.js} +45 -22
  12. package/lib/cloud-connection/bootstrap-agent.js.map +1 -0
  13. package/lib/cloud-connection/connection-manager.d.ts +18 -6
  14. package/lib/cloud-connection/connection-manager.d.ts.map +1 -1
  15. package/lib/cloud-connection/connection-manager.js +85 -38
  16. package/lib/cloud-connection/connection-manager.js.map +1 -1
  17. package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
  18. package/lib/cloud-connection/device-agent-cloud-connection.js +13 -12
  19. package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
  20. package/lib/cloud-connection/device-agent-message-handler.d.ts.map +1 -1
  21. package/lib/cloud-connection/device-agent-message-handler.js +19 -18
  22. package/lib/cloud-connection/device-agent-message-handler.js.map +1 -1
  23. package/lib/cloud-connection/live-updates-handler.d.ts.map +1 -1
  24. package/lib/cloud-connection/live-updates-handler.js +11 -4
  25. package/lib/cloud-connection/live-updates-handler.js.map +1 -1
  26. package/lib/cloud-connection/passthrough-handler.d.ts +3 -3
  27. package/lib/cloud-connection/passthrough-handler.d.ts.map +1 -1
  28. package/lib/cloud-connection/passthrough-handler.js +105 -79
  29. package/lib/cloud-connection/passthrough-handler.js.map +1 -1
  30. package/lib/cloud-connection/publisher.d.ts +1 -1
  31. package/lib/cloud-connection/publisher.d.ts.map +1 -1
  32. package/lib/cloud-connection/publisher.js +22 -20
  33. package/lib/cloud-connection/publisher.js.map +1 -1
  34. package/lib/cloud-connection/shadow-handler.js +3 -3
  35. package/lib/cloud-connection/shadow-handler.js.map +1 -1
  36. package/lib/cloud-connection/shadow.d.ts.map +1 -1
  37. package/lib/cloud-connection/shadow.js +1 -1
  38. package/lib/cloud-connection/shadow.js.map +1 -1
  39. package/lib/cloud-connection/transaction-manager.d.ts.map +1 -1
  40. package/lib/cloud-connection/transaction-manager.js +17 -7
  41. package/lib/cloud-connection/transaction-manager.js.map +1 -1
  42. package/lib/cloud-connection/transaction-manager.test.js +52 -44
  43. package/lib/cloud-connection/transaction-manager.test.js.map +1 -1
  44. package/lib/device-control/device-control.d.ts.map +1 -1
  45. package/lib/device-control/device-control.js +13 -9
  46. package/lib/device-control/device-control.js.map +1 -1
  47. package/lib/docker/docker-compose.d.ts.map +1 -1
  48. package/lib/docker/docker-compose.js +1 -1
  49. package/lib/docker/docker-compose.js.map +1 -1
  50. package/lib/environment.d.ts.map +1 -1
  51. package/lib/environment.js +1 -1
  52. package/lib/environment.js.map +1 -1
  53. package/lib/index.js +12 -0
  54. package/lib/index.js.map +1 -1
  55. package/lib/infrastructure/config-check-utility.js +2 -2
  56. package/lib/infrastructure/config-check-utility.js.map +1 -1
  57. package/lib/infrastructure/legacy-migration/legacy-migration.d.ts.map +1 -1
  58. package/lib/infrastructure/legacy-migration/legacy-migration.js +6 -10
  59. package/lib/infrastructure/legacy-migration/legacy-migration.js.map +1 -1
  60. package/lib/local-connection/rabbitmq-container.d.ts +6 -0
  61. package/lib/local-connection/rabbitmq-container.d.ts.map +1 -0
  62. package/lib/local-connection/rabbitmq-container.js +111 -0
  63. package/lib/local-connection/rabbitmq-container.js.map +1 -0
  64. package/lib/local-connection/rabbitmq-container.test.d.ts +2 -0
  65. package/lib/local-connection/rabbitmq-container.test.d.ts.map +1 -0
  66. package/lib/local-connection/rabbitmq-container.test.js +219 -0
  67. package/lib/local-connection/rabbitmq-container.test.js.map +1 -0
  68. package/lib/subcommands/device/clean.d.ts.map +1 -1
  69. package/lib/subcommands/device/clean.js +15 -17
  70. package/lib/subcommands/device/clean.js.map +1 -1
  71. package/lib/subcommands/device/index.d.ts.map +1 -1
  72. package/lib/subcommands/device/index.js +3 -1
  73. package/lib/subcommands/device/index.js.map +1 -1
  74. package/lib/subcommands/device/local-connection.d.ts +2 -0
  75. package/lib/subcommands/device/local-connection.d.ts.map +1 -0
  76. package/lib/subcommands/device/local-connection.js +17 -0
  77. package/lib/subcommands/device/local-connection.js.map +1 -0
  78. package/lib/subcommands/index.d.ts +4 -1
  79. package/lib/subcommands/index.d.ts.map +1 -1
  80. package/lib/subcommands/index.js +1 -3
  81. package/lib/subcommands/index.js.map +1 -1
  82. package/lib/util/check-for-updates.d.ts.map +1 -1
  83. package/lib/util/check-for-updates.js +2 -2
  84. package/lib/util/check-for-updates.js.map +1 -1
  85. package/lib/util/file.d.ts.map +1 -1
  86. package/lib/util/file.js +6 -1
  87. package/lib/util/file.js.map +1 -1
  88. package/lib/util/file.test.js +1 -1
  89. package/lib/util/file.test.js.map +1 -1
  90. package/lib/util/get-device-id.d.ts.map +1 -1
  91. package/lib/util/get-device-id.js +1 -1
  92. package/lib/util/get-device-id.js.map +1 -1
  93. package/package.json +3 -3
  94. package/src/application-control/config.ts +1 -1
  95. package/src/application-control/install.ts +3 -2
  96. package/src/cloud-connection/base-message-handler.ts +10 -5
  97. package/src/cloud-connection/{device-agent.ts → bootstrap-agent.ts} +68 -35
  98. package/src/cloud-connection/connection-manager.ts +151 -51
  99. package/src/cloud-connection/device-agent-cloud-connection.ts +17 -19
  100. package/src/cloud-connection/device-agent-message-handler.ts +10 -7
  101. package/src/cloud-connection/live-updates-handler.ts +12 -5
  102. package/src/cloud-connection/passthrough-handler.ts +137 -92
  103. package/src/cloud-connection/publisher.ts +30 -28
  104. package/src/cloud-connection/shadow-handler.ts +3 -3
  105. package/src/cloud-connection/shadow.ts +3 -1
  106. package/src/cloud-connection/transaction-manager.test.ts +60 -41
  107. package/src/cloud-connection/transaction-manager.ts +26 -12
  108. package/src/device-control/device-control.ts +23 -13
  109. package/src/docker/docker-compose.ts +3 -1
  110. package/src/environment.ts +1 -2
  111. package/src/index.ts +19 -0
  112. package/src/infrastructure/config-check-utility.ts +2 -2
  113. package/src/infrastructure/legacy-migration/legacy-migration.ts +8 -13
  114. package/src/local-connection/rabbitmq-container.test.ts +255 -0
  115. package/src/local-connection/rabbitmq-container.ts +151 -0
  116. package/src/subcommands/device/clean.ts +20 -19
  117. package/src/subcommands/device/index.ts +3 -1
  118. package/src/subcommands/device/local-connection.ts +16 -0
  119. package/src/subcommands/index.ts +1 -3
  120. package/src/util/check-for-updates.ts +4 -2
  121. package/src/util/file.test.ts +1 -1
  122. package/src/util/file.ts +7 -1
  123. package/src/util/get-device-id.ts +3 -1
  124. package/lib/cloud-connection/bootstrap-provision.d.ts +0 -2
  125. package/lib/cloud-connection/bootstrap-provision.d.ts.map +0 -1
  126. package/lib/cloud-connection/bootstrap-provision.js +0 -35
  127. package/lib/cloud-connection/bootstrap-provision.js.map +0 -1
  128. package/lib/cloud-connection/device-agent.d.ts +0 -21
  129. package/lib/cloud-connection/device-agent.d.ts.map +0 -1
  130. package/lib/cloud-connection/device-agent.js.map +0 -1
  131. package/lib/local-connection/rabbitmq-connection.d.ts +0 -7
  132. package/lib/local-connection/rabbitmq-connection.d.ts.map +0 -1
  133. package/lib/local-connection/rabbitmq-connection.js +0 -95
  134. package/lib/local-connection/rabbitmq-connection.js.map +0 -1
  135. package/lib/subcommands/rabbitmq-connection.d.ts +0 -2
  136. package/lib/subcommands/rabbitmq-connection.d.ts.map +0 -1
  137. package/lib/subcommands/rabbitmq-connection.js +0 -14
  138. package/lib/subcommands/rabbitmq-connection.js.map +0 -1
  139. package/lib/util/clean-certs.d.ts +0 -2
  140. package/lib/util/clean-certs.d.ts.map +0 -1
  141. package/lib/util/clean-certs.js +0 -17
  142. package/lib/util/clean-certs.js.map +0 -1
  143. package/src/cloud-connection/bootstrap-provision.ts +0 -43
  144. package/src/local-connection/rabbitmq-connection.ts +0 -124
  145. package/src/subcommands/rabbitmq-connection.ts +0 -11
  146. package/src/util/clean-certs.ts +0 -16
@@ -1 +1 @@
1
- {"version":3,"file":"file.test.js","sourceRoot":"","sources":["../../src/util/file.test.ts"],"names":[],"mappings":";;AAAA,8CAA+C;AAC/C,0CAA2D;AAE3D,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AAEzB,QAAQ,CAAC,mBAAmB,EAAE,GAAG,EAAE;IACjC,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wDAAwD,EAAE,KAAK,IAAI,EAAE;QACtE,MAAM,IAAI,GAAG,oBAAoB,CAAC;QAClC,MAAM,KAAK,GAAG,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;QACxC,kBAAqB,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAC/C,eAAkB,CAAC,iBAAiB,CAAC,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC;QAEpE,MAAM,MAAM,GAAG,MAAM,IAAA,eAAQ,EAAC,EAAE,IAAI,EAAE,CAAC,CAAC;QAExC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC1B,MAAM,CAAC,kBAAO,CAAC,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;QAC3C,MAAM,CAAC,eAAI,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC,uBAAuB;QAC9D,MAAM,CAAC,iBAAM,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC,uBAAuB;IAClE,CAAC,CAAC,CAAC;IAEH,EAAE,C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BAAkB,CAAC,CAAC,IAAI,EAAE,EAAE;YAC9C,IAAI,IAAI,KAAK,GAAG,QAAQ,YAAY;gBAAE,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC;YAC1E,IAAI,IAAI,KAAK,UAAU;gBAAE,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC;QAC9D,CAAC,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,MAAM,IAAA,eAAQ,EAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;QAElE,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC3B,MAAM,CAAC,kBAAO,CAAC,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;QAC/C,MAAM,CAAC,eAAI,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC,kCAAkC;QACzE,MAAM,CAAC,iBAAM,CAAC,CAAC,oBAAoB,CAAC,GAAG,QAAQ,YAAY,CAAC,CAAC;QAC7D,MAAM,CAAC,iBAAM,CAAC,CAAC,GAAG,CAAC,oBAAoB,CAAC,GAAG,UAAU,YAAY,CAAC,CAAC;QACnE,MAAM,CAAC,gBAAK,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC;IACvC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"get-device-id.d.ts","sourceRoot":"","sources":["../../src/util/get-device-id.ts"],"names":[],"mappings":"AAKA,wBAAgB,aAAa,WAc5B"}
1
+ {"version":3,"file":"get-device-id.d.ts","sourceRoot":"","sources":["../../src/util/get-device-id.ts"],"names":[],"mappings":"AAKA,wBAAgB,aAAa,WAgB5B"}
@@ -15,7 +15,7 @@ function getDeviceUuid() {
15
15
  return cfg.deviceUuid;
16
16
  }
17
17
  catch (e) {
18
- logger_1.logger.error(`Failed to read device configuration!\n${(0, util_1.stringifyError)(e)}`);
18
+ logger_1.logger.error(`Failed to read device configuration! Error:\n${(0, util_1.stringifyError)(e)}`);
19
19
  throw new aai_error_1.default('Failed to read device configuration', {
20
20
  cause: deviceCfgFile.getErrors()
21
21
  });
@@ -1 +1 @@
1
- {"version":3,"file":"get-device-id.js","sourceRoot":"","sources":["../../src/util/get-device-id.ts"],"names":[],"mappings":";;;AAAA,qDAA4D;AAC5D,qCAAkC;AAClC,4CAAmD;AACnD,2CAAmC;AAEnC,SAAgB,aAAa;IAC3B,MAAM,aAAa,GAAG,IAAA,yBAAgB,GAAE,CAAC;IACzC,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,EAAE;QAC3B,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;IACD,IAAI;QACF,MAAM,GAAG,GAAG,aAAa,CAAC,IAAI,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC,UAAU,CAAC;KACvB;IAAC,OAAO,CAAC,EAAE;QACV,eAAM,CAAC,KAAK,CAAC,yCAAyC,IAAA,qBAAc,EAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAC3E,MAAM,IAAI,mBAAQ,CAAC,qCAAqC,EAAE;YACxD,KAAK,EAAE,aAAa,CAAC,SAAS,EAAE;SACjC,CAAC,CAAC;KACJ;AACH,CAAC;AAdD,sCAcC"}
1
+ {"version":3,"file":"get-device-id.js","sourceRoot":"","sources":["../../src/util/get-device-id.ts"],"names":[],"mappings":";;;AAAA,qDAA4D;AAC5D,qCAAkC;AAClC,4CAAmD;AACnD,2CAAmC;AAEnC,SAAgB,aAAa;IAC3B,MAAM,aAAa,GAAG,IAAA,yBAAgB,GAAE,CAAC;IACzC,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,EAAE;QAC3B,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;IACD,IAAI;QACF,MAAM,GAAG,GAAG,aAAa,CAAC,IAAI,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC,UAAU,CAAC;KACvB;IAAC,OAAO,CAAC,EAAE;QACV,eAAM,CAAC,KAAK,CACV,gDAAgD,IAAA,qBAAc,EAAC,CAAC,CAAC,EAAE,CACpE,CAAC;QACF,MAAM,IAAI,mBAAQ,CAAC,qCAAqC,EAAE;YACxD,KAAK,EAAE,aAAa,CAAC,SAAS,EAAE;SACjC,CAAC,CAAC;KACJ;AACH,CAAC;AAhBD,sCAgBC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alwaysai/device-agent",
3
3
  "description": "The alwaysAI Device Agent",
4
- "version": "2.0.2",
4
+ "version": "2.1.0-0",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "publishConfig": {
@@ -40,9 +40,9 @@
40
40
  "@alwaysai/device-agent-schemas": "3.3.0",
41
41
  "@carnesen/coded-error": "0.4.0",
42
42
  "ajv": "8.11.0",
43
- "alwaysai": "2.6.1",
43
+ "alwaysai": "2.8.0",
44
44
  "amqplib": "0.10.3",
45
- "aws-iot-device-sdk": "2.2.15",
45
+ "aws-iot-device-sdk-v2": "1.21.4",
46
46
  "docker-compose": "0.24.8",
47
47
  "lodash": "4.17.21",
48
48
  "node-os-utils": "1.3.7",
@@ -121,7 +121,7 @@ export async function writeDockerCompose(props: {
121
121
  try {
122
122
  await compose.config({ cwd: appDir, configAsString: composeOutput });
123
123
  } catch (e) {
124
- logger.error(stringifyError(e));
124
+ logger.error(`Error:\n${stringifyError(e)}`);
125
125
  }
126
126
  await spawner.writeFile('docker-compose.yaml', composeOutput);
127
127
  }
@@ -34,6 +34,7 @@ import { downloadToFile } from '../util/file';
34
34
  import { ALWAYSAI_TARGET_HW_OVERRIDE, parseTargetHW } from '../environment';
35
35
  import { setEnvInternal } from './environment-variables';
36
36
  import { AppConfig } from '@alwaysai/app-configuration-schemas';
37
+ import { CliTerseError } from '@alwaysai/alwayscli';
37
38
 
38
39
  type SignedUrlPayloadType = {
39
40
  appInstallPayload: {
@@ -82,7 +83,7 @@ export async function installApp(props: {
82
83
  await stopApp({ projectId });
83
84
  } catch (e) {
84
85
  logger.error(
85
- `Could not stop the application. Old container might still be present after installation.\n${stringifyError(
86
+ `Could not stop the application. Old container might still be present after installation. Error:\n${stringifyError(
86
87
  e
87
88
  )}`
88
89
  );
@@ -265,7 +266,7 @@ export async function uninstallApp(props: {
265
266
  await stopApp({ projectId });
266
267
  } catch (e) {
267
268
  logger.warn(
268
- `Failed to stop ${projectId}, may be left running...\n${stringifyError(
269
+ `Failed to stop ${projectId}, may be left running... Error:\n${stringifyError(
269
270
  e
270
271
  )}`
271
272
  );
@@ -1,4 +1,4 @@
1
- import { logger, stringifyError } from 'alwaysai/lib/util';
1
+ import { stringifyError } from 'alwaysai/lib/util';
2
2
  import { uninstallApp, rollbackApp } from '../application-control';
3
3
  import { createAppBackup } from '../application-control/backup';
4
4
  import { AgentConfigFile } from '../infrastructure/agent-config';
@@ -12,6 +12,7 @@ import {
12
12
  ErrorFunction,
13
13
  SuccessFunction
14
14
  } from './transaction-manager';
15
+ import { logger } from '../util/logger';
15
16
 
16
17
  export interface HandlerContext {
17
18
  clientId: string;
@@ -52,7 +53,9 @@ export abstract class BaseHandler {
52
53
  await uninstallApp({ projectId });
53
54
  this.shadowHandler.clearProjectShadow(projectId);
54
55
  } catch (e) {
55
- logger.error(`Failed to uninstall ${projectId}!\n${stringifyError(e)}`);
56
+ logger.error(
57
+ `Failed to uninstall ${projectId}! Error:\n${stringifyError(e)}`
58
+ );
56
59
  throw e;
57
60
  }
58
61
  }
@@ -73,7 +76,7 @@ export abstract class BaseHandler {
73
76
  await createAppBackup({ projectId });
74
77
  } catch (e) {
75
78
  logger.error(
76
- `Could not create a backup for the project: ${projectId}!\n${stringifyError(
79
+ `Could not create a backup for the project: ${projectId}! Error:\n${stringifyError(
77
80
  e
78
81
  )}`
79
82
  );
@@ -87,14 +90,16 @@ export abstract class BaseHandler {
87
90
  return out;
88
91
  } catch (errorAppUpdate) {
89
92
  logger.error(
90
- `Failed to update ${projectId}!\n${stringifyError(errorAppUpdate)}`
93
+ `Failed to update ${projectId}! Error:\n${stringifyError(
94
+ errorAppUpdate
95
+ )}`
91
96
  );
92
97
  // If something goes wrong, first try to rollback
93
98
  try {
94
99
  await rollbackApp({ projectId });
95
100
  } catch (errorRollbackApp) {
96
101
  logger.error(
97
- `Application rollback failed for ${projectId}!\n${stringifyError(
102
+ `Application rollback failed for ${projectId}! Error:\n${stringifyError(
98
103
  errorRollbackApp
99
104
  )}`
100
105
  );
@@ -1,11 +1,10 @@
1
- // eslint-disable-next-line
2
- const awsIot = require('aws-iot-device-sdk');
3
1
  import { getTargetHardwareUuid } from 'alwaysai/lib/core/app';
4
2
  import {
5
3
  DEVICE_CERTIFICATE_FILE_NAME,
6
4
  DEVICE_CERTIFICATE_ID_FILE_NAME,
7
5
  DEVICE_PRIVATE_KEY_FILE_NAME,
8
- LOCAL_CERT_AND_KEY_DIR
6
+ LOCAL_CERT_AND_KEY_DIR,
7
+ LocalDeviceCertificates
9
8
  } from 'alwaysai/lib/infrastructure';
10
9
  import { JsSpawner } from 'alwaysai/lib/util';
11
10
  import {
@@ -14,18 +13,20 @@ import {
14
13
  } from '../infrastructure/device-certificate';
15
14
  import { getDeviceUuid } from '../util/get-device-id';
16
15
  import { logger } from '../util/logger';
16
+ import { ConnectionManager, DeviceAgentConfigType } from './connection-manager';
17
+ import { Publisher } from './publisher';
18
+ import { MessageHandler } from './message-dispatcher';
19
+ import {
20
+ getBootstrapCertificateFilePath,
21
+ getBootstrapPrivateKeyFilePath
22
+ } from '../infrastructure/device-certificate';
23
+ import { getIoTCoreEndpointUrl } from '../infrastructure/urls';
24
+ ``;
25
+ import { AWS_ROOT_CERTIFICATE_FILE_PATH } from '../util/directories';
17
26
 
18
27
  // eslint-disable-next-line
19
28
  const process = require('process');
20
29
 
21
- interface DeviceAgentConfigType {
22
- keyPath: string;
23
- certPath: string;
24
- caPath: string;
25
- clientId: string;
26
- host: string;
27
- }
28
-
29
30
  interface FleetProvisionTemplateMessageType {
30
31
  certificateOwnershipToken: string;
31
32
  parameters: {
@@ -36,38 +37,40 @@ interface FleetProvisionTemplateMessageType {
36
37
  };
37
38
  }
38
39
 
39
- export class DeviceAgent {
40
- constructor(config: DeviceAgentConfigType) {
41
- this.device = awsIot.device(config);
40
+ export class BootstrapAgent implements MessageHandler {
41
+ private connectionManager: ConnectionManager;
42
+ private publisher: Publisher;
43
+ private readonly clientId = getDeviceUuid();
44
+ private readonly host = getIoTCoreEndpointUrl();
45
+ private readonly port = 8883;
46
+
47
+ constructor() {
48
+ // Initialize & setup the connection
49
+ const bootstrapAgentConfig = {
50
+ clientId: this.clientId,
51
+ host: this.host,
52
+ port: this.port,
53
+ keyPath: getBootstrapPrivateKeyFilePath(),
54
+ certPath: getBootstrapCertificateFilePath(),
55
+ caPath: AWS_ROOT_CERTIFICATE_FILE_PATH
56
+ };
57
+ this.connectionManager = new ConnectionManager(bootstrapAgentConfig);
58
+
59
+ this.publisher = new Publisher(
60
+ this.connectionManager,
61
+ bootstrapAgentConfig.clientId
62
+ );
42
63
  }
43
64
 
44
- public deviceType = 'aai-device';
45
- public device = awsIot.device;
46
65
  public hardwareId = async () => await getTargetHardwareUuid(JsSpawner());
47
66
  public deviceId = getDeviceUuid();
67
+ public deviceType = 'aai-device';
48
68
 
49
69
  public publishMessage(topic: string, message: string) {
50
- this.device.publish(topic, message);
51
- }
52
- }
53
-
54
- export class BootstrapAgent extends DeviceAgent {
55
- public async subscribeToAllTopics() {
56
- const AWS_CERTIFICATE_ACCEPT_TOPIC =
57
- '$aws/certificates/create/json/accepted';
58
- const PROVISIONING_ACCEPTED_TOPIC =
59
- '$aws/provisioning-templates/FleetProvisionTemplate/provision/json/accepted';
60
-
61
- const topics = [AWS_CERTIFICATE_ACCEPT_TOPIC, PROVISIONING_ACCEPTED_TOPIC];
62
- const resp = this.device.subscribe(
63
- topics,
64
- function (err: any, granted: { topic: string; qos: number }[]) {
65
- logger.debug(`Bootstrap Agent: ${JSON.stringify(granted, null, 2)}`);
66
- }
67
- );
70
+ this.publisher.publish(topic, message);
68
71
  }
69
72
 
70
- public async handleAwsCertificateTopics(topic: string, payload: any) {
73
+ public async handle(payload: any, topic: string) {
71
74
  switch (topic) {
72
75
  case '$aws/certificates/create/json/accepted': {
73
76
  logger.debug(
@@ -124,4 +127,34 @@ export class BootstrapAgent extends DeviceAgent {
124
127
  }
125
128
  }
126
129
  }
130
+
131
+ public async bootstrapProvision() {
132
+ const rmBootstrapCertsAndClose = async () => {
133
+ const deviceCertificates = new LocalDeviceCertificates();
134
+ const spawner = JsSpawner();
135
+ await spawner.rimraf(getBootstrapCertificateDirectoryPath());
136
+ await spawner.rimraf(deviceCertificates.getCertificateDirectoryPath());
137
+ logger.error('Could not provision device. Try again.');
138
+ process.exit(1);
139
+ };
140
+
141
+ setTimeout(rmBootstrapCertsAndClose, 60000);
142
+
143
+ this.connectionManager.initConnectionHandlers(() => {
144
+ logger.info('Your device is being provisioned');
145
+ });
146
+
147
+ await this.connectionManager.waitForConnection();
148
+
149
+ this.connectionManager.registerHandler(
150
+ '$aws/certificates/create/json/accepted',
151
+ this
152
+ );
153
+ this.connectionManager.registerHandler(
154
+ '$aws/provisioning-templates/FleetProvisionTemplate/provision/json/accepted',
155
+ this
156
+ );
157
+
158
+ this.publishMessage('$aws/certificates/create/json', '{}');
159
+ }
127
160
  }
@@ -1,4 +1,4 @@
1
- import * as awsIot from 'aws-iot-device-sdk';
1
+ import { mqtt5, iot } from 'aws-iot-device-sdk-v2';
2
2
  import {
3
3
  DEVICE_CERTIFICATE_FILE_PATH,
4
4
  DEVICE_PRIVATE_KEY_FILE_PATH
@@ -9,35 +9,64 @@ import { logger } from '../util/logger';
9
9
  import { promisify } from 'util';
10
10
  import { exec } from 'child_process';
11
11
  import { MessageDispatcher, MessageHandler } from './message-dispatcher';
12
+ import { once } from 'events';
12
13
 
13
14
  const exec_promise = promisify(exec);
14
15
 
16
+ // replace with direct params: instead of interface
17
+ export interface DeviceAgentConfigType {
18
+ clientId: string;
19
+ host: string;
20
+ port: number;
21
+ keyPath?: string;
22
+ certPath?: string;
23
+ caPath?: string;
24
+ }
25
+
15
26
  export class ConnectionManager extends MessageDispatcher<any> {
16
27
  private clientId: string;
17
28
  private host: string;
18
29
  private port: number;
19
- private device: awsIot.device;
30
+ private config: mqtt5.Mqtt5ClientConfig;
31
+ private client: mqtt5.Mqtt5Client;
20
32
  private subscribedTopics: Set<string> = new Set();
21
33
  private connected = false;
34
+ readonly qos: mqtt5.QoS;
22
35
 
23
- constructor(clientId: string, host: string, port: number) {
36
+ constructor(deviceAgentConfig: DeviceAgentConfigType) {
24
37
  super();
25
- this.clientId = clientId;
26
- this.host = host;
27
- this.port = port;
28
- this.device = awsIot.device({
29
- keyPath: DEVICE_PRIVATE_KEY_FILE_PATH,
30
- certPath: DEVICE_CERTIFICATE_FILE_PATH,
31
- caPath: AWS_ROOT_CERTIFICATE_FILE_PATH,
38
+
39
+ // Use config values if provided, otherwise use defaults
40
+ this.clientId = deviceAgentConfig.clientId;
41
+ this.host = deviceAgentConfig.host;
42
+ this.port = deviceAgentConfig.port;
43
+ this.qos = mqtt5.QoS.AtLeastOnce;
44
+
45
+ const certPath = deviceAgentConfig.certPath || DEVICE_CERTIFICATE_FILE_PATH;
46
+ const keyPath = deviceAgentConfig.keyPath || DEVICE_PRIVATE_KEY_FILE_PATH;
47
+ const caPath = deviceAgentConfig.caPath || AWS_ROOT_CERTIFICATE_FILE_PATH;
48
+
49
+ const builder =
50
+ iot.AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
51
+ this.host,
52
+ certPath,
53
+ keyPath
54
+ );
55
+
56
+ builder.withConnectProperties({
32
57
  clientId: this.clientId,
33
- host: this.host,
34
- port: this.port,
35
- keepalive: 10 // time before re-connect attempt on dropped connection, default is 400 seconds
58
+ keepAliveIntervalSeconds: 10
36
59
  });
60
+
61
+ builder.withCertificateAuthorityFromPath(undefined, caPath);
62
+
63
+ this.config = builder.build();
64
+ this.client = new mqtt5.Mqtt5Client(this.config);
65
+ this.client.start();
37
66
  }
38
67
 
39
68
  public getIoTDevice() {
40
- return this.device;
69
+ return this.client;
41
70
  }
42
71
 
43
72
  public isConnected(): boolean {
@@ -46,76 +75,147 @@ export class ConnectionManager extends MessageDispatcher<any> {
46
75
 
47
76
  public registerHandler(topic: string, handler: MessageHandler) {
48
77
  super.registerHandler(topic, handler);
49
- this.subscribe(topic);
78
+ void this.subscribe(topic);
50
79
  }
51
80
 
52
- public disconnect(): void {
53
- this.device.end();
81
+ public async disconnect(): Promise<void> {
82
+ const stopped = once(this.client, mqtt5.Mqtt5Client.STOPPED);
83
+ this.client.stop();
84
+ await stopped;
54
85
  logger.debug(`Device Agent has been disconnected from the AWS IoT Core.`);
55
86
  }
56
87
 
57
- public subscribe(topic: string): void {
88
+ public async subscribe(topic: string): Promise<void> {
58
89
  if (!this.subscribedTopics.has(topic)) {
59
- this.device.subscribe(topic);
90
+ const suback = await this.client.subscribe({
91
+ subscriptions: [{ qos: mqtt5.QoS.AtLeastOnce, topicFilter: topic }]
92
+ });
60
93
  this.subscribedTopics.add(topic);
61
- logger.debug(`Subscribed to topic: ${topic}`);
94
+ logger.debug(
95
+ `Subscribed to topic: ${topic}, result: ${JSON.stringify(suback)}`
96
+ );
62
97
  }
63
98
  }
64
99
 
65
- public unsubscribe(topic: string): void {
100
+ public async unsubscribe(topic: string): Promise<void> {
66
101
  if (this.subscribedTopics.has(topic)) {
67
- this.device.unsubscribe(topic);
102
+ const unsuback = await this.client.unsubscribe({
103
+ topicFilters: [topic]
104
+ });
105
+ logger.debug(
106
+ `Unsubscribed from topic: ${topic}, result: ${JSON.stringify(unsuback)}`
107
+ );
68
108
  this.subscribedTopics.delete(topic);
69
- logger.debug(`Unsubscribed from topic: ${topic}`);
70
109
  }
71
110
  }
72
111
 
73
112
  public initConnectionHandlers(connectCallback: () => void): void {
74
- this.device.on('connect', (connack: any) => {
75
- logger.info('Device Agent has connected to the cloud.');
76
- this.connected = true;
77
- connectCallback();
113
+ this.client.on('attemptingConnect', () => {
114
+ logger.debug(`Device Agent is attempting connection`);
78
115
  });
79
116
 
80
- this.device.on('disconnect', () => {
117
+ this.client.on(
118
+ 'connectionSuccess',
119
+ (eventData: mqtt5.ConnectionSuccessEvent) => {
120
+ logger.info('Device Agent has connected to the cloud.');
121
+ logger.debug(`Connack: ${JSON.stringify(eventData.connack)}`);
122
+ logger.debug(`Settings: ${JSON.stringify(eventData.settings)}`);
123
+ this.connected = true;
124
+ connectCallback();
125
+ }
126
+ );
127
+
128
+ this.client.on('disconnection', (eventData: mqtt5.DisconnectionEvent) => {
81
129
  logger.warn('Device Agent has been disconnected from the cloud');
130
+ logger.warn(`Disconnection event: ${eventData.error.toString()}`);
131
+ if (eventData.disconnect !== undefined) {
132
+ logger.debug(
133
+ 'Disconnect packet: ' + JSON.stringify(eventData.disconnect)
134
+ );
135
+ }
82
136
  this.connected = false;
83
137
  });
84
138
 
85
- this.device.on('reconnect', () => {
86
- logger.info(
87
- `Device Agent attempting to re-connect ${new Date().toLocaleString()}`
88
- );
89
- });
139
+ this.client.on(
140
+ 'connectionFailure',
141
+ (eventData: mqtt5.ConnectionFailureEvent) => {
142
+ logger.error(
143
+ `Error connecting to the AWS IoT Core! Disconnection event: ${eventData.error.toString()}`
144
+ );
145
+ if (eventData.connack) {
146
+ logger.debug(
147
+ 'Disconnect packet: ' + JSON.stringify(eventData.connack)
148
+ );
149
+ }
90
150
 
91
- this.device.on('error', function (e) {
151
+ this.connected = false;
152
+ }
153
+ );
154
+
155
+ this.client.on('error', (error) => {
92
156
  logger.error(
93
- `Error connecting to the AWS IoT Core!\n${stringifyError(e)}`
157
+ `Error connecting to the AWS IoT Core! Error:\n${stringifyError(
158
+ error as Error
159
+ )}`
94
160
  );
95
161
  this.connected = false;
96
162
  });
97
163
 
98
- this.device.on('close', () => {
99
- logger.warn('Device Agent AWS IoT Core connection closed.');
100
- this.connected = false;
101
- });
102
-
103
- this.device.on('offline', () => {
104
- logger.warn(`Device Agent is offline ${new Date().toLocaleString()}`);
164
+ this.client.on('stopped', () => {
165
+ logger.warn(`Device Agent has stopped ${new Date().toLocaleString()}`);
105
166
  void this.logConnectionInfo();
106
167
  this.connected = false;
107
168
  });
108
169
 
109
- this.device.on('message', async (topic: string, payload: string) => {
110
- logger.debug(
111
- `Message received on topic: ${topic}:\n${payload.toString()}`
112
- );
113
- try {
114
- const jsonPacket = JSON.parse(payload);
115
- this.dispatch(topic, jsonPacket);
116
- } catch (e) {
117
- logger.error(`Error parsing message!\n${stringifyError(e)}`);
170
+ this.client.on(
171
+ 'messageReceived',
172
+ (eventData: mqtt5.MessageReceivedEvent) => {
173
+ const topic = eventData.message.topicName;
174
+ const payloadBuffer = eventData.message.payload as ArrayBuffer;
175
+ try {
176
+ const payloadString = payloadBuffer
177
+ ? Buffer.from(new Uint8Array(payloadBuffer)).toString('utf-8')
178
+ : null;
179
+ logger.debug(
180
+ `Message received on topic: ${topic}:\n${payloadString}`
181
+ );
182
+ this.dispatch(eventData.message.topicName, payloadString);
183
+ } catch (e) {
184
+ logger.error(
185
+ `Error parsing message on topic ${
186
+ eventData.message.topicName
187
+ }!:\n${stringifyError(e)}`
188
+ );
189
+ }
118
190
  }
191
+ );
192
+ }
193
+
194
+ public waitForConnection(timeoutMs = 10000): Promise<void> {
195
+ return new Promise((resolve, reject) => {
196
+ if (this.connected) {
197
+ return resolve(); // already connected
198
+ }
199
+
200
+ const timeout = setTimeout(() => {
201
+ reject(new Error('Timed out waiting for MQTT connection'));
202
+ }, timeoutMs);
203
+
204
+ this.client.on('connectionSuccess', () => {
205
+ clearTimeout(timeout);
206
+ this.connected = true;
207
+ resolve();
208
+ });
209
+
210
+ this.client.on('disconnection', (event) => {
211
+ const errorString = event.error
212
+ ? event.error.toString()
213
+ : 'Unknown reason';
214
+ logger.warn(
215
+ `Disconnected while waiting for connection: ${errorString}`
216
+ );
217
+ reject(new Error(`Disconnected: ${errorString}`));
218
+ });
119
219
  });
120
220
  }
121
221
 
@@ -15,16 +15,16 @@ import { getDeviceAgentVersion } from '../util/check-for-updates';
15
15
  import { getDeviceUuid } from '../util/get-device-id';
16
16
  import { logger } from '../util/logger';
17
17
  import sleep from '../util/sleep';
18
- import { bootstrapProvision } from './bootstrap-provision';
19
18
  import { LiveUpdatesHandler } from './live-updates-handler';
20
19
  import { PassthroughHandler } from './passthrough-handler';
21
20
  import { Publisher } from './publisher';
22
21
  import { ShadowHandler, ProjectShadowMessageHandler } from './shadow-handler';
23
22
  import { JobHandler } from '../jobs/job-handler';
24
23
  import { TransactionManager } from './transaction-manager';
25
- import { ConnectionManager } from './connection-manager';
24
+ import { ConnectionManager, DeviceAgentConfigType } from './connection-manager';
26
25
  import { DeviceAgentMessageHandler } from './device-agent-message-handler';
27
26
  import { HandlerContext } from './base-message-handler';
27
+ import { BootstrapAgent } from './bootstrap-agent';
28
28
 
29
29
  export class DeviceAgentCloudConnection {
30
30
  private connectionManager: ConnectionManager;
@@ -42,11 +42,12 @@ export class DeviceAgentCloudConnection {
42
42
 
43
43
  constructor() {
44
44
  // Initialize & setup the connection
45
- this.connectionManager = new ConnectionManager(
46
- this.clientId,
47
- this.host,
48
- this.port
49
- );
45
+ const deviceAgentConfig: DeviceAgentConfigType = {
46
+ clientId: this.clientId,
47
+ host: this.host,
48
+ port: this.port
49
+ };
50
+ this.connectionManager = new ConnectionManager(deviceAgentConfig);
50
51
 
51
52
  this.publisher = new Publisher(this.connectionManager, this.clientId);
52
53
  this.shadowHandler = new ShadowHandler(this.clientId, this.publisher);
@@ -153,7 +154,7 @@ export class DeviceAgentCloudConnection {
153
154
  // lost since we aren't waiting for responses so sleep for a short time to
154
155
  // receive them
155
156
  await sleep(1000);
156
- this.connectionManager.disconnect();
157
+ await this.connectionManager.disconnect();
157
158
  }
158
159
 
159
160
  // CLI methods
@@ -180,7 +181,8 @@ export async function runDeviceAgentCloudInterface() {
180
181
  `Starting alwaysAI Device Agent v${await getDeviceAgentVersion()}`
181
182
  );
182
183
  if (existsSync(getBootstrapPrivateKeyFilePath())) {
183
- await bootstrapProvision();
184
+ const bootstrapAgent = new BootstrapAgent();
185
+ await bootstrapAgent.bootstrapProvision();
184
186
  return;
185
187
  }
186
188
 
@@ -191,16 +193,12 @@ export async function runDeviceAgentCloudInterface() {
191
193
  }
192
194
 
193
195
  if (await requiredConfigFilesPresentAndValid()) {
194
- const deviceAgent = new DeviceAgentCloudConnection();
195
- if (ALWAYSAI_ANALYTICS_PASSTHROUGH === true) {
196
- const shadowHandler = deviceAgent.shadowHandler;
197
- const publisher = deviceAgent.publisher;
198
- const passthroughHandler = new PassthroughHandler(
199
- publisher,
200
- shadowHandler
201
- );
202
- await passthroughHandler.setup();
203
- }
196
+ const cloudConnection = new DeviceAgentCloudConnection();
197
+ const passthroughHandler = new PassthroughHandler(
198
+ cloudConnection.publisher,
199
+ cloudConnection.shadowHandler
200
+ );
201
+ await passthroughHandler.run();
204
202
  } else {
205
203
  throw new Error(
206
204
  "Set device agent to local mode and retry the 'aai-agent device init' command"