From 54a4cb09a387903640f5b95aea078f2f2cfc84bc Mon Sep 17 00:00:00 2001
From: Dominik Grzelak <dominik.grzelak@tu-dresden.de>
Date: Mon, 23 Jul 2018 10:40:04 +0200
Subject: [PATCH] user can now configure what sensor data should be send (mqtt
 topic): brightness and/or rotation

---
 .../inf/st/sensorsharing/MainActivity.java    | 60 +++++++++++++------
 mobile/src/main/res/layout/activity_main.xml  | 14 ++++-
 mobile/src/main/res/values/strings.xml        |  3 +-
 3 files changed, 56 insertions(+), 21 deletions(-)

diff --git a/mobile/src/main/java/de/tudresden/inf/st/sensorsharing/MainActivity.java b/mobile/src/main/java/de/tudresden/inf/st/sensorsharing/MainActivity.java
index a432107..b8d05c5 100644
--- a/mobile/src/main/java/de/tudresden/inf/st/sensorsharing/MainActivity.java
+++ b/mobile/src/main/java/de/tudresden/inf/st/sensorsharing/MainActivity.java
@@ -33,12 +33,12 @@ import org.eclipse.paho.client.mqttv3.MqttException;
 import org.eclipse.paho.client.mqttv3.MqttMessage;
 
 import java.nio.ByteBuffer;
-import java.nio.charset.Charset;
 import java.text.DateFormat;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.Locale;
 import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
 
 import de.tudresden.inf.st.sensorsharing.common.MqttUtils;
 import de.tudresden.inf.st.sensorsharing.common.Utils;
@@ -71,7 +71,7 @@ public class MainActivity extends AppCompatActivity implements
     private MqttTopics wearTopics;
     private MqttTopics phoneTopics;
     private int sensorChangedCounter = 0;
-    private boolean sendMqttUpdates;
+    private final ConcurrentHashMap<String, Boolean> sendMqttUpdates = new ConcurrentHashMap<>();
 
     // wearable management
     /** Delay in milliseconds after which the wearable is assumed to be offline */
@@ -95,8 +95,10 @@ public class MainActivity extends AppCompatActivity implements
 
         System.setProperty("log.tag." + TAG, "INFO");
 
+        initMqttClientId();
+        updateMqttTopics("unknown");
+
         settings = new AppSettings(this);
-        setName();
         initViews();
 
         // setup sensor manager and light sensor
@@ -141,14 +143,17 @@ public class MainActivity extends AppCompatActivity implements
         };
         handler.post(runnable);
 
-        setupSendUpdatesCheckbox();
-        updateMqttTopics("unknown");
+
     }
 
     private void initViews() {
+        setName();
+
         valueServerURI = findViewById(R.id.value_server_uri);
         String mqttServer = settings.get(SETTINGS_MQTT_SERVER, getResources().getString(R.string.default_mqtt_server_uri));
         valueServerURI.setText(mqttServer);
+
+        setupSendUpdatesCheckbox();
     }
 
     private void updateMqttTopics(String wearableName) {
@@ -164,19 +169,32 @@ public class MainActivity extends AppCompatActivity implements
                 subTopic = "polar";
         }
         wearTopics = new MqttTopics(subTopic);
-        // use fixed topic for phone for now
-        phoneTopics = new MqttTopics("samsung");
+        // use for phone model for now
+        phoneTopics = new MqttTopics(Build.DEVICE);
     }
 
     private void setupSendUpdatesCheckbox() {
-        final CheckBox sendUpdates = findViewById(R.id.checkBox_send_updates);
-        sendMqttUpdates = sendUpdates.isChecked();
-        sendUpdates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+        final CheckBox sendUpdates = findViewById(R.id.checkBox_send_brightness);
+        final CheckBox sendUpdatesRotation = findViewById(R.id.checkBox_send_rotation_ahrs);
+
+        sendMqttUpdates.put(phoneTopics.mqtt_topic_brightness, sendUpdates.isChecked());
+        sendMqttUpdates.put(phoneTopics.mqtt_topic_rotation, sendUpdatesRotation.isChecked());
+
+        CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
-                sendMqttUpdates = isChecked;
+                switch (compoundButton.getId()) {
+                    case R.id.checkBox_send_brightness:
+                        sendMqttUpdates.put(phoneTopics.mqtt_topic_brightness, isChecked);
+                        break;
+                    case R.id.checkBox_send_rotation_ahrs:
+                        sendMqttUpdates.put(phoneTopics.mqtt_topic_rotation, sendUpdatesRotation.isChecked());
+                        break;
+                }
             }
