mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-18 06:21:25 +02:00
Do delete saved measurements as a separate step
Hopefully this fixes #398, but note that the code is untested.
This commit is contained in:
@@ -213,14 +213,25 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
|
|||||||
Timber.d("Request saved measurements for %s", currentRemoteUser.name);
|
Timber.d("Request saved measurements for %s", currentRemoteUser.name);
|
||||||
sendCommand(CMD_GET_SAVED_MEASUREMENTS, encodeUserId(currentRemoteUser));
|
sendCommand(CMD_GET_SAVED_MEASUREMENTS, encodeUserId(currentRemoteUser));
|
||||||
|
|
||||||
// Return to this state until all users have been processed
|
// Wait for all measurements to be received
|
||||||
repeatMachineStateStep();
|
|
||||||
stopMachineState();
|
stopMachineState();
|
||||||
} else {
|
} else {
|
||||||
nextMachineStateStep();
|
nextMachineStateStep();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 6:
|
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
|
// Create a remote user for selected openScale user if needed
|
||||||
currentRemoteUser = null;
|
currentRemoteUser = null;
|
||||||
final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser();
|
final ScaleUser selectedUser = OpenScale.getInstance().getSelectedScaleUser();
|
||||||
@@ -237,7 +248,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
|
|||||||
nextMachineStateStep();
|
nextMachineStateStep();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 8:
|
||||||
sendCommand(CMD_USER_DETAILS, encodeUserId(currentRemoteUser));
|
sendCommand(CMD_USER_DETAILS, encodeUserId(currentRemoteUser));
|
||||||
stopMachineState();
|
stopMachineState();
|
||||||
break;
|
break;
|
||||||
@@ -292,7 +303,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
|
|||||||
|
|
||||||
if (data[0] == getAlternativeStartByte(ID_START_NIBBLE_INIT)) {
|
if (data[0] == getAlternativeStartByte(ID_START_NIBBLE_INIT)) {
|
||||||
Timber.d("Got init ack from scale; scale is ready");
|
Timber.d("Got init ack from scale; scale is ready");
|
||||||
resumeMachineState();
|
resumeMachineState(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,7 +377,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// All users received
|
// All users received
|
||||||
resumeMachineState();
|
resumeMachineState(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processMeasurementData(byte[] data, int offset, boolean firstPart) {
|
private void processMeasurementData(byte[] data, int offset, boolean firstPart) {
|
||||||
@@ -390,11 +401,11 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
|
|||||||
int current = data[3] & 0xFF;
|
int current = data[3] & 0xFF;
|
||||||
|
|
||||||
processMeasurementData(data, 4, current % 2 == 1);
|
processMeasurementData(data, 4, current % 2 == 1);
|
||||||
sendAck(data);
|
|
||||||
|
|
||||||
if (current == count) {
|
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) {
|
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);
|
Timber.d("Set scale unit to %s (%d)", user.getScaleUnit(), requestedUnit);
|
||||||
sendCommand(CMD_SET_UNIT, requestedUnit);
|
sendCommand(CMD_SET_UNIT, requestedUnit);
|
||||||
} else {
|
} else {
|
||||||
resumeMachineState();
|
resumeMachineState(true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -480,7 +491,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
|
|||||||
if (data[3] == 0) {
|
if (data[3] == 0) {
|
||||||
Timber.d("Scale unit successfully set");
|
Timber.d("Scale unit successfully set");
|
||||||
}
|
}
|
||||||
resumeMachineState();
|
resumeMachineState(true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CMD_USER_LIST:
|
case CMD_USER_LIST:
|
||||||
@@ -488,7 +499,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
|
|||||||
int maxUserCount = data[5] & 0xFF;
|
int maxUserCount = data[5] & 0xFF;
|
||||||
Timber.d("Have %d users (max is %d)", userCount, maxUserCount);
|
Timber.d("Have %d users (max is %d)", userCount, maxUserCount);
|
||||||
if (userCount == 0) {
|
if (userCount == 0) {
|
||||||
resumeMachineState();
|
resumeMachineState(true);
|
||||||
}
|
}
|
||||||
// Otherwise wait for CMD_USER_INFO notifications
|
// Otherwise wait for CMD_USER_INFO notifications
|
||||||
break;
|
break;
|
||||||
@@ -496,18 +507,19 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication {
|
|||||||
case CMD_GET_SAVED_MEASUREMENTS:
|
case CMD_GET_SAVED_MEASUREMENTS:
|
||||||
int measurementCount = data[3] & 0xFF;
|
int measurementCount = data[3] & 0xFF;
|
||||||
if (measurementCount == 0) {
|
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,
|
// Otherwise wait for CMD_SAVED_MEASUREMENT notifications which will,
|
||||||
// once all measurements have been received, trigger a call to delete them.
|
// once all measurements have been received, resume the state machine.
|
||||||
// Once the ack for that is received, we resume the state machine (see below).
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CMD_DELETE_SAVED_MEASUREMENTS:
|
case CMD_DELETE_SAVED_MEASUREMENTS:
|
||||||
if (data[3] == 0) {
|
if (data[3] == 0) {
|
||||||
Timber.d("Saved measurements successfully deleted");
|
Timber.d("Saved measurements successfully deleted");
|
||||||
}
|
}
|
||||||
resumeMachineState();
|
resumeMachineState(true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CMD_USER_ADD:
|
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",
|
Timber.d("Name: %s, Birthday: %d-%02d-%02d, Height: %d, Sex: %s, activity: %d",
|
||||||
name, year, month, day, height, male ? "male" : "female", activity);
|
name, year, month, day, height, male ? "male" : "female", activity);
|
||||||
}
|
}
|
||||||
resumeMachineState();
|
resumeMachineState(true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@@ -207,18 +207,20 @@ public abstract class BluetoothCommunication {
|
|||||||
abstract protected boolean nextBluetoothCmd(int stateNr);
|
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) {
|
switch (btMachineState) {
|
||||||
case BT_INIT_STATE:
|
case BT_INIT_STATE:
|
||||||
initStepNr = initStepNr - 1;
|
initStepNr = initStepNr - steps;
|
||||||
break;
|
break;
|
||||||
case BT_CMD_STATE:
|
case BT_CMD_STATE:
|
||||||
cmdStepNr = cmdStepNr - 1;
|
cmdStepNr = cmdStepNr - steps;
|
||||||
break;
|
break;
|
||||||
case BT_CLEANUP_STATE:
|
case BT_CLEANUP_STATE:
|
||||||
cleanupStepNr = cleanupStepNr - 1;
|
cleanupStepNr = cleanupStepNr - steps;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -235,11 +237,13 @@ public abstract class BluetoothCommunication {
|
|||||||
/**
|
/**
|
||||||
* Resumed the current machine state
|
* Resumed the current machine state
|
||||||
*/
|
*/
|
||||||
protected void resumeMachineState() {
|
protected void resumeMachineState(boolean doNextStep) {
|
||||||
Timber.d("Machine state resumed");
|
Timber.d("Machine state resumed");
|
||||||
btMachineState = btStopppedMachineState;
|
btMachineState = btStopppedMachineState;
|
||||||
|
|
||||||
nextMachineStateStep();
|
if (doNextStep) {
|
||||||
|
nextMachineStateStep();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user