From 30b57d14bb7b03c24b2ce49898f09e35509919e7 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 9 Oct 2017 12:46:12 -0700 Subject: [PATCH] Fixes URL for HTTP example and adds snippets for MQTT. --- iot/api-client/http_example/README.md | 25 +++++++++++++++---- .../cloud/iot/examples/HttpExample.java | 2 ++ .../cloud/iot/examples/MqttExample.java | 12 ++++++++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/iot/api-client/http_example/README.md b/iot/api-client/http_example/README.md index 079a72c81f5..825640f7d58 100644 --- a/iot/api-client/http_example/README.md +++ b/iot/api-client/http_example/README.md @@ -20,11 +20,12 @@ The following command summarizes the sample usage: ``` mvn exec:java \ -Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \ - -Dexec.args="-project_id=my-iot-project \ - -registry_id=my-registry \ - -device_id=my-device \ - -private_key_file=rsa_private_pkcs8 \ - -algorithm=RS256" + -Dexec.args="-project_id= \ + -registry_id= \ + -device_id= \ + -private_key_file= \ + -message_type= \ + -algorithm=" ``` For example, if your project ID is `blue-jet-123`, your service account @@ -42,6 +43,20 @@ provided in the parent folder, you can run the sample as: -algorithm=RS256" ``` +To publish state messages, run the sample as follows: + +``` + mvn exec:java \ + -Dexec.mainClass="com.google.cloud.iot.examples.HttpExample" \ + -Dexec.args="-project_id=blue-jet-123 \ + -registry_id=my-registry \ + -device_id=my-java-device \ + -private_key_file=../rsa_private_pkcs8 \ + -message_type=state \ + -algorithm=RS256" +``` + + ## Reading the messages written by the sample client 1. Create a subscription to your topic. diff --git a/iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java b/iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java index 5de22b85f94..361f076c60f 100644 --- a/iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java +++ b/iot/api-client/http_example/src/main/java/com/google/cloud/iot/examples/HttpExample.java @@ -105,6 +105,8 @@ public static void publishMessage(String payload, String urlPath, String message String encPayload = encoder.encodeToString(payload.getBytes("UTF-8")); + + urlPath = urlPath + devicePath + ":" + urlSuffix; URL url = new URL(urlPath); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); diff --git a/iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java b/iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java index c74b38b0330..5c21f7aceda 100644 --- a/iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java +++ b/iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java @@ -14,6 +14,7 @@ package com.google.cloud.iot.examples; +// [START cloudiotcore_mqtt_imports] import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; @@ -26,6 +27,7 @@ import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.joda.time.DateTime; +// [END cloudiotcore_mqtt_imports] /** * Java sample of connecting to Google Cloud IoT Core vice via MQTT, using JWT. @@ -52,7 +54,8 @@ * */ public class MqttExample { - /** Create a Cloud IoT Core JWT for the given project id, signed with the given private key. */ + // [START cloudiotcore_mqtt_createjwt] + /** Create a Cloud IoT Core JWT for the given project id, signed with the given RSA key. */ private static String createJwtRsa(String projectId, String privateKeyFile) throws Exception { DateTime now = new DateTime(); // Create a JWT to authenticate this device. The device will be disconnected after the token @@ -71,6 +74,7 @@ private static String createJwtRsa(String projectId, String privateKeyFile) thro return jwtBuilder.signWith(SignatureAlgorithm.RS256, kf.generatePrivate(spec)).compact(); } + /** Create a Cloud IoT Core JWT for the given project id, signed with the given ES key. */ private static String createJwtEs(String projectId, String privateKeyFile) throws Exception { DateTime now = new DateTime(); // Create a JWT to authenticate this device. The device will be disconnected after the token @@ -88,8 +92,11 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact(); } + // [END cloudiotcore_mqtt_createjwt] + /** Parse arguments, configure MQTT, and publish messages. */ public static void main(String[] args) throws Exception { + // [START cloudiotcore_mqtt_configuremqtt] MqttExampleOptions options = MqttExampleOptions.fromFlags(args); if (options == null) { // Could not parse. @@ -131,7 +138,9 @@ public static void main(String[] args) throws Exception { throw new IllegalArgumentException( "Invalid algorithm " + options.algorithm + ". Should be one of 'RS256' or 'ES256'."); } + // [END cloudiotcore_mqtt_configuremqtt] + // [START cloudiotcore_mqtt_publish] // Create a client, and connect to the Google MQTT bridge. MqttClient client = new MqttClient(mqttServerAddress, mqttClientId, new MemoryPersistence()); try { @@ -171,5 +180,6 @@ public static void main(String[] args) throws Exception { client.disconnect(); } System.out.println("Finished loop successfully. Goodbye!"); + // [END cloudiotcore_mqtt_publish] } }