Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit of sample #669

Merged
merged 5 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions iot/api-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Cloud IoT Core Java Samples
This folder contains Java samples that demonstrate an overview of the
Google Cloud IoT Core platform.

## Quickstart

1. Install the gCloud CLI as described in [the device manager guide](https://cloud-dot-devsite.googleplex.com/iot/docs/device_manager_guide).
2. Create a PubSub topic:

gcloud beta pubsub topics create projects/my-iot-project/topics/device-events

3. Add the special account `[email protected]` to that
PubSub topic from the [Cloud Developer Console](https://console.cloud.google.com)
or by using the helper script in the [/scripts](./scripts) folder.

4. Create a registry:

gcloud alpha iot registries create my-registry \
--project=my-iot-project \
--region=us-central1 \
--pubsub-topic=projects/my-iot-project/topics/device-events

5. Use the [`generate_keys.sh`](generate_keys.sh) script to generate your signing keys:

./generate_keys.sh

6. Create a device.

gcloud alpha iot devices create my-java-device \
--project=my-iot-project \
--region=us-central1 \
--registry=my-registry \
--public-key path=rsa_cert.pem,type=rs256

7. Connect a sample device using the sample app in the [`mqtt_example`](./mqtt_example) folder.
8. Learn how to manage devices programatically with the sample app in the
`manager` folder.

24 changes: 24 additions & 0 deletions iot/api-client/generate_keys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes -out \
rsa_cert.pem -subj "/CN=unused"
openssl ecparam -genkey -name prime256v1 -noout -out ec_private.pem
openssl ec -in ec_private.pem -pubout -out ec_public.pem
openssl pkcs8 -topk8 -inform PEM -outform DER -in rsa_private.pem \
-nocrypt > rsa_private_pkcs8
openssl pkcs8 -topk8 -inform PEM -outform DER -in ec_private.pem \
-nocrypt > ec_private_pkcs8
43 changes: 43 additions & 0 deletions iot/api-client/manager/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Cloud IoT Core Java Device Management example

This sample app demonstrates device management for Google Cloud IoT Core.

Note that before you can run the sample, you must configure a Google Cloud
PubSub topic for Cloud IoT as described in [the parent README](../README.md).

## Setup

Manually install [the provided client library](https://cloud.google.com/iot/resources/java/cloud-iot-core-library.jar)
for Cloud IoT Core to Maven:

mvn install:install-file -Dfile=cloud-iot-core-library.jar -DgroupId=com.google.apis \
-DartifactId=google-api-services-cloudiot -Dversion=v1beta1-rev20170418-1.22.0-SNAPSHOT \
-Dpackaging=jar

Run the following command to install the libraries and build the sample with
Maven:

mvn clean compile assembly:single

## Running the sample

The following command summarizes the sample usage:

mvn exec:java \
-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 \
-rsa_certificate_file=/path/to/rsa_cert.pem"

For example, if your project ID is `blue-jet-123`, your service account
credentials are stored in your home folder in creds.json and you have generated
your credentials using the shell script provided in the parent folder, you can
run the sample as:

mvn exec:java \
-Dexec.mainClass="com.google.cloud.iot.examples.DeviceRegistryExample" \
-Dexec.args="-project_id=blue-jet-123 \
-pubsub_topic=projects/blue-jet-123/topics/device-events \
-ec_public_key_file=../ec_public.pem \
-rsa_certificate_file=../rsa_cert.pem"
79 changes: 79 additions & 0 deletions iot/api-client/manager/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!--
Copyright 2017 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.cloud</groupId>
<artifactId>cloudiot-manager-demo</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>cloudiot-manager-demo</name>
<url>http://maven.apache.org</url>

<!-- Parent defines config for testing & linting. -->
<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>../../../</relativePath>
</parent>

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-cloudiot</artifactId>
<version>v1beta1-rev20170418-1.22.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.22.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.22.0</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.cloudiot.Manage</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading