Before You Begin
This tutorial shows you how to build and run an application that simulates a virtual device communicating with Oracle Internet of Things (IoT) Cloud Service through the Apache Felix framework. This tutorial takes approximately 15 minutes to complete.
Background
Oracle IoT Cloud Service provides communication services between devices and enterprise applications. Using an Open Services Gateway initiative (OSGi) framework like Apache Felix, an application can communicate with Oracle IoT Cloud Service.
In this tutorial, you:
- Build an application using the core libraries for the Apache Felix framework.
- Run an application that sends a message to Oracle IoT Cloud Service through a simulator device using the Apache Felix framework.
This tutorial assumes that you are familiar with OSGi.
What Do You Need?
- Access to an instance of the Oracle IoT Cloud Service
- A computer running Windows 7 or later, Apple macOS 10.9 or later, or Linux with a web browser and a recent version of Java Developer Kit (JDK) installed
- Completion of the following tutorial: Configuring an Application in Oracle Internet of Things Cloud Service tutorial to obtain the provisioning file
- The latest version of the Java SE Client Software Library from Oracle Technologies Network
- The latest version of the Apache Felix framework
- The
HumiditySensor.jsonandTemperatureSensor.jsonfiles - The
SampleActivator.javafile - A REST client utility, such as RESTClient for Firefox or Advanced REST Client for Google Chrome
- Knowledge of Java, basic knowledge of command-line tools. For information on how to correctly configure your Java Environment, see Setting Up Your Java SE Development Environment to Use Oracle IoT Cloud Service Client Software Libraries
Install and Configure the Required Software
- Download the latest version of the Java SE Client Software Library binary, source and sample files from the Oracle Technologies Network downloads page.
The name of the zip files areiotcs-csl-javase-bin-release.zip,iotcs-cls-javase-src-release.zipandiotcs-csl-javase-samples-release.zip,wherereleaseis the release and version number. - Extract the
iotcs-csl-javase-bin-release.zip,iotcs-cls-javase-src-release.zipandiotcs-csl-javase-samples-release.zipfiles.
The following subdirectories are listed under theiotcs/csl/javasedirectory:binlibsamplessrc
CL_HOMErefers to theiotcs/csl/javasedirectory. - Download the latest version of the Apache Felix framework.
The name of the file isorg.apache.felix.main.distribution-release,wherereleaseis the release and version number. - Extract the
org.apache.felix.main.distribution-releasefile.Apache_Felix_pathrefers to the Apache Felix installation directory.
Register Your Device Models
This tutorial uses JavaScript Object Notation (JSON) files to register your device models.
- Download and save the
HumiditySensor.jsonandTemperatureSensor.jsonJSON files that contain descriptions of your device models (links in the prerequisites section). - Open your REST client and use the following information to register the humidity sensor device model:
- Method: POST
- URL:
Oracle_IoT_Cloud_Service_Instance:443/iot/api/v2/deviceModels - Headers:
Content-Type:application/json - Body: Content of the
HumiditySensor.jsonfile
- Use the following information to register the temperature sensor device
model:
- Method: POST
- URL:
Oracle_IoT_Cloud_Service_Instance:443/iot/api/v2/deviceModels - Headers:
Content-Type:application/json - Body: Content of the
TemperatureSensor.jsonfile
- On the Oracle IoT Cloud Service home page, click Action
,
select Devices and Model, and verify that
your device models are registered correctly.
Description of the illustration device_models.png
Run Your Application
- Change directories to
CL_HOME, edit thebuild.gradlefile and locate the following line:
Then replace it with this line:def OSGI_IMPORT_PACKAGE = "javax.crypto,javax.crypto.spec,javax.net.ssl,org.json,javax.sql,org.apache.derby.jdbc"def OSGI_IMPORT_PACKAGE = "javax.crypto,javax.crypto.spec,javax.net.ssl,org.json,javax.sql,org.apache.derby.jdbc,sun.net.www.protocol.https" - Open a command-line interface and change your directory to
CL_HOME/samples. - Run the following command:
gradle - In the
CL_HOME/samples/src/main/java/com/oracle/iot/sampledirectory, save theSampleActivator.javafile. - Change your directory to
CL_HOME/samples. -
Build the
SampleActivatorapplication.
Command for Linux / macOSjavac -d ./build/classes -cp Apache_Felix_path/bin/felix.jar:../lib/device-library.jar src/main/java/com/oracle/iot/sample/GatewayDeviceSample.java src/main/java/com/oracle/iot/sample/SampleActivator.java src/main/java/com/oracle/iot/sample/TemperatureSensor.java src/main/java/com/oracle/iot/sample/HumiditySensor.java
Command for Windowsjavac -d .\build\classes -cp Apache_Felix_path\bin\felix.jar;..\lib\device-library.jar src\main\java\com\oracle\iot\sample\GatewayDeviceSample.java src\main\java\com\oracle\iot\sample\SampleActivator.java src\main\java\com\oracle\iot\sample\TemperatureSensor.java src\main\java\com\oracle\iot\sample\HumiditySensor.java
This command adds the.classfiles in theCL_HOME/samples/build/classes/com/oracle/iot/sampledirectory. - In the
CL_HOME/samplesdirectory, create aMANIFEST.MFfile with the following content:Bundle-ManifestVersion: 2 Bundle-Name: GatewayDevice Sample Bundle-SymbolicName: com.oracle.iot.sample Bundle-Version: 1.0.0 Bundle-Activator: com.oracle.iot.sample.SampleActivator Import-Package: org.osgi.framework,oracle.iot.client.device,oracle.iot.client
Note: Make sure that there is a line break after the last line of theMANIFEST.MFfile. - Create the bundle
jarfile.
Command for Linux / macOSjar -cmf MANIFEST.MF osgi-sample.jar -C ./build/classes com
Command for Windowsjar -cmf MANIFEST.MF osgi-sample.jar -C .\build\classes com - Change your directory to
Apache_Felix_path. - Copy the provisioning file your downloaded from the Oracle IoT Cloud Service into that directory.
- Run the application using the Apache Felix framework:
Command for Linux / macOSjava -Dorg.osgi.framework.system.packages.extra=sun.net.www.protocol.https -Dsample.name=GatewayDeviceSample -Dsample.args="Activation_ID5 File_Protection_Password6" -jar bin/felix.jar
Command for Windowsjava -Dorg.osgi.framework.system.packages.extra=sun.net.www.protocol.https -Dsample.name=GatewayDeviceSample -Dsample.args="Activation_ID5 File_Protection_Password6" -jar .\bin\felix.jar - Install the device library bundle, and then start your bundle.
Welcome to Apache Felix Gogo g! install CL_HOME/lib/derby-10.11.1.1.jar Bundle ID: g! install CL_HOME/lib/device-library.jar Bundle ID: 5 g! install CL_HOME/lib/json-20160212.jar Bundle ID: 6 g! install CL_HOME/samples/osgi-sample.jar Bundle ID: 7 g! start 7 Starting to listen for service events. Creating the gateway instance... Created virtural temperature sensor 0-2UJQ Created virtural humidity sensor 0-2YJQ Press enter to exit. Tue Aug 02 17:18:18 EDT 2016 : 0-2YJQ : Set : "humidity"=81,"maxThreshold"=90 Tue Aug 02 17:18:17 EDT 2016 : 0-2UJQ : Set : "power"=true,"temp"=58.5,"unit"=°C,"minTemp"=58.5,"maxTemp"=58.5,"minThreshold"=0,"maxThreshold"=65
Note: The bundle ID values may vary.
Note: To stop the application, enter the stop command and the bundle ID of theosgi-sample.jarfile.
Command for Windows / Linux / macOSstop 7
Quick Start Tutorial Using the Apache Felix Framework