-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Final additions in private beta (#489)
* Changes to registry create * Adds HTTP client and state support for MQTT * Removes extra code and discovery doc * Add product link to readme, options to yargs. * Adds includecode blocks to MQTT / HTTP * Add license to package.json
- Loading branch information
Showing
8 changed files
with
391 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/> | ||
|
||
# Google Cloud IoT Core NodeJS HTTP example | ||
|
||
This sample app publishes messages to [Google Cloud Pub/Sub](pubsub) or updates | ||
device states using the HTTP bridge provided as part of Google Cloud IoT Core. | ||
|
||
Note that before you can run this sample, you must register a device as | ||
described in the parent README. | ||
|
||
[pubsub]: https://cloud.google.com/pubsub/docs | ||
# Setup | ||
|
||
Run the following command to install the library dependencies for NodeJS: | ||
|
||
npm install | ||
|
||
# Running the sample | ||
|
||
The following command summarizes the sample usage: | ||
|
||
Usage: cloudiot_http_example_nodejs [options] | ||
|
||
Example Google Cloud IoT Core HTTP device connection code. | ||
|
||
Options: | ||
|
||
-h, --help output usage information | ||
--project_id <project_id> GCP cloud project name. | ||
--registry_id <registry_id> Cloud IoT Core registry id. | ||
--device_id <device_id> Cloud IoT Core device id. | ||
--private_key_file <key_file> Path to private key file. | ||
--algorithm <algorithm> Encryption algorithm to generate the JWT. | ||
Either RS256 (RSA) or ES256 (Eliptic Curve) | ||
--cloud_region [region] GCP cloud region | ||
--num_messages [num] Number of messages to publish. | ||
--http_bridge_address [address] HTTP bridge address. | ||
--message_type [events|state] The message type to publish. | ||
|
||
For example, if your project ID is `blue-jet-123`, your service account | ||
credentials are stored in your home folder in creds.json and you have generated | ||
your credentials using the shell script provided in the parent folder, you can | ||
run the sample as: | ||
|
||
node cloudiot_http_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 | ||
|
||
# Reading Cloud Pub/Sub messages written by the sample client | ||
|
||
1. Create a subscription to your topic. | ||
|
||
gcloud beta pubsub subscriptions create \ | ||
projects/your-project-id/subscriptions/my-subscription \ | ||
--topic device-events | ||
|
||
2. Read messages published to the topic | ||
|
||
gcloud beta pubsub subscriptions pull --auto-ack \ | ||
projects/my-iot-project/subscriptions/my-subscription |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* Copyright 2017, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
// [START iot_http_includes] | ||
const fs = require('fs'); | ||
const jwt = require('jsonwebtoken'); | ||
const request = require('request'); | ||
// [END iot_http_includes] | ||
|
||
console.log('Google Cloud IoT Core HTTP example.'); | ||
var argv = require(`yargs`) | ||
.options({ | ||
project_id: { | ||
default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT, | ||
description: 'The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variables.', | ||
requiresArg: true, | ||
type: 'string' | ||
}, | ||
cloud_region: { | ||
default: 'us-central1', | ||
description: 'GCP cloud region.', | ||
requiresArg: true, | ||
type: 'string' | ||
}, | ||
registry_id: { | ||
description: 'Cloud IoT registry ID.', | ||
requiresArg: true, | ||
demandOption: true, | ||
type: 'string' | ||
}, | ||
device_id: { | ||
description: 'Cloud IoT device ID.', | ||
requiresArg: true, | ||
demandOption: true, | ||
type: 'string' | ||
}, | ||
private_key_file: { | ||
description: 'Path to private key file.', | ||
requiresArg: true, | ||
demandOption: true, | ||
type: 'string' | ||
}, | ||
algorithm: { | ||
description: 'Encryption algorithm to generate the RSA or EC JWT.', | ||
requiresArg: true, | ||
demandOption: true, | ||
choices: ['RS256', 'ES256'], | ||
type: 'string' | ||
}, | ||
num_messages: { | ||
default: 100, | ||
description: 'Number of messages to publish.', | ||
requiresArg: true, | ||
type: 'number' | ||
}, | ||
http_bridge_address: { | ||
default: 'cloudiot-device.googleapis.com', | ||
description: 'HTTP bridge address.', | ||
requiresArg: true, | ||
type: 'string' | ||
}, | ||
message_type: { | ||
default: 'events', | ||
description: 'Message type to publish.', | ||
requiresArg: true, | ||
choices: ['events', 'state'], | ||
type: 'string' | ||
} | ||
}) | ||
.example(`node $0 cloudiot_http_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`) | ||
.wrap(120) | ||
.recommendCommands() | ||
.epilogue(`For more information, see https://cloud.google.com/iot-core/docs`) | ||
.help() | ||
.strict() | ||
.argv; | ||
|
||
// Create a Cloud IoT Core JWT for the given project ID, signed with the given | ||
// private key. | ||
// [START iot_http_jwt] | ||
function createJwt (projectId, privateKeyFile, algorithm) { | ||
// Create a JWT to authenticate this device. The device will be disconnected | ||
// after the token expires, and will have to reconnect with a new token. The | ||
// audience field should always be set to the GCP project ID. | ||
const token = { | ||
'iat': parseInt(Date.now() / 1000), | ||
'exp': parseInt(Date.now() / 1000) + 20 * 60, // 20 minutes | ||
'aud': projectId | ||
}; | ||
const privateKey = fs.readFileSync(privateKeyFile); | ||
return jwt.sign(token, privateKey, { algorithm: algorithm }); | ||
} | ||
// [END iot_http_jwt] | ||
|
||
// Publish numMessages message asynchronously, starting from message | ||
// messageCount. Telemetry events are published at a rate of 1 per second and | ||
// states at a rate of 1 every 2 seconds. | ||
// [START iot_http_publish] | ||
function publishAsync (messageCount, numMessages) { | ||
const payload = `${argv.registry_id}/${argv.device_id}-payload-${messageCount}`; | ||
console.log('Publishing message:', payload); | ||
const binaryData = Buffer.from(payload).toString('base64'); | ||
const postData = argv.message_type === 'events' ? { | ||
binary_data: binaryData | ||
} : { | ||
state: { | ||
binary_data: binaryData | ||
} | ||
}; | ||
const options = { | ||
url: url, | ||
headers: { | ||
'Authorization': 'Bearer ' + authToken | ||
}, | ||
json: true, | ||
body: postData | ||
}; | ||
// Send events for high-frequency updates, update state only occasionally. | ||
const delayMs = argv.message_type === 'events' ? 1000 : 2000; | ||
request.post(options, function (error, response, body) { | ||
if (error) { | ||
return console.error('Received error: ', error); | ||
} | ||
console.log('Received response: '); | ||
console.dir(response); | ||
if (messageCount < numMessages) { | ||
// If we have published fewer than numMessage messages, publish payload | ||
// messageCount + 1. | ||
setTimeout(function () { | ||
publishAsync(messageCount + 1, numMessages); | ||
}, delayMs); | ||
} | ||
}); | ||
} | ||
// [END iot_http_publish] | ||
|
||
// [START iot_run_http] | ||
// A unique string that identifies this device. For Google Cloud IoT Core, it | ||
// must be in the format below. | ||
const devicePath = `projects/${argv.project_id}/locations/${argv.cloud_region}/registries/${argv.registry_id}/devices/${argv.device_id}`; | ||
|
||
// The request path, set accordingly depending on the message type. | ||
const pathSuffix = argv.message_type === 'events' | ||
? ':publishEvent' : ':setState'; | ||
const url = `https://${argv.http_bridge_address}/v1beta1/${devicePath}${pathSuffix}`; | ||
const authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm); | ||
|
||
// Publish messages. | ||
publishAsync(1, argv.num_messages); | ||
// [END iot_run_http] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "nodejs-docs-samples-iot-http-example", | ||
"version": "0.0.1", | ||
"description": "HTTP Example for Google Cloud IoT Core using NodeJS.", | ||
"license": "Apache-2.0", | ||
"author": "Google Inc.", | ||
"main": "cloudiot_http_example_nodejs.js", | ||
"dependencies": { | ||
"yargs": "8.0.2", | ||
"jsonwebtoken": "7.4.1", | ||
"request": "2.82.0" | ||
}, | ||
"devDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.