Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Merge b723624 into cb2c949
Browse files Browse the repository at this point in the history
  • Loading branch information
gcseh authored Feb 9, 2021
2 parents cb2c949 + b723624 commit af40a4f
Show file tree
Hide file tree
Showing 4 changed files with 1,265 additions and 14 deletions.
6 changes: 6 additions & 0 deletions samples/mqtt_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Run the following command to install the library dependencies for NodeJS:

npm install

Download server certificate as described in the [Cloud IoT MQTT guides](https://cloud.google.com/iot/docs/how-tos/mqtt-bridge#downloading_mqtt_server_certificates).

# Running the sample

The following command summarizes the sample usage:
Expand All @@ -35,6 +37,7 @@ The following command summarizes the sample usage:
--registryId Cloud IoT registry ID.
--deviceId Cloud IoT device ID.
--privateKeyFile Path to private key file.
--serverCertFile Path to server certificate file.
--algorithm Encryption algorithm to generate the JWT.
--numMessages Number of messages to publish.
--tokenExpMins Minutes to JWT token expiration.
Expand All @@ -54,6 +57,7 @@ run the following examples:
--registryId=my-registry \
--deviceId=my-device \
--privateKeyFile=../rsa_private.pem \
--serverCertFile=../roots.pem \
--algorithm=RS256

node cloudiot_mqtt_example_nodejs.js sendDataFromBoundDevice \
Expand All @@ -63,6 +67,7 @@ run the following examples:
--gatewayId=my-gateway \
--deviceId=my-device \
--privateKeyFile=../rsa_private.pem \
--serverCertFile=../roots.pem \
--algorithm=RS256

node cloudiot_mqtt_example_nodejs.js listenForConfigMessages \
Expand All @@ -72,6 +77,7 @@ run the following examples:
--gatewayid=my-gateway \
--deviceId=my-device \
--privateKeyFile=../rsa_private.pem \
--serverCertFile=../roots.pem \
--algorithm=RS256
--clientDuration=60000

Expand Down
30 changes: 26 additions & 4 deletions samples/mqtt_example/cloudiot_mqtt_example_nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ const mqttDeviceDemo = (
region,
algorithm,
privateKeyFile,
serverCertFile,
mqttBridgeHostname,
mqttBridgePort,
messageType,
Expand All @@ -186,6 +187,7 @@ const mqttDeviceDemo = (
// const region = `us-central1`;
// const algorithm = `RS256`;
// const privateKeyFile = `./rsa_private.pem`;
// const serverCertFile = `./roots.pem`;
// const mqttBridgeHostname = `mqtt.googleapis.com`;
// const mqttBridgePort = 8883;
// const messageType = `events`;
Expand All @@ -207,6 +209,7 @@ const mqttDeviceDemo = (
password: createJwt(projectId, privateKeyFile, algorithm),
protocol: 'mqtts',
secureProtocol: 'TLSv1_2_method',
ca: [readFileSync(serverCertFile)],
};

// Create a client, and connect to the Google MQTT bridge.
Expand Down Expand Up @@ -405,6 +408,7 @@ const sendDataFromBoundDevice = (
region,
algorithm,
privateKeyFile,
serverCertFile,
mqttBridgeHostname,
mqttBridgePort,
numMessages,
Expand All @@ -417,6 +421,7 @@ const sendDataFromBoundDevice = (
// const region = `us-central1`;
// const algorithm = `RS256`;
// const privateKeyFile = `./rsa_private.pem`;
// const serverCertFile = `./roots.pem`;
// const mqttBridgeHostname = `mqtt.googleapis.com`;
// const mqttBridgePort = 8883;
// const numMessages = 5;
Expand All @@ -433,6 +438,7 @@ const sendDataFromBoundDevice = (
protocol: 'mqtts',
qos: 1,
secureProtocol: 'TLSv1_2_method',
ca: [readFileSync(serverCertFile)],
};

// Create a client, and connect to the Google MQTT bridge.
Expand Down Expand Up @@ -496,6 +502,7 @@ const listenForConfigMessages = (
region,
algorithm,
privateKeyFile,
serverCertFile,
mqttBridgeHostname,
mqttBridgePort,
clientDuration
Expand All @@ -507,6 +514,7 @@ const listenForConfigMessages = (
// const region = `us-central1`;
// const algorithm = `RS256`;
// const privateKeyFile = `./rsa_private.pem`;
// const serverCertFile = `./roots.pem`;
// const mqttBridgeHostname = `mqtt.googleapis.com`;
// const mqttBridgePort = 8883;
// const clientDuration = 60000;
Expand All @@ -522,6 +530,7 @@ const listenForConfigMessages = (
protocol: 'mqtts',
qos: 1,
secureProtocol: 'TLSv1_2_method',
ca: [readFileSync(serverCertFile)],
};

// Create a client, and connect to the Google MQTT bridge.
Expand Down Expand Up @@ -582,6 +591,7 @@ const listenForErrorMessages = (
region,
algorithm,
privateKeyFile,
serverCertFile,
mqttBridgeHostname,
mqttBridgePort,
clientDuration
Expand All @@ -594,6 +604,7 @@ const listenForErrorMessages = (
// const region = `us-central1`;
// const algorithm = `RS256`;
// const privateKeyFile = `./rsa_private.pem`;
// const serverCertFile = `./roots.pem`;
// const mqttBridgeHostname = `mqtt.googleapis.com`;
// const mqttBridgePort = 8883;
// const clientDuration = 60000;
Expand All @@ -609,6 +620,7 @@ const listenForErrorMessages = (
protocol: 'mqtts',
qos: 1,
secureProtocol: 'TLSv1_2_method',
ca: [readFileSync(serverCertFile)],
};

// Create a client, and connect to the Google MQTT bridge.
Expand Down Expand Up @@ -686,6 +698,12 @@ const {argv} = require('yargs')
demandOption: true,
type: 'string',
},
serverCertFile: {
description: 'Path to server certificate file.',
requiresArg: true,
demandOption: true,
type: 'string',
},
algorithm: {
description: 'Encryption algorithm to generate the JWT.',
requiresArg: true,
Expand Down Expand Up @@ -738,6 +756,7 @@ const {argv} = require('yargs')
opts.cloudRegion,
opts.algorithm,
opts.privateKeyFile,
opts.serverCertFile,
opts.mqttBridgeHostname,
opts.mqttBridgePort,
opts.messageType,
Expand Down Expand Up @@ -771,6 +790,7 @@ const {argv} = require('yargs')
opts.cloudRegion,
opts.algorithm,
opts.privateKeyFile,
opts.serverCertFile,
opts.mqttBridgeHostname,
opts.mqttBridgePort,
opts.numMessages,
Expand Down Expand Up @@ -804,6 +824,7 @@ const {argv} = require('yargs')
opts.cloudRegion,
opts.algorithm,
opts.privateKeyFile,
opts.serverCertFile,
opts.mqttBridgeHostname,
opts.mqttBridgePort,
opts.clientDuration
Expand Down Expand Up @@ -836,23 +857,24 @@ const {argv} = require('yargs')
opts.cloudRegion,
opts.algorithm,
opts.privateKeyFile,
opts.serverCertFile,
opts.mqttBridgeHostname,
opts.mqttBridgePort,
opts.clientDuration
);
}
)
.example(
'node $0 mqttDeviceDemo --projectId=blue-jet-123 \\\n\t--registryId=my-registry --deviceId=my-node-device \\\n\t--privateKeyFile=../rsa_private.pem --algorithm=RS256 \\\n\t--cloudRegion=us-central1 --numMessages=10 \\\n'
'node $0 mqttDeviceDemo --projectId=blue-jet-123 \\\n\t--registryId=my-registry --deviceId=my-node-device \\\n\t--privateKeyFile=../rsa_private.pem \\\n\t--serverCertFile=../roots.pem --algorithm=RS256 \\\n\t--cloudRegion=us-central1 --numMessages=10 \\\n'
)
.example(
'node $0 sendDataFromBoundDevice --projectId=blue-jet-123 \\\n\t--registryId=my-registry --deviceId=my-node-device \\\n\t--privateKeyFile=../rsa_private.pem --algorithm=RS256 \\\n\t--cloudRegion=us-central1 --gatewayId=my-node-gateway \\\n'
'node $0 sendDataFromBoundDevice --projectId=blue-jet-123 \\\n\t--registryId=my-registry --deviceId=my-node-device \\\n\t--privateKeyFile=../rsa_private.pem \\\n\t--serverCertFile=../roots.pem --algorithm=RS256 \\\n\t--cloudRegion=us-central1 --gatewayId=my-node-gateway \\\n'
)
.example(
'node $0 listenForConfigMessages --projectId=blue-jet-123 \\\n\t--registryId=my-registry --deviceId=my-node-device \\\n\t--privateKeyFile=../rsa_private.pem --algorithm=RS256 \\\n\t--cloudRegion=us-central1 --gatewayId=my-node-gateway \\\n\t--clientDuration=300000 \\\n'
'node $0 listenForConfigMessages --projectId=blue-jet-123 \\\n\t--registryId=my-registry --deviceId=my-node-device \\\n\t--privateKeyFile=../rsa_private.pem \\\n\t--serverCertFile=../roots.pem --algorithm=RS256 \\\n\t--cloudRegion=us-central1 --gatewayId=my-node-gateway \\\n\t--clientDuration=300000 \\\n'
)
.example(
'node $0 listenForErrorMessages --projectId=blue-jet-123 \\\n\t--registryId=my-registry --deviceId=my-node-device \\\n\t--privateKeyFile=../rsa_private.pem --algorithm=RS256 \\\n\t--cloudRegion=us-central1 --gatewayId=my-node-gateway \\\n\t--clientDuration=300000 \\\n'
'node $0 listenForErrorMessages --projectId=blue-jet-123 \\\n\t--registryId=my-registry --deviceId=my-node-device \\\n\t--privateKeyFile=../rsa_private.pem \\\n\t--serverCertFile=../roots.pem --algorithm=RS256 \\\n\t--cloudRegion=us-central1 --gatewayId=my-node-gateway \\\n\t--clientDuration=300000 \\\n'
)
.wrap(120)
.recommendCommands()
Expand Down
Loading

0 comments on commit af40a4f

Please sign in to comment.