1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Mi2: Keep fetch activity data until data is from today

When the fetch operation finishes successfully, double check if
the last received data is from today. If it is older, fetch again.
Closes #611
This commit is contained in:
cpfeiffer 2017-05-15 00:38:26 +02:00
parent 7dc9c28c74
commit 0e4b9a4eb8

View File

@ -20,6 +20,8 @@ import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.util.TimeUtils;
import android.text.format.DateUtils;
import android.widget.Toast; import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -68,6 +70,7 @@ public class FetchActivityOperation extends AbstractMiBand2Operation {
private byte lastPacketCounter = -1; private byte lastPacketCounter = -1;
private Calendar startTimestamp; private Calendar startTimestamp;
private int fetchCount;
public FetchActivityOperation(MiBand2Support support) { public FetchActivityOperation(MiBand2Support support) {
super(support); super(support);
@ -83,12 +86,22 @@ public class FetchActivityOperation extends AbstractMiBand2Operation {
@Override @Override
protected void doPerform() throws IOException { protected void doPerform() throws IOException {
startFetching();
}
private void startFetching() throws IOException {
TransactionBuilder builder = performInitialized("fetching activity data"); TransactionBuilder builder = performInitialized("fetching activity data");
getSupport().setLowLatency(builder); getSupport().setLowLatency(builder);
builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext())); if (fetchCount == 0) {
builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext()));
}
fetchCount++;
BluetoothGattCharacteristic characteristicActivityData = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_5_ACTIVITY_DATA);
builder.notify(characteristicActivityData, false);
BluetoothGattCharacteristic characteristicFetch = getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC4); BluetoothGattCharacteristic characteristicFetch = getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC4);
builder.notify(characteristicFetch, true); builder.notify(characteristicFetch, true);
BluetoothGattCharacteristic characteristicActivityData = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_5_ACTIVITY_DATA);
GregorianCalendar sinceWhen = getLastSuccessfulSyncTime(); GregorianCalendar sinceWhen = getLastSuccessfulSyncTime();
builder.write(characteristicFetch, BLETypeConversions.join(new byte[] { MiBand2Service.COMMAND_ACTIVITY_DATA_START_DATE, 0x01 }, getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES))); builder.write(characteristicFetch, BLETypeConversions.join(new byte[] { MiBand2Service.COMMAND_ACTIVITY_DATA_START_DATE, 0x01 }, getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES)));
@ -136,13 +149,40 @@ public class FetchActivityOperation extends AbstractMiBand2Operation {
} }
private void handleActivityFetchFinish() { private void handleActivityFetchFinish() {
LOG.info("Fetching activity data has finished."); LOG.info("Fetching activity data has finished round " + fetchCount);
saveSamples(); GregorianCalendar lastSyncTimestamp = saveSamples();
if (needsAnotherFetch(lastSyncTimestamp)) {
try {
startFetching();
return;
} catch (IOException ex) {
LOG.error("Error starting another round of fetching activity data", ex);
}
}
operationFinished(); operationFinished();
unsetBusy(); unsetBusy();
} }
private void saveSamples() { private boolean needsAnotherFetch(GregorianCalendar lastSyncTimestamp) {
if (fetchCount > 5) {
LOG.warn("Already jave 5 fetch rounds, not doing another one.");
return false;
}
if (DateUtils.isToday(lastSyncTimestamp.getTimeInMillis())) {
LOG.info("Hopefully no further fetch needed, last synced timestamp is from today.");
return false;
}
if (lastSyncTimestamp.getTimeInMillis() > System.currentTimeMillis()) {
LOG.warn("Not doing another fetch since last synced timestamp is in the future: " + DateTimeUtils.formatDateTime(lastSyncTimestamp.getTime()));
return false;
}
LOG.info("Doing another fetch since last sync timestamp is still too old: " + DateTimeUtils.formatDateTime(lastSyncTimestamp.getTime()));
return true;
}
private GregorianCalendar saveSamples() {
if (samples.size() > 0) { if (samples.size() > 0) {
// save all the samples that we got // save all the samples that we got
try (DBHandler handler = GBApplication.acquireDB()) { try (DBHandler handler = GBApplication.acquireDB()) {
@ -168,6 +208,7 @@ public class FetchActivityOperation extends AbstractMiBand2Operation {
saveLastSyncTimestamp(timestamp); saveLastSyncTimestamp(timestamp);
LOG.info("Mi2 activity data: last sample timestamp: " + DateTimeUtils.formatDateTime(timestamp.getTime())); LOG.info("Mi2 activity data: last sample timestamp: " + DateTimeUtils.formatDateTime(timestamp.getTime()));
return timestamp;
} catch (Exception ex) { } catch (Exception ex) {
GB.toast(getContext(), "Error saving activity samples", Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "Error saving activity samples", Toast.LENGTH_LONG, GB.ERROR);
@ -175,6 +216,7 @@ public class FetchActivityOperation extends AbstractMiBand2Operation {
samples.clear(); samples.clear();
} }
} }
return null;
} }
/** /**