@@ -24,31 +24,31 @@ const mqtt = require('mqtt');
2424console . log ( 'Google Cloud IoT Core MQTT example.' ) ;
2525var argv = require ( `yargs` )
2626 . options ( {
27- project_id : {
27+ projectId : {
2828 default : process . env . GCLOUD_PROJECT || process . env . GOOGLE_CLOUD_PROJECT ,
2929 description : 'The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variables.' ,
3030 requiresArg : true ,
3131 type : 'string'
3232 } ,
33- cloud_region : {
33+ cloudRegion : {
3434 default : 'us-central1' ,
3535 description : 'GCP cloud region.' ,
3636 requiresArg : true ,
3737 type : 'string'
3838 } ,
39- registry_id : {
39+ registryId : {
4040 description : 'Cloud IoT registry ID.' ,
4141 requiresArg : true ,
4242 demandOption : true ,
4343 type : 'string'
4444 } ,
45- device_id : {
45+ deviceId : {
4646 description : 'Cloud IoT device ID.' ,
4747 requiresArg : true ,
4848 demandOption : true ,
4949 type : 'string'
5050 } ,
51- private_key_file : {
51+ privateKeyFile : {
5252 description : 'Path to private key file.' ,
5353 requiresArg : true ,
5454 demandOption : true ,
@@ -61,39 +61,39 @@ var argv = require(`yargs`)
6161 choices : [ 'RS256' , 'ES256' ] ,
6262 type : 'string'
6363 } ,
64- num_messages : {
64+ numMessages : {
6565 default : 100 ,
6666 description : 'Number of messages to publish.' ,
6767 requiresArg : true ,
6868 type : 'number'
6969 } ,
70- token_exp_mins : {
70+ tokenExpMins : {
7171 default : 20 ,
7272 description : 'Minutes to JWT token expiration.' ,
7373 requiresArg : true ,
7474 type : 'number'
7575 } ,
76- mqtt_bridge_hostname : {
76+ mqttBridgeHostname : {
7777 default : 'mqtt.googleapis.com' ,
7878 description : 'MQTT bridge hostname.' ,
7979 requiresArg : true ,
8080 type : 'string'
8181 } ,
82- mqtt_bridge_port : {
82+ mqttBridgePort : {
8383 default : 8883 ,
8484 description : 'MQTT bridge port.' ,
8585 requiresArg : true ,
8686 type : 'number'
8787 } ,
88- message_type : {
88+ messageType : {
8989 default : 'events' ,
9090 description : 'Message type to publish.' ,
9191 requiresArg : true ,
9292 choices : [ 'events' , 'state' ] ,
9393 type : 'string'
9494 }
9595 } )
96- . example ( `node $0 cloudiot_mqtt_example_nodejs.js --project_id =blue-jet-123 --registry_id =my-registry --device_id =my-node-device --private_key_file =../rsa_private.pem --algorithm=RS256` )
96+ . example ( `node $0 cloudiot_mqtt_example_nodejs.js --projectId =blue-jet-123 --registryId =my-registry --deviceId =my-node-device --privateKeyFile =../rsa_private.pem --algorithm=RS256` )
9797 . wrap ( 120 )
9898 . recommendCommands ( )
9999 . epilogue ( `For more information, see https://cloud.google.com/iot-core/docs` )
@@ -122,30 +122,30 @@ function createJwt (projectId, privateKeyFile, algorithm) {
122122// messageCount.
123123// [START iot_mqtt_publish]
124124function publishAsync ( messageCount , numMessages ) {
125- const payload = `${ argv . registry_id } /${ argv . device_id } -payload-${ messageCount } ` ;
125+ const payload = `${ argv . registryId } /${ argv . deviceId } -payload-${ messageCount } ` ;
126126 // Publish "payload" to the MQTT topic. qos=1 means at least once delivery.
127127 // Cloud IoT Core also supports qos=0 for at most once delivery.
128128 console . log ( 'Publishing message:' , payload ) ;
129129 client . publish ( mqttTopic , payload , { qos : 1 } ) ;
130130
131- const delayMs = argv . message_type === 'events' ? 1000 : 2000 ;
131+ const delayMs = argv . messageType === 'events' ? 1000 : 2000 ;
132132 if ( messageCount < numMessages ) {
133133 // If we have published fewer than numMessage messages, publish payload
134134 // messageCount + 1 in 1 second.
135135 setTimeout ( function ( ) {
136136 let secsFromIssue = parseInt ( Date . now ( ) / 1000 ) - iatTime ;
137- if ( secsFromIssue > argv . token_exp_mins * 60 ) {
137+ if ( secsFromIssue > argv . tokenExpMins * 60 ) {
138138 iatTime = parseInt ( Date . now ( ) / 1000 ) ;
139139 console . log ( `\tRefreshing token after ${ secsFromIssue } seconds.` ) ;
140140
141141 client . end ( ) ;
142- connectionArgs . password = createJwt ( argv . project_id , argv . private_key_file , argv . algorithm ) ;
142+ connectionArgs . password = createJwt ( argv . projectId , argv . privateKeyFile , argv . algorithm ) ;
143143 client = mqtt . connect ( connectionArgs ) ;
144144
145145 client . on ( 'connect' , ( success ) => {
146146 console . log ( 'connect' ) ;
147147 if ( success ) {
148- publishAsync ( 1 , argv . num_messages ) ;
148+ publishAsync ( 1 , argv . numMessages ) ;
149149 } else {
150150 console . log ( 'Client not connected...' ) ;
151151 }
@@ -180,18 +180,18 @@ function publishAsync (messageCount, numMessages) {
180180// [START iot_mqtt_run]
181181// The mqttClientId is a unique string that identifies this device. For Google
182182// Cloud IoT Core, it must be in the format below.
183- const mqttClientId = `projects/${ argv . project_id } /locations/${ argv . cloud_region } /registries/${ argv . registry_id } /devices/${ argv . device_id } ` ;
183+ const mqttClientId = `projects/${ argv . projectId } /locations/${ argv . cloudRegion } /registries/${ argv . registryId } /devices/${ argv . deviceId } ` ;
184184
185185// With Google Cloud IoT Core, the username field is ignored, however it must be
186186// non-empty. The password field is used to transmit a JWT to authorize the
187187// device. The "mqtts" protocol causes the library to connect using SSL, which
188188// is required for Cloud IoT Core.
189189let connectionArgs = {
190- host : argv . mqtt_bridge_hostname ,
191- port : argv . mqtt_bridge_port ,
190+ host : argv . mqttBridgeHostname ,
191+ port : argv . mqttBridgePort ,
192192 clientId : mqttClientId ,
193193 username : 'unused' ,
194- password : createJwt ( argv . project_id , argv . private_key_file , argv . algorithm ) ,
194+ password : createJwt ( argv . projectId , argv . privateKeyFile , argv . algorithm ) ,
195195 protocol : 'mqtts' ,
196196 secureProtocol : 'TLSv1_2_method'
197197} ;
@@ -200,18 +200,18 @@ let connectionArgs = {
200200let iatTime = parseInt ( Date . now ( ) / 1000 ) ;
201201let client = mqtt . connect ( connectionArgs ) ;
202202
203- client . subscribe ( `/devices/${ argv . device_id } /config` ) ;
203+ client . subscribe ( `/devices/${ argv . deviceId } /config` ) ;
204204
205205// The MQTT topic that this device will publish data to. The MQTT
206206// topic name is required to be in the format below. The topic name must end in
207207// 'state' to publish state and 'events' to publish telemetry. Note that this is
208208// not the same as the device registry's Cloud Pub/Sub topic.
209- const mqttTopic = `/devices/${ argv . device_id } /${ argv . message_type } ` ;
209+ const mqttTopic = `/devices/${ argv . deviceId } /${ argv . messageType } ` ;
210210
211211client . on ( 'connect' , ( success ) => {
212212 console . log ( 'connect' ) ;
213213 if ( success ) {
214- publishAsync ( 1 , argv . num_messages ) ;
214+ publishAsync ( 1 , argv . numMessages ) ;
215215 } else {
216216 console . log ( 'Client not connected...' ) ;
217217 }
0 commit comments