Skip to content
Snippets Groups Projects
Commit 4ae33e6b authored by René Schöne's avatar René Schöne
Browse files

Wear status and mqtt topics by wearable name.

parent d46b0514
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
......@@ -49,10 +50,6 @@ public class MainActivity extends AppCompatActivity implements
MessageClient.OnMessageReceivedListener {
private static final String TAG = "SensorSharing";
private static final String MQTT_TOPIC_POLAR_BASE = "sensors/polar/";
private static final String MQTT_TOPIC_POLAR_BRIGHTNESS = MQTT_TOPIC_POLAR_BASE + "brightness";
private static final String MQTT_TOPIC_POLAR_ACCELERATION = MQTT_TOPIC_POLAR_BASE + "acceleration";
private static final String MQTT_TOPIC_POLAR_ROTATION = MQTT_TOPIC_POLAR_BASE + "rotation";
private static final String MQTT_TOPIC_SAMSUNG = "sensors/samsung/brightness";
private static final int MQTT_DEFAULT_QOS = 0;
/** Delay in milliseconds after which the wearable is assumed to be offline */
......@@ -63,9 +60,11 @@ public class MainActivity extends AppCompatActivity implements
private TextView mValueWearBrightness;
private MqttAndroidClient mqttAndroidClient;
private int sensorChangedCounter = 0;
private long lastMessageFromWearable;
private Date lastMessageFromWearable;
private DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(
DateFormat.SHORT, android.icu.text.DateFormat.SHORT);
private boolean sendMqttUpdates;
private MqttTopics wearTopics;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -85,22 +84,52 @@ public class MainActivity extends AppCompatActivity implements
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
mValueWearBrightness = findViewById(R.id.value_wear_brightness);
setChecked(false);
updateWearStatus(false);
connectMqtt(null);
// repeating connectivity check
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
long now = System.currentTimeMillis();
if (now - lastMessageFromWearable > DELAY_WEARABLE_ASSUMED_OFFLINE) {
setChecked(false);
if (now - lastMessageFromWearable.getTime() > DELAY_WEARABLE_ASSUMED_OFFLINE) {
updateWearStatus(false);
}
// repeat after 30 seconds
// repeat after delay
handler.postDelayed(this, DELAY_WEARABLE_ASSUMED_OFFLINE);
}
};
handler.post(runnable);
setupSendUpdatesCheckbox();
updateMqttTopics("unknown");
}
private void updateMqttTopics(String wearableName) {
// moto: "Moto 360"
// polar: ??
String subTopic;
switch (wearableName) {
case "Moto 360":
subTopic = "moto360";
break;
default:
// should be a specific name for the Polar, once the app runs again
subTopic = "polar";
}
wearTopics = new MqttTopics(subTopic);
}
private void setupSendUpdatesCheckbox() {
final CheckBox sendUpdates = findViewById(R.id.checkBox_send_updates);
sendMqttUpdates = sendUpdates.isChecked();
sendUpdates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
sendMqttUpdates = isChecked;
}
});
}
private void setName() {
......@@ -167,9 +196,15 @@ public class MainActivity extends AppCompatActivity implements
}
}
private void setChecked(boolean checked) {
CheckBox checkBox = findViewById(R.id.checkBox);
checkBox.setChecked(checked);
private void updateWearStatus(boolean connected) {
String text;
if (connected) {
text = "Connected (last contact: %s)";
} else {
text = "Disconnected since %s";
}
TextView wearStatus = findViewById(R.id.value_wear_status);
wearStatus.setText(String.format(text, dateTimeFormat.format(lastMessageFromWearable)));
}
@Override
......@@ -209,8 +244,7 @@ public class MainActivity extends AppCompatActivity implements
String path = messageEvent.getPath();
if (path == null) return;
if (path.startsWith(BASE_KEY)) {
final Date contactTime = new Date();
lastMessageFromWearable = contactTime.getTime();
lastMessageFromWearable = new Date();
final String updatedValue;
String topic = null;
final TextView textViewToUpdate;
......@@ -220,7 +254,7 @@ public class MainActivity extends AppCompatActivity implements
float mWearBrightness = ByteBuffer.wrap(messageEvent.getData()).getFloat();
updatedValue = Float.toString(mWearBrightness);
textViewToUpdate = mValueWearBrightness;
topic = MQTT_TOPIC_POLAR_BRIGHTNESS;
topic = wearTopics.mqtt_topic_brightness;
break;
case SUB_KEY_LINEAR_ACCELERATION:
// float[] accelerationData = new float[3];
......@@ -228,7 +262,7 @@ public class MainActivity extends AppCompatActivity implements
// updatedValue = Arrays.toString(accelerationData);
updatedValue = formatByteFloatArray(ByteBuffer.wrap(messageEvent.getData()));
textViewToUpdate = findViewById(R.id.value_wear_acceleration);
topic = MQTT_TOPIC_POLAR_ACCELERATION;
topic = wearTopics.mqtt_topic_acceleration;
break;
case SUB_KEY_ROTATION_VECTOR:
// float[] rotationData = new float[3];
......@@ -236,11 +270,12 @@ public class MainActivity extends AppCompatActivity implements
// updatedValue = Arrays.toString(rotationData);
updatedValue = formatByteFloatArray(ByteBuffer.wrap(messageEvent.getData()));
textViewToUpdate = findViewById(R.id.value_wear_rotation);
topic = MQTT_TOPIC_POLAR_ROTATION;
topic = wearTopics.mqtt_topic_rotation;
break;
case SUB_KEY_NAME:
updatedValue = new String(messageEvent.getData(), MQTT_CHARSET);
textViewToUpdate = findViewById(R.id.value_wear_name);
updateMqttTopics(updatedValue);
break;
default:
Log.w(TAG, "Unknown path: " + path);
......@@ -249,9 +284,7 @@ public class MainActivity extends AppCompatActivity implements
runOnUiThread(new Runnable() {
@Override
public void run() {
setChecked(true);
TextView lastContact = findViewById(R.id.value_last_contact);
lastContact.setText(dateTimeFormat.format(contactTime));
updateWearStatus(true);
textViewToUpdate.setText(updatedValue);
}
......@@ -291,6 +324,9 @@ public class MainActivity extends AppCompatActivity implements
}
private void sendUpdate(String topic, String newValue) {
if (!sendMqttUpdates) {
return;
}
if (mqttAndroidClient == null) {
Log.d(TAG, "mqtt client is null");
return;
......
package de.tudresden.inf.st.sensorsharing;
public class MqttTopics {
private static final String MQTT_TOPIC_BASE = "sensors/";
public final String mqtt_topic_brightness;
public final String mqtt_topic_acceleration;
public final String mqtt_topic_rotation;
public MqttTopics(String subTopic) {
mqtt_topic_brightness = MQTT_TOPIC_BASE + subTopic + "/brightness";
mqtt_topic_acceleration = MQTT_TOPIC_BASE + subTopic + "/acceleration";
mqtt_topic_rotation = MQTT_TOPIC_BASE + subTopic + "/rotation";
}
}
......@@ -10,12 +10,12 @@
android:id="@+id/label_wear_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/text_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
app:layout_constraintTop_toBottomOf="@+id/label_wear_status" />
<TextView
android:id="@+id/value_wear_name"
......@@ -25,7 +25,7 @@
android:layout_marginTop="8dp"
android:text="@android:string/unknownName"
app:layout_constraintStart_toEndOf="@+id/label_wear_name"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
app:layout_constraintTop_toBottomOf="@+id/label_wear_status" />
<TextView
android:id="@+id/label_own_name"
......@@ -113,7 +113,7 @@
android:id="@+id/label_wear_brightness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/text_wear_brightness"
app:layout_constraintStart_toStartOf="parent"
......@@ -123,7 +123,7 @@
android:id="@+id/label_wear_acceleration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/text_wear_acceleration"
app:layout_constraintStart_toStartOf="parent"
......@@ -133,7 +133,7 @@
android:id="@+id/label_wear_rotation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/text_wear_rotation"
app:layout_constraintStart_toStartOf="parent"
......@@ -179,19 +179,6 @@
app:layout_constraintStart_toEndOf="@+id/label_wear_rotation"
app:layout_constraintTop_toBottomOf="@+id/label_wear_acceleration" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:checked="false"
android:clickable="false"
android:duplicateParentState="false"
android:text="@string/text_connected"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/separator_wear" />
<EditText
android:id="@+id/value_server_uri"
android:layout_width="128dp"
......@@ -218,13 +205,33 @@
app:layout_constraintTop_toBottomOf="@+id/separator_mqtt" />
<TextView
android:id="@+id/value_last_contact"
android:id="@+id/value_wear_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginTop="8dp"
android:text="@android:string/unknownName"
app:layout_constraintStart_toEndOf="@+id/checkBox"
app:layout_constraintStart_toEndOf="@+id/label_wear_status"
app:layout_constraintTop_toBottomOf="@+id/separator_wear" />
<CheckBox
android:id="@+id/checkBox_send_updates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="@string/text_send_updates"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/label_server_uri" />
<TextView
android:id="@+id/label_wear_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Status"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/separator_wear" />
</android.support.constraint.ConstraintLayout>
......@@ -11,4 +11,5 @@
<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>
</resources>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment