mirror of
https://repository.entgra.net/community/device-mgt-plugins.git
synced 2025-09-16 23:42:15 +00:00
Android Sense iBeacon
This commit is contained in:
parent
a8371e046e
commit
bc08e31c4c
@ -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'
|
||||
|
||||
}
|
||||
|
||||
@ -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<Beacon> arrayL = new ArrayList<>();
|
||||
private LayoutInflater inflater;
|
||||
|
||||
@ -62,48 +63,12 @@ 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<Beacon> 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();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onBeaconServiceConnect() {
|
||||
|
||||
iBeaconManager.setBackgroundMode(true);
|
||||
|
||||
|
||||
iBeaconManager.setRangeNotifier(new RangeNotifier() {
|
||||
@Override
|
||||
public void didRangeBeaconsInRegion(Collection<Beacon> iBeacons, Region region) {
|
||||
@ -113,9 +78,9 @@ public class MonitoringActivity extends Activity implements BeaconConsumer {
|
||||
|
||||
}
|
||||
|
||||
// arrayL.clear();
|
||||
// arrayL.addAll((ArrayList<Beacon>) 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<Beacon> 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
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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<BLE> 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<BLE> read()
|
||||
{
|
||||
List<BLE> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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<BLE> bleData;
|
||||
|
||||
public static List<BLE> getBleData() {
|
||||
return bleData;
|
||||
}
|
||||
|
||||
public static void setBleData(List<BLE> 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();
|
||||
}
|
||||
|
||||
}
|
||||
@ -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<BLE> bleData = new ArrayList<BLE>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -51,4 +51,70 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>wso2-nexus</id>
|
||||
<name>WSO2 internal Repository</name>
|
||||
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>daily</updatePolicy>
|
||||
<checksumPolicy>ignore</checksumPolicy>
|
||||
</releases>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>wso2.releases</id>
|
||||
<name>WSO2 internal Repository</name>
|
||||
<url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>daily</updatePolicy>
|
||||
<checksumPolicy>ignore</checksumPolicy>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<!--Dependency on Sidhdhi - required for PolicyMgt-->
|
||||
<dependency>
|
||||
<groupId>org.wso2.siddhi</groupId>
|
||||
<artifactId>siddhi-query-compiler</artifactId>
|
||||
<version>${sidhdhi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.siddhi</groupId>
|
||||
<artifactId>siddhi-query-api</artifactId>
|
||||
<version>${sidhdhi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.siddhi</groupId>
|
||||
<artifactId>siddhi-core</artifactId>
|
||||
<version>${sidhdhi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${apache-httpclient.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- Java Version Compatibility -->
|
||||
<wso2.maven.compiler.source>1.7</wso2.maven.compiler.source>
|
||||
<wso2.maven.compiler.target>1.7</wso2.maven.compiler.target>
|
||||
<!--Apache HTTP Components-->
|
||||
<apache-httpclient.version>4.5</apache-httpclient.version>
|
||||
<!-- Sidhdhi Version -->
|
||||
<sidhdhi.version>3.0.5</sidhdhi.version>
|
||||
<gson.version>2.3.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Loading…
Reference in New Issue
Block a user