From d44170e52eb59c5e3bfc9e2167826f87b6f385b9 Mon Sep 17 00:00:00 2001 From: Waruna Samarsinghe Date: Sun, 5 Jun 2016 22:58:38 +0530 Subject: [PATCH] rapid acceleration and turns- android sense --- .../data/publisher/DataPublisherService.java | 15 +- .../android/sense/data/publisher/Event.java | 38 ++++ .../iot/android/sense/event/SenseService.java | 2 + .../streams/Location/LocationDataReader.java | 61 ++++++ .../event/streams/SenseDataCollector.java | 6 +- .../sense/event/streams/Speed/SpeedData.java | 56 ++++++ .../event/streams/Speed/SpeedDataReader.java | 183 ++++++++++++++++++ .../android/sense/util/SenseDataHolder.java | 15 ++ 8 files changed, 374 insertions(+), 2 deletions(-) create mode 100644 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Speed/SpeedData.java create mode 100644 components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Speed/SpeedDataReader.java 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/data/publisher/DataPublisherService.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/data/publisher/DataPublisherService.java index 78a47fe947..c1c4a93b9d 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/DataPublisherService.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/data/publisher/DataPublisherService.java @@ -30,7 +30,7 @@ import org.wso2.carbon.iot.android.sense.data.publisher.mqtt.transport.Transport import org.wso2.carbon.iot.android.sense.constants.SenseConstants; import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationData; import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationDataReader; - +import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedData; import org.wso2.carbon.iot.android.sense.event.streams.Sensor.SensorData; import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData; import org.wso2.carbon.iot.android.sense.speech.detector.util.ProcessWords; @@ -134,6 +134,19 @@ public class DataPublisherService extends Service { } SenseDataHolder.resetLocationDataHolder(); + //retrieve speed data. + List speedDataMap = SenseDataHolder.getSpeedDataHolder(); + if (!speedDataMap.isEmpty()) { + for (SpeedData speedData : speedDataMap) { + Event event = new Event(); + event.setTimestamp(speedData.getTimeStamp()); + event.setSpeed(speedData.getSpeed()); + event.setTurns(speedData.getTurns()); + events.add(event); + } + } + SenseDataHolder.resetSpeedDataHolder(); + //retrieve words ProcessWords.cleanAndPushToWordMap(); List wordDatMap = SenseDataHolder.getWordDataHolder(); 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/data/publisher/Event.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/data/publisher/Event.java index e086bc6ef4..d5ec057ac3 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/data/publisher/Event.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/data/publisher/Event.java @@ -25,6 +25,9 @@ public class Event { private String word; private String wordStatus; private long timestamp; + private static float speed; + private String turn; + public static final float SPEED_LIMIT = 60; private int getBattery() { return battery; @@ -165,6 +168,32 @@ public class Event { this.wordStatus = wordStatus; } + public void setSpeed(float speed) { + this.type = "speed"; + this.speed = speed; + } + + public float getSpeed() { + + this.type = "speed"; + + return speed; + } + + public void setTurns(String turn) { + + this.type = "turn"; + this.turn = turn; + } + + public String getTurns() { + + if (turn == null || turn.isEmpty() || turn.equals("null")){ + turn = "No Turns"; + } + return turn; + } + public JSONObject getEvent() throws JSONException { JSONObject jsonEvent = new JSONObject(); JSONObject jsonMetaData = new JSONObject(); @@ -185,6 +214,15 @@ public class Event { jsonPayloadData.put("accelerometer_x", events[0]); jsonPayloadData.put("accelerometer_y", events[1]); jsonPayloadData.put("accelerometer_z", events[2]); + + //speed + + //if (getSpeed()>SPEED_LIMIT) { + jsonPayloadData.put("speed_limit", getSpeed()); + //} + + //turn + jsonPayloadData.put("turn_way", getTurns()); //magnetic events = getMagnetic(); jsonPayloadData.put("magnetic_x", events[0]); 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/event/SenseService.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/event/SenseService.java index 69285234f7..0aac980762 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/SenseService.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/event/SenseService.java @@ -49,6 +49,8 @@ public class SenseService extends Service { SenseDataCollector Sensor = new SenseDataCollector(this, SenseDataCollector.DataType.SENSOR); SenseDataCollector Location = new SenseDataCollector(this, SenseDataCollector.DataType.LOCATION); registerReceiver(new BatteryDataReceiver(), new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + SenseDataCollector speed = new SenseDataCollector(this, SenseDataCollector.DataType.SPEED); + //service will not be stopped until we manually stop the service return Service.START_NOT_STICKY; 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/event/streams/Location/LocationDataReader.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/event/streams/Location/LocationDataReader.java index 88d8627ae4..97eb6a9188 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/Location/LocationDataReader.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/event/streams/Location/LocationDataReader.java @@ -14,13 +14,19 @@ package org.wso2.carbon.iot.android.sense.event.streams.Location; import android.content.Context; +import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.util.Log; +import android.widget.Toast; + import org.wso2.carbon.iot.android.sense.event.streams.DataReader; import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; + +import java.util.Calendar; import java.util.concurrent.TimeUnit; /** @@ -32,6 +38,9 @@ public class LocationDataReader extends DataReader implements LocationListener { LocationData gps; + static final Double EARTH_RADIUS = 6371.00; + + // flag for GPS status private boolean isGPSEnabled = false; @@ -47,6 +56,12 @@ public class LocationDataReader extends DataReader implements LocationListener { double latitude; // latitude double longitude; // longitude + double lat_old=0.0; + double lon_old=0.0; + double time; + float speed = 0.0f; + private long lastUpdate; + // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters @@ -150,6 +165,39 @@ public class LocationDataReader extends DataReader implements LocationListener { @Override public void onLocationChanged(Location arg0) { // TODO Auto-generated method stub + Log.v("Debug", "in onLocation changed.."); + if(location!=null){ + long curTime = System.currentTimeMillis(); + + long diffTime = (curTime - lastUpdate); + lastUpdate = curTime; + Calendar c=Calendar.getInstance(); + c.setTimeInMillis(diffTime); + + time=c.get(Calendar.HOUR); + + + + locationManager.removeUpdates(LocationDataReader.this); + //String Speed = "Device Speed: " +location.getSpeed(); + latitude=location.getLongitude(); + longitude =location.getLatitude(); + + double distance =CalculationByDistance(latitude, longitude, lat_old, lon_old)/1000; + + + speed = (float)distance/(float)time; + Toast.makeText(mContext, longitude+"\n"+latitude+"\nDistance is: " + +distance+"\nSpeed is: "+speed , Toast.LENGTH_SHORT).show(); + + + Intent intent = new Intent("speedUpdate"); + intent.putExtra("speed", speed); + LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent); + + lat_old=latitude; + lon_old=longitude; + } } @@ -192,4 +240,17 @@ public class LocationDataReader extends DataReader implements LocationListener { } } + public double CalculationByDistance(double lat1, double lon1, double lat2, double lon2) { + double Radius = EARTH_RADIUS; + double dLat = Math.toRadians(lat2-lat1); + double dLon = Math.toRadians(lon2-lon1); + double a = Math.sin(dLat/2) * Math.sin(dLat/2) + + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * + Math.sin(dLon/2) * Math.sin(dLon/2); + double c = 2 * Math.asin(Math.sqrt(a)); + return Radius * c; + } + + + } 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/event/streams/SenseDataCollector.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/event/streams/SenseDataCollector.java index 2febb8ba20..41a28a0aef 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/event/streams/SenseDataCollector.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/event/streams/SenseDataCollector.java @@ -17,13 +17,14 @@ package org.wso2.carbon.iot.android.sense.event.streams; import android.content.Context; import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationDataReader; import org.wso2.carbon.iot.android.sense.event.streams.Sensor.SensorDataReader; +import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedDataReader; /** * This class triggered by service to collect the sensor data. */ public class SenseDataCollector { public enum DataType { - SENSOR, LOCATION + SENSOR, LOCATION,SPEED } public SenseDataCollector(Context ctx, DataType dt) { @@ -35,6 +36,9 @@ public class SenseDataCollector { case LOCATION: dr = new LocationDataReader(ctx); break; + case SPEED: + dr = new SpeedDataReader(ctx); + break; } if (dr != null) { Thread DataCollector = new Thread(dr); 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/event/streams/Speed/SpeedData.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/event/streams/Speed/SpeedData.java new file mode 100644 index 0000000000..a4e71301c4 --- /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/event/streams/Speed/SpeedData.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.event.streams.Speed; + +import java.util.Date; + +public class SpeedData { + private float speed; // speed + private String turnAxis; // turns + private long timestamp; + + SpeedData(float speed, String turnAxis) { + this.speed = speed; + this.turnAxis = turnAxis; + timestamp = new Date().getTime(); + + } + + public float getSpeed() { + return speed; + } + + public void setSpeed(float speed) { + this.speed = speed; + } + + public String getTurns() { + return turnAxis; + } + + public void setTurns(String turnAxis) { + this.turnAxis = turnAxis; + } + + public long getTimeStamp() { + return timestamp; + } + + public void setTimeStamp(long timeStamp) { + timestamp = timeStamp; + } + +} 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/event/streams/Speed/SpeedDataReader.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/event/streams/Speed/SpeedDataReader.java new file mode 100644 index 0000000000..f306150f4c --- /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/event/streams/Speed/SpeedDataReader.java @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.event.streams.Speed; + +import org.wso2.carbon.iot.android.sense.event.streams.DataReader; +import org.wso2.carbon.iot.android.sense.event.streams.Sensor.SensorData; +import org.wso2.carbon.iot.android.sense.realtimeviewer.sensorlisting.SupportedSensors; +import org.wso2.carbon.iot.android.sense.util.SenseDataHolder; +import android.content.Context; +import android.content.IntentFilter; +import android.content.SharedPreferences; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.support.v4.content.LocalBroadcastManager; +import android.util.Log; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Vector; +import java.util.concurrent.TimeUnit; +import android.content.BroadcastReceiver; + +import android.content.Intent; +import android.app.Activity; +import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationDataReader; + + + +public class SpeedDataReader extends DataReader implements SensorEventListener { + + SpeedData data; + private SensorManager mSensorManager; + private Map senseDataStruct = new HashMap<>(); + private Vector sensorVector = new Vector<>(); + private static final String TAG = SpeedDataReader.class.getName(); + private float last_x, last_y, last_z; + private long lastUpdate; + private String xTurnAxis; + float speed; + private float x,y,z; + Context ctx; + private List sensorList = new ArrayList<>(); + private SupportedSensors supportedSensors = SupportedSensors.getInstance(); + + @Override + public void onSensorChanged(SensorEvent event) { + + Sensor devSensor = event.sensor; + + if (devSensor.getType() == Sensor.TYPE_ACCELEROMETER) { + x = event.values[0]; + y = event.values[1]; + z = event.values[2]; + + } + } + + public SpeedDataReader(Context context) { + ctx = context; + SharedPreferences sharedPreferences = ctx.getSharedPreferences(SupportedSensors.SELECTED_SENSORS, Context + .MODE_MULTI_PROCESS); + Set selectedSet = sharedPreferences.getStringSet(SupportedSensors.SELECTED_SENSORS_BY_USER, null); + mSensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); + selectedSensorList(selectedSet); + for (Sensor sensor : sensorList) { + mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); + } + + LocalBroadcastManager.getInstance(ctx).registerReceiver(mMessageReceiver, + new IntentFilter("speedUpdate")); + + } + + private void collectSensorData() { + for (Sensor sensor : sensorList) { + try { + if (senseDataStruct.containsKey(sensor.getName())) { + SensorData sensorInfo = senseDataStruct.get(sensor.getName()); + sensorVector.add(sensorInfo); + Log.d(TAG, "Sensor Name " + sensor.getName() + ", Type " + sensor.getType() + " " + + ", sensorValue :" + sensorInfo.getSensorValues()); + } + } catch (Throwable e) { + Log.d(TAG, "error on sensors"); + } + } + mSensorManager.unregisterListener(this); + } + + + public String getTurns() { + + if(Round(x,4)>10.0000){ + Log.d("sensor", "X Right axis: " + x); + xTurnAxis = "Right"; + return xTurnAxis; + }else if(Round(x,4)<-10.0000){ + Log.d("sensor", "X Left axis: " + x); + xTurnAxis = "Left"; + return xTurnAxis; + }else { + xTurnAxis = "No Turns"; + + } + return xTurnAxis; + } + + public float getSpeed(){ + + return speed; + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + // can be safely ignored for this demo + } + + public static float Round(float Rval, int Rpl) { + float p = (float)Math.pow(10,Rpl); + Rval = Rval * p; + float tmp = Math.round(Rval); + Log.d("round", "round: " + tmp/p); + + return tmp/p; + } + + @Override + public void run() { + Log.d(TAG, "running - Device Speed"); + try { + TimeUnit.MILLISECONDS.sleep(10000); + // String trn = getTurns(); + // double spd = getSpeed(); + //if (trn != 0 && spd != 0) { + data = new SpeedData(getSpeed(), getTurns()); + SenseDataHolder.getSpeedDataHolder().add(data); + collectSensorData(); + + //} + } catch (InterruptedException e) { + // Restore the interrupted status + Thread.currentThread().interrupt(); + Log.e(TAG, " Speed Data Retrieval Failed"); + } + } + + public void selectedSensorList(Set set) { + if (set != null) { + String[] sensorsSet = set.toArray(new String[set.size()]); + for (String s : sensorsSet) { + sensorList.add(mSensorManager.getDefaultSensor(supportedSensors.getType(s.toLowerCase()))); + } + } + } + + private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + // Get extra data included in the Intent + speed = intent.getFloatExtra("speed",speed); + + Log.d("receiver", "Got message: " + speed); + } + }; + + +} 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/util/SenseDataHolder.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/util/SenseDataHolder.java index 8f0cefb56c..2d6868849b 100755 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/SenseDataHolder.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/util/SenseDataHolder.java @@ -15,6 +15,7 @@ package org.wso2.carbon.iot.android.sense.util; import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationData; import org.wso2.carbon.iot.android.sense.event.streams.Sensor.SensorData; +import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedData; import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData; import org.wso2.carbon.iot.android.sense.speech.detector.util.WordData; import java.util.List; @@ -31,6 +32,8 @@ public class SenseDataHolder { private static List batteryDataHolder; private static List locationDataHolder; private static List wordDataHolder; + private static List speedDataHolder; + //LocationData gps; @@ -70,6 +73,13 @@ public class SenseDataHolder { return wordDataHolder; } + public static List getSpeedDataHolder(){ + if(speedDataHolder == null){ + speedDataHolder = new CopyOnWriteArrayList<>(); + } + return speedDataHolder; + } + public static void resetSensorDataHolder(){ sensorDataHolder = null; } @@ -86,4 +96,9 @@ public class SenseDataHolder { wordDataHolder = null; } + public static void resetSpeedDataHolder() { + speedDataHolder = null; + } + + }