rapid acceleration and turns- resolved

revert-dabc3590
Waruna Samarsinghe 9 years ago
parent bf7975c5d4
commit fec14a8509

@ -39,6 +39,7 @@ import org.wso2.carbon.iot.android.sense.util.LocalRegistry;
import java.util.ArrayList;
import java.util.List;
import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedData;
/**
* This is an android service which publishes the data to the server.
@ -148,6 +149,20 @@ public class DataPublisherService extends Service {
}
}
SenseDataHolder.resetWordDataHolder();
//retrieve speed data.
List<SpeedData> 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();
//publish the data
if (events.size() > 0 && LocalRegistry.isEnrolled(context)) {
String user = LocalRegistry.getUsername(context);

@ -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,33 @@ 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 +215,14 @@ public class Event {
jsonPayloadData.put("accelerometer_x", events[0]);
jsonPayloadData.put("accelerometer_y", events[1]);
jsonPayloadData.put("accelerometer_z", events[2]);
//speed
jsonPayloadData.put("speed_limit", getSpeed());
//turn
jsonPayloadData.put("turn_way", getTurns());
//magnetic
events = getMagnetic();
jsonPayloadData.put("magnetic_x", events[0]);

@ -49,6 +49,7 @@ 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;

@ -18,8 +18,14 @@ 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 java.util.Calendar;
import android.content.Intent;
import org.wso2.carbon.iot.android.sense.event.streams.DataReader;
import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedData;
import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedDataReader;
import org.wso2.carbon.iot.android.sense.util.SenseDataHolder;
import java.util.concurrent.TimeUnit;
@ -28,41 +34,69 @@ import java.util.concurrent.TimeUnit;
*/
public class LocationDataReader extends DataReader implements LocationListener {
protected LocationManager locationManager;
private Context mContext;
private final Context mContext;
LocationData gps;
static final Double EARTH_RADIUS = 6371.00;
// flag for GPS status
private boolean isGPSEnabled = false;
// flag for network status
private boolean isNetworkEnabled = false;
// flag for GPS status
private boolean canGetLocation = false;
//private boolean canGetLocation = false;
private static final String TAG = LocationDataReader.class.getName();
Location location; // location
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
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
public LocationDataReader(Context context) {
mContext = context;
this.mContext = context;
getLocation();
}
public Location getLocation() {
locationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
try {
locationManager = (LocationManager) mContext
.getSystemService(mContext.LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = locationManager
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = locationManager
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, this);
// MIN_TIME_BW_UPDATES,
// MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
@ -76,11 +110,10 @@ public class LocationDataReader extends DataReader implements LocationListener {
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, this);
//MIN_TIME_BW_UPDATES,
//MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d(TAG, "GPS Enabled");
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
@ -92,6 +125,11 @@ public class LocationDataReader extends DataReader implements LocationListener {
}
}
}
} catch (Exception e) {
Log.e(TAG, "Failed to capture location data.");
}
return location;
}
@ -127,25 +165,52 @@ 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;
}
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
@ -156,7 +221,12 @@ public class LocationDataReader extends DataReader implements LocationListener {
double lat = getLatitude();
double longit = getLongitude();
if (lat != 0 && longit != 0) {
SenseDataHolder.getLocationDataHolder().add(new LocationData(getLatitude(), getLongitude()));
Log.d(TAG, "YYY " + getLatitude() + ", XXX " + getLongitude());
gps = new LocationData(getLatitude(), getLongitude());
SenseDataHolder.getLocationDataHolder().add(gps);
}
} catch (InterruptedException e) {
// Restore the interrupted status
@ -165,4 +235,15 @@ 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;
}
}

@ -17,13 +17,15 @@ 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 +37,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);

@ -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;
}
}

@ -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<String, SensorData> senseDataStruct = new HashMap<>();
private Vector<SensorData> 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<Sensor> 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<String> 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<String> 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);
}
};
}

@ -17,6 +17,7 @@ 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.battery.BatteryData;
import org.wso2.carbon.iot.android.sense.speech.detector.util.WordData;
import org.wso2.carbon.iot.android.sense.event.streams.Speed.SpeedData;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@ -30,6 +31,8 @@ public class SenseDataHolder {
private static List<BatteryData> batteryDataHolder;
private static List<LocationData> locationDataHolder;
private static List<WordData> wordDataHolder;
private static List<SpeedData> speedDataHolder;
public static List<SensorData> getSensorDataHolder(){
if(sensorDataHolder == null){
@ -59,6 +62,14 @@ public class SenseDataHolder {
return wordDataHolder;
}
public static List<SpeedData> getSpeedDataHolder(){
if(speedDataHolder == null){
speedDataHolder = new CopyOnWriteArrayList<>();
}
return speedDataHolder;
}
public static void resetSensorDataHolder(){
sensorDataHolder = null;
}
@ -75,4 +86,8 @@ public class SenseDataHolder {
wordDataHolder = null;
}
public static void resetSpeedDataHolder() {
speedDataHolder = null;
}
}

@ -6,7 +6,7 @@ buildscript {
maven { url 'https://repo.eclipse.org/content/repositories/paho-releases/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

@ -1,6 +1,6 @@
#Fri Dec 11 10:25:01 IST 2015
#Sat Jun 04 23:55:41 IST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

Loading…
Cancel
Save