revert-dabc3590
ayyoob 9 years ago
commit aa9fe6006a

@ -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.data.publisher.mqtt.transport.TransportHandlerException;
import org.wso2.carbon.iot.android.sense.constants.SenseConstants; 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.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.Sensor.SensorData;
import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData; import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData;
import org.wso2.carbon.iot.android.sense.speech.detector.util.ProcessWords; 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"; private static String VALUE_TAG = "value";
public static Context context; public static Context context;
LocationData gps;
@Nullable @Nullable
@Override @Override
@ -66,8 +69,9 @@ public class DataPublisherService extends Service {
public void run() { public void run() {
try { try {
List<Event> events = new ArrayList<>(); List<Event> events = new ArrayList<>();
//retreive sensor data. //retrieve sensor data.
List<SensorData> sensorDataMap = SenseDataHolder.getSensorDataHolder(); List<SensorData> sensorDataMap = SenseDataHolder.getSensorDataHolder();
if (!sensorDataMap.isEmpty()) {
for (SensorData sensorData : sensorDataMap) { for (SensorData sensorData : sensorDataMap) {
Event event = new Event(); Event event = new Event();
event.setTimestamp(sensorData.getTimestamp()); event.setTimestamp(sensorData.getTimestamp());
@ -105,25 +109,34 @@ public class DataPublisherService extends Service {
break; break;
} }
} }
}
SenseDataHolder.resetSensorDataHolder(); SenseDataHolder.resetSensorDataHolder();
//retreive batter data. //retrieve batter data.
List<BatteryData> batteryDataMap = SenseDataHolder.getBatteryDataHolder(); List<BatteryData> batteryDataMap = SenseDataHolder.getBatteryDataHolder();
if (!batteryDataMap.isEmpty()) {
for (BatteryData batteryData : batteryDataMap) { for (BatteryData batteryData : batteryDataMap) {
Event event = new Event(); Event event = new Event();
event.setTimestamp(batteryData.getTimestamp()); event.setTimestamp(batteryData.getTimestamp());
event.setBattery(batteryData.getLevel()); event.setBattery(batteryData.getLevel());
events.add(event); events.add(event);
} }
}
SenseDataHolder.resetBatteryDataHolder(); SenseDataHolder.resetBatteryDataHolder();
//retreive location data. //retrieve location data.
List<LocationData> locationDataMap = SenseDataHolder.getLocationDataHolder(); List<LocationData> locationDataMap = SenseDataHolder.getLocationDataHolder();
if (!locationDataMap.isEmpty()) {
for (LocationData locationData : locationDataMap) { for (LocationData locationData : locationDataMap) {
Event event = new Event(); Event event = new Event();
event.setTimestamp(locationData.getTimeStamp()); event.setTimestamp(locationData.getTimeStamp());
event.setGps(new double[]{locationData.getLatitude(), locationData.getLongitude()}); event.setGps(new double[]{locationData.getLatitude(), locationData.getLongitude()});
events.add(event); events.add(event);
} }
}
SenseDataHolder.resetLocationDataHolder(); SenseDataHolder.resetLocationDataHolder();
//retrieve words //retrieve words
@ -158,13 +171,19 @@ public class DataPublisherService extends Service {
event.setDeviceId(deviceId); event.setDeviceId(deviceId);
jsonArray.put(new JSONObject().put("event", event.getEvent())); jsonArray.put(new JSONObject().put("event", event.getEvent()));
} }
MQTTTransportHandler mqttTransportHandler = AndroidSenseMQTTHandler.getInstance(context); MQTTTransportHandler mqttTransportHandler = AndroidSenseMQTTHandler.getInstance(context);
if (!mqttTransportHandler.isConnected()) { if (!mqttTransportHandler.isConnected()) {
mqttTransportHandler.connect(); mqttTransportHandler.connect();
} }
String topic = LocalRegistry.getTenantDomain(context) + "/" + SenseConstants
String topic = "wso2/" + LocalRegistry.getTenantDomain(context) + "/" + SenseConstants
.DEVICE_TYPE + "/" + deviceId + "/data"; .DEVICE_TYPE + "/" + deviceId + "/data";
mqttTransportHandler.publishDeviceData(user, deviceId, jsonArray.toString(), topic); mqttTransportHandler.publishDeviceData(user, deviceId, jsonArray.toString(), topic);
} }
} catch (JSONException e) { } catch (JSONException e) {
Log.e(TAG, "Json Data Parsing Exception", 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.JSONException;
import org.json.JSONObject; 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. * This hold the definition of the stream that android sense is publishing to.
@ -26,8 +29,11 @@ public class Event {
private String wordStatus; private String wordStatus;
private long timestamp; private long timestamp;
private static final String TAG = Event.class.getName();
LocationData gpsLoc;
private int getBattery() { private int getBattery() {
return battery; return this.battery;
} }
public void setBattery(int battery) { public void setBattery(int battery) {
@ -36,7 +42,8 @@ public class Event {
} }
private double[] getGps() { 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) { public void setGps(double[] gps) {
@ -45,7 +52,7 @@ public class Event {
} }
private float[] getAccelerometer() { 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) { public void setAccelerometer(float[] accelerometer) {
@ -54,7 +61,7 @@ public class Event {
} }
private float[] getMagnetic() { 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) { public void setMagnetic(float[] magnetic) {
@ -63,7 +70,7 @@ public class Event {
} }
private float[] getGyroscope() { 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) { public void setGyroscope(float[] gyroscope) {
@ -72,7 +79,7 @@ public class Event {
} }
public float getLight() { public float getLight() {
return light; return this.light;
} }
public void setLight(float light) { public void setLight(float light) {
@ -81,7 +88,7 @@ public class Event {
} }
public float getPressure() { public float getPressure() {
return pressure; return this.pressure;
} }
public void setPressure(float pressure) { public void setPressure(float pressure) {
@ -90,7 +97,7 @@ public class Event {
} }
public float getProximity() { public float getProximity() {
return proximity; return this.proximity;
} }
public void setProximity(float proximity) { public void setProximity(float proximity) {
@ -99,7 +106,7 @@ public class Event {
} }
private float[] getGravity() { 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[]) { public void setGravity(float gravity[]) {
@ -108,7 +115,7 @@ public class Event {
} }
private float[] getRotation() { 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[]) { public void setRotation(float rotation[]) {
@ -125,7 +132,7 @@ public class Event {
} }
private String getWord() { private String getWord() {
return word != null ? word : ""; return this.word != null ? this.word : "";
} }
public void setWord(String word) { public void setWord(String word) {
@ -176,44 +183,60 @@ public class Event {
JSONObject jsonPayloadData = new JSONObject(); JSONObject jsonPayloadData = new JSONObject();
jsonPayloadData.put("battery", getBattery()); jsonPayloadData.put("battery", getBattery());
//gps //gps & accelerometer
double gpsEvents[] = getGps();
jsonPayloadData.put("gps_lat", gpsEvents[0]); if (gps !=null && accelerometer !=null) {
jsonPayloadData.put("gps_long", gpsEvents[1]); jsonPayloadData.put("gps_lat", gps[0]);
//acceleromter jsonPayloadData.put("gps_long", gps[1]);
float events[] = getAccelerometer();
jsonPayloadData.put("accelerometer_x", events[0]); jsonPayloadData.put("accelerometer_x", accelerometer[0]);
jsonPayloadData.put("accelerometer_y", events[1]); jsonPayloadData.put("accelerometer_y", accelerometer[1]);
jsonPayloadData.put("accelerometer_z", events[2]); jsonPayloadData.put("accelerometer_z", accelerometer[2]);
}
//magnetic //magnetic
events = getMagnetic(); //events = getMagnetic();
jsonPayloadData.put("magnetic_x", events[0]); if (magnetic !=null) {
jsonPayloadData.put("magnetic_y", events[1]); jsonPayloadData.put("magnetic_x", magnetic[0]);
jsonPayloadData.put("magnetic_z", events[2]); jsonPayloadData.put("magnetic_y", magnetic[1]);
jsonPayloadData.put("magnetic_z", magnetic[2]);
}
//gyroscope //gyroscope
events = getGyroscope(); //events = getGyroscope();
jsonPayloadData.put("gyroscope_x", events[0]);
jsonPayloadData.put("gyroscope_y", events[1]); if (gyroscope != null) {
jsonPayloadData.put("gyroscope_z", events[2]); jsonPayloadData.put("gyroscope_x", gyroscope[0]);
jsonPayloadData.put("gyroscope_y", gyroscope[1]);
jsonPayloadData.put("gyroscope_z", gyroscope[2]);
}
jsonPayloadData.put("light", getLight()); jsonPayloadData.put("light", getLight());
jsonPayloadData.put("pressure", getPressure()); jsonPayloadData.put("pressure", getPressure());
jsonPayloadData.put("proximity", getProximity()); jsonPayloadData.put("proximity", getProximity());
//gravity //gravity
events = getGravity(); //events = getGravity();
jsonPayloadData.put("gravity_x", events[0]); if (gravity!=null) {
jsonPayloadData.put("gravity_y", events[1]); jsonPayloadData.put("gravity_x", gravity[0]);
jsonPayloadData.put("gravity_z", events[2]); jsonPayloadData.put("gravity_y", gravity[1]);
jsonPayloadData.put("gravity_z", gravity[2]);
}
//rotation //rotation
events = getRotation(); //events = getRotation();
jsonPayloadData.put("rotation_x", events[0]); if (rotation!=null) {
jsonPayloadData.put("rotation_y", events[1]); jsonPayloadData.put("rotation_x", rotation[0]);
jsonPayloadData.put("rotation_z", events[2]); jsonPayloadData.put("rotation_y", rotation[1]);
jsonPayloadData.put("rotation_z", rotation[2]);
}
//word //word
jsonPayloadData.put("word", getWord()); jsonPayloadData.put("word", getWord());
jsonPayloadData.put("word_sessionId", getWordSessionId()); jsonPayloadData.put("word_sessionId", getWordSessionId());
jsonPayloadData.put("word_status", getWordStatus()); jsonPayloadData.put("word_status", getWordStatus());
jsonEvent.put("payloadData", jsonPayloadData); jsonEvent.put("payloadData", jsonPayloadData);
return jsonEvent; return jsonEvent;

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

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

@ -97,8 +97,8 @@ public class SensorDataReader extends DataReader implements SensorEventListener
@Override @Override
public void run() { public void run() {
Log.d(TAG, "running -sensorDataMap"); Log.d(TAG, "running -sensorDataMap");
Vector<SensorData> sensorDatas = getSensorData(); Vector<SensorData> sensorDetails = getSensorData();
for (SensorData data : sensorDatas) { for (SensorData data : sensorDetails) {
SenseDataHolder.getSensorDataHolder().add(data); 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.Sensor.SensorData;
import org.wso2.carbon.iot.android.sense.event.streams.battery.BatteryData; 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.speech.detector.util.WordData;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import android.util.Log;
/** /**
* This holds the sensor,battery and location data inmemory. * This holds the sensor,battery and location data inmemory.
@ -31,6 +32,12 @@ public class SenseDataHolder {
private static List<LocationData> locationDataHolder; private static List<LocationData> locationDataHolder;
private static List<WordData> wordDataHolder; private static List<WordData> wordDataHolder;
//LocationData gps;
private static final String TAG = SenseDataHolder.class.getName();
public static List<SensorData> getSensorDataHolder(){ public static List<SensorData> getSensorDataHolder(){
if(sensorDataHolder == null){ if(sensorDataHolder == null){
sensorDataHolder = new CopyOnWriteArrayList<>(); sensorDataHolder = new CopyOnWriteArrayList<>();
@ -46,10 +53,14 @@ public class SenseDataHolder {
} }
public static List<LocationData> getLocationDataHolder(){ public static List<LocationData> getLocationDataHolder(){
if(locationDataHolder == null){ if(locationDataHolder == null){
locationDataHolder = new CopyOnWriteArrayList<>(); locationDataHolder = new CopyOnWriteArrayList<>();
} }
return locationDataHolder; return locationDataHolder;
} }
public static List<WordData> getWordDataHolder(){ public static List<WordData> getWordDataHolder(){

@ -6,7 +6,7 @@ buildscript {
maven { url 'https://repo.eclipse.org/content/repositories/paho-releases/' } maven { url 'https://repo.eclipse.org/content/repositories/paho-releases/' }
} }
dependencies { 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 // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // 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 distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists 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

@ -107,13 +107,14 @@ if [ "$mode" = "N" ]; then
sudo python setup.py install sudo python setup.py install
cd .. cd ..
# ----------------------------------------------------- # -----------------------------------------------------
fi
# Install Paho-MQTT-Library for MQTT Communication # Install Paho-MQTT-Library for MQTT Communication
git clone https://github.com/eclipse/paho.mqtt.python.git git clone https://github.com/eclipse/paho.mqtt.python.git
cd ./paho.mqtt.python cd ./paho.mqtt.python
sudo python setup.py install sudo python setup.py install
cd .. cd ..
# ----------------------------------------------------- # -----------------------------------------------------
fi
chmod +x ./src/RaspberryAgent.py chmod +x ./src/RaspberryAgent.py
sudo python ./src/RaspberryAgent.py -i $input -m $mode sudo python ./src/RaspberryAgent.py -i $input -m $mode

Loading…
Cancel
Save