You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/04/12 19:56:23 UTC

[04/11] camel git commit: initial commit of camel-pubnub component

http://git-wip-us.apache.org/repos/asf/camel/blob/289a6728/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubSensor2Example.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubSensor2Example.java b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubSensor2Example.java
new file mode 100644
index 0000000..01d51fa
--- /dev/null
+++ b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubSensor2Example.java
@@ -0,0 +1,122 @@
+/**
+ * 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.camel.component.pubnub.example;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.pubnub.PubNubConstants;
+import org.apache.camel.main.Main;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public final class PubNubSensor2Example {
+
+    private PubNubSensor2Example() {
+    }
+
+    public static void main(String[] args) throws Exception {
+        Main main = new Main();
+        main.addRouteBuilder(new PubsubRoute());
+        main.addRouteBuilder(new SimulatedDeviceEventGeneratorRoute());
+        main.run();
+    }
+
+    static class SimulatedDeviceEventGeneratorRoute extends RouteBuilder {
+        private final String deviceEP = "pubnub://pubsub:iot?uuid=device2&publisherKey=" + PubNubExampleConstants.PUBNUB_PUBLISHER_KEY + "&subscriberKey="
+                                        + PubNubExampleConstants.PUBNUB_SUBSCRIBER_KEY;
+        private final String devicePrivateEP = "pubnub://pubsub:device2private?uuid=device2&publisherKey=" + PubNubExampleConstants.PUBNUB_PUBLISHER_KEY + "&subscriberKey="
+                                               + PubNubExampleConstants.PUBNUB_SUBSCRIBER_KEY;
+
+        @Override
+        public void configure() throws Exception {
+            //@formatter:off
+            from("timer:device2").routeId("device-event-route")
+                .bean(PubNubSensor2Example.EventGeneratorBean.class, "getRandomEvent('device2')")
+                .convertBodyTo(JSONObject.class)
+                .to(deviceEP);
+            
+            from(devicePrivateEP)
+                .routeId("device-unicast-route")
+                .log("Message from master to device2 : ${body}");
+            //@formatter:on
+        }
+    }
+
+    static class PubsubRoute extends RouteBuilder {
+        private static String masterEP = "pubnub://pubsub:iot?uuid=master&subscriberKey=" + PubNubExampleConstants.PUBNUB_SUBSCRIBER_KEY + "&publisherKey="
+                                         + PubNubExampleConstants.PUBNUB_PUBLISHER_KEY;
+        private static Map<String, String> devices = new ConcurrentHashMap<String, String>();
+
+        @Override
+        public void configure() throws Exception {
+            //@formatter:off
+            from(masterEP)
+                .routeId("master-route")
+                .convertBodyTo(JSONObject.class)
+                .bean(PubNubSensor2Example.PubsubRoute.DataProcessorBean.class, "doSomethingInteresting(${body})")
+                .log("${body} headers : ${headers}").to("mock:result");
+            
+            //TODO Could remote control device to turn on/off sensor measurement 
+            from("timer:master?delay=15s&period=5s").routeId("unicast2device-route")
+                .setHeader(PubNubConstants.CHANNEL, method(PubNubSensor2Example.PubsubRoute.DataProcessorBean.class, "getUnicastChannelOfDevice()"))
+                .setBody(constant("Hello device"))
+                .to(masterEP);
+            //@formatter:on
+        }
+
+        static class DataProcessorBean {
+            @EndpointInject(uri = "pubnub://pubsub:iot?uuid=master&subscriberKey=" + PubNubExampleConstants.PUBNUB_SUBSCRIBER_KEY)
+            private static ProducerTemplate template;
+
+            public static String getUnicastChannelOfDevice() {
+                // just get the first channel
+                return devices.values().iterator().next();
+            }
+
+            public static void doSomethingInteresting(JSONObject message) {
+                String deviceUUID;
+                try {
+                    deviceUUID = message.getString("uuid");
+                    if (devices.get(deviceUUID) == null) {
+                        Map<String, Object> headers = new HashMap<String, Object>();
+                        headers.put(PubNubConstants.OPERATION, "WHERE_NOW");
+                        headers.put(PubNubConstants.UUID, deviceUUID);
+                        JSONObject response = (JSONObject)template.requestBodyAndHeaders(null, headers);
+                        JSONArray listofDeviceChannels = response.getJSONArray("channels");
+                        devices.put(deviceUUID, listofDeviceChannels.getString(0));
+                    }
+                } catch (JSONException e) {
+                }
+            }
+        }
+    }
+
+    public static class EventGeneratorBean {
+        public static String getRandomEvent(String device) throws JSONException {
+            Random rand = new Random();
+            String s = "{uuid:" + device + ", humidity:" + rand.nextInt(100) + ", temperature:" + rand.nextInt(40) + "}";
+            return s;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/289a6728/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubSensorExample.java
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubSensorExample.java b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubSensorExample.java
new file mode 100644
index 0000000..0625453
--- /dev/null
+++ b/components/camel-pubnub/src/test/java/org/apache/camel/component/pubnub/example/PubNubSensorExample.java
@@ -0,0 +1,40 @@
+/**
+ * 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.camel.component.pubnub.example;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.main.Main;
+
+public final class PubNubSensorExample {
+
+    private PubNubSensorExample() {
+    }
+
+    public static void main(String[] args) throws Exception {
+        Main main = new Main();
+        main.addRouteBuilder(new SensorRoute());
+        main.run();
+    }
+
+    static class SensorRoute extends RouteBuilder {
+        @Override
+        public void configure() throws Exception {
+            from("pubnub://pubsub:pubnub-sensor-network?subscriberKey=sub-c-5f1b7c8e-fbee-11e3-aa40-02ee2ddab7fe").log("${body}").to("mock:result");
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/289a6728/components/camel-pubnub/src/test/resources/log4j2.properties
----------------------------------------------------------------------
diff --git a/components/camel-pubnub/src/test/resources/log4j2.properties b/components/camel-pubnub/src/test/resources/log4j2.properties
new file mode 100644
index 0000000..25b59c8
--- /dev/null
+++ b/components/camel-pubnub/src/test/resources/log4j2.properties
@@ -0,0 +1,28 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+appender.file.type = File
+appender.file.name = file
+appender.file.fileName = target/camel-pubnub-test.log
+appender.file.layout.type = PatternLayout
+appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+appender.out.type = Console
+appender.out.name = out
+appender.out.layout.type = PatternLayout
+appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+rootLogger.level = INFO
+rootLogger.appenderRef.file.ref = file

http://git-wip-us.apache.org/repos/asf/camel/blob/289a6728/components/pom.xml
----------------------------------------------------------------------
diff --git a/components/pom.xml b/components/pom.xml
index 429ee4e..49a423c 100644
--- a/components/pom.xml
+++ b/components/pom.xml
@@ -220,6 +220,7 @@
     <module>camel-pgevent</module>
     <module>camel-printer</module>
     <module>camel-protobuf</module>
+    <module>camel-pubnub</module>
     <module>camel-quartz</module>
     <module>camel-quartz2</module>
     <module>camel-quickfix</module>

http://git-wip-us.apache.org/repos/asf/camel/blob/289a6728/parent/pom.xml
----------------------------------------------------------------------
diff --git a/parent/pom.xml b/parent/pom.xml
index 65387c8..546f7ce 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -517,6 +517,7 @@
     <pgjdbc-ng-driver-version>0.7.1</pgjdbc-ng-driver-version>
     <powermock-version>1.6.6</powermock-version>
     <protobuf-version>3.1.0</protobuf-version>
+    <pubnub-version>3.7.4</pubnub-version>
     <qpid-bundle-version>0.28_1</qpid-bundle-version>
     <qpid-proton-j-version>0.16.0</qpid-proton-j-version>
     <qpid-proton-j-bundle-version>0.14.0</qpid-proton-j-bundle-version>