Skip to content

Commit

Permalink
Integrate gateway management features (#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhuushmgl authored and gguuss committed Feb 14, 2019
1 parent 49f44b4 commit 75a52b2
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 144 deletions.
35 changes: 35 additions & 0 deletions iot/api-client/manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ located in the `asia-east1` region, and you have generated your
credentials using the [`generate_keys.sh`](../generate_keys.sh) script
provided in the parent folder, you can run the sample as:

Run mqtt example:

mvn exec:java \
-Dexec.mainClass="com.example.cloud.iot.examples.MqttExample" \
-Dexec.args="-project_id=blue-jet-123 \
Expand All @@ -334,6 +336,39 @@ provided in the parent folder, you can run the sample as:
-private_key_file=../rsa_private_pkcs8 \
-algorithm=RS256"

Listen for configuration messages:

mvn exec:java \
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
-Dexec.args="-project_id=blue-jet-123 \
-cloud_region=us-central1 \
-registry_id=my-registry \
-gateway_id=test-gateway \
-ec_public_key_file=../ec_public.pem \
-algorithm='ES256' or 'RS256'
-device_id=java-device-0 \
-mqtt_bridge_hostname=mqtt.googleapis.com \
-mqtt_bridge_port=443 or 8883 \
-command=listen-for-config-messages"

Send data on behalf of device:

mvn exec:java \
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
-Dexec.args="-project_id=blue-jet-123 \
-cloud_region=us-central1 \
-registry_id=my-registry \
-gateway_id=test-gateway \
-ec_public_key_file=../ec_public.pem \
-algorithm='ES256' or 'RS256' \
-device_id=java-device-0 \
-message_type='event' or 'state' \
-telemetry_data='your telemetry msg' \
-mqtt_bridge_hostname=mqtt.googleapis.com \
-mqtt_bridge_port=443 or 8883 \
-command=send-data-from-bound-device"


## Reading the messages written by the sample client

1. Create a subscription to your topic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
* <code>
* $ mvn clean compile assembly:single
* $ mvn exec:java \
* -Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
* -Dexec.mainClass="com.google.cloud.iot.examples.DeviceRegistryExample" \
* -Dexec.args="-project_id=my-project-id \
* -pubsub_topic=projects/my-project-id/topics/my-topic-id \
* -ec_public_key_file=/path/to/ec_public.pem \
Expand Down Expand Up @@ -887,23 +887,6 @@ public static void unbindDeviceFromGateway(
// [END unbind_device_from_gateway]
}

public static void sendDataFromDevice(
MqttClient client, String deviceId, String messageType, String data) throws MqttException {
// [START send_data_from_bound_device]
if (!messageType.equals("events") && !messageType.equals("state")) {
System.err.println(
String.format(
"%s is invalid message type, must ether be 'state' or events'", messageType));
return;
}
final String dataTopic = String.format("/devices/%s/%s", deviceId, messageType);
MqttMessage message = new MqttMessage(data.getBytes());
message.setQos(1);
client.publish(dataTopic, message);
System.out.println("Data sent");
// [END send_data_from_bound_device]
}

public static void attachDeviceToGateway(MqttClient client, String deviceId)
throws MqttException {
// [START attach_device]
Expand Down Expand Up @@ -1151,25 +1134,6 @@ public static void main(String[] args) throws Exception {
}

switch (options.command) {
case "bind-device-to-gateway":
System.out.println("Binding device to gateway:");
bindDeviceToGateway(
options.projectId,
options.cloudRegion,
options.registryName,
options.deviceId,
options.gatewayId);
break;
case "create-gateway":
System.out.println("Creating Gateway:");
createGateway(
options.projectId,
options.cloudRegion,
options.registryName,
options.gatewayId,
options.publicKeyFile,
options.algorithm);
break;
case "create-iot-topic":
System.out.println("Create IoT Topic:");
createIotTopic(options.projectId, options.pubsubTopic);
Expand Down Expand Up @@ -1239,16 +1203,6 @@ public static void main(String[] args) throws Exception {
case "list-devices":
System.out.println("List devices");
listDevices(options.projectId, options.cloudRegion, options.registryName);
break;
case "list-gateways":
System.out.println("Listing gateways:");
listGateways(options.projectId, options.cloudRegion, options.registryName);
break;
case "list-devices-for-gateway":
System.out.println("Listing devices for gateway:");
listDevicesForGateway(
options.projectId, options.cloudRegion, options.registryName, options.gatewayId);

break;
case "list-registries":
System.out.println("List registries");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/** Command line options for the Device Manager example. */
public class DeviceRegistryExampleOptions {
String algorithm;
static final Options options = new Options();
String projectId;
String ecPublicKeyFile = "ec_public.pem";
String rsaCertificateFile = "rsa_cert.pem";
Expand All @@ -35,14 +35,11 @@ public class DeviceRegistryExampleOptions {
String commandData = "Specify with --data";
String configuration = "Specify with -configuration";
String deviceId; // Default to UUID?
String gatewayId;
String pubsubTopic;
String publicKeyFile;
String registryName;
String member;
String role;
long version = 0;
static final Options options = new Options();

/** Construct an DeviceRegistryExampleOptions class from command line flags. */
public static DeviceRegistryExampleOptions fromFlags(String[] args) {
Expand All @@ -54,8 +51,6 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
.hasArg()
.desc(
"Command to run:"
+ "\n\tbind-device-to-gateway"
+ "\n\tcreate-gateway"
+ "\n\tcreate-iot-topic" // TODO: Descriptions or too verbose?
+ "\n\tcreate-rsa"
+ "\n\tcreate-es"
Expand All @@ -69,25 +64,15 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
+ "\n\tget-registry"
+ "\n\tlist-devices"
+ "\n\tlist-registries"
+ "\n\tlist-gateways"
+ "\n\tlist-devices-for-gateway"
+ "\n\tpatch-device-es"
+ "\n\tpatch-device-rsa"
+ "\n\tset-config"
+ "\n\tset-iam-permissions"
+ "\n\tsend-command"
+ "\n\tunbind-device-from-gateway")
+ "\n\tsend-command")
.required()
.build());

// Optional arguments.
options.addOption(
Option.builder()
.type(String.class)
.longOpt("algorithm")
.hasArg()
.desc("Algorithm used for public/private keys.")
.build());
options.addOption(
Option.builder()
.type(String.class)
Expand Down Expand Up @@ -123,13 +108,6 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
.hasArg()
.desc("GCP cloud project name.")
.build());
options.addOption(
Option.builder()
.type(String.class)
.longOpt("public_key_file")
.hasArg()
.desc("Public key file used for registering devices and gateways.")
.build());
options.addOption(
Option.builder()
.type(String.class)
Expand All @@ -144,13 +122,6 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
.hasArg()
.desc("Name for your Device.")
.build());
options.addOption(
Option.builder()
.type(String.class)
.longOpt("gateway_id")
.hasArg()
.desc("The identifier for the Gateway.")
.build());
options.addOption(
Option.builder()
.type(String.class)
Expand Down Expand Up @@ -199,9 +170,6 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
throw new ParseException("Invalid command, showing help.");
}

if (commandLine.hasOption("algorithm")) {
res.algorithm = commandLine.getOptionValue("algorithm");
}
if (commandLine.hasOption("cloud_region")) {
res.cloudRegion = commandLine.getOptionValue("cloud_region");
}
Expand All @@ -211,9 +179,7 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
if (commandLine.hasOption("device_id")) {
res.deviceId = commandLine.getOptionValue("device_id");
}
if (commandLine.hasOption("gateway_id")) {
res.gatewayId = commandLine.getOptionValue("gateway_id");
}

if (commandLine.hasOption("project_id")) {
res.projectId = commandLine.getOptionValue("project_id");
} else {
Expand All @@ -229,9 +195,7 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
} else {
// TODO: Get from environment variable
}
if (commandLine.hasOption("public_key_file")) {
res.publicKeyFile = commandLine.getOptionValue("public_key_file");
}

if (commandLine.hasOption("ec_public_key_file")) {
res.ecPublicKeyFile = commandLine.getOptionValue("ec_public_key_file");
}
Expand Down Expand Up @@ -266,7 +230,8 @@ public static DeviceRegistryExampleOptions fromFlags(String[] args) {
String footer = "\nhttps://cloud.google.com/iot-core";

HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("DeviceRegistryExample", header, options, footer, true);
formatter.printHelp(
"DeviceRegistryExample", header, options, footer, true);

System.err.println(e.getMessage());
return null;
Expand Down
Loading

0 comments on commit 75a52b2

Please sign in to comment.