1- import { iotjobs } from 'aws-iot-device-sdk-v2' ;
1+ import { iotjobs , mqtt5 , iot } from 'aws-iot-device-sdk-v2' ;
22import readline from 'readline' ;
33import { once } from "events" ;
4+ import { v4 as uuidv4 } from 'uuid' ;
45import {
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
3165function 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+ } ) ;
0 commit comments