8000 fixed region tags and refactored some methods (#1437) · llozano100/java-docs-samples@5e9bf25 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e9bf25

Browse files
authored
fixed region tags and refactored some methods (GoogleCloudPlatform#1437)
* fixed region tags and refactored some methods * added missing iot tags
1 parent 63728b3 commit 5e9bf25

File tree

4 files changed

+82
-67
lines changed

4 files changed

+82
-67
lines changed

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.google.api.services.cloudiot.v1.model.SetIamPolicyRequest;
4343
import com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayRequest;
4444
import com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayResponse;
45+
4546
import com.google.cloud.Role;
4647
import com.google.cloud.pubsub.v1.TopicAdminClient;
4748
import com.google.common.io.Files;
@@ -57,11 +58,8 @@
5758
import java.util.Arrays;
5859
import java.util.Base64;
5960
import java.util.List;
60-
import org.apache.commons.cli.HelpFormatter;
61-
import org.eclipse.paho.client.mqttv3.MqttClient;
62-
import org.eclipse.paho.client.mqttv3.MqttException;
63-
import org.eclipse.paho.client.mqttv3.MqttMessage;
6461

62+
import org.apache.commons.cli.HelpFormatter;
6563
/**
6664
* Example of using Cloud IoT device manager API to administer devices, registries and projects.
6765
*
@@ -93,6 +91,7 @@
9391
* </code>
9492
* </pre>
9593
*/
94+
9695
public class DeviceRegistryExample {
9796

9897
static final String APP_NAME = "DeviceRegistryExample";
@@ -780,7 +779,7 @@ public static void setIamPermissions(
780779
// [END iot_set_iam_policy]
781780

782781
/** Send a command to a device. * */
783-
// [START send_command]
782+
// [START iot_send_command]
784783
public static void sendCommand(
785784
String deviceId, String projectId, String cloudRegion, String registryName, String data)
786785
throws GeneralSecurityException, IOException {
@@ -817,12 +816,12 @@ public static void sendCommand(
817816

818817
System.out.println("Command response: " + res.toString());
819818
}
820-
// [END send_command]
819+
// [END iot_send_command]
821820

822821
public static void bindDeviceToGateway(
823822
String projectId, String cloudRegion, String registryName, String deviceId, String gatewayId)
824823
throws GeneralSecurityException, IOException {
825-
// [START bind_device_to_gateway]
824+
// [START iot_bind_device_to_gateway]
826825
createDevice(projectId, cloudRegion, registryName, deviceId);
827826

828827
GoogleCredential credential =
@@ -851,13 +850,13 @@ public static void bindDeviceToGateway(
851850
.execute();
852851

853852
System.out.println(String.format("Device bound: %s", response.toPrettyString()));
854-
// [END bind_device_to_gateway]
853+
// [END iot_bind_device_to_gateway]
855854
}
856855

857856
public static void unbindDeviceFromGateway(
858857
String projectId, String cloudRegion, String registryName, String deviceId, String gatewayId)
859858
throws GeneralSecurityException, IOException {
860-
// [START unbind_device_from_gateway]
859+
// [START iot_unbind_device_from_gateway]
861860
GoogleCredential credential =
862861
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
863862
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
@@ -884,39 +883,14 @@ public static void unbindDeviceFromGateway(
884883
.execute();
885884

886885
System.out.println(String.format("Device unbound: %s", response.toPrettyString()));
887-
// [END unbind_device_from_gateway]
888-
}
889-
890-
public static void attachDeviceToGateway(MqttClient client, String deviceId)
891-
throws MqttException {
892-
// [START attach_device]
893-
final String attachTopic = String.format("/devices/%s/attach", deviceId);
894-
System.out.println(String.format("Attaching: %s", attachTopic));
895-
String attachPayload = "{}";
896-
MqttMessage message = new MqttMessage(attachPayload.getBytes());
897-
message.setQos(1);
898-
client.publish(attachTopic, message);
899-
// [END attach_device]
900-
}
901-
902-
/** Detaches a bound device from the Gateway. */
903-
public static void detachDeviceFromGateway(MqttClient client, String deviceId)
904-
throws MqttException {
905-
// [START detach_device]
906-
final String detachTopic = String.format("/devices/%s/detach", deviceId);
907-
System.out.println(String.format("Detaching: %s", detachTopic));
908-
String attachPayload = "{}";
909-
MqttMessage message = new MqttMessage(attachPayload.getBytes());
910-
message.setQos(1);
911-
client.publish(detachTopic, message);
912-
// [END detach_device]
886+
// [END iot_unbind_device_from_gateway]
913887
}
914888

915889
/** Create a device to bind to a gateway. */
916890
public static void createDevice(
917891
String projectId, String cloudRegion, String registryName, String deviceId)
918892
throws GeneralSecurityException, IOException {
919-
// [START create_device]
893+
// [START iot_create_device]
920894
GoogleCredential credential =
921895
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
922896
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
@@ -971,7 +945,7 @@ public static void createDevice(
971945
.execute();
972946

973947
System.out.println("Created device: " + createdDevice.toPrettyString());
974-
// [END create_device]
948+
// [END iot_create_device]
975949
}
976950

977951
/** Create a gateway to bind devices to. */
@@ -983,7 +957,7 @@ public static void createGateway(
983957
String certificateFilePath,
984958
String algorithm)
985959
throws GeneralSecurityException, IOException {
986-
// [START create_gateway]
960+
// [START iot_create_gateway]
987961
GoogleCredential credential =
988962
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
989963
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
@@ -1030,12 +1004,12 @@ public static void createGateway(
10301004
.execute();
10311005

10321006
System.out.println("Created gateway: " + createdDevice.toPrettyString());
1033-
// [END create_gateway]
1007+
// [END iot_create_gateway]
10341008
}
10351009

10361010
public static void listGateways(String projectId, String cloudRegion, String registryName)
10371011
throws IOException, GeneralSecurityException {
1038-
// [START list_gateways]
1012+
// [START iot_list_gateways]
10391013
GoogleCredential credential =
10401014
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
10411015
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
@@ -1049,42 +1023,38 @@ public static void listGateways(String projectId, String cloudRegion, String reg
10491023
String.format(
10501024
"projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
10511025

1052-
List<Device> devices =
1026+
List<Device> gateways =
10531027
service
10541028
.projects()
10551029
.locations()
10561030
.registries()
10571031
.devices()
10581032
.list(registryPath)
1059-
.setFieldMask("config,gatewayConfig")
1033+
.setGatewayListOptionsGatewayType("GATEWAY")
10601034
.execute()
10611035
.getDevices();
10621036

1063-
if (devices != null) {
1064-
System.out.println("Found " + devices.size() + " devices");
1065-
for (Device d : devices) {
1066-
if (d.getGatewayConfig() != null
1067-
&& d.getGatewayConfig().getGatewayType() != null
1068-
&& d.getGatewayConfig().getGatewayType().equals("GATEWAY")) {
1069-
System.out.println("Id: " + d.getId());
1070-
if (d.getConfig() != null) {
1071-
// Note that this will show the device config in Base64 encoded format.
1072-
System.out.println("Config: " + d.getGatewayConfig().toPrettyString());
1073-
}
1074-
System.out.println();
1037+
if (gateways != null) {
1038+
System.out.println("Found " + gateways.size() + " devices");
1039+
for (Device d : gateways) {
1040+
System.out.println("Id: " + d.getId());
1041+
if (d.getConfig() != null) {
1042+
// Note that this will show the device config in Base64 encoded format.
1043+
System.out.println("Config: " + d.getGatewayConfig().toPrettyString());
10751044
}
1045+
System.out.println();
10761046
}
10771047
} else {
10781048
System.out.println("Registry has no devices.");
10791049
}
1080-
// [END list_gateways]
1050+
// [END iot_list_gateways]
10811051
}
10821052

10831053
/** List devices bound to a gateway. */
10841054
public static void listDevicesForGateway(
10851055
String projectId, String cloudRegion, String registryName, String gatewayId)
10861056
throws IOException, GeneralSecurityException {
1087-
// [START list_devices_for_gateway]
1057+
// [START iot_list_devices_for_gateway]
10881058
GoogleCredential credential =
10891059
GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
10901060
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
@@ -1094,11 +1064,6 @@ public static void listDevicesForGateway(
10941064
.setApplicationName(APP_NAME)
10951065
.build();
10961066

1097-
final String gatewayPath =
1098-
String.format(
1099-
"projects/%s/locations/%s/registries/%s/devices/%s",
1100-
projectId, cloudRegion, registryName, gatewayId);
1101-
11021067
final String registryPath =
11031068
String.format(
11041069
"projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
@@ -1122,7 +1087,7 @@ public static void listDevicesForGateway(
11221087
} else {
11231088
System.out.println("Gateway has no bound devices.");
11241089
}
1125-
// [END list_devices_for_gateway]
1090+
// [END iot_list_devices_for_gateway]
11261091
}
11271092

11281093
/** Entry poit for CLI. */
@@ -1253,6 +1218,15 @@ public static void main(String[] args) throws Exception {
12531218
options.role);
12541219
}
12551220
break;
1221+
case "list-gateways":
1222+
System.out.println("Listing gateways: ");
1223+
listGateways(options.projectId, options.cloudRegion, options.registryName);
1224+
break;
1225+
case "list-devices-for-gateway":
1226+
System.out.println("Listing devices for a gateway: ");
1227+
listDevicesForGateway(
1228+
options.projectId, options.cloudRegion, options.registryName, options.gatewayId);
1229+
break;
12561230
case "send-command":
12571231
System.out.println("Sending command to device:");
12581232
sendCommand(

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExampleOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class DeviceRegistryExampleOptions {
3535
String commandData = "Specify with --data";
3636
String configuration = "Specify with -configuration";
3737
String deviceId; // Default to UUID?
38+
String gatewayId;
3839
String pubsubTopic;
3940
String registryName;
4041
String member;
@@ -122,6 +123,13 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
122123
.hasArg()
123124
.desc("Name for your Device.")
124125
.build());
126+
options.addOption(
127+
Option.builder()
128+
.type(String.class)
129+
.longOpt("gateway_id")
130+
.hasArg()
131+
.desc("Name for your Device.")
132+
.build());
125133
options.addOption(
126134
Option.builder()
127135
.type(String.class)
@@ -180,6 +188,10 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
180188
res.deviceId = commandLine.getOptionValue("device_id");
181189
}
182190

191+
if (commandLine.hasOption("device_id")) {
192+
res.gatewayId = commandLine.getOptionValue("gateway_id");
193+
}
194+
183195
if (commandLine.hasOption("project_id")) {
184196
res.projectId = commandLine.getOptionValue("project_id");
185197
} else {
@@ -211,6 +223,9 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
211223
if (commandLine.hasOption("device_id")) {
212224
res.deviceId = commandLine.getOptionValue("device_id");
213225
}
226+
if (commandLine.hasOption("gateway_id")) {
227+
res.gatewayId = commandLine.getOptionValue("gateway_id");
228+
}
214229
if (commandLine.hasOption("configuration")) {
215230
res.configuration = commandLine.getOptionValue("configuration");
216231
}

iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.cloud.iot.examples;
1818

1919
// [START iot_mqtt_includes]
20+
2021
import io.jsonwebtoken.JwtBuilder;
2122
import io.jsonwebtoken.Jwts;
2223
import io.jsonwebtoken.SignatureAlgorithm;
@@ -254,9 +255,9 @@ public static void sendDataFromBoundDevice(
254255
gatewayId,
255256
privateKeyFile,
256257
algorithm);
257-
DeviceRegistryExample.attachDeviceToGateway(client, deviceId);
258+
attachDeviceToGateway(client, deviceId);
258259
sendDataFromDevice(client, deviceId, messageType, telemetryData);
259-
DeviceRegistryExample.detachDeviceFromGateway(client, deviceId);
260+
detachDeviceFromGateway(client, deviceId);
260261
// [END send_data_from_bound_device]
261262
}
262263

@@ -284,10 +285,35 @@ public static void listenForConfigMessages(
284285
privateKeyFile,
285286
algorithm);
286287
// Connect the bound device and listen for configuration messages.
287-
DeviceRegistryExample.attachDeviceToGateway(client, deviceId);
288+
attachDeviceToGateway(client, deviceId);
288289
attachCallback(client, deviceId);
289290

290-
DeviceRegistryExample.detachDeviceFromGateway(client, deviceId);
291+
detachDeviceFromGateway(client, deviceId);
292+
}
293+
294+
public static void attachDeviceToGateway(MqttClient client, String deviceId)
295+
throws MqttException {
296+
// [START iot_attach_device]
297+
final String attachTopic = String.format("/devices/%s/attach", deviceId);
298+
System.out.println(String.format("Attaching: %s", attachTopic));
299+
String attachPayload = "{}";
300+
MqttMessage message = new MqttMessage(attachPayload.getBytes());
301+
message.setQos(1);
302+
client.publish(attachTopic, message);
303+
// [END iot_attach_device]
304+
}
305+
306+
/** Detaches a bound device from the Gateway. */
307+
public static void detachDeviceFromGateway(MqttClient client, String deviceId)
308+
throws MqttException {
309+
// [START iot_detach_device]
310+
final String detachTopic = String.format("/devices/%s/detach", deviceId);
311+
System.out.println(String.format("Detaching: %s", detachTopic));
312+
String attachPayload = "{}";
313+
MqttMessage message = new MqttMessage(attachPayload.getBytes());
314+
message.setQos(1);
315+
client.publish(detachTopic, message);
316+
// [END iot_detach_device]
291317
}
292318

293319
public static void mqttDeviceDemo(MqttExampleOptions options)

iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ public void testErrorTopic() throws Exception {
615615
new Thread() {
616616
public void run() {
617617
try {
618-
DeviceRegistryExample.attachDeviceToGateway(client, "garbage-device");
618+
MqttExample.attachDeviceToGateway(client, "garbage-device");
619619
MqttExample.attachCallback(client, "garbage-device");
620620
} catch (Exception e) {
621621
// TODO: Fail

0 commit comments

Comments
 (0)
0