Skip to content
Snippets Groups Projects
Commit 54a4cb09 authored by Dominik Grzelak's avatar Dominik Grzelak
Browse files

user can now configure what sensor data should be send (mqtt topic): brightness and/or rotation

parent c72a53d1
Branches
No related tags found
No related merge requests found
...@@ -33,12 +33,12 @@ import org.eclipse.paho.client.mqttv3.MqttException; ...@@ -33,12 +33,12 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.MqttMessage;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import de.tudresden.inf.st.sensorsharing.common.MqttUtils; import de.tudresden.inf.st.sensorsharing.common.MqttUtils;
import de.tudresden.inf.st.sensorsharing.common.Utils; import de.tudresden.inf.st.sensorsharing.common.Utils;
...@@ -71,7 +71,7 @@ public class MainActivity extends AppCompatActivity implements ...@@ -71,7 +71,7 @@ public class MainActivity extends AppCompatActivity implements
private MqttTopics wearTopics; private MqttTopics wearTopics;
private MqttTopics phoneTopics; private MqttTopics phoneTopics;
private int sensorChangedCounter = 0; private int sensorChangedCounter = 0;
private boolean sendMqttUpdates; private final ConcurrentHashMap<String, Boolean> sendMqttUpdates = new ConcurrentHashMap<>();
// wearable management // wearable management
/** Delay in milliseconds after which the wearable is assumed to be offline */ /** Delay in milliseconds after which the wearable is assumed to be offline */
...@@ -95,8 +95,10 @@ public class MainActivity extends AppCompatActivity implements ...@@ -95,8 +95,10 @@ public class MainActivity extends AppCompatActivity implements
System.setProperty("log.tag." + TAG, "INFO"); System.setProperty("log.tag." + TAG, "INFO");
initMqttClientId();
updateMqttTopics("unknown");
settings = new AppSettings(this); settings = new AppSettings(this);
setName();
initViews(); initViews();
// setup sensor manager and light sensor // setup sensor manager and light sensor
...@@ -141,14 +143,17 @@ public class MainActivity extends AppCompatActivity implements ...@@ -141,14 +143,17 @@ public class MainActivity extends AppCompatActivity implements
}; };
handler.post(runnable); handler.post(runnable);
setupSendUpdatesCheckbox();
updateMqttTopics("unknown");
} }
private void initViews() { private void initViews() {
setName();
valueServerURI = findViewById(R.id.value_server_uri); valueServerURI = findViewById(R.id.value_server_uri);
String mqttServer = settings.get(SETTINGS_MQTT_SERVER, getResources().getString(R.string.default_mqtt_server_uri)); String mqttServer = settings.get(SETTINGS_MQTT_SERVER, getResources().getString(R.string.default_mqtt_server_uri));
valueServerURI.setText(mqttServer); valueServerURI.setText(mqttServer);
setupSendUpdatesCheckbox();
} }
private void updateMqttTopics(String wearableName) { private void updateMqttTopics(String wearableName) {
...@@ -164,19 +169,32 @@ public class MainActivity extends AppCompatActivity implements ...@@ -164,19 +169,32 @@ public class MainActivity extends AppCompatActivity implements
subTopic = "polar"; subTopic = "polar";
} }
wearTopics = new MqttTopics(subTopic); wearTopics = new MqttTopics(subTopic);
// use fixed topic for phone for now // use for phone model for now
phoneTopics = new MqttTopics("samsung"); phoneTopics = new MqttTopics(Build.DEVICE);
} }
private void setupSendUpdatesCheckbox() { private void setupSendUpdatesCheckbox() {
final CheckBox sendUpdates = findViewById(R.id.checkBox_send_updates); final CheckBox sendUpdates = findViewById(R.id.checkBox_send_brightness);
sendMqttUpdates = sendUpdates.isChecked(); final CheckBox sendUpdatesRotation = findViewById(R.id.checkBox_send_rotation_ahrs);
sendUpdates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
sendMqttUpdates.put(phoneTopics.mqtt_topic_brightness, sendUpdates.isChecked());
sendMqttUpdates.put(phoneTopics.mqtt_topic_rotation, sendUpdatesRotation.isChecked());
CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 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() { private void setName() {
...@@ -299,9 +317,7 @@ public class MainActivity extends AppCompatActivity implements ...@@ -299,9 +317,7 @@ public class MainActivity extends AppCompatActivity implements
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if(clientId == null || clientId.isEmpty()) { initMqttClientId();
clientId = MqttUtils.createClientId();
}
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, mRotationVector, SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, mRotationVector, SensorManager.SENSOR_DELAY_UI);
Wearable.getMessageClient(this).addListener(this); Wearable.getMessageClient(this).addListener(this);
...@@ -314,6 +330,12 @@ public class MainActivity extends AppCompatActivity implements ...@@ -314,6 +330,12 @@ public class MainActivity extends AppCompatActivity implements
Wearable.getMessageClient(this).removeListener(this); Wearable.getMessageClient(this).removeListener(this);
} }
private void initMqttClientId() {
if(clientId == null || clientId.isEmpty()) {
clientId = MqttUtils.createClientId();
}
}
@Override @Override
public void onMessageReceived(@NonNull final MessageEvent messageEvent) { public void onMessageReceived(@NonNull final MessageEvent messageEvent) {
String path = messageEvent.getPath(); String path = messageEvent.getPath();
...@@ -396,9 +418,11 @@ public class MainActivity extends AppCompatActivity implements ...@@ -396,9 +418,11 @@ public class MainActivity extends AppCompatActivity implements
sendUpdate(topic, newValue); 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() { 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_brightness, Float.toString(mLightValue));
sendUpdate(phoneTopics.mqtt_topic_rotation, Arrays.toString(mRotationVectorValues)); sendUpdate(phoneTopics.mqtt_topic_rotation, Arrays.toString(mRotationVectorValues));
} }
...@@ -408,7 +432,7 @@ public class MainActivity extends AppCompatActivity implements ...@@ -408,7 +432,7 @@ public class MainActivity extends AppCompatActivity implements
} }
private void sendUpdate(String topic, byte[] bytes) { private void sendUpdate(String topic, byte[] bytes) {
if (!sendMqttUpdates) { if (sendMqttUpdates.get(topic) == null || !sendMqttUpdates.get(topic)) {
return; return;
} }
if (mqttAndroidClient == null) { if (mqttAndroidClient == null) {
......
...@@ -235,15 +235,25 @@ ...@@ -235,15 +235,25 @@
app:layout_constraintTop_toBottomOf="@+id/separator_wear" /> app:layout_constraintTop_toBottomOf="@+id/separator_wear" />
<CheckBox <CheckBox
android:id="@+id/checkBox_send_updates" android:id="@+id/checkBox_send_brightness"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:text="@string/text_send_updates" android:text="@string/text_send_updates_brightness"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/label_server_uri" /> 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 <TextView
android:id="@+id/label_wear_status" android:id="@+id/label_wear_status"
android:layout_width="wrap_content" android:layout_width="wrap_content"
......
...@@ -10,5 +10,6 @@ ...@@ -10,5 +10,6 @@
<string name="text_separator_smartphone_values">Smartphone Values</string> <string name="text_separator_smartphone_values">Smartphone Values</string>
<string name="text_separator_wear_values">Wear Values</string> <string name="text_separator_wear_values">Wear Values</string>
<string name="text_separator_mqtt_settings">MQTT Settings</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> </resources>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment