From bc08e31c4cd04defa70ec1b99f0ce7189b76a377 Mon Sep 17 00:00:00 2001 From: Waruna Samarsinghe Date: Wed, 29 Jun 2016 17:50:22 +0530 Subject: [PATCH] Android Sense iBeacon --- .../app/build.gradle | 6 + .../sense/beacon/MonitoringActivity.java | 96 +++++++------ .../iot/android/sense/siddhi/dto/BLE.java | 55 +++++++ .../core/SidhdhiQueryExecutor.java | 136 ++++++++++++++++++ .../wrapper/SidhdhiWrapper.java | 59 ++++++++ .../sense/siddhi/reader/BLEReader.java | 71 +++++++++ .../pom.xml | 66 +++++++++ 7 files changed, 447 insertions(+), 42 deletions(-) create mode 100755 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/dto/BLE.java create mode 100755 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/eventprocessor/core/SidhdhiQueryExecutor.java create mode 100755 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/eventprocessor/wrapper/SidhdhiWrapper.java create mode 100755 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/reader/BLEReader.java diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/build.gradle b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/build.gradle index dc2ec083c4..3c6c7507fe 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/build.gradle +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/build.gradle @@ -29,6 +29,11 @@ android { packagingOptions { exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' + exclude 'META-INF/DEPENDENCIES' + exclude 'META-INF/NOTICE' + exclude 'META-INF/LICENSE' + exclude 'META-INF/LICENSE.txt' + exclude 'META-INF/NOTICE.txt' } productFlavors { } @@ -48,4 +53,5 @@ dependencies { compile 'com.netflix.feign:feign-jaxrs:8.16.0' compile 'com.netflix.feign:feign-jackson:8.16.0' compile 'org.altbeacon:android-beacon-library:2.8.1' + } diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/MonitoringActivity.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/MonitoringActivity.java index b95512f4a1..5b24c38587 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/MonitoringActivity.java +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/beacon/MonitoringActivity.java @@ -47,13 +47,14 @@ import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; +import java.util.logging.Handler; import agent.sense.android.iot.carbon.wso2.org.wso2_senseagent.R; public class MonitoringActivity extends Activity implements BeaconConsumer { protected static final String TAG = MonitoringActivity.class.getName(); private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1; private ListView list = null; - private BeaconAdapter adapter = null; + private BeaconAdapter adapter; private ArrayList arrayL = new ArrayList<>(); private LayoutInflater inflater; @@ -63,46 +64,10 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { BeaconScanedData beaconData; @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_monitor); - beaconUtill = new BeaconServiceUtility(this); - list = (ListView) findViewById(R.id.list); - adapter = new BeaconAdapter(); - list.setAdapter(adapter); - inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); - - iBeaconManager.setRangeNotifier(new RangeNotifier() { - @Override - public void didRangeBeaconsInRegion(Collection beacons, Region region) { - for(Beacon beacon : beacons) { - Log.d(TAG, "UUID:" + beacon.getId1() + ", major:" + beacon.getId2() + ", minor:" + beacon.getId3() + ", Distance:" + beacon.getDistance() + ",RSSI" + beacon.getRssi() + ", TxPower" + beacon.getTxPower()); - } - } - }); - - } - - @Override - protected void onDestroy() { - super.onDestroy(); - } - - @Override - protected void onStart() { - super.onStart(); - beaconUtill.onStart(iBeaconManager, this); - } - - @Override - protected void onStop() { - beaconUtill.onStop(iBeaconManager, this); - super.onStop(); - } + public void onBeaconServiceConnect() { + iBeaconManager.setBackgroundMode(true); - @Override - public void onBeaconServiceConnect() { iBeaconManager.setRangeNotifier(new RangeNotifier() { @Override @@ -113,9 +78,9 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { } -// arrayL.clear(); -// arrayL.addAll((ArrayList) iBeacons); -// adapter.notifyDataSetChanged(); + arrayL.clear(); + arrayL.addAll(iBeacons); + //adapter.notifyDataSetChanged(); } }); @@ -154,6 +119,53 @@ public class MonitoringActivity extends Activity implements BeaconConsumer { } } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_monitor); + beaconUtill = new BeaconServiceUtility(this); + list = (ListView) findViewById(R.id.list); + adapter = new BeaconAdapter(); + list.setAdapter(adapter); + inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + + //iBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25")); + iBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); + + iBeaconManager.bind(this); + + iBeaconManager.setRangeNotifier(new RangeNotifier() { + @Override + public void didRangeBeaconsInRegion(Collection beacons, Region region) { + for(Beacon beacon : beacons) { + Log.d(TAG, "UUID:" + beacon.getId1() + ", major:" + beacon.getId2() + ", minor:" + beacon.getId3() + ", Distance:" + beacon.getDistance() + ",RSSI" + beacon.getRssi() + ", TxPower" + beacon.getTxPower()); + } + arrayL.clear(); + arrayL.addAll(beacons); + } + }); + + } + + @Override + protected void onDestroy() { + super.onDestroy(); + } + + @Override + protected void onStart() { + super.onStart(); + beaconUtill.onStart(iBeaconManager, this); + beaconUtill = new BeaconServiceUtility(this); + } + + @Override + protected void onStop() { + beaconUtill.onStop(iBeaconManager, this); + super.onStop(); + } + + private class BeaconAdapter extends BaseAdapter { @Override diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/dto/BLE.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/dto/BLE.java new file mode 100755 index 0000000000..0dec7d2ad7 --- /dev/null +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/dto/BLE.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.wso2.carbon.iot.android.sense.siddhi.dto; + +public class BLE { + int id; + long timeStamp; + String location; + + public BLE(int id, String location){ + this.id = id; + this.location = location; + timeStamp = System.currentTimeMillis(); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public long getTimeStamp() { + return timeStamp; + } + + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } +} diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/eventprocessor/core/SidhdhiQueryExecutor.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/eventprocessor/core/SidhdhiQueryExecutor.java new file mode 100755 index 0000000000..6496a40bc6 --- /dev/null +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/eventprocessor/core/SidhdhiQueryExecutor.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.wso2.carbon.iot.android.sense.siddhi.eventprocessor.core; + +import org.wso2.carbon.iot.android.sense.siddhi.dto.BLE; +import org.wso2.carbon.iot.android.sense.siddhi.eventprocessor.wrapper.SidhdhiWrapper; +import org.wso2.siddhi.core.SiddhiManager; +import org.wso2.siddhi.core.event.Event; +import org.wso2.siddhi.core.stream.input.InputHandler; +import org.wso2.siddhi.core.ExecutionPlanRuntime; +import org.wso2.siddhi.core.stream.output.StreamCallback; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + + +public class SidhdhiQueryExecutor implements Runnable { + + protected String query = null; + private static SiddhiManager siddhiManager = new SiddhiManager(); + private static final int INTERVAL = 3000; + + public volatile Thread sidhdiThread; + private ReadWriteLock rwlock = new ReentrantReadWriteLock(); + + //TODO have another array initialized so that a list of callbacks can be stored, String[] callbackList + public SidhdhiQueryExecutor(String query){ + this.query = query; + } + + public void run(){ + //TODO the array of callbacks needs to be passed to invoke + //TODO what is retruned should be a map of callbacks and outputs + ExecutionPlan executionPlan = new ExecutionPlan().invoke(); + + Thread thisThread = Thread.currentThread(); + + while (sidhdiThread == thisThread) { + InputHandler inputHandler = executionPlan.getInputHandler(); + + //Sending events to Siddhi + try { + List bleReadings = read(); + for(BLE ble : bleReadings){ + System.out.println("Publishing data..."); + inputHandler.send(new Object[]{ble.getId(), ble.getTimeStamp(), ble.getLocation()}); + } + + thisThread.sleep(INTERVAL); + } catch (InterruptedException e) { + e.printStackTrace(); + break; + } + } + } + + public List read() + { + List bleData; + rwlock.readLock().lock(); + try { + //TODO Reading BLE VALUES + bleData = SidhdhiWrapper.getBleData(); + } finally { + rwlock.readLock().unlock(); + } + return bleData; + } + + public void stop(){ + sidhdiThread = null; + } + + public void start(){ + sidhdiThread = new Thread(this); + sidhdiThread.start(); + } + + private class ExecutionPlan { + + private InputHandler inputHandler; + + public InputHandler getInputHandler() { + return inputHandler; + } + + //TODO should expect an array of callbacks + public ExecutionPlan invoke() { + + //Generating runtime + ExecutionPlanRuntime runtime = siddhiManager.createExecutionPlanRuntime(query); + + + + //TODO logic needs to be revised so that array of callbacks are processed + runtime.addCallback("dataOut", new StreamCallback() { + @Override + public void receive(Event[] events) { + System.out.println("Location Match event initiated."); + if (events.length > 0) { + //TODO Configure Event here! + System.out.println("Firing location match event..."); + } + } + }); + + + //Retrieving InputHandler to push events into Siddhi + inputHandler = runtime.getInputHandler("dataIn"); + + //Starting event processing + runtime.start(); + System.out.println("Execution Plan Started!"); + return this; + } + } + +} diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/eventprocessor/wrapper/SidhdhiWrapper.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/eventprocessor/wrapper/SidhdhiWrapper.java new file mode 100755 index 0000000000..e6db9c6a9e --- /dev/null +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/eventprocessor/wrapper/SidhdhiWrapper.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.wso2.carbon.iot.android.sense.siddhi.eventprocessor.wrapper; + +import org.wso2.carbon.iot.android.sense.siddhi.dto.BLE; +import org.wso2.carbon.iot.android.sense.siddhi.eventprocessor.core.SidhdhiQueryExecutor; +import org.wso2.carbon.iot.android.sense.siddhi.reader.BLEReader; + +import java.util.List; +import java.util.Map; + + +public class SidhdhiWrapper { + + private static List bleData; + + public static List getBleData() { + return bleData; + } + + public static void setBleData(List bleData) { + SidhdhiWrapper.bleData = bleData; + } + + public static void main(String args[]){ + String query = "@Import('iot.sample.input:1.0.0')\n" + + "define stream dataIn (id int, timestamp long, location string);\n" + + "\n" + + "@Export('iot.sample.output:1.0.0')\n" + + "define stream dataOut (action string, timestamp long);\n" + + "\n" + + "from every e1=dataIn[location=='loc_1'] -> e2=dataIn[location=='loc_2'] -> e3=dataIn[location=='loc_3']\n" + + "select 'x' as action, e3.timestamp\n" + + "insert into dataOut;"; + + BLEReader blEReader = new BLEReader(); + blEReader.start(); + + SidhdhiQueryExecutor queryExecutor = new SidhdhiQueryExecutor(query); + queryExecutor.start(); + } + +} diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/reader/BLEReader.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/reader/BLEReader.java new file mode 100755 index 0000000000..36fb195f8a --- /dev/null +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/siddhi/reader/BLEReader.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.wso2.carbon.iot.android.sense.siddhi.reader; + +import org.wso2.carbon.iot.android.sense.siddhi.dto.BLE; +import org.wso2.carbon.iot.android.sense.siddhi.eventprocessor.wrapper.SidhdhiWrapper; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +public class BLEReader implements Runnable{ + + private ReadWriteLock rwlock = new ReentrantReadWriteLock(); + List bleData = new ArrayList(); + public volatile Thread bleReader; + private static final int INTERVAL = 2000; + + public void run(){ + Thread thisThread = Thread.currentThread(); + while (bleReader == thisThread) { + write(); + try { + thisThread.sleep(INTERVAL); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public void stop(){ + bleReader = null; + } + + public void start(){ + bleReader = new Thread(this); + bleReader.start(); + } + + public void write() + { + rwlock.writeLock().lock(); + try { + bleData.add(new BLE(123, "loc_1")); + bleData.add(new BLE(123, "loc_2")); + bleData.add(new BLE(123, "loc_3")); + SidhdhiWrapper.setBleData(bleData); + } finally { + rwlock.writeLock().unlock(); + } + } +} diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/pom.xml b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/pom.xml index 7476a92b0c..66ce992b6d 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/pom.xml +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/pom.xml @@ -51,4 +51,70 @@ + + + + wso2-nexus + WSO2 internal Repository + http://maven.wso2.org/nexus/content/groups/wso2-public/ + + true + daily + ignore + + + + wso2.releases + WSO2 internal Repository + http://maven.wso2.org/nexus/content/repositories/releases/ + + true + daily + ignore + + + + + + + + org.wso2.siddhi + siddhi-query-compiler + ${sidhdhi.version} + + + org.wso2.siddhi + siddhi-query-api + ${sidhdhi.version} + + + org.wso2.siddhi + siddhi-core + ${sidhdhi.version} + + + + org.apache.httpcomponents + httpclient + ${apache-httpclient.version} + + + + com.google.code.gson + gson + ${gson.version} + + + + + + 1.7 + 1.7 + + 4.5 + + 3.0.5 + 2.3.1 + + \ No newline at end of file