Skip to content

Commit 3c32979

Browse files
committed
remove cli_args from shadow sample
1 parent 6e607ca commit 3c32979

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

samples/node/service_clients/shadow/index.ts

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
1-
import { iotshadow } from 'aws-iot-device-sdk-v2';
1+
import { iotshadow, 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

56
interface SampleContext {
67
thingName: string,
78
client: iotshadow.IotShadowClientv2
89
}
910

10-
type Args = { [index: string]: any };
11-
const yargs = require('yargs');
11+
const TIMEOUT = 100000;
1212

13-
// The relative path is '../../util/cli_args' from here, but the compiled javascript file gets put one level
14-
// deeper inside the 'dist' folder
15-
const common_args = require('../../../util/cli_args');
13+
// --------------------------------- ARGUMENT PARSING -----------------------------------------
14+
const args = require('yargs')
15+
.option('endpoint', {
16+
alias: 'e',
17+
description: 'IoT endpoint hostname',
18+
type: 'string',
19+
required: true
20+
})
21+
.option('cert', {
22+
alias: 'c',
23+
description: 'Path to the certificate file to use during mTLS connection establishment',
24+
type: 'string',
25+
required: true
26+
})
27+
.option('key', {
28+
alias: 'k',
29+
description: 'Path to the private key file to use during mTLS connection establishment',
30+
type: 'string',
31+
required: true
32+
})
33+
.option('client_id', {
34+
alias: 'C',
35+
description: 'Client ID',
36+
type: 'string',
37+
default: `shadow-sample-${uuidv4().substring(0, 8)}`
38+
})
39+
.option('thing_name', {
40+
alias: 't',
41+
description: 'Thing name',
42+
type: 'string',
43+
required: true
44+
})
45+
.help()
46+
.argv;
1647

17-
yargs.command('*', false, (yargs: any) => {
18-
common_args.add_direct_connection_establishment_arguments(yargs);
19-
common_args.add_shadow_arguments(yargs);
20-
}, main).parse();
48+
// --------------------------------- ARGUMENT PARSING END -----------------------------------------
2149

2250
function printHelp() {
2351
console.log('Supported commands:');
@@ -88,15 +116,31 @@ async function handleCommand(context: SampleContext, input: string) : Promise<bo
88116
return false;
89117
}
90118

91-
async function main(argv: Args) {
92-
common_args.apply_sample_arguments(argv);
93-
119+
async function main() {
94120
console.log("Connecting...");
95121

96-
let protocolClient = common_args.build_mqtt5_client_from_cli_args(argv);
122+
// Create MQTT5 client using mutual TLS via X509 Certificate and Private Key
123+
const builder = iot.AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
124+
args.endpoint,
125+
args.cert,
126+
args.key
127+
);
128+
129+
builder.withConnectProperties({
130+
clientId: args.client_id,
131+
keepAliveIntervalSeconds: 1200
132+
});
133+
134+
const config = builder.build();
135+
const protocolClient = new mqtt5.Mqtt5Client(config);
136+
97137
const connectionSuccess = once(protocolClient, "connectionSuccess");
98138
protocolClient.start();
99-
await connectionSuccess;
139+
140+
await Promise.race([
141+
connectionSuccess,
142+
new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), TIMEOUT))
143+
]);
100144
console.log("Connected!");
101145

102146
let shadowClient = iotshadow.IotShadowClientv2.newFromMqtt5(protocolClient, {
@@ -106,7 +150,7 @@ async function main(argv: Args) {
106150
});
107151

108152
let context: SampleContext = {
109-
thingName: argv.thing_name,
153+
thingName: args.thing_name,
110154
client: shadowClient
111155
};
112156

@@ -155,3 +199,8 @@ async function main(argv: Args) {
155199
// Quit NodeJS
156200
process.exit(0);
157201
}
202+
203+
main().catch((error) => {
204+
console.error(error);
205+
process.exit(1);
206+
});

samples/node/service_clients/shadow/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
"prepare": "npm run tsc"
1818
},
1919
"devDependencies": {
20-
"@types/node": "^10.17.50",
20+
"@types/node": "^14.18.63",
21+
"@types/uuid": "^8.3.4",
2122
"typescript": "^4.7.4"
2223
},
2324
"dependencies": {
2425
"aws-iot-device-sdk-v2": "file:../../../../",
25-
"yargs": "^16.2.0"
26+
"yargs": "^16.2.0",
27+
"uuid": "^8.3.2"
2628
}
2729
}

0 commit comments

Comments
 (0)