fix bugs location and accelerometer

revert-dabc3590
Waruna Samarsinghe 9 years ago
parent bf7975c5d4
commit 1fdb6fd54c

@ -29,6 +29,8 @@ import org.wso2.carbon.iot.android.sense.data.publisher.mqtt.transport.MQTTTrans
import org.wso2.carbon.iot.android.sense.data.publisher.mqtt.transport.TransportHandlerException;
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.Sensor.SensorData;
import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData;
import org.wso2.carbon.iot.android.sense.speech.detector.util.ProcessWords;
@ -50,6 +52,7 @@ public class DataPublisherService extends Service {
private static String VALUE_TAG = "value";
public static Context context;
LocationData gps;
@Nullable
@Override
@ -66,64 +69,74 @@ public class DataPublisherService extends Service {
public void run() {
try {
List<Event> events = new ArrayList<>();
//retreive sensor data.
//retrieve sensor data.
List<SensorData> sensorDataMap = SenseDataHolder.getSensorDataHolder();
for (SensorData sensorData : sensorDataMap) {
Event event = new Event();
event.setTimestamp(sensorData.getTimestamp());
switch (sensorData.getSensorType()) {
case Sensor.TYPE_ACCELEROMETER:
event.setAccelerometer(sensorData.getSensorValues());
events.add(event);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
event.setMagnetic(sensorData.getSensorValues());
events.add(event);
break;
case Sensor.TYPE_GYROSCOPE:
event.setGyroscope(sensorData.getSensorValues());
events.add(event);
break;
case Sensor.TYPE_LIGHT:
event.setLight(sensorData.getSensorValues()[0]);
break;
case Sensor.TYPE_PRESSURE:
event.setPressure(sensorData.getSensorValues()[0]);
events.add(event);
break;
case Sensor.TYPE_PROXIMITY:
event.setProximity(sensorData.getSensorValues()[0]);
events.add(event);
break;
case Sensor.TYPE_GRAVITY:
event.setGravity(sensorData.getSensorValues());
events.add(event);
break;
case Sensor.TYPE_GAME_ROTATION_VECTOR:
event.setRotation(sensorData.getSensorValues());
events.add(event);
break;
if (!sensorDataMap.isEmpty()) {
for (SensorData sensorData : sensorDataMap) {
Event event = new Event();
event.setTimestamp(sensorData.getTimestamp());
switch (sensorData.getSensorType()) {
case Sensor.TYPE_ACCELEROMETER:
event.setAccelerometer(sensorData.getSensorValues());
events.add(event);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
event.setMagnetic(sensorData.getSensorValues());
events.add(event);
break;
case Sensor.TYPE_GYROSCOPE:
event.setGyroscope(sensorData.getSensorValues());
events.add(event);
break;
case Sensor.TYPE_LIGHT:
event.setLight(sensorData.getSensorValues()[0]);
break;
case Sensor.TYPE_PRESSURE:
event.setPressure(sensorData.getSensorValues()[0]);
events.add(event);
break;
case Sensor.TYPE_PROXIMITY:
event.setProximity(sensorData.getSensorValues()[0]);
events.add(event);
break;
case Sensor.TYPE_GRAVITY:
event.setGravity(sensorData.getSensorValues());
events.add(event);
break;
case Sensor.TYPE_GAME_ROTATION_VECTOR:
event.setRotation(sensorData.getSensorValues());
events.add(event);
break;
}
}
}
SenseDataHolder.resetSensorDataHolder();
//retreive batter data.
//retrieve batter data.
List<BatteryData> batteryDataMap = SenseDataHolder.getBatteryDataHolder();
for (BatteryData batteryData : batteryDataMap) {
Event event = new Event();
event.setTimestamp(batteryData.getTimestamp());
event.setBattery(batteryData.getLevel());
events.add(event);
if (!batteryDataMap.isEmpty()) {
for (BatteryData batteryData : batteryDataMap) {
Event event = new Event();
event.setTimestamp(batteryData.getTimestamp());
event.setBattery(batteryData.getLevel());
events.add(event);
}
}
SenseDataHolder.resetBatteryDataHolder();
//retreive location data.
//retrieve location data.
List<LocationData> locationDataMap = SenseDataHolder.getLocationDataHolder();
for (LocationData locationData : locationDataMap) {
Event event = new Event();
event.setTimestamp(locationData.getTimeStamp());
event.setGps(new double[]{locationData.getLatitude(), locationData.getLongitude()});
events.add(event);
}
if (!locationDataMap.isEmpty()) {
for (LocationData locationData : locationDataMap) {
Event event = new Event();
event.setTimestamp(locationData.getTimeStamp());
event.setGps(new double[]{locationData.getLatitude(), locationData.getLongitude()});
events.add(event);
}
}
SenseDataHolder.resetLocationDataHolder();
//retrieve words
@ -158,13 +171,19 @@ public class DataPublisherService extends Service {
event.setDeviceId(deviceId);
jsonArray.put(new JSONObject().put("event", event.getEvent()));
}
MQTTTransportHandler mqttTransportHandler = AndroidSenseMQTTHandler.getInstance(context);
if (!mqttTransportHandler.isConnected()) {
mqttTransportHandler.connect();
}
String topic = LocalRegistry.getTenantDomain(context) + "/" + SenseConstants
.DEVICE_TYPE + "/" + deviceId + "/data";
mqttTransportHandler.publishDeviceData(user, deviceId, jsonArray.toString(), topic);
String topic = "wso2/" + LocalRegistry.getTenantDomain(context) + "/" + SenseConstants
.DEVICE_TYPE + "/" + deviceId + "/data";
mqttTransportHandler.publishDeviceData(user, deviceId, jsonArray.toString(), topic);
}
} catch (JSONException e) {
Log.e(TAG, "Json Data Parsing Exception", e);

@ -2,6 +2,9 @@ package org.wso2.carbon.iot.android.sense.data.publisher;
import org.json.JSONException;
import org.json.JSONObject;
import org.wso2.carbon.iot.android.sense.event.streams.Location.LocationData;
import android.util.Log;
/**
* This hold the definition of the stream that android sense is publishing to.
@ -26,8 +29,11 @@ public class Event {
private String wordStatus;
private long timestamp;
private static final String TAG = Event.class.getName();
LocationData gpsLoc;
private int getBattery() {
return battery;
return this.battery;
}
public void setBattery(int battery) {
@ -36,7 +42,8 @@ public class Event {
}
private double[] getGps() {
return gps != null ? gps : new double[]{0, 0};
return gps != null ? this.gps : new double[]{gps[0],gps[1]};
}
public void setGps(double[] gps) {
@ -45,7 +52,7 @@ public class Event {
}
private float[] getAccelerometer() {
return accelerometer != null ? accelerometer : new float[]{0, 0, 0};
return this.accelerometer != null ? this.accelerometer : new float[]{0, 0, 0};
}
public void setAccelerometer(float[] accelerometer) {
@ -54,7 +61,7 @@ public class Event {
}
private float[] getMagnetic() {
return magnetic != null ? magnetic : new float[]{0, 0, 0};
return this.magnetic != null ? this.magnetic : new float[]{0, 0, 0};
}
public void setMagnetic(float[] magnetic) {
@ -63,7 +70,7 @@ public class Event {
}
private float[] getGyroscope() {
return gyroscope != null ? gyroscope : new float[]{0, 0, 0};
return this.gyroscope != null ? this.gyroscope : new float[]{0, 0, 0};
}
public void setGyroscope(float[] gyroscope) {
@ -72,7 +79,7 @@ public class Event {
}
public float getLight() {
return light;
return this.light;
}
public void setLight(float light) {
@ -81,7 +88,7 @@ public class Event {
}
public float getPressure() {
return pressure;
return this.pressure;
}
public void setPressure(float pressure) {
@ -90,7 +97,7 @@ public class Event {
}
public float getProximity() {
return proximity;
return this.proximity;
}
public void setProximity(float proximity) {
@ -99,7 +106,7 @@ public class Event {
}
private float[] getGravity() {
return gravity != null ? gravity : new float[]{0, 0, 0};
return this.gravity != null ? this.gravity : new float[]{0, 0, 0};
}
public void setGravity(float gravity[]) {
@ -108,7 +115,7 @@ public class Event {
}
private float[] getRotation() {
return rotation != null ? rotation : new float[]{0, 0, 0};
return this.rotation != null ? this.rotation : new float[]{0, 0, 0};
}
public void setRotation(float rotation[]) {
@ -125,7 +132,7 @@ public class Event {
}
private String getWord() {
return word != null ? word : "";
return this.word != null ? this.word : "";
}
public void setWord(String word) {
@ -176,44 +183,60 @@ public class Event {
JSONObject jsonPayloadData = new JSONObject();
jsonPayloadData.put("battery", getBattery());
//gps
double gpsEvents[] = getGps();
jsonPayloadData.put("gps_lat", gpsEvents[0]);
jsonPayloadData.put("gps_long", gpsEvents[1]);
//acceleromter
float events[] = getAccelerometer();
jsonPayloadData.put("accelerometer_x", events[0]);
jsonPayloadData.put("accelerometer_y", events[1]);
jsonPayloadData.put("accelerometer_z", events[2]);
//gps & accelerometer
if (gps !=null && accelerometer !=null) {
jsonPayloadData.put("gps_lat", gps[0]);
jsonPayloadData.put("gps_long", gps[1]);
jsonPayloadData.put("accelerometer_x", accelerometer[0]);
jsonPayloadData.put("accelerometer_y", accelerometer[1]);
jsonPayloadData.put("accelerometer_z", accelerometer[2]);
}
//magnetic
events = getMagnetic();
jsonPayloadData.put("magnetic_x", events[0]);
jsonPayloadData.put("magnetic_y", events[1]);
jsonPayloadData.put("magnetic_z", events[2]);
//events = getMagnetic();
if (magnetic !=null) {
jsonPayloadData.put("magnetic_x", magnetic[0]);
jsonPayloadData.put("magnetic_y", magnetic[1]);
jsonPayloadData.put("magnetic_z", magnetic[2]);
}
//gyroscope
events = getGyroscope();
jsonPayloadData.put("gyroscope_x", events[0]);
jsonPayloadData.put("gyroscope_y", events[1]);
jsonPayloadData.put("gyroscope_z", events[2]);
//events = getGyroscope();
if (gyroscope != null) {
jsonPayloadData.put("gyroscope_x", gyroscope[0]);
jsonPayloadData.put("gyroscope_y", gyroscope[1]);
jsonPayloadData.put("gyroscope_z", gyroscope[2]);
}
jsonPayloadData.put("light", getLight());
jsonPayloadData.put("pressure", getPressure());
jsonPayloadData.put("proximity", getProximity());
//gravity
events = getGravity();
jsonPayloadData.put("gravity_x", events[0]);
jsonPayloadData.put("gravity_y", events[1]);
jsonPayloadData.put("gravity_z", events[2]);
//events = getGravity();
if (gravity!=null) {
jsonPayloadData.put("gravity_x", gravity[0]);
jsonPayloadData.put("gravity_y", gravity[1]);
jsonPayloadData.put("gravity_z", gravity[2]);
}
//rotation
events = getRotation();
jsonPayloadData.put("rotation_x", events[0]);
jsonPayloadData.put("rotation_y", events[1]);
jsonPayloadData.put("rotation_z", events[2]);
//events = getRotation();
if (rotation!=null) {
jsonPayloadData.put("rotation_x", rotation[0]);
jsonPayloadData.put("rotation_y", rotation[1]);
jsonPayloadData.put("rotation_z", rotation[2]);
}
//word
jsonPayloadData.put("word", getWord());
jsonPayloadData.put("word_sessionId", getWordSessionId());
jsonPayloadData.put("word_status", getWordStatus());
jsonEvent.put("payloadData", jsonPayloadData);
return jsonEvent;

@ -77,7 +77,7 @@ public abstract class MQTTTransportHandler implements MqttCallback, TransportHan
String username = LocalRegistry.getUsername(context);
String deviceId = LocalRegistry.getDeviceId(context);
this.clientId = deviceId + ":" + SenseConstants.DEVICE_TYPE;
this.subscribeTopic = LocalRegistry.getTenantDomain(context)+ "/" + SenseConstants.DEVICE_TYPE + "/" +
this.subscribeTopic = "wso2/" + LocalRegistry.getTenantDomain(context)+ "/" + SenseConstants.DEVICE_TYPE + "/" +
deviceId + "/command/#";
this.clientWillTopic = DISCONNECTION_WILL_TOPIC_PREFIX + SenseConstants.DEVICE_TYPE;
this.mqttBrokerEndPoint = LocalRegistry.getMqttEndpoint(context);

@ -23,75 +23,101 @@ import org.wso2.carbon.iot.android.sense.event.streams.DataReader;
import org.wso2.carbon.iot.android.sense.util.SenseDataHolder;
import java.util.concurrent.TimeUnit;
/**
* This is used to retrieve the location data using GPS and used Network connection to increase the accuracy.
*/
public class LocationDataReader extends DataReader implements LocationListener {
protected LocationManager locationManager;
private Context mContext;
private final Context mContext;
LocationData gps;
// 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
// 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);
// getting GPS status
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean 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);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, this);
//MIN_TIME_BW_UPDATES,
//MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
try {
locationManager = (LocationManager) mContext
.getSystemService(mContext.LOCATION_SERVICE);
Log.d(TAG, "GPS Enabled");
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
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);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
@ -148,6 +174,7 @@ public class LocationDataReader extends DataReader implements LocationListener {
}
@Override
public void run() {
Log.d(TAG, "running -Location");
@ -155,8 +182,16 @@ public class LocationDataReader extends DataReader implements LocationListener {
TimeUnit.MILLISECONDS.sleep(10000);
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

@ -97,8 +97,8 @@ public class SensorDataReader extends DataReader implements SensorEventListener
@Override
public void run() {
Log.d(TAG, "running -sensorDataMap");
Vector<SensorData> sensorDatas = getSensorData();
for (SensorData data : sensorDatas) {
Vector<SensorData> sensorDetails = getSensorData();
for (SensorData data : sensorDetails) {
SenseDataHolder.getSensorDataHolder().add(data);
}
}

@ -17,9 +17,10 @@ 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 java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import android.util.Log;
/**
* This holds the sensor,battery and location data inmemory.
@ -31,6 +32,12 @@ public class SenseDataHolder {
private static List<LocationData> locationDataHolder;
private static List<WordData> wordDataHolder;
//LocationData gps;
private static final String TAG = SenseDataHolder.class.getName();
public static List<SensorData> getSensorDataHolder(){
if(sensorDataHolder == null){
sensorDataHolder = new CopyOnWriteArrayList<>();
@ -46,10 +53,14 @@ public class SenseDataHolder {
}
public static List<LocationData> getLocationDataHolder(){
if(locationDataHolder == null){
locationDataHolder = new CopyOnWriteArrayList<>();
}
return locationDataHolder;
return locationDataHolder;
}
public static List<WordData> getWordDataHolder(){

@ -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
#Wed May 11 14:40:10 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