Skip to content

Commit 6e8c925

Browse files
authored
Merge pull request #669 from GoogleCloudPlatform/cloud-iot-core
Initial commit of sample
2 parents 4128819 + 0aa4b9b commit 6e8c925

File tree

15 files changed

+1280
-0
lines changed

15 files changed

+1280
-0
lines changed

iot/api-client/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Cloud IoT Core Java Samples
2+
This folder contains Java samples that demonstrate an overview of the
3+
Google Cloud IoT Core platform.
4+
5+
## Quickstart
6+
7+
1. Install the gCloud CLI as described in [the device manager guide](https://cloud-dot-devsite.googleplex.com/iot/docs/device_manager_guide).
8+
2. Create a PubSub topic:
9+
10+
gcloud beta pubsub topics create projects/my-iot-project/topics/device-events
11+
12+
3. Add the special account `[email protected]` to that
13+
PubSub topic from the [Cloud Developer Console](https://console.cloud.google.com)
14+
or by using the helper script in the [/scripts](./scripts) folder.
15+
16+
4. Create a registry:
17+
18+
gcloud alpha iot registries create my-registry \
19+
--project=my-iot-project \
20+
--region=us-central1 \
21+
--pubsub-topic=projects/my-iot-project/topics/device-events
22+
23+
5. Use the [`generate_keys.sh`](generate_keys.sh) script to generate your signing keys:
24+
25+
./generate_keys.sh
26+
27+
6. Create a device.
28+
29+
gcloud alpha iot devices create my-java-device \
30+
--project=my-iot-project \
31+
--region=us-central1 \
32+
--registry=my-registry \
33+
--public-key path=rsa_cert.pem,type=rs256
34+
35+
7. Connect a sample device using the sample app in the [`mqtt_example`](./mqtt_example) folder.
36+
8. Learn how to manage devices programatically with the sample app in the
37+
`manager` folder.
38+

iot/api-client/generate_keys.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
# Copyright 2017 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes -out \
18+
rsa_cert.pem -subj "/CN=unused"
19+
openssl ecparam -genkey -name prime256v1 -noout -out ec_private.pem
20+
openssl ec -in ec_private.pem -pubout -out ec_public.pem
21+
openssl pkcs8 -topk8 -inform PEM -outform DER -in rsa_private.pem \
22+
-nocrypt > rsa_private_pkcs8
23+
openssl pkcs8 -topk8 -inform PEM -outform DER -in ec_private.pem \
24+
-nocrypt > ec_private_pkcs8

iot/api-client/manager/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Cloud IoT Core Java Device Management example
2+
3+
This sample app demonstrates device management for Google Cloud IoT Core.
4+
5+
Note that before you can run the sample, you must configure a Google Cloud
6+
PubSub topic for Cloud IoT as described in [the parent README](../README.md).
7+
8+
## Setup
9+
10+
Manually install [the provided client library](https://cloud.google.com/iot/resources/java/cloud-iot-core-library.jar)
11+
for Cloud IoT Core to Maven:
12+
13+
mvn install:install-file -Dfile=cloud-iot-core-library.jar -DgroupId=com.google.apis \
14+
-DartifactId=google-api-services-cloudiot -Dversion=v1beta1-rev20170418-1.22.0-SNAPSHOT \
15+
-Dpackaging=jar
16+
17+
Run the following command to install the libraries and build the sample with
18+
Maven:
19+
20+
mvn clean compile assembly:single
21+
22+
## Running the sample
23+
24+
The following command summarizes the sample usage:
25+
26+
mvn exec:java \
27+
-Dexec.mainClass="com.google.cloud.iot.examples.DeviceRegistryExample" \
28+
-Dexec.args="-project_id=my-project-id \
29+
-pubsub_topic=projects/my-project-id/topics/my-topic-id \
30+
-ec_public_key_file=/path/to/ec_public.pem \
31+
-rsa_certificate_file=/path/to/rsa_cert.pem"
32+
33+
For example, if your project ID is `blue-jet-123`, your service account
34+
credentials are stored in your home folder in creds.json and you have generated
35+
your credentials using the shell script provided in the parent folder, you can
36+
run the sample as:
37+
38+
mvn exec:java \
39+
-Dexec.mainClass="com.google.cloud.iot.examples.DeviceRegistryExample" \
40+
-Dexec.args="-project_id=blue-jet-123 \
41+
-pubsub_topic=projects/blue-jet-123/topics/device-events \
42+
-ec_public_key_file=../ec_public.pem \
43+
-rsa_certificate_file=../rsa_cert.pem"

iot/api-client/manager/pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!--
2+
Copyright 2017 Google Inc.
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+
<!-- Parent defines config for testing & linting. -->
27+
<parent>
28+
<artifactId>doc-samples</artifactId>
29+
<groupId>com.google.cloud</groupId>
30+
<version>1.0.0</version>
31+
<relativePath>../../../</relativePath>
32+
</parent>
33+
34+
<properties>
35+
<maven.compiler.source>1.7</maven.compiler.source>
36+
<maven.compiler.target>1.7</maven.compiler.target>
37+
</properties>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>com.google.apis</groupId>
42+
<artifactId>google-api-services-cloudiot</artifactId>
43+
<version>v1beta1-rev20170418-1.22.0-SNAPSHOT</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.google.oauth-client</groupId>
47+
<artifactId>google-oauth-client</artifactId>
48+
<version>1.22.0</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.api-client</groupId>
52+
<artifactId>google-api-client</artifactId>
53+
<version>1.22.0</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>commons-cli</groupId>
57+
<artifactId>commons-cli</artifactId>
58+
<version>1.3</version>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<artifactId>maven-assembly-plugin</artifactId>
66+
<configuration>
67+
<archive>
68+
<manifest>
69+
<mainClass>com.example.cloudiot.Manage</mainClass>
70+
</manifest>
71+
</archive>
72+
<descriptorRefs>
73+
<descriptorRef>jar-with-dependencies</descriptorRef>
74+
</descriptorRefs>
75+
</configuration>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
</project>

0 commit comments

Comments
 (0)