-        });
+        };
+        sendUpdates.setOnCheckedChangeListener(onCheckedChangeListener);
+        sendUpdatesRotation.setOnCheckedChangeListener(onCheckedChangeListener);
     }
 
     private void setName() {
@@ -299,9 +317,7 @@ public class MainActivity extends AppCompatActivity implements
     @Override
     protected void onResume() {
         super.onResume();
-        if(clientId == null || clientId.isEmpty()) {
-            clientId = MqttUtils.createClientId();
-        }
+        initMqttClientId();
         mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_UI);
         mSensorManager.registerListener(this, mRotationVector, SensorManager.SENSOR_DELAY_UI);
         Wearable.getMessageClient(this).addListener(this);
@@ -314,6 +330,12 @@ public class MainActivity extends AppCompatActivity implements
         Wearable.getMessageClient(this).removeListener(this);
     }
 
+    private void initMqttClientId() {
+        if(clientId == null || clientId.isEmpty()) {
+            clientId = MqttUtils.createClientId();
+        }
+    }
+
     @Override
     public void onMessageReceived(@NonNull final MessageEvent messageEvent) {
         String path = messageEvent.getPath();
@@ -396,9 +418,11 @@ public class MainActivity extends AppCompatActivity implements
         sendUpdate(topic, newValue);
     }
 
+    /**
+     * Method to initiate the mqtt publish action of the required sensor data.
+     * Whether the data is really published depends on the checkboxes.
+     */
     private void sendUpdateOfSmartphone() {
-//        sendUpdate(phoneTopics.mqtt_topic_brightness, ByteBuffer.allocate(4).putFloat(mLightValue).array());
-//        sendUpdate(phoneTopics.mqtt_topic_rotation, Utils.floatArray2ByteArray(mRotationVectorValues));
         sendUpdate(phoneTopics.mqtt_topic_brightness, Float.toString(mLightValue));
         sendUpdate(phoneTopics.mqtt_topic_rotation, Arrays.toString(mRotationVectorValues));
     }
@@ -408,7 +432,7 @@ public class MainActivity extends AppCompatActivity implements
     }
 
     private void sendUpdate(String topic, byte[] bytes) {
-        if (!sendMqttUpdates) {
+        if (sendMqttUpdates.get(topic) == null || !sendMqttUpdates.get(topic)) {
             return;
         }
         if (mqttAndroidClient == null) {
diff --git a/mobile/src/main/res/layout/activity_main.xml b/mobile/src/main/res/layout/activity_main.xml
index 88f3342..2bfe8b0 100644
--- a/mobile/src/main/res/layout/activity_main.xml
+++ b/mobile/src/main/res/layout/activity_main.xml
@@ -235,15 +235,25 @@
         app:layout_constraintTop_toBottomOf="@+id/separator_wear" />
 
     <CheckBox
-        android:id="@+id/checkBox_send_updates"
+        android:id="@+id/checkBox_send_brightness"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginStart="8dp"
         android:layout_marginTop="16dp"
-        android:text="@string/text_send_updates"
+        android:text="@string/text_send_updates_brightness"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/label_server_uri" />
 
+    <CheckBox
+        android:id="@+id/checkBox_send_rotation_ahrs"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="8dp"
+        android:layout_marginTop="16dp"
+        android:text="@string/text_send_updates_rotationahrs"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/checkBox_send_brightness" />
+
     <TextView
         android:id="@+id/label_wear_status"
         android:layout_width="wrap_content"
diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml
index 6ff8406..1224f8a 100644
--- a/mobile/src/main/res/values/strings.xml
+++ b/mobile/src/main/res/values/strings.xml
@@ -10,5 +10,6 @@
     <string name="text_separator_smartphone_values">Smartphone Values</string>
     <string name="text_separator_wear_values">Wear Values</string>
     <string name="text_separator_mqtt_settings">MQTT Settings</string>
-    <string name="text_send_updates">Send updates</string>
+    <string name="text_send_updates_brightness">Send Updates Brightness</string>
+    <string name="text_send_updates_rotationahrs">Send Updates Rotation (AHRS)</string>
 </resources>
-- 
GitLab