Skip to content

Commit 75a52b2

Browse files
munkhuushmglgguuss
authored andcommitted
Integrate gateway management features (GoogleCloudPlatform#1336)
1 parent 49f44b4 commit 75a52b2

File tree

6 files changed

+440
-144
lines changed

6 files changed

+440
-144
lines changed

iot/api-client/manager/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ located in the `asia-east1` region, and you have generated your
325325
credentials using the [`generate_keys.sh`](../generate_keys.sh) script
326326
provided in the parent folder, you can run the sample as:
327327

328+
Run mqtt example:
329+
328330
mvn exec:java \
329331
-Dexec.mainClass="com.example.cloud.iot.examples.MqttExample" \
330332
-Dexec.args="-project_id=blue-jet-123 \
@@ -334,6 +336,39 @@ provided in the parent folder, you can run the sample as:
334336
-private_key_file=../rsa_private_pkcs8 \
335337
-algorithm=RS256"
336338

339+
Listen for configuration messages:
340+
341+
mvn exec:java \
342+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
343+
-Dexec.args="-project_id=blue-jet-123 \
344+
-cloud_region=us-central1 \
345+
-registry_id=my-registry \
346+
-gateway_id=test-gateway \
347+
-ec_public_key_file=../ec_public.pem \
348+
-algorithm='ES256' or 'RS256'
349+
-device_id=java-device-0 \
350+
-mqtt_bridge_hostname=mqtt.googleapis.com \
351+
-mqtt_bridge_port=443 or 8883 \
352+
-command=listen-for-config-messages"
353+
354+
Send data on behalf of device:
355+
356+
mvn exec:java \
357+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
358+
-Dexec.args="-project_id=blue-jet-123 \
359+
-cloud_region=us-central1 \
360+
-registry_id=my-registry \
361+
-gateway_id=test-gateway \
362+
-ec_public_key_file=../ec_public.pem \
363+
-algorithm='ES256' or 'RS256' \
364+
-device_id=java-device-0 \
365+
-message_type='event' or 'state' \
366+
-telemetry_data='your telemetry msg' \
367+
-mqtt_bridge_hostname=mqtt.googleapis.com \
368+
-mqtt_bridge_port=443 or 8883 \
369+
-command=send-data-from-bound-device"
370+
371+
337372
## Reading the messages written by the sample client
338373

339374
1. Create a subscription to your topic.

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

+1-47
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
* <code>
8686
* $ mvn clean compile assembly:single
8787
* $ mvn exec:java \
88-
* -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
88+
* -Dexec.mainClass="com.google.cloud.iot.examples.DeviceRegistryExample" \
8989
* -Dexec.args="-project_id=my-project-id \
9090
* -pubsub_topic=projects/my-project-id/topics/my-topic-id \
9191
* -ec_public_key_file=/path/to/ec_public.pem \
@@ -887,23 +887,6 @@ public static void unbindDeviceFromGateway(
887887
// [END unbind_device_from_gateway]
888888
}
889889

890-
public static void sendDataFromDevice(
891-
MqttClient client, String deviceId, String messageType, String data) throws MqttException {
892-
// [START send_data_from_bound_device]
893-
if (!messageType.equals("events") && !messageType.equals("state")) {
894-
System.err.println(
895-
String.format(
896-
"%s is invalid message type, must ether be 'state' or events'", messageType));
897-
return;
898-
}
899-
final String dataTopic = String.format("/devices/%s/%s", deviceId, messageType);
900-
MqttMessage message = new MqttMessage(data.getBytes());
901-
message.setQos(1);
902-
client.publish(dataTopic, message);
903-
System.out.println("Data sent");
904-
// [END send_data_from_bound_device]
905-
}
906-
907890
public static void attachDeviceToGateway(MqttClient client, String deviceId)
908891
throws MqttException {
909892
// [START attach_device]
@@ -1151,25 +1134,6 @@ public static void main(String[] args) throws Exception {
11511134
}
11521135

11531136
switch (options.command) {
1154-
case "bind-device-to-gateway":
1155-
System.out.println("Binding device to gateway:");
1156-
bindDeviceToGateway(
1157-
options.projectId,
1158-
options.cloudRegion,
1159-
options.registryName,
1160-
options.deviceId,
1161-
options.gatewayId);
1162-
break;
1163-
case "create-gateway":
1164-
System.out.println("Creating Gateway:");
1165-
createGateway(
1166-
options.projectId,
1167-
options.cloudRegion,
1168-
options.registryName,
1169-
options.gatewayId,
1170-
options.publicKeyFile,
1171-
options.algorithm);
1172-
break;
11731137
case "create-iot-topic":
11741138
System.out.println("Create IoT Topic:");
11751139
createIotTopic(options.projectId, options.pubsubTopic);
@@ -1239,16 +1203,6 @@ public static void main(String[] args) throws Exception {
12391203
case "list-devices":
12401204
System.out.println("List devices");
12411205
listDevices(options.projectId, options.cloudRegion, options.registryName);
1242-
break;
1243-
case "list-gateways":
1244-
System.out.println("Listing gateways:");
1245-
listGateways(options.projectId, options.cloudRegion, options.registryName);
1246-
break;
1247-
case "list-devices-for-gateway":
1248-
System.out.println("Listing devices for gateway:");
1249-
listDevicesForGateway(
1250-
options.projectId, options.cloudRegion, options.registryName, options.gatewayId);
1251-
12521206
break;
12531207
case "list-registries":
12541208
System.out.println("List registries");

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

+6-41
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
/** Command line options for the Device Manager example. */
2828
public class DeviceRegistryExampleOptions {
29-
String algorithm;
29+
static final Options options = new Options();
3030
String projectId;
3131
String ecPublicKeyFile = "ec_public.pem";
3232
String rsaCertificateFile = "rsa_cert.pem";
@@ -35,14 +35,11 @@ public class DeviceRegistryExampleOptions {
3535
String commandData = "Specify with --data";
3636
String configuration = "Specify with -configuration";
3737
String deviceId; // Default to UUID?
38-
String gatewayId;
3938
String pubsubTopic;
40-
String publicKeyFile;
4139
String registryName;
4240
String member;
4341
String role;
4442
long version = 0;
45-
static final Options options = new Options();
4643

4744
/** Construct an DeviceRegistryExampleOptions class from command line flags. */
4845
public static DeviceRegistryExampleOptions fromFlags(String[] args) {
@@ -54,8 +51,6 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
5451
.hasArg()
5552
.desc(
5653
"Command to run:"
57-
+ "\n\tbind-device-to-gateway"
58-
+ "\n\tcreate-gateway"
5954
+ "\n\tcreate-iot-topic" // TODO: Descriptions or too verbose?
6055
+ "\n\tcreate-rsa"
6156
+ "\n\tcreate-es"
@@ -69,25 +64,15 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
6964
+ "\n\tget-registry"
7065
+ "\n\tlist-devices"
7166
+ "\n\tlist-registries"
72-
+ "\n\tlist-gateways"
73-
+ "\n\tlist-devices-for-gateway"
7467
+ "\n\tpatch-device-es"
7568
+ "\n\tpatch-device-rsa"
7669
+ "\n\tset-config"
7770
+ "\n\tset-iam-permissions"
78-
+ "\n\tsend-command"
79-
+ "\n\tunbind-device-from-gateway")
71+
+ "\n\tsend-command")
8072
.required()
8173
.build());
8274

8375
// Optional arguments.
84-
options.addOption(
85-
Option.builder()
86-
.type(String.class)
87-
.longOpt("algorithm")
88-
.hasArg()
89-
.desc("Algorithm used for public/private keys.")
90-
.build());
9176
options.addOption(
9277
Option.builder()
9378
.type(String.class)
@@ -123,13 +108,6 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
123108
.hasArg()
124109
.desc("GCP cloud project name.")
125110
.build());
126-
options.addOption(
127-
Option.builder()
128-
.type(String.class)
129-
.longOpt("public_key_file")
130-
.hasArg()
131-
.desc("Public key file used for registering devices and gateways.")
132-
.build());
133111
options.addOption(
134112
Option.builder()
135113
.type(String.class)
@@ -144,13 +122,6 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
144122
.hasArg()
145123
.desc("Name for your Device.")
146124
.build());
147-
options.addOption(
148-
Option.builder()
149-
.type(String.class)
150-
.longOpt("gateway_id")
151-
.hasArg()
152-
.desc("The identifier for the Gateway.")
153-
.build());
154125
options.addOption(
155126
Option.builder()
156127
.type(String.class)
@@ -199,9 +170,6 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
199170
throw new ParseException("Invalid command, showing help.");
200171
}
201172

202-
if (commandLine.hasOption("algorithm")) {
203-
res.algorithm = commandLine.getOptionValue("algorithm");
204-
}
205173
if (commandLine.hasOption("cloud_region")) {
206174
res.cloudRegion = commandLine.getOptionValue("cloud_region");
207175
}
@@ -211,9 +179,7 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
211179
if (commandLine.hasOption("device_id")) {
212180
res.deviceId = commandLine.getOptionValue("device_id");
213181
}
214-
if (commandLine.hasOption("gateway_id")) {
215-
res.gatewayId = commandLine.getOptionValue("gateway_id");
216-
}
182+
217183
if (commandLine.hasOption("project_id")) {
218184
res.projectId = commandLine.getOptionValue("project_id");
219185
} else {
@@ -229,9 +195,7 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
229195
} else {
230196
// TODO: Get from environment variable
231197
}
232-
if (commandLine.hasOption("public_key_file")) {
233-
res.publicKeyFile = commandLine.getOptionValue("public_key_file");
234-
}
198+
235199
if (commandLine.hasOption("ec_public_key_file")) {
236200
res.ecPublicKeyFile = commandLine.getOptionValue("ec_public_key_file");
237201
}
@@ -266,7 +230,8 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
266230
String footer = "\nhttps://cloud.google.com/iot-core";
267231

268232
HelpFormatter formatter = new HelpFormatter();
269-
formatter.printHelp("DeviceRegistryExample", header, options, footer, true);
233+
formatter.printHelp(
234+
"DeviceRegistryExample", header, options, footer, true);
270235

271236
System.err.println(e.getMessage());
272237
return null;

0 commit comments

Comments
 (0)