Skip to content

Commit a2f064a

Browse files
authored
Adds commands beta sample (GoogleCloudPlatform#1228)
* Adds commands beta sample
1 parent 24d202f commit a2f064a

File tree

14 files changed

+2609
-0
lines changed

14 files changed

+2609
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Cloud IoT Core Java Command Sample
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iot/api-client/manager/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
This sample app demonstrates sending a command to a Cloud IoT Core device.
7+
8+
Note that before you can run the sample, you must configure a Google Cloud
9+
PubSub topic for Cloud IoT as described in [the parent README](../README.md).
10+
11+
Before running the samples, you can set the `GOOGLE_CLOUD_PROJECT` and
12+
`GOOGLE_APPLICATION_CREDENTIALS` environment variables to avoid passing them to
13+
the sample every time you run it.
14+
15+
## Setup
16+
Run the following command to install the libraries and build the sample with
17+
Maven:
18+
19+
mvn clean compile assembly:single
20+
21+
## Running the sample
22+
23+
The following description summarizes the sample usage:
24+
25+
usage: DeviceRegistryExample [--cloud_region <arg>] --command <arg>
26+
[--data <arg>] [--device_id <arg>] [--project_id <arg>]
27+
[--registry_name <arg>]
28+
Cloud IoT Core Commandline Example (Device / Registry management):
29+
30+
--cloud_region <arg> GCP cloud region.
31+
--command <arg> Command to run:
32+
send-command
33+
--data <arg> The command data (string or JSON) to send to
34+
the specified device.
35+
--device_id <arg> Name for your Device.
36+
--project_id <arg> GCP cloud project name.
37+
--registry_name <arg> Name for your Device Registry.
38+
39+
For example, if your project ID is `blue-jet-123`, your service account
40+
credentials are stored in your home folder in creds.json and you have generated
41+
your credentials using the shell script provided in the parent folder, you can
42+
run the sample as:
43+
44+
mvn exec:java \
45+
-Dexec.mainClass="com.example.cloud.iot.examples.DeviceRegistryExample" \
46+
-Dexec.args="-project_id=blue-jet-123 \
47+
-registry_name=your-registry-id \
48+
-device_id=your-device-id \
49+
-command=send-command \
50+
-data=hello"
51+
52+
# Cloud IoT Core Java MQTT Command example
53+
54+
This sample app receives commands sent by the command app.
55+
56+
Note that before you can run the sample, you must configure a Google Cloud
57+
PubSub topic for Cloud IoT Core and register a device as described in the
58+
[parent README](../README.md).
59+
60+
## Setup
61+
62+
Run the following command to install the dependencies using Maven:
63+
64+
mvn clean compile
65+
66+
## Running the sample
67+
68+
The following command summarizes the sample usage:
69+
70+
usage: DeviceRegistryExample [--cloud_region <arg>] --command <arg>
71+
[--command_data <arg>] [--device_id <arg>] [--project_id <arg>]
72+
[--registry_name <arg>]
73+
Cloud IoT Core Commandline Example (Device / Registry management):
74+
75+
--cloud_region <arg> GCP cloud region.
76+
--command <arg> Command to run:
77+
send-command
78+
--command_data <arg> The command data (string or JSON) to send to
79+
the specified device.
80+
--device_id <arg> Name for your Device.
81+
--project_id <arg> GCP cloud project name.
82+
--registry_name <arg> Name for your Device Registry.
83+
84+
For example, if your project ID is `blue-jet-123`, your device registry is
85+
located in the `asia-east1` region, and you have generated your
86+
credentials using the [`generate_keys.sh`](../generate_keys.sh) script
87+
provided in the parent folder, you can run the sample as:
88+
89+
mvn exec:java \
90+
-Dexec.mainClass="com.example.cloud.iot.examples.MqttExample" \
91+
-Dexec.args="-project_id=blue-jet-123 \
92+
-registry_id=my-registry \
93+
-cloud_region=asia-east1 \
94+
-device_id=my-device \
95+
-private_key_file=../rsa_private_pkcs8 \
96+
-algorithm=RS256"
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!--
2+
Copyright 2018 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>com.example.cloud</groupId>
20+
<artifactId>cloudiot-manager-demo</artifactId>
21+
<packaging>jar</packaging>
22+
<version>1.0</version>
23+
<name>cloudiot-manager-demo</name>
24+
<url>http://maven.apache.org</url>
25+
26+
<!--
27+
The parent pom defines common style checks and testing strategies for our samples.
28+
Removing or replacing it should not affect the execution of the samples in anyway.
29+
-->
30+
<parent>
31+
<groupId>com.google.cloud.samples</groupId>
32+
<artifactId>shared-configuration</artifactId>
33+
<version>1.0.8</version>
34+
</parent>
35+
<repositories>
36+
<!-- Use a local directory for the Text API dependency. -->
37+
<repository>
38+
<id>project.local</id>
39+
<name>project</name>
40+
<url>file:${project.basedir}/./repo</url>
41+
</repository>
42+
</repositories>
43+
<properties>
44+
<maven.compiler.target>1.8</maven.compiler.target>
45+
<maven.compiler.source>1.8</maven.compiler.source>
46+
</properties>
47+
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.eclipse.paho</groupId>
51+
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
52+
<version>1.2.0</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.json</groupId>
56+
<artifactId>json</artifactId>
57+
<version>20090211</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.jsonwebtoken</groupId>
61+
<artifactId>jjwt</artifactId>
62+
<version>0.7.0</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>joda-time</groupId>
66+
<artifactId>joda-time</artifactId>
67+
<version>2.1</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.google.apis</groupId>
71+
<artifactId>google-api-services-cloudiot</artifactId>
72+
<version>v1-rev49-1.25.0</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.google.cloud</groupId>
76+
<artifactId>google-cloud-pubsub</artifactId>
77+
<version>0.24.0-beta</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.google.oauth-client</groupId>
81+
<artifactId>google-oauth-client</artifactId>
82+
<version>1.22.0</version>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.google.guava</groupId>
86+
<artifactId>guava</artifactId>
87+
<version>23.0</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>com.google.api-client</groupId>
91+
<artifactId>google-api-client</artifactId>
92+
<version>1.23.0</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>commons-cli</groupId>
96+
<artifactId>commons-cli</artifactId>
97+
<version>1.3</version>
98+
</dependency>
99+
100+
<!-- Test dependencies -->
101+
<dependency>
102+
<groupId>junit</groupId>
103+
<artifactId>junit</artifactId>
104+
<version>4.12</version>
105+
<scope>test</scope>
106+
</dependency>
107+
<dependency>
108+
<groupId>com.google.truth</groupId>
109+
<artifactId>truth</artifactId>
110+
<version>0.34</version>
111+
<scope>test</scope>
112+
</dependency>
113+
</dependencies>
114+
115+
<build>
116+
<plugins>
117+
<plugin>
118+
<artifactId>maven-assembly-plugin</artifactId>
119+
<configuration>
120+
<archive>
121+
<manifest>
122+
<mainClass>com.example.cloudiot.Manage</mainClass>
123+
</manifest>
124+
</archive>
125+
<descriptorRefs>
126+
<descriptorRef>jar-with-dependencies</descriptorRef>
127+
</descriptorRefs>
128+
</configuration>
129+
</plugin>
130+
</plugins>
131+
</build>
132+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Test public certificate files
2+
3+
The certificates in this folder are only provided for testing and should not be
4+
used for registering your devices. Instructions and a shell script are in the
5+
parent folder for getting started.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJ/zj75PhGtoayWD9pm/IbXRXi9ai
3+
RQDMFQ87sOOQIWDTxnsHFplqb8cDCtO1h5QsutcAVTPZL0CZiqIg5j7b1w==
4+
-----END PUBLIC KEY-----

0 commit comments

Comments
 (0)