forked from community/device-mgt-plugins
parent
a8371e046e
commit
bc08e31c4c
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue