@alwaysai/device-agent 2.1.0-1 → 2.1.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.
- package/lib/cloud-connection/bootstrap-agent.d.ts +16 -0
- package/lib/cloud-connection/bootstrap-agent.d.ts.map +1 -0
- package/lib/cloud-connection/{device-agent.js → bootstrap-agent.js} +47 -24
- package/lib/cloud-connection/bootstrap-agent.js.map +1 -0
- package/lib/cloud-connection/connection-manager.d.ts +18 -6
- package/lib/cloud-connection/connection-manager.d.ts.map +1 -1
- package/lib/cloud-connection/connection-manager.js +108 -41
- package/lib/cloud-connection/connection-manager.js.map +1 -1
- package/lib/cloud-connection/device-agent-cloud-connection.d.ts.map +1 -1
- package/lib/cloud-connection/device-agent-cloud-connection.js +10 -4
- package/lib/cloud-connection/device-agent-cloud-connection.js.map +1 -1
- package/lib/cloud-connection/passthrough-handler.d.ts.map +1 -1
- package/lib/cloud-connection/passthrough-handler.js +50 -46
- package/lib/cloud-connection/passthrough-handler.js.map +1 -1
- package/lib/cloud-connection/publisher.d.ts +1 -1
- package/lib/cloud-connection/publisher.d.ts.map +1 -1
- package/lib/cloud-connection/publisher.js +22 -20
- package/lib/cloud-connection/publisher.js.map +1 -1
- package/package.json +2 -2
- package/src/cloud-connection/{device-agent.ts → bootstrap-agent.ts} +70 -39
- package/src/cloud-connection/connection-manager.ts +180 -54
- package/src/cloud-connection/device-agent-cloud-connection.ts +11 -9
- package/src/cloud-connection/passthrough-handler.ts +61 -56
- package/src/cloud-connection/publisher.ts +30 -28
- package/lib/cloud-connection/bootstrap-provision.d.ts +0 -2
- package/lib/cloud-connection/bootstrap-provision.d.ts.map +0 -1
- package/lib/cloud-connection/bootstrap-provision.js +0 -35
- package/lib/cloud-connection/bootstrap-provision.js.map +0 -1
- package/lib/cloud-connection/device-agent.d.ts +0 -21
- package/lib/cloud-connection/device-agent.d.ts.map +0 -1
- package/lib/cloud-connection/device-agent.js.map +0 -1
- package/lib/util/clean-certs.d.ts +0 -2
- package/lib/util/clean-certs.d.ts.map +0 -1
- package/lib/util/clean-certs.js +0 -17
- package/lib/util/clean-certs.js.map +0 -1
- package/src/cloud-connection/bootstrap-provision.ts +0 -43
- package/src/util/clean-certs.ts +0 -16
|
@@ -19,8 +19,6 @@ import sleep from '../util/sleep';
|
|
|
19
19
|
import { Publisher } from './publisher';
|
|
20
20
|
import { ShadowHandler } from './shadow-handler';
|
|
21
21
|
|
|
22
|
-
const messageQueue: any[] = [];
|
|
23
|
-
const ackQueue: any[] = [];
|
|
24
22
|
const MAX_LOCAL_CONNECTION_ATTEMPTS = 10;
|
|
25
23
|
|
|
26
24
|
/*
|
|
@@ -42,71 +40,78 @@ export class PassthroughHandler {
|
|
|
42
40
|
|
|
43
41
|
private async runLocalConnectionChannel() {
|
|
44
42
|
logger.debug('Beginning to consume packets from Local Connection');
|
|
43
|
+
|
|
45
44
|
await this.channel.consume(
|
|
46
45
|
this.packetQueue,
|
|
47
|
-
(msg) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
async (msg) => {
|
|
47
|
+
if (!msg?.content) return;
|
|
48
|
+
|
|
49
|
+
let packet: string;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const raw = msg.content.toString();
|
|
53
|
+
// Currently, edgeIQ is calling json.dumps twice, so a single JSON.parse
|
|
54
|
+
// results in a string return value, which is needed by publishToCloudWithAck.
|
|
55
|
+
// We will update the edgeIQ implementation in the future.
|
|
56
|
+
packet = JSON.parse(raw);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
logger.error(`Error parsing RabbitMQ packet: ${stringifyError(err)}`);
|
|
59
|
+
this.channel.ack(msg);
|
|
60
|
+
logger.debug('Problematic packet was acknowledged');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// We do a second call to JSON.parse here only to retrieve the 'action' field
|
|
65
|
+
// NOTE: this second call to JSON.parse seems to alter the packet contents
|
|
66
|
+
// (at least when a rounded floating point value is included)
|
|
67
|
+
const action = JSON.parse(packet)?.['action'];
|
|
68
|
+
|
|
69
|
+
switch (action) {
|
|
70
|
+
case 'status':
|
|
71
|
+
case 'analytics': {
|
|
55
72
|
try {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// FIXME: put real topic here
|
|
62
|
-
this.publisher.publishToCloudWithAck(
|
|
63
|
-
packet,
|
|
64
|
-
(errOrResp) => {
|
|
65
|
-
while (ackQueue.length > 0) {
|
|
66
|
-
const msg = ackQueue.shift();
|
|
67
|
-
if (errOrResp === true) {
|
|
68
|
-
this.channel.ack(msg); // acknowledge, allow queue to discard
|
|
69
|
-
} else if (errOrResp === false) {
|
|
70
|
-
this.channel.reject(msg, true); // reject and requeue
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
);
|
|
75
|
-
break;
|
|
76
|
-
case 'heartbeat':
|
|
77
|
-
this.channel.ack(msg);
|
|
78
|
-
logger.silly(
|
|
79
|
-
`Heartbeat package received & acknowledged: ${packet}`
|
|
80
|
-
);
|
|
81
|
-
break;
|
|
82
|
-
default:
|
|
83
|
-
this.channel.ack(msg);
|
|
84
|
-
logger.debug(
|
|
85
|
-
`Unknown 'action' package received & acknowledged: ${packet}`
|
|
86
|
-
);
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
} else {
|
|
73
|
+
const success = await this.publisher.publishToCloudWithAck(
|
|
74
|
+
packet
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
if (success) {
|
|
90
78
|
this.channel.ack(msg);
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
);
|
|
79
|
+
} else {
|
|
80
|
+
this.channel.reject(msg, true); // requeue on failure
|
|
94
81
|
}
|
|
95
|
-
} catch (
|
|
82
|
+
} catch (err) {
|
|
96
83
|
logger.error(
|
|
97
|
-
`
|
|
98
|
-
e
|
|
99
|
-
)}`
|
|
84
|
+
`Error publishing analytics packet: ${stringifyError(err)}`
|
|
100
85
|
);
|
|
101
|
-
this.channel.
|
|
102
|
-
logger.debug('Problematic packet was acknowledged');
|
|
86
|
+
this.channel.reject(msg, true);
|
|
103
87
|
}
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
case 'heartbeat': {
|
|
92
|
+
this.channel.ack(msg);
|
|
93
|
+
logger.silly(
|
|
94
|
+
`Heartbeat packet received & acknowledged:\n${packet}`
|
|
95
|
+
);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
default: {
|
|
100
|
+
this.channel.ack(msg);
|
|
101
|
+
if (action) {
|
|
102
|
+
logger.debug(
|
|
103
|
+
`Unknown 'action' packet received & acknowledged:\n${packet}`
|
|
104
|
+
);
|
|
105
|
+
} else {
|
|
106
|
+
logger.debug(
|
|
107
|
+
`Received & acknowledged a RabbitMQ packet of unknown structure:\n${packet}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
104
111
|
}
|
|
105
112
|
}
|
|
106
113
|
},
|
|
107
|
-
{
|
|
108
|
-
noAck: false // When true, RabbitMQ deletes message as soon as it is consumed
|
|
109
|
-
}
|
|
114
|
+
{ noAck: false } // message must be explicitly acked/rejected
|
|
110
115
|
);
|
|
111
116
|
}
|
|
112
117
|
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from '@alwaysai/device-agent-schemas';
|
|
8
8
|
import * as winston from 'winston';
|
|
9
9
|
import { ConnectionManager } from './connection-manager';
|
|
10
|
+
import { mqtt5 } from 'aws-iot-device-sdk-v2';
|
|
10
11
|
|
|
11
12
|
export class Publisher {
|
|
12
13
|
private connectionManager: ConnectionManager;
|
|
@@ -28,6 +29,12 @@ export class Publisher {
|
|
|
28
29
|
) {
|
|
29
30
|
// TODO: topic validation
|
|
30
31
|
// By default, log the published message at debug level, unless otherwise specified
|
|
32
|
+
const publishPacket: mqtt5.PublishPacket = {
|
|
33
|
+
topicName: topic,
|
|
34
|
+
qos: this.connectionManager.qos,
|
|
35
|
+
payload: Buffer.from(payload)
|
|
36
|
+
};
|
|
37
|
+
|
|
31
38
|
customLogger(
|
|
32
39
|
`Publishing message:\nTopic: ${topic}\nMessage: ${JSON.stringify(
|
|
33
40
|
JSON.parse(payload),
|
|
@@ -35,37 +42,32 @@ export class Publisher {
|
|
|
35
42
|
2
|
|
36
43
|
)}`
|
|
37
44
|
);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (err) {
|
|
42
|
-
logger.error(
|
|
43
|
-
`Error publishing message: \nTopic: ${topic}\nMessage: ${payload}\nError: ${err}`
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
45
|
+
|
|
46
|
+
// TODO: Change publish() to async and resolve issues where it's used synchronously.
|
|
47
|
+
void this.connectionManager.getIoTDevice().publish(publishPacket);
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
public publishToCloudWithAck(
|
|
50
|
-
payload: string,
|
|
51
|
-
ackNackCallback: CallableFunction
|
|
52
|
-
) {
|
|
50
|
+
public async publishToCloudWithAck(payload: string): Promise<boolean> {
|
|
53
51
|
const topic = this.toCloudTopic;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
52
|
+
|
|
53
|
+
const publishPacket: mqtt5.PublishPacket = {
|
|
54
|
+
topicName: topic,
|
|
55
|
+
qos: this.connectionManager.qos,
|
|
56
|
+
payload: Buffer.from(payload)
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
await this.connectionManager.getIoTDevice().publish(publishPacket);
|
|
61
|
+
logger.debug(
|
|
62
|
+
`Successfully published message:\nTopic: ${topic}\nMessage: ${payload}`
|
|
63
|
+
);
|
|
64
|
+
return true;
|
|
65
|
+
} catch (err) {
|
|
66
|
+
logger.error(
|
|
67
|
+
`Error publishing message:\nTopic: ${topic}\nMessage: ${payload}\nError: ${err}`
|
|
68
|
+
);
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
public publishDeviceAgentMessage(
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bootstrap-provision.d.ts","sourceRoot":"","sources":["../../src/cloud-connection/bootstrap-provision.ts"],"names":[],"mappings":"AAWA,wBAAsB,kBAAkB,kBA+BvC"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.bootstrapProvision = void 0;
|
|
4
|
-
const device_certificate_1 = require("../infrastructure/device-certificate");
|
|
5
|
-
const urls_1 = require("../infrastructure/urls");
|
|
6
|
-
const clean_certs_1 = require("../util/clean-certs");
|
|
7
|
-
const directories_1 = require("../util/directories");
|
|
8
|
-
const get_device_id_1 = require("../util/get-device-id");
|
|
9
|
-
const logger_1 = require("../util/logger");
|
|
10
|
-
const device_agent_1 = require("./device-agent");
|
|
11
|
-
async function bootstrapProvision() {
|
|
12
|
-
setTimeout(clean_certs_1.rmBootstrapCertsAndClose, 60000);
|
|
13
|
-
const clientId = (0, get_device_id_1.getDeviceUuid)();
|
|
14
|
-
const bootstrapConfig = {
|
|
15
|
-
keyPath: (0, device_certificate_1.getBootstrapPrivateKeyFilePath)(),
|
|
16
|
-
certPath: (0, device_certificate_1.getBootstrapCertificateFilePath)(),
|
|
17
|
-
caPath: directories_1.AWS_ROOT_CERTIFICATE_FILE_PATH,
|
|
18
|
-
clientId,
|
|
19
|
-
host: (0, urls_1.getIoTCoreEndpointUrl)()
|
|
20
|
-
};
|
|
21
|
-
const bootstrapAgent = new device_agent_1.BootstrapAgent(bootstrapConfig);
|
|
22
|
-
await bootstrapAgent.subscribeToAllTopics();
|
|
23
|
-
bootstrapAgent.publishMessage('$aws/certificates/create/json', '');
|
|
24
|
-
bootstrapAgent.device.on('connect', () => {
|
|
25
|
-
logger_1.logger.info('Your device is being provisioned');
|
|
26
|
-
});
|
|
27
|
-
bootstrapAgent.device.on('message', async (topic, payload) => {
|
|
28
|
-
await bootstrapAgent.handleAwsCertificateTopics(topic, payload);
|
|
29
|
-
});
|
|
30
|
-
bootstrapAgent.device.on('packetsend', (packet) => {
|
|
31
|
-
logger_1.logger.debug(`Sending packet: ${JSON.stringify({ packet }, null, 2)}`);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
exports.bootstrapProvision = bootstrapProvision;
|
|
35
|
-
//# sourceMappingURL=bootstrap-provision.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bootstrap-provision.js","sourceRoot":"","sources":["../../src/cloud-connection/bootstrap-provision.ts"],"names":[],"mappings":";;;AAAA,6EAG8C;AAC9C,iDAA+D;AAC/D,qDAA+D;AAC/D,qDAAqE;AACrE,yDAAsD;AACtD,2CAAwC;AACxC,iDAAgD;AAEzC,KAAK,UAAU,kBAAkB;IACtC,UAAU,CAAC,sCAAwB,EAAE,KAAK,CAAC,CAAC;IAE5C,MAAM,QAAQ,GAAG,IAAA,6BAAa,GAAE,CAAC;IACjC,MAAM,eAAe,GAAG;QACtB,OAAO,EAAE,IAAA,mDAA8B,GAAE;QACzC,QAAQ,EAAE,IAAA,oDAA+B,GAAE;QAC3C,MAAM,EAAE,4CAA8B;QACtC,QAAQ;QACR,IAAI,EAAE,IAAA,4BAAqB,GAAE;KAC9B,CAAC;IAEF,MAAM,cAAc,GAAG,IAAI,6BAAc,CAAC,eAAe,CAAC,CAAC;IAC3D,MAAM,cAAc,CAAC,oBAAoB,EAAE,CAAC;IAE5C,cAAc,CAAC,cAAc,CAAC,+BAA+B,EAAE,EAAE,CAAC,CAAC;IAEnE,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE;QACvC,eAAM,CAAC,IAAI,CAAC,kCAAkC,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;IAEH,cAAc,CAAC,MAAM,CAAC,EAAE,CACtB,SAAS,EACT,KAAK,EAAE,KAAa,EAAE,OAAe,EAAE,EAAE;QACvC,MAAM,cAAc,CAAC,0BAA0B,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAClE,CAAC,CACF,CAAC;IAEF,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC,YAAY,EAAE,CAAC,MAAW,EAAE,EAAE;QACrD,eAAM,CAAC,KAAK,CAAC,mBAAmB,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;IACzE,CAAC,CAAC,CAAC;AACL,CAAC;AA/BD,gDA+BC"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
interface DeviceAgentConfigType {
|
|
2
|
-
keyPath: string;
|
|
3
|
-
certPath: string;
|
|
4
|
-
caPath: string;
|
|
5
|
-
clientId: string;
|
|
6
|
-
host: string;
|
|
7
|
-
}
|
|
8
|
-
export declare class DeviceAgent {
|
|
9
|
-
constructor(config: DeviceAgentConfigType);
|
|
10
|
-
deviceType: string;
|
|
11
|
-
device: any;
|
|
12
|
-
hardwareId: () => Promise<string>;
|
|
13
|
-
deviceId: string;
|
|
14
|
-
publishMessage(topic: string, message: string): void;
|
|
15
|
-
}
|
|
16
|
-
export declare class BootstrapAgent extends DeviceAgent {
|
|
17
|
-
subscribeToAllTopics(): Promise<void>;
|
|
18
|
-
handleAwsCertificateTopics(topic: string, payload: any): Promise<void>;
|
|
19
|
-
}
|
|
20
|
-
export {};
|
|
21
|
-
//# sourceMappingURL=device-agent.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"device-agent.d.ts","sourceRoot":"","sources":["../../src/cloud-connection/device-agent.ts"],"names":[],"mappings":"AAoBA,UAAU,qBAAqB;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd;AAYD,qBAAa,WAAW;gBACV,MAAM,EAAE,qBAAqB;IAIlC,UAAU,SAAgB;IAC1B,MAAM,MAAiB;IACvB,UAAU,wBAAwD;IAClE,QAAQ,SAAmB;IAE3B,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAGrD;AAED,qBAAa,cAAe,SAAQ,WAAW;IAChC,oBAAoB;IAepB,0BAA0B,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG;CAyDpE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"device-agent.js","sourceRoot":"","sources":["../../src/cloud-connection/device-agent.ts"],"names":[],"mappings":"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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clean-certs.d.ts","sourceRoot":"","sources":["../../src/util/clean-certs.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,wBAAwB,sBAOpC,CAAC"}
|
package/lib/util/clean-certs.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rmBootstrapCertsAndClose = void 0;
|
|
4
|
-
const infrastructure_1 = require("alwaysai/lib/infrastructure");
|
|
5
|
-
const util_1 = require("alwaysai/lib/util");
|
|
6
|
-
const device_certificate_1 = require("../infrastructure/device-certificate");
|
|
7
|
-
const logger_1 = require("../util/logger");
|
|
8
|
-
const rmBootstrapCertsAndClose = async () => {
|
|
9
|
-
const deviceCertificates = new infrastructure_1.LocalDeviceCertificates();
|
|
10
|
-
const spawner = (0, util_1.JsSpawner)();
|
|
11
|
-
await spawner.rimraf((0, device_certificate_1.getBootstrapCertificateDirectoryPath)());
|
|
12
|
-
await spawner.rimraf(deviceCertificates.getCertificateDirectoryPath());
|
|
13
|
-
logger_1.logger.error('Could not provision device. Try again.');
|
|
14
|
-
process.exit(1);
|
|
15
|
-
};
|
|
16
|
-
exports.rmBootstrapCertsAndClose = rmBootstrapCertsAndClose;
|
|
17
|
-
//# sourceMappingURL=clean-certs.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clean-certs.js","sourceRoot":"","sources":["../../src/util/clean-certs.ts"],"names":[],"mappings":";;;AAAA,gEAGqC;AACrC,4CAA8C;AAC9C,6EAA4F;AAC5F,2CAAwC;AAEjC,MAAM,wBAAwB,GAAG,KAAK,IAAI,EAAE;IACjD,MAAM,kBAAkB,GAAG,IAAI,wCAAuB,EAAE,CAAC;IACzD,MAAM,OAAO,GAAG,IAAA,gBAAS,GAAE,CAAC;IAC5B,MAAM,OAAO,CAAC,MAAM,CAAC,IAAA,yDAAoC,GAAE,CAAC,CAAC;IAC7D,MAAM,OAAO,CAAC,MAAM,CAAC,kBAAkB,CAAC,2BAA2B,EAAE,CAAC,CAAC;IACvE,eAAM,CAAC,KAAK,CAAC,wCAAwC,CAAC,CAAC;IACvD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC;AAPW,QAAA,wBAAwB,4BAOnC"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getBootstrapCertificateFilePath,
|
|
3
|
-
getBootstrapPrivateKeyFilePath
|
|
4
|
-
} from '../infrastructure/device-certificate';
|
|
5
|
-
import { getIoTCoreEndpointUrl } from '../infrastructure/urls';
|
|
6
|
-
import { rmBootstrapCertsAndClose } from '../util/clean-certs';
|
|
7
|
-
import { AWS_ROOT_CERTIFICATE_FILE_PATH } from '../util/directories';
|
|
8
|
-
import { getDeviceUuid } from '../util/get-device-id';
|
|
9
|
-
import { logger } from '../util/logger';
|
|
10
|
-
import { BootstrapAgent } from './device-agent';
|
|
11
|
-
|
|
12
|
-
export async function bootstrapProvision() {
|
|
13
|
-
setTimeout(rmBootstrapCertsAndClose, 60000);
|
|
14
|
-
|
|
15
|
-
const clientId = getDeviceUuid();
|
|
16
|
-
const bootstrapConfig = {
|
|
17
|
-
keyPath: getBootstrapPrivateKeyFilePath(),
|
|
18
|
-
certPath: getBootstrapCertificateFilePath(),
|
|
19
|
-
caPath: AWS_ROOT_CERTIFICATE_FILE_PATH,
|
|
20
|
-
clientId,
|
|
21
|
-
host: getIoTCoreEndpointUrl()
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const bootstrapAgent = new BootstrapAgent(bootstrapConfig);
|
|
25
|
-
await bootstrapAgent.subscribeToAllTopics();
|
|
26
|
-
|
|
27
|
-
bootstrapAgent.publishMessage('$aws/certificates/create/json', '');
|
|
28
|
-
|
|
29
|
-
bootstrapAgent.device.on('connect', () => {
|
|
30
|
-
logger.info('Your device is being provisioned');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
bootstrapAgent.device.on(
|
|
34
|
-
'message',
|
|
35
|
-
async (topic: string, payload: string) => {
|
|
36
|
-
await bootstrapAgent.handleAwsCertificateTopics(topic, payload);
|
|
37
|
-
}
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
bootstrapAgent.device.on('packetsend', (packet: any) => {
|
|
41
|
-
logger.debug(`Sending packet: ${JSON.stringify({ packet }, null, 2)}`);
|
|
42
|
-
});
|
|
43
|
-
}
|
package/src/util/clean-certs.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LOCAL_CERT_AND_KEY_DIR,
|
|
3
|
-
LocalDeviceCertificates
|
|
4
|
-
} from 'alwaysai/lib/infrastructure';
|
|
5
|
-
import { JsSpawner } from 'alwaysai/lib/util';
|
|
6
|
-
import { getBootstrapCertificateDirectoryPath } from '../infrastructure/device-certificate';
|
|
7
|
-
import { logger } from '../util/logger';
|
|
8
|
-
|
|
9
|
-
export const rmBootstrapCertsAndClose = async () => {
|
|
10
|
-
const deviceCertificates = new LocalDeviceCertificates();
|
|
11
|
-
const spawner = JsSpawner();
|
|
12
|
-
await spawner.rimraf(getBootstrapCertificateDirectoryPath());
|
|
13
|
-
await spawner.rimraf(deviceCertificates.getCertificateDirectoryPath());
|
|
14
|
-
logger.error('Could not provision device. Try again.');
|
|
15
|
-
process.exit(1);
|
|
16
|
-
};
|