From 7670e56ac56c4b4fc0887de5ce583114e54c1dde Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Mon, 11 Feb 2019 22:23:15 +0100 Subject: [PATCH] Do delete saved measurements as a separate step Hopefully this fixes #398, but note that the code is untested. --- .../bluetooth/BluetoothBeurerSanitas.java | 46 ++++++++++++------- .../bluetooth/BluetoothCommunication.java | 18 +++++--- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java index 12e67fa5..cf094fc5 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java @@ -213,14 +213,25 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { Timber.d("Request saved measurements for %s", currentRemoteUser.name); sendCommand(CMD_GET_SAVED_MEASUREMENTS, encodeUserId(currentRemoteUser)); - // Return to this state until all users have been processed - repeatMachineStateStep(); + // Wait for all measurements to be received stopMachineState(); - } else { + } else { nextMachineStateStep(); } break; case 6: + if (currentRemoteUser != null) { + Timber.d("Deleting saved measurements for %s", currentRemoteUser.name); + sendCommand(CMD_DELETE_SAVED_MEASUREMENTS, encodeUserId(currentRemoteUser)); + + // Return to the previous state until all users have been processed + repeatMachineStateSteps(2); + stopMachineState(); + } else { + nextMachineStateStep(); + } + break; + case 7: // Create a remote user for selected openScale user if needed currentRemoteUser = null; final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser(); @@ -237,7 +248,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { nextMachineStateStep(); } break; - case 7: + case 8: sendCommand(CMD_USER_DETAILS, encodeUserId(currentRemoteUser)); stopMachineState(); break; @@ -292,7 +303,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { if (data[0] == getAlternativeStartByte(ID_START_NIBBLE_INIT)) { Timber.d("Got init ack from scale; scale is ready"); - resumeMachineState(); + resumeMachineState(true); return; } @@ -366,7 +377,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { } // All users received - resumeMachineState(); + resumeMachineState(true); } private void processMeasurementData(byte[] data, int offset, boolean firstPart) { @@ -390,11 +401,11 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { int current = data[3] & 0xFF; processMeasurementData(data, 4, current % 2 == 1); - sendAck(data); - if (current == count) { - sendCommand(CMD_DELETE_SAVED_MEASUREMENTS, encodeUserId(currentRemoteUser)); + // Resume but don't do next step until ACK has been sent + resumeMachineState(false); } + sendAck(data); } private void processWeightMeasurement(byte[] data) { @@ -472,7 +483,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { Timber.d("Set scale unit to %s (%d)", user.getScaleUnit(), requestedUnit); sendCommand(CMD_SET_UNIT, requestedUnit); } else { - resumeMachineState(); + resumeMachineState(true); } break; @@ -480,7 +491,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { if (data[3] == 0) { Timber.d("Scale unit successfully set"); } - resumeMachineState(); + resumeMachineState(true); break; case CMD_USER_LIST: @@ -488,7 +499,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { int maxUserCount = data[5] & 0xFF; Timber.d("Have %d users (max is %d)", userCount, maxUserCount); if (userCount == 0) { - resumeMachineState(); + resumeMachineState(true); } // Otherwise wait for CMD_USER_INFO notifications break; @@ -496,18 +507,19 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { case CMD_GET_SAVED_MEASUREMENTS: int measurementCount = data[3] & 0xFF; if (measurementCount == 0) { - resumeMachineState(); + // Skip delete all measurements step (since there are no measurements to delete) + repeatMachineStateSteps(1); + resumeMachineState(true); } // Otherwise wait for CMD_SAVED_MEASUREMENT notifications which will, - // once all measurements have been received, trigger a call to delete them. - // Once the ack for that is received, we resume the state machine (see below). + // once all measurements have been received, resume the state machine. break; case CMD_DELETE_SAVED_MEASUREMENTS: if (data[3] == 0) { Timber.d("Saved measurements successfully deleted"); } - resumeMachineState(); + resumeMachineState(true); break; case CMD_USER_ADD: @@ -544,7 +556,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { Timber.d("Name: %s, Birthday: %d-%02d-%02d, Height: %d, Sex: %s, activity: %d", name, year, month, day, height, male ? "male" : "female", activity); } - resumeMachineState(); + resumeMachineState(true); break; default: diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java index 1b62548c..b952c115 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java @@ -207,18 +207,20 @@ public abstract class BluetoothCommunication { abstract protected boolean nextBluetoothCmd(int stateNr); /** - * Step the current machine state one step back. Needs to be called before a command + * Step the current machine state backwards. Needs to be called before a command. + * + * @param steps Number of steps to back the machine. */ - protected void repeatMachineStateStep() { + protected void repeatMachineStateSteps(int steps) { switch (btMachineState) { case BT_INIT_STATE: - initStepNr = initStepNr - 1; + initStepNr = initStepNr - steps; break; case BT_CMD_STATE: - cmdStepNr = cmdStepNr - 1; + cmdStepNr = cmdStepNr - steps; break; case BT_CLEANUP_STATE: - cleanupStepNr = cleanupStepNr - 1; + cleanupStepNr = cleanupStepNr - steps; break; } } @@ -235,11 +237,13 @@ public abstract class BluetoothCommunication { /** * Resumed the current machine state */ - protected void resumeMachineState() { + protected void resumeMachineState(boolean doNextStep) { Timber.d("Machine state resumed"); btMachineState = btStopppedMachineState; - nextMachineStateStep(); + if (doNextStep) { + nextMachineStateStep(); + } } /**