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/RegisterActivity.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/RegisterActivity.java index e9f56a8b16..0ef131041e 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/RegisterActivity.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/RegisterActivity.java @@ -52,7 +52,6 @@ public class RegisterActivity extends Activity { private EditText mUsernameView; private EditText mPasswordView; private EditText mHostView; - private EditText mMqttPortView; private View mProgressView; private View mLoginFormView; private Handler mUiHandler = new Handler(); @@ -71,7 +70,6 @@ public class RegisterActivity extends Activity { mUsernameView = (EditText) findViewById(R.id.username); mPasswordView = (EditText) findViewById(R.id.password); mHostView = (EditText) findViewById(R.id.hostname); - mMqttPortView = (EditText) findViewById(R.id.mqttPort); AvailableSensorsInDevice availableSensorsInDevice = new AvailableSensorsInDevice(getApplicationContext()); availableSensorsInDevice.setContent(); @@ -99,7 +97,6 @@ public class RegisterActivity extends Activity { final String username = mUsernameView.getText().toString(); final String password = mPasswordView.getText().toString(); final String hostname = mHostView.getText().toString(); - String mqttPort = mMqttPortView.getText().toString(); boolean cancel = false; View focusView = null; @@ -124,7 +121,6 @@ public class RegisterActivity extends Activity { if (cancel) { focusView.requestFocus(); } else { - final int mqttPortNo= Integer.parseInt(mqttPort); Thread myThread = new Thread(new Runnable() { @Override public void run() { @@ -143,7 +139,6 @@ public class RegisterActivity extends Activity { LocalRegistry.setEnrolled(getApplicationContext(), true); LocalRegistry.addUsername(getApplicationContext(), username); LocalRegistry.addDeviceId(getApplicationContext(), deviceId); - LocalRegistry.addMqttPort(getApplicationContext(), mqttPortNo); MQTTTransportHandler mqttTransportHandler = AndroidSenseMQTTHandler.getInstance(getApplicationContext()); if (!mqttTransportHandler.isConnected()) { mqttTransportHandler.connect(); 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/constants/SenseConstants.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/constants/SenseConstants.java index 0661144ada..6ef46a7d3b 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/constants/SenseConstants.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/constants/SenseConstants.java @@ -23,14 +23,12 @@ public class SenseConstants { public final static String TOKEN_ISSUER_CONTEXT = "/oauth2"; public final static String API_APPLICATION_REGISTRATION_CONTEXT = "/api-application-registration"; - public static final int MQTT_BROKER_PORT = 1883; public static final String EVENT_LISTENER_STARTED = "xxStartedxx"; public static final String EVENT_LISTENER_FINISHED = "xxFinishedxx"; public static final String EVENT_LISTENER_ONGOING = "xxOngoingxx"; public final class Request { public final static String REQUEST_SUCCESSFUL = "200"; - public final static String REQUEST_CONFLICT = "409"; public final static int MAX_ATTEMPTS = 2; } } 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 d1ae9ad038..8840adf70e 100644 --- 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 @@ -50,6 +50,7 @@ public class DataPublisherService extends Service { private static String VALUE_TAG = "value"; public static Context context; + @Nullable @Override public IBinder onBind(Intent intent) { @@ -70,34 +71,39 @@ public class DataPublisherService extends Service { 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_ROTATION_VECTOR: - event.setGravity(sensorData.getSensorValues()); + case Sensor.TYPE_GAME_ROTATION_VECTOR: + event.setRotation(sensorData.getSensorValues()); + events.add(event); break; } - events.add(event); } SenseDataHolder.resetSensorDataHolder(); @@ -120,7 +126,7 @@ public class DataPublisherService extends Service { } SenseDataHolder.resetLocationDataHolder(); - //retreive words + //retrieve words ProcessWords.cleanAndPushToWordMap(); List wordDatMap = SenseDataHolder.getWordDataHolder(); for (WordData wordData : wordDatMap) { @@ -156,7 +162,9 @@ public class DataPublisherService extends Service { if (!mqttTransportHandler.isConnected()) { mqttTransportHandler.connect(); } - mqttTransportHandler.publishDeviceData(user, deviceId, jsonArray.toString()); + 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); 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/mqtt/AndroidSenseMQTTHandler.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/mqtt/AndroidSenseMQTTHandler.java index 989ad6d388..5d607546fe 100644 --- 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/mqtt/AndroidSenseMQTTHandler.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/mqtt/AndroidSenseMQTTHandler.java @@ -26,6 +26,7 @@ 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.speech.detector.util.ProcessWords; +import org.wso2.carbon.iot.android.sense.util.LocalRegistry; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -46,7 +47,7 @@ import java.util.Arrays; public class AndroidSenseMQTTHandler extends MQTTTransportHandler { private static final String TAG = "AndroidSenseMQTTHandler"; private static volatile AndroidSenseMQTTHandler mInstance; - + private Context context; /** * return a sigleton Instance @@ -54,6 +55,7 @@ public class AndroidSenseMQTTHandler extends MQTTTransportHandler { * @return AndroidSenseMQTTHandler. */ public static AndroidSenseMQTTHandler getInstance(Context context) { + context = context; if (mInstance == null) { Class clazz = AndroidSenseMQTTHandler.class; synchronized (clazz) { @@ -157,7 +159,7 @@ public class AndroidSenseMQTTHandler extends MQTTTransportHandler { */ @Override public void publishDeviceData(String... publishData) throws TransportHandlerException { - if (publishData.length != 3) { + if (publishData.length != 4) { String errorMsg = "Incorrect number of arguments received to SEND-MQTT Message. " + "Need to be [owner, deviceId, content]"; Log.e(TAG, errorMsg); @@ -169,7 +171,7 @@ public class AndroidSenseMQTTHandler extends MQTTTransportHandler { String resource = publishData[2]; MqttMessage pushMessage = new MqttMessage(); - String publishTopic = "wso2/" + SenseConstants.DEVICE_TYPE + "/" + deviceId + "/data"; + String publishTopic = publishData[3]; String actualMessage = resource; pushMessage.setPayload(actualMessage.getBytes(StandardCharsets.UTF_8)); pushMessage.setQos(DEFAULT_MQTT_QUALITY_OF_SERVICE); 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/mqtt/transport/MQTTTransportHandler.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/mqtt/transport/MQTTTransportHandler.java index 5fc5112d60..c2bd4b5d29 100644 --- 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/mqtt/transport/MQTTTransportHandler.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/mqtt/transport/MQTTTransportHandler.java @@ -77,9 +77,10 @@ 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 = "wso2/" + SenseConstants.DEVICE_TYPE + "/" + deviceId + "/command/#"; + this.subscribeTopic = "wso2/" + LocalRegistry.getTenantDomain(context)+ "/" + SenseConstants.DEVICE_TYPE + "/" + + deviceId + "/command/#"; this.clientWillTopic = DISCONNECTION_WILL_TOPIC_PREFIX + SenseConstants.DEVICE_TYPE; - this.mqttBrokerEndPoint = "tcp://" + LocalRegistry.getServerHost(context) + ":" + LocalRegistry.getMqttPort(context); + this.mqttBrokerEndPoint = LocalRegistry.getMqttEndpoint(context); this.timeoutInterval = DEFAULT_TIMEOUT_INTERVAL; this.initMQTTClient(); setUsernameAndPassword(LocalRegistry.getAccessToken(context), ""); 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/Sensor/SensorDataReader.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/Sensor/SensorDataReader.java index 0c872eaa61..f1d9445623 100644 --- 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/Sensor/SensorDataReader.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/Sensor/SensorDataReader.java @@ -70,7 +70,6 @@ public class SensorDataReader extends DataReader implements SensorEventListener } catch (Throwable e) { Log.d(TAG, "error on sensors"); } - } mSensorManager.unregisterListener(this); } 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/realtimeviewer/ActivitySelectSensor.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/realtimeviewer/ActivitySelectSensor.java index 2ceeba026d..433cb064d9 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/realtimeviewer/ActivitySelectSensor.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/realtimeviewer/ActivitySelectSensor.java @@ -182,7 +182,8 @@ public class ActivitySelectSensor extends AppCompatActivity LocalRegistry.removeServerURL(getApplicationContext()); LocalRegistry.removeAccessToken(getApplicationContext()); LocalRegistry.removeRefreshToken(getApplicationContext()); - LocalRegistry.removeMqttPort(getApplicationContext()); + LocalRegistry.removeMqttEndpoint(getApplicationContext()); + LocalRegistry.removeTenantDomain(getApplicationContext()); LocalRegistry.setExist(false); //Stop the current running background services. stopService(new Intent(this, SenseService.class)); //Stop sensor reading service 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/LocalRegistry.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/LocalRegistry.java index be6af804fe..8f26a91b99 100644 --- 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/LocalRegistry.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/LocalRegistry.java @@ -34,8 +34,9 @@ public class LocalRegistry { private static final String SERVER_HOST_KEY = "serverHostKey"; private static final String ACCESS_TOKEN_KEY = "accessTokenKey"; private static final String REFRESH_TOKEN_KEY = "refreshTokenKey"; - private static final String MQTT_PORT_KEY = "mqttPort"; - private static final String IS_ENROLLED_KEY = "enrolled"; + private static final String MQTT_ENDPOINT_KEY = "mqttEndpointKey"; + private static final String IS_ENROLLED_KEY = "enrolledKey"; + private static final String TENANT_DOMAIN_KEY = "tenantDomainKey"; private static boolean exists = false; private static String username; private static String deviceId; @@ -43,8 +44,9 @@ public class LocalRegistry { private static MQTTTransportHandler mqttTransportHandler; private static String accessToken; private static String refreshToken; - private static int mqttPort; + private static String mqttEndpoint; private static boolean enrolled; + private static String tenantDomain; public static boolean isExist(Context context) { if (!exists) { @@ -186,29 +188,29 @@ public class LocalRegistry { return LocalRegistry.refreshToken; } - public static void addMqttPort(Context context, int port) { + public static void addMqttEndpoint(Context context, String endpoint) { SharedPreferences sharedpreferences = context.getSharedPreferences(SENSE_SHARED_PREFERENCES, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putInt(MQTT_PORT_KEY, port); + editor.putString(MQTT_ENDPOINT_KEY, endpoint); editor.commit(); - LocalRegistry.mqttPort = port; + LocalRegistry.mqttEndpoint = endpoint; } - public static void removeMqttPort(Context context) { + public static void removeMqttEndpoint(Context context) { SharedPreferences sharedpreferences = context.getSharedPreferences(SENSE_SHARED_PREFERENCES, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.remove(MQTT_PORT_KEY); + editor.remove(MQTT_ENDPOINT_KEY); editor.clear(); editor.commit(); - LocalRegistry.mqttPort = 0; + LocalRegistry.mqttEndpoint = null; } - public static int getMqttPort(Context context) { - if (LocalRegistry.mqttPort == 0) { + public static String getMqttEndpoint(Context context) { + if (LocalRegistry.mqttEndpoint == null) { SharedPreferences sharedpreferences = context.getSharedPreferences(SENSE_SHARED_PREFERENCES, Context.MODE_PRIVATE); - LocalRegistry.mqttPort = sharedpreferences.getInt(MQTT_PORT_KEY, SenseConstants.MQTT_BROKER_PORT); + LocalRegistry.mqttEndpoint = sharedpreferences.getString(MQTT_ENDPOINT_KEY, ""); } - return LocalRegistry.mqttPort; + return LocalRegistry.mqttEndpoint; } public static void setEnrolled(Context context, boolean enrolled) { @@ -227,6 +229,31 @@ public class LocalRegistry { return LocalRegistry.enrolled; } + public static void addTenantDomain(Context context, String tenantDomain) { + SharedPreferences sharedpreferences = context.getSharedPreferences(SENSE_SHARED_PREFERENCES, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(TENANT_DOMAIN_KEY, tenantDomain); + editor.commit(); + LocalRegistry.tenantDomain = tenantDomain; + } + + public static void removeTenantDomain(Context context) { + SharedPreferences sharedpreferences = context.getSharedPreferences(SENSE_SHARED_PREFERENCES, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.remove(TENANT_DOMAIN_KEY); + editor.clear(); + editor.commit(); + LocalRegistry.tenantDomain = null; + } + + public static String getTenantDomain(Context context) { + if (LocalRegistry.tenantDomain == null) { + SharedPreferences sharedpreferences = context.getSharedPreferences(SENSE_SHARED_PREFERENCES, Context.MODE_PRIVATE); + LocalRegistry.tenantDomain = sharedpreferences.getString(TENANT_DOMAIN_KEY, ""); + } + return LocalRegistry.tenantDomain; + } + public static String getServerHost(Context context) { URL url = null; 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/SenseClient.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/SenseClient.java index 0572682852..b5d0bea502 100644 --- 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/SenseClient.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/SenseClient.java @@ -51,11 +51,7 @@ public class SenseClient { String responseStatus = response.get("status"); RegisterInfo registerInfo = new RegisterInfo(); if (responseStatus.trim().contains(SenseConstants.Request.REQUEST_SUCCESSFUL)) { - registerInfo.setMsg("Device Registered"); - registerInfo.setIsRegistered(true); - return registerInfo; - } else if (responseStatus.trim().contains(SenseConstants.Request.REQUEST_CONFLICT)) { - registerInfo.setMsg("Login Successful"); + registerInfo.setMsg("Login Succesful"); registerInfo.setIsRegistered(true); return registerInfo; } else { 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/SenseClientAsyncExecutor.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/SenseClientAsyncExecutor.java index c16759ee76..1d0e809cf5 100644 --- 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/SenseClientAsyncExecutor.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/SenseClientAsyncExecutor.java @@ -21,6 +21,7 @@ import android.util.Log; import org.wso2.carbon.iot.android.sense.constants.SenseConstants; import org.wso2.carbon.iot.android.sense.util.dto.AccessTokenInfo; +import org.wso2.carbon.iot.android.sense.util.dto.AndroidConfiguration; import org.wso2.carbon.iot.android.sense.util.dto.AndroidSenseManagerService; import org.wso2.carbon.iot.android.sense.util.dto.ApiApplicationRegistrationService; import org.wso2.carbon.iot.android.sense.util.dto.ApiRegistrationProfile; @@ -138,18 +139,16 @@ public class SenseClientAsyncExecutor extends AsyncTask + > @@ -43,32 +43,14 @@ android:inputType="text" android:maxLines="1" android:singleLine="true"/> - - - -