Repository: incubator-edgent Updated Branches: refs/heads/master c23d628fd -> 3699553fd
[Edgent-376] Support use in WIoTP Managed Device context - add IotpDevice(DeviceClient) ctor - add a quickstart sample that uses it Project: http://git-wip-us.apache.org/repos/asf/incubator-edgent/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-edgent/commit/dbc094e4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-edgent/tree/dbc094e4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-edgent/diff/dbc094e4 Branch: refs/heads/master Commit: dbc094e4064435638b1f64c436e8035fa857deb3 Parents: c23d628 Author: Dale LaBossiere <[email protected]> Authored: Tue Feb 7 15:07:24 2017 -0500 Committer: Dale LaBossiere <[email protected]> Committed: Tue Feb 7 15:55:25 2017 -0500 ---------------------------------------------------------------------- connectors/.classpath | 10 +- .../edgent/connectors/iotp/IotpDevice.java | 44 ++++++-- .../connectors/iotp/runtime/IotpConnector.java | 9 +- .../connectors/iotp/IotpQuickstart2.java | 107 +++++++++++++++++++ scripts/connectors/iotp/runiotpquickstart2.sh | 40 +++++++ 5 files changed, 196 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/dbc094e4/connectors/.classpath ---------------------------------------------------------------------- diff --git a/connectors/.classpath b/connectors/.classpath index fa2b4e2..f166304 100644 --- a/connectors/.classpath +++ b/connectors/.classpath @@ -27,12 +27,12 @@ <classpathentry kind="src" path="wsclient/src/main/java"/> <classpathentry kind="src" path="wsclient-javax.websocket/src/main/java"/> <classpathentry kind="src" path="wsclient-javax.websocket/src/test/java"/> - <classpathentry kind="src" path="javax.websocket-client/src/main/resources"/> + <classpathentry kind="src" path="javax.websocket-client/src/main/resources"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/http/ext/commons-codec-1.9.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/http/ext/commons-logging-1.2.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/http/ext/httpclient-4.5.1.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/http/ext/httpcore-4.4.4.jar"/> - <classpathentry kind="lib" path="../externalJars/java8/connectors/iotp/ext/watson-iot-0.1.5.jar"/> + <classpathentry exported="true" kind="lib" path="../externalJars/java8/connectors/iotp/ext/watson-iot-0.1.5.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/iotp/ext/commons-lang3-3.4.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/iotp/ext/commons-codec-1.10.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/iotp/ext/commons-logging-1.2.jar"/> @@ -40,7 +40,7 @@ <classpathentry kind="lib" path="../externalJars/java8/connectors/iotp/ext/httpclient-4.5.1.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/iotp/ext/httpcore-4.4.3.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/iotp/ext/joda-time-2.9.2.jar"/> - <classpathentry kind="lib" path="../externalJars/java8/connectors/iotp/ext/org.eclipse.paho.client.mqttv3-1.1.0.jar"/> + <classpathentry exported="true" kind="lib" path="../externalJars/java8/connectors/iotp/ext/org.eclipse.paho.client.mqttv3-1.1.0.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/kafka/ext/kafka_2.10-0.8.2.2.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/kafka/ext/kafka-clients-0.8.2.2.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/kafka/ext/log4j-1.2.16.jar"/> @@ -55,13 +55,13 @@ <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-client/ext/websocket-api-9.3.6.v20151106.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-client/ext/websocket-client-9.3.6.v20151106.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-client/ext/websocket-common-9.3.6.v20151106.jar"/> - <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/javax-websocket-server-impl-9.3.6.v20151106.jar"/> + <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/javax-websocket-server-impl-9.3.6.v20151106.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/jetty-http-9.3.6.v20151106.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/jetty-io-9.3.6.v20151106.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/jetty-security-9.3.6.v20151106.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/jetty-server-9.3.6.v20151106.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/jetty-servlet-9.3.6.v20151106.jar"/> - <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/javax.servlet-api-3.1.0.jar"/> + <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/javax.servlet-api-3.1.0.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/websocket-server-9.3.6.v20151106.jar"/> <classpathentry kind="lib" path="../externalJars/java8/connectors/javax.websocket-server/ext/websocket-servlet-9.3.6.v20151106.jar"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/dbc094e4/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/IotpDevice.java ---------------------------------------------------------------------- diff --git a/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/IotpDevice.java b/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/IotpDevice.java index ad26d7d..e0a4127 100644 --- a/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/IotpDevice.java +++ b/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/IotpDevice.java @@ -40,9 +40,10 @@ import org.apache.edgent.topology.Topology; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.ibm.iotf.client.device.Command; +import com.ibm.iotf.client.device.DeviceClient; /** - * Connector for IBM Watson IoT Platform. + * A Device Connector to IBM Watson IoT Platform (WIoTP). * <BR> * IBM Watson IoT Platform is a cloud based internet of things * scale message hub that provides a device model on top of MQTT. @@ -52,9 +53,26 @@ import com.ibm.iotf.client.device.Command; * <BR> * <em>Note IBM Watson IoT Platform was previously known as * IBM Internet of Things Foundation.</em> + * <p> + * This connector is a thin wrapper over the WIoTP {@code DeviceClient} Java API. + * The constructor {@code Properties} and {@code optionsFile} contents are those + * demanded by {@code DeviceClient}. + * <p> + * For more complex use cases this connector can be constructed to use + * a supplied {@code DeviceClient}. This gives an application complete + * control over the construction and configuration of the underlying + * connection to WIoTP. + * <p> + * An application that wants to be a WIoTP <i>managed device</i> + * and an Edgent application must construct a WIoTP {@code ManagedDevice} instance + * and construct this device connector using it. + * <P> + * If the following link's page doesn't render in javadoc, paste it into a browser window. + * <BR> + * See the IBM Watson IoT Platform documentation for Developing devices at + * <a href="https://internetofthings.ibmcloud.com/">https://internetofthings.ibmcloud.com/</a> * * @see <a href="{@docRoot}/org/apache/edgent/connectors/iot/package-summary.html">Edgent generic device model</a> - * @see <a href="http://www.ibm.com/internet-of-things/iot-platform.html">IBM Watson IoT Platform</a> * @see org.apache.edgent.samples.connectors.iotp.IotpSensors Sample application */ public class IotpDevice implements IotDevice { @@ -70,8 +88,7 @@ public class IotpDevice implements IotDevice { private TStream<Command> commandStream; /** - * Create a connector to the IBM Watson IoT Platform Bluemix service with the device - * specified by {@code options}. + * Create a connector for the IoT device specified by {@code options}. * <BR> * These properties must be set in {@code options}. * @@ -95,7 +112,6 @@ public class IotpDevice implements IotDevice { * IotDevice iotDevice = new IotpDevice(options); * </code> * </pre> - * <p> * Connecting to the server occurs when the topology is submitted for * execution. @@ -104,6 +120,8 @@ public class IotpDevice implements IotDevice { * @param options control options * @param topology * the connector's associated {@code Topology}. + * + * @see the IBM Watson IoT Platform documentation for additional properties. */ public IotpDevice(Topology topology, Properties options) { this.topology = topology; @@ -111,9 +129,7 @@ public class IotpDevice implements IotDevice { } /** - * Create a connector to the IBM Watson IoT Platform Bluemix service. - * Device identifier and authorization are specified - * by a configuration file. + * Create a connector for the IoT device specified by {@code optionsFile}. * <BR> * The format of the file is: * <pre> @@ -143,6 +159,8 @@ public class IotpDevice implements IotDevice { * </p> * @param topology the connector's associated {@code Topology}. * @param optionsFile File containing connection information. + * + * @see the IBM Watson IoT Platform documentation for additional properties. */ public IotpDevice(Topology topology, File optionsFile) { this.topology = topology; @@ -150,6 +168,16 @@ public class IotpDevice implements IotDevice { } /** + * Create a connector using the supplied WIoTP {@code DeviceClient}. + * @param topology the connector's associated {@code Topology}. + * @param deviceClient a WIoTP device client API object. + */ + public IotpDevice(Topology topology, DeviceClient deviceClient) { + this.topology = topology; + this.connector = new IotpConnector(deviceClient); + } + + /** * Create an {@code IotpDevice} connector to the Quickstart service. * Quickstart service requires no-sign up to use to allow evaluation * but has limitations on functionality, such as only supporting http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/dbc094e4/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/runtime/IotpConnector.java ---------------------------------------------------------------------- diff --git a/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/runtime/IotpConnector.java b/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/runtime/IotpConnector.java index d2fb227..af16e61 100644 --- a/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/runtime/IotpConnector.java +++ b/connectors/iotp/src/main/java/org/apache/edgent/connectors/iotp/runtime/IotpConnector.java @@ -38,6 +38,7 @@ public class IotpConnector implements Serializable, AutoCloseable { private Properties options; private File optionsFile; private transient DeviceClient client; + private boolean disconnectOnClose = true; /** * Create a new connector to the specified MQTT server. @@ -52,6 +53,11 @@ public class IotpConnector implements Serializable, AutoCloseable { this.optionsFile = optionsFile; } + public IotpConnector(DeviceClient iotpDeviceClient) { + this.client = iotpDeviceClient; + this.disconnectOnClose = false; + } + synchronized DeviceClient connect() { DeviceClient client; try { @@ -100,7 +106,8 @@ public class IotpConnector implements Serializable, AutoCloseable { if (client == null) return; - client.disconnect(); + if (disconnectOnClose) + client.disconnect(); client = null; } } http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/dbc094e4/samples/connectors/src/main/java/org/apache/edgent/samples/connectors/iotp/IotpQuickstart2.java ---------------------------------------------------------------------- diff --git a/samples/connectors/src/main/java/org/apache/edgent/samples/connectors/iotp/IotpQuickstart2.java b/samples/connectors/src/main/java/org/apache/edgent/samples/connectors/iotp/IotpQuickstart2.java new file mode 100644 index 0000000..eff81c2 --- /dev/null +++ b/samples/connectors/src/main/java/org/apache/edgent/samples/connectors/iotp/IotpQuickstart2.java @@ -0,0 +1,107 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +*/ +package org.apache.edgent.samples.connectors.iotp; + +import java.util.Properties; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +import org.apache.edgent.connectors.iot.IotDevice; +import org.apache.edgent.connectors.iot.QoS; +import org.apache.edgent.connectors.iotp.IotpDevice; +import org.apache.edgent.providers.direct.DirectProvider; +import org.apache.edgent.topology.TStream; +import org.apache.edgent.topology.Topology; + +import com.google.gson.JsonObject; +import com.ibm.iotf.client.device.DeviceClient; +import com.ibm.iotf.devicemgmt.DeviceData; +import com.ibm.iotf.devicemgmt.device.ManagedDevice; + +/** + * IBM Watson IoT Platform Quickstart sample. + * Submits a JSON device event every second using the + * same format as the Quickstart device simulator, + * with keys {@code temp}, {@code humidity} and {@code objectTemp} + * and random values. + * <P> + * The device type is {@code iotsamples-edgent} and a random + * device identifier is generated. Both are printed out when + * the application starts. + * <P> + * A URL is also printed that allows viewing of the data + * as it received by the Quickstart service. + * <P> + * This sample demonstrates using the WIoTP API to initialize the IotpDevice + * connector. + */ +public class IotpQuickstart2 { + + public static void main(String[] args) throws Exception { + boolean useDeviceClient = args.length > 0 && args[0].equals("useDeviceClient"); + + DirectProvider tp = new DirectProvider(); + Topology topology = tp.newTopology("IotpQuickstart"); + + // Declare a connector to IoTP Quickstart service, initializing with WIoTP API + String deviceId = "qs" + Long.toHexString(new Random().nextLong()); + Properties options = new Properties(); + options.setProperty("org", "quickstart"); + options.setProperty("type", IotpDevice.QUICKSTART_DEVICE_TYPE); + options.setProperty("id", deviceId); + IotDevice device; + if (useDeviceClient) { + System.out.println("Using WIoTP DeviceClient"); + device = new IotpDevice(topology, new DeviceClient(options)); + } + else { + System.out.println("Using WIoTP ManagedDevice"); + DeviceData deviceData = new DeviceData.Builder().build(); + device = new IotpDevice(topology, new ManagedDevice(options, deviceData)); + } + + System.out.println("Quickstart device type:" + IotpDevice.QUICKSTART_DEVICE_TYPE); + System.out.println("Quickstart device id :" + deviceId); + System.out.println("https://quickstart.internetofthings.ibmcloud.com/#/device/" + + deviceId); + + Random r = new Random(); + TStream<double[]> raw = topology.poll(() -> { + double[] v = new double[3]; + + v[0] = r.nextGaussian() * 10.0 + 40.0; + v[1] = r.nextGaussian() * 10.0 + 50.0; + v[2] = r.nextGaussian() * 10.0 + 60.0; + + return v; + }, 1, TimeUnit.SECONDS); + + TStream<JsonObject> json = raw.map(v -> { + JsonObject j = new JsonObject(); + j.addProperty("temp", v[0]); + j.addProperty("humidity", v[1]); + j.addProperty("objectTemp", v[2]); + return j; + }); + + device.events(json, "sensors", QoS.FIRE_AND_FORGET); + + tp.submit(topology); + } + } http://git-wip-us.apache.org/repos/asf/incubator-edgent/blob/dbc094e4/scripts/connectors/iotp/runiotpquickstart2.sh ---------------------------------------------------------------------- diff --git a/scripts/connectors/iotp/runiotpquickstart2.sh b/scripts/connectors/iotp/runiotpquickstart2.sh new file mode 100755 index 0000000..74a3f75 --- /dev/null +++ b/scripts/connectors/iotp/runiotpquickstart2.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# + +edgent=../../.. + +# Runs IBM Watson IoT Plaform Quickstart sample. +# +# runiotpquickstart2.sh +# +# This connects to the Quickstart IBM Watson IoT Platform service +# which requires no registration at all. +# +# The application prints out a URL which allows a browser +# to see the data being sent from this sample to +# IBM Watson IoT Plaform Quickstart sample. + +# Avoid a paho mqtt class security exception. +# The connector samples jar has dependencies on both the iotp and mqtt connectors +# and those have dependencies on different versions of the paho mqtt jar. +# Ensure the right one is used for this sample. +#export CLASSPATH=${edgent}/samples/lib/edgent.samples.connectors.jar +IOTP_MQTT_JAR=`ls ${edgent}/connectors/iotp/ext/org.eclipse.paho.client.mqtt*.jar` +export CLASSPATH=${IOTP_MQTT_JAR}:${edgent}/samples/lib/edgent.samples.connectors.jar + +java org.apache.edgent.samples.connectors.iotp.IotpQuickstart2 $*
