mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-01-27 18:17:33 +01:00
Use batched database commits for Mi Band activity samples
This commit is contained in:
parent
d4f070f0aa
commit
952a383856
@ -15,9 +15,6 @@ import java.text.DateFormat;
|
|||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
//import java.util.concurrent.Executors;
|
|
||||||
//import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
//import java.util.concurrent.ScheduledFuture;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
@ -26,7 +23,10 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter;
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEOperation;
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEOperation;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction;
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction;
|
||||||
@ -34,6 +34,10 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
|
||||||
|
//import java.util.concurrent.Executors;
|
||||||
|
//import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
//import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An operation that fetches activity data. For every fetch, a new operation must
|
* An operation that fetches activity data. For every fetch, a new operation must
|
||||||
* be created, i.e. an operation may not be reused for multiple fetches.
|
* be created, i.e. an operation may not be reused for multiple fetches.
|
||||||
@ -300,21 +304,31 @@ public class FetchActivityOperation extends AbstractBTLEOperation<MiBandSupport>
|
|||||||
int minutes = 0;
|
int minutes = 0;
|
||||||
try (SQLiteDatabase db = dbHandler.getWritableDatabase()) { // explicitly keep the db open while looping over the samples
|
try (SQLiteDatabase db = dbHandler.getWritableDatabase()) { // explicitly keep the db open while looping over the samples
|
||||||
int timestampInSeconds = (int) (activityStruct.activityDataTimestampProgress.getTimeInMillis() / 1000);
|
int timestampInSeconds = (int) (activityStruct.activityDataTimestampProgress.getTimeInMillis() / 1000);
|
||||||
for (int i = 0; i < activityStruct.activityDataHolderProgress; i += 3) { //TODO: check if multiple of 3, if not something is wrong
|
if ((activityStruct.activityDataHolderProgress % 3) != 0) {
|
||||||
|
throw new IllegalStateException("Unexpected data, progress should be mutiple of 3: " + activityStruct.activityDataHolderProgress);
|
||||||
|
}
|
||||||
|
int numSamples = activityStruct.activityDataHolderProgress/3;
|
||||||
|
ActivitySample[] samples = new ActivitySample[numSamples];
|
||||||
|
SampleProvider sampleProvider = new MiBandSampleProvider();
|
||||||
|
int s = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < activityStruct.activityDataHolderProgress; i += 3) {
|
||||||
category = activityStruct.activityDataHolder[i];
|
category = activityStruct.activityDataHolder[i];
|
||||||
intensity = activityStruct.activityDataHolder[i + 1];
|
intensity = activityStruct.activityDataHolder[i + 1];
|
||||||
steps = activityStruct.activityDataHolder[i + 2];
|
steps = activityStruct.activityDataHolder[i + 2];
|
||||||
|
|
||||||
dbHandler.addGBActivitySample(
|
samples[minutes] = new GBActivitySample(
|
||||||
|
sampleProvider,
|
||||||
timestampInSeconds,
|
timestampInSeconds,
|
||||||
SampleProvider.PROVIDER_MIBAND,
|
|
||||||
(short) (intensity & 0xff),
|
(short) (intensity & 0xff),
|
||||||
(short) (steps & 0xff),
|
(short) (steps & 0xff),
|
||||||
category);
|
category);
|
||||||
|
|
||||||
// next minute
|
// next minute
|
||||||
minutes++;
|
minutes++;
|
||||||
timestampInSeconds += 60;
|
timestampInSeconds += 60;
|
||||||
}
|
}
|
||||||
|
dbHandler.addGBActivitySamples(samples);
|
||||||
} finally {
|
} finally {
|
||||||
activityStruct.bufferFlushed(minutes);
|
activityStruct.bufferFlushed(minutes);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user