Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iot: add mqtt gateway samples to GA #1169

Merged
merged 7 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
iot: update kokoro iot presubmits, codereview check
  • Loading branch information
hongalex committed Feb 19, 2019
commit a5b1d185f01fe7363b7ecbf3f70615b599259d51
13 changes: 0 additions & 13 deletions .kokoro/iot-scripts.cfg

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion iot/beta-features/gateway/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ function sendDataFromBoundDevice(
if (!success) {
console.log('Client not connected...');
} else if (!publishChainInProgress) {
// TODO: wait for commands
console.log('Client connected: Attaching device');
attachDevice(deviceId, client);
setTimeout(() => {
Expand Down
62 changes: 40 additions & 22 deletions iot/mqtt_example/cloudiot_mqtt_example_nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ var argv = require(`yargs`)
},
opts => {
sendDataFromBoundDevice(
opts.gatewayId,
opts.deviceId,
opts.gatewayId,
opts.registryId,
opts.projectId,
opts.cloudRegion,
Expand Down Expand Up @@ -170,8 +170,8 @@ var argv = require(`yargs`)
},
opts => {
listenForConfigMessages(
opts.gatewayId,
opts.deviceId,
opts.gatewayId,
opts.registryId,
opts.projectId,
opts.cloudRegion,
Expand Down Expand Up @@ -202,6 +202,7 @@ var argv = require(`yargs`)
},
opts => {
listenForErrorMessages(
opts.deviceId,
opts.gatewayId,
opts.registryId,
opts.projectId,
Expand All @@ -210,8 +211,7 @@ var argv = require(`yargs`)
opts.privateKeyFile,
opts.mqttBridgeHostname,
opts.mqttBridgePort,
opts.clientDuration,
opts.deviceId
opts.clientDuration
);
}
)
Expand Down Expand Up @@ -497,7 +497,7 @@ function detachDevice(deviceId, client, jwt) {
}

// Publish numMessages messages asynchronously through a gateway client connection
function publishAsyncGateways(
function publishAsyncGateway(
client,
iatTime,
tokenExpMins,
Expand Down Expand Up @@ -567,7 +567,7 @@ function publishAsyncGateways(
);
client = mqtt.connect(connectionArgs);
}
publishAsyncGateways(
publishAsyncGateway(
client,
iatTime,
tokenExpMins,
Expand All @@ -583,8 +583,8 @@ function publishAsyncGateways(

// Sends data from a gateway on behalf of a device that is bound to that gateway.
function sendDataFromBoundDevice(
gatewayId,
deviceId,
gatewayId,
registryId,
projectId,
region,
Expand All @@ -596,13 +596,16 @@ function sendDataFromBoundDevice(
tokenExpMins
) {
// [START iot_send_data_from_bound_device]
// const parentName = `projects/${projectId}/locations/${region}`;
// const registryName = `${parentName}/registries/${registryId}`;
// const binaryData = Buffer.from(data).toString('base64');
// const request = {
// name: `${registryName}/devices/${deviceId}`,
// binaryData: binaryData
// };
// const deviceId = `myDevice`;
// const gatewayId = `mygateway`;
// const registryId = `myRegistry`;
// const region = `us-central1`;
// const algorithm = `RS256`;
// const privateKeyFile = `./rsa_private.pem`;
// const mqttBridgeHostname = `mqtt.googleapis.com`;
// const mqttBridgePort = 8883;
// const numMessages = 5;
// const tokenExpMins = 60;

const mqttClientId = `projects/${projectId}/locations/${region}/registries/${registryId}/devices/${gatewayId}`;
console.log(`MQTT client id: ${mqttClientId}`);
Expand All @@ -629,7 +632,7 @@ function sendDataFromBoundDevice(
attachDevice(deviceId, client);
setTimeout(() => {
console.log('Client connected: Gateway is ready to relay');
publishAsyncGateways(
publishAsyncGateway(
client,
iatTime,
tokenExpMins,
Expand Down Expand Up @@ -671,8 +674,8 @@ function sendDataFromBoundDevice(

// Listen for configuration messages on a gateway and bound device.
function listenForConfigMessages(
gatewayId,
deviceId,
gatewayId,
registryId,
projectId,
region,
Expand All @@ -683,8 +686,15 @@ function listenForConfigMessages(
clientDuration
) {
// [START iot_listen_for_config_messages]
// const parentName = `projects/${projectId}/locations/${region}`;
// const registryName = `${parentName}/registries/${registryId}`;
// const deviceId = `myDevice`;
// const gatewayId = `mygateway`;
// const registryId = `myRegistry`;
// const region = `us-central1`;
// const algorithm = `RS256`;
// const privateKeyFile = `./rsa_private.pem`;
// const mqttBridgeHostname = `mqtt.googleapis.com`;
// const mqttBridgePort = 8883;
// const clientDuration = 60000;

const mqttClientId = `projects/${projectId}/locations/${region}/registries/${registryId}/devices/${gatewayId}`;
console.log(mqttClientId);
Expand Down Expand Up @@ -750,6 +760,7 @@ function listenForConfigMessages(

// Listen for error messages on a gateway.
function listenForErrorMessages(
deviceId,
gatewayId,
registryId,
projectId,
Expand All @@ -758,12 +769,19 @@ function listenForErrorMessages(
privateKeyFile,
mqttBridgeHostname,
mqttBridgePort,
clientDuration,
deviceId
clientDuration
) {
// [START iot_listen_for_error_messages]
// const parentName = `projects/${projectId}/locations/${region}`;
// const registryName = `${parentName}/registries/${registryId}`;
// const deviceId = `myDevice`;
// const gatewayId = `mygateway`;
// const registryId = `myRegistry`;
// const projectId = `my-project-123`;
// const region = `us-central1`;
// const algorithm = `RS256`;
// const privateKeyFile = `./rsa_private.pem`;
// const mqttBridgeHostname = `mqtt.googleapis.com`;
// const mqttBridgePort = 8883;
// const clientDuration = 60000;

const mqttClientId = `projects/${projectId}/locations/${region}/registries/${registryId}/devices/${gatewayId}`;
console.log(mqttClientId);
Expand Down