Skip to content

Commit 6a84279

Browse files
committed
remove cki_args from jobs sample
1 parent 3c32979 commit 6a84279

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

samples/node/service_clients/jobs/index.ts

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { iotjobs } from 'aws-iot-device-sdk-v2';
1+
import { iotjobs, mqtt5, iot } from 'aws-iot-device-sdk-v2';
22
import readline from 'readline';
33
import {once} from "events";
4+
import { v4 as uuidv4 } from 'uuid';
45
import {
56
CreateJobCommand,
67
CreateThingCommand,
@@ -16,17 +17,50 @@ interface SampleContext {
1617
thingArn?: string,
1718
}
1819

19-
type Args = { [index: string]: any };
20-
const yargs = require('yargs');
20+
const TIMEOUT = 100000;
2121

22-
// The relative path is '../../util/cli_args' from here, but the compiled javascript file gets put one level
23-
// deeper inside the 'dist' folder
24-
const common_args = require('../../../util/cli_args');
22+
// --------------------------------- ARGUMENT PARSING -----------------------------------------
23+
const args = require('yargs')
24+
.option('endpoint', {
25+
alias: 'e',
26+
description: 'IoT endpoint hostname',
27+
type: 'string',
28+
required: true
29+
})
30+
.option('cert', {
31+
alias: 'c',
32+
description: 'Path to the certificate file to use during mTLS connection establishment',
33+
type: 'string',
34+
required: true
35+
})
36+
.option('key', {
37+
alias: 'k',
38+
description: 'Path to the private key file to use during mTLS connection establishment',
39+
type: 'string',
40+
required: true
41+
})
42+
.option('client_id', {
43+
alias: 'C',
44+
description: 'Client ID',
45+
type: 'string',
46+
default: `jobs-sample-${uuidv4().substring(0, 8)}`
47+
})
48+
.option('thing_name', {
49+
alias: 't',
50+
description: 'Thing name',
51+
type: 'string',
52+
required: true
53+
})
54+
.option('region', {
55+
alias: 'r',
56+
description: 'AWS region',
57+
type: 'string',
58+
required: true
59+
})
60+
.help()
61+
.argv;
2562

26-
yargs.command('*', false, (yargs: any) => {
27-
common_args.add_direct_connection_establishment_arguments(yargs);
28-
common_args.add_jobs_arguments(yargs);
29-
}, main).parse();
63+
// --------------------------------- ARGUMENT PARSING END -----------------------------------------
3064

3165
function printHelp() {
3266
console.log('IoT control plane commands:');
@@ -157,19 +191,32 @@ async function createThingIfNeeded(context: SampleContext) {
157191
console.log(`Thing "${context.thingName}" successfully created`);
158192
}
159193

160-
async function main(argv: Args) {
161-
common_args.apply_sample_arguments(argv);
194+
async function main() {
195+
let controlPlaneClient = new IoTClient({ region: args.region });
196+
197+
// Create MQTT5 client using mutual TLS via X509 Certificate and Private Key
198+
const builder = iot.AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
199+
args.endpoint,
200+
args.cert,
201+
args.key
202+
);
203+
204+
builder.withConnectProperties({
205+
clientId: args.client_id,
206+
keepAliveIntervalSeconds: 1200
207+
});
162208

163-
let controlPlaneClient = new IoTClient({});
164-
let protocolClient = common_args.build_mqtt5_client_from_cli_args(argv);
209+
const config = builder.build();
210+
const protocolClient = new mqtt5.Mqtt5Client(config);
211+
165212
let jobsClient = iotjobs.IotJobsClientv2.newFromMqtt5(protocolClient, {
166213
maxRequestResponseSubscriptions: 5,
167214
maxStreamingSubscriptions: 2,
168215
operationTimeoutInSeconds: 60
169216
});
170217

171218
let context: SampleContext = {
172-
thingName: argv.thing_name,
219+
thingName: args.thing_name,
173220
jobsClient: jobsClient,
174221
controlPlaneClient: controlPlaneClient
175222
};
@@ -180,7 +227,11 @@ async function main(argv: Args) {
180227

181228
const connectionSuccess = once(protocolClient, "connectionSuccess");
182229
protocolClient.start();
183-
await connectionSuccess;
230+
231+
await Promise.race([
232+
connectionSuccess,
233+
new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), TIMEOUT))
234+
]);
184235
console.log("Connected!");
185236

186237

@@ -229,3 +280,8 @@ async function main(argv: Args) {
229280
// Quit NodeJS
230281
process.exit(0);
231282
}
283+
284+
main().catch((error) => {
285+
console.error(error);
286+
process.exit(1);
287+
});

samples/node/service_clients/jobs/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
},
1919
"devDependencies": {
2020
"@types/node": "^14.18.63",
21+
"@types/uuid": "^8.3.4",
2122
"typescript": "^4.7.4"
2223
},
2324
"dependencies": {
2425
"@aws-sdk/client-iot": "^3",
2526
"aws-iot-device-sdk-v2": "file:../../../../",
26-
"yargs": "^16.2.0"
27+
"yargs": "^16.2.0",
28+
"uuid": "^8.3.2"
2729
}
2830
}

0 commit comments

Comments
 (0)