1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 10:35:50 +01:00

Import the old samples and map the ranges to overlays for pebble.

This commit is contained in:
Daniele Gobbetti 2016-08-19 21:09:32 +02:00
parent 3fb558c536
commit 6119f3501a
2 changed files with 59 additions and 5 deletions

View File

@ -1,7 +1,7 @@
TODO before 0.12.0 release: TODO before 0.12.0 release:
* Patch GreenDAO to support composite primary keys * Patch GreenDAO to support composite primary keys
* Support importing Pebble Health data from old database * ~~Support importing Pebble Health data from old database~~ DONE, needs check.
* Fix user attribute table being spammed * Fix user attribute table being spammed
* Add back UUID_CHARACTERISTIC_PAIR support, at least optionally * Add back UUID_CHARACTERISTIC_PAIR support, at least optionally
* CSV Export * CSV Export

View File

@ -19,16 +19,20 @@ import de.greenrobot.dao.query.Query;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleHealthSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes; import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributesDao; import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributesDao;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao; import nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao;
import nodomain.freeyourgadget.gadgetbridge.entities.User; import nodomain.freeyourgadget.gadgetbridge.entities.User;
import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes; import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes;
import nodomain.freeyourgadget.gadgetbridge.entities.UserDao; import nodomain.freeyourgadget.gadgetbridge.entities.UserDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.ValidByDate; import nodomain.freeyourgadget.gadgetbridge.model.ValidByDate;
@ -46,7 +50,7 @@ import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.TABLE_GB
/** /**
* Provides utiliy access to some common entities, so you won't need to use * Provides utiliy access to some common entities, so you won't need to use
* their DAO classes. * their DAO classes.
* * <p/>
* Maybe this code should actually be in the DAO classes themselves, but then * Maybe this code should actually be in the DAO classes themselves, but then
* these should be under revision control instead of 100% generated at build time. * these should be under revision control instead of 100% generated at build time.
*/ */
@ -61,6 +65,7 @@ public class DBHelper {
* Closes the database and returns its name. * Closes the database and returns its name.
* Important: after calling this, you have to DBHandler#openDb() it again * Important: after calling this, you have to DBHandler#openDb() it again
* to get it back to work. * to get it back to work.
*
* @param dbHandler * @param dbHandler
* @return * @return
* @throws IllegalStateException * @throws IllegalStateException
@ -355,6 +360,7 @@ public class DBHelper {
/** /**
* Returns the old activity database handler if there is any content in that * Returns the old activity database handler if there is any content in that
* db, or null otherwise. * db, or null otherwise.
*
* @return the old activity db handler or null * @return the old activity db handler or null
*/ */
@Nullable @Nullable
@ -398,8 +404,21 @@ public class DBHelper {
String order = "timestamp"; String order = "timestamp";
final String where = "provider=" + sampleProvider.getID(); final String where = "provider=" + sampleProvider.getID();
boolean convertActivityTypeToRange = false;
int currentTypeRun, previousTypeRun, currentTimeStamp, currentTypeStartTimeStamp, currentTypeEndTimeStamp;
List<PebbleHealthActivityOverlay> overlayList = new ArrayList<>();
final int BATCH_SIZE = 100000; // 100.000 samples = rougly 20 MB per batch final int BATCH_SIZE = 100000; // 100.000 samples = rougly 20 MB per batch
List<T> newSamples; List<T> newSamples;
if (sampleProvider instanceof PebbleHealthSampleProvider) {
convertActivityTypeToRange = true;
previousTypeRun = ActivitySample.NOT_MEASURED;
currentTypeStartTimeStamp = -1;
currentTypeEndTimeStamp = -1;
} else {
previousTypeRun = currentTypeStartTimeStamp = currentTypeEndTimeStamp = 0;
}
try (Cursor cursor = fromDb.query(TABLE_GBACTIVITYSAMPLES, null, where, null, null, null, order)) { try (Cursor cursor = fromDb.query(TABLE_GBACTIVITYSAMPLES, null, where, null, null, null, order)) {
int colTimeStamp = cursor.getColumnIndex(KEY_TIMESTAMP); int colTimeStamp = cursor.getColumnIndex(KEY_TIMESTAMP);
int colIntensity = cursor.getColumnIndex(KEY_INTENSITY); int colIntensity = cursor.getColumnIndex(KEY_INTENSITY);
@ -414,9 +433,33 @@ public class DBHelper {
newSample.setProvider(sampleProvider); newSample.setProvider(sampleProvider);
newSample.setUserId(userId); newSample.setUserId(userId);
newSample.setDeviceId(deviceId); newSample.setDeviceId(deviceId);
newSample.setTimestamp(cursor.getInt(colTimeStamp)); currentTimeStamp = cursor.getInt(colTimeStamp);
newSample.setRawKind(getNullableInt(cursor, colType, ActivitySample.NOT_MEASURED)); newSample.setTimestamp(currentTimeStamp);
newSample.setRawIntensity(getNullableInt(cursor, colIntensity, ActivitySample.NOT_MEASURED)); newSample.setRawIntensity(getNullableInt(cursor, colIntensity, ActivitySample.NOT_MEASURED));
currentTypeRun = getNullableInt(cursor, colType, ActivitySample.NOT_MEASURED);
newSample.setRawKind(currentTypeRun);
if (convertActivityTypeToRange) {
//at the beginning there is no start timestamp
if (currentTypeStartTimeStamp == -1) {
currentTypeStartTimeStamp = currentTypeEndTimeStamp = currentTimeStamp;
previousTypeRun = currentTypeRun;
}
if (currentTypeRun != previousTypeRun) {
//if the Type has changed, the run has ended. Only store light and deep sleep data
if (previousTypeRun == 4) {
overlayList.add(new PebbleHealthActivityOverlay(currentTypeStartTimeStamp, currentTypeEndTimeStamp, sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP), deviceId, userId, null));
} else if (previousTypeRun == 5) {
overlayList.add(new PebbleHealthActivityOverlay(currentTypeStartTimeStamp, currentTypeEndTimeStamp, sampleProvider.toRawActivityKind(ActivityKind.TYPE_DEEP_SLEEP), deviceId, userId, null));
}
currentTypeStartTimeStamp = currentTypeEndTimeStamp = currentTimeStamp;
previousTypeRun = currentTypeRun;
} else {
//just expand the run
currentTypeEndTimeStamp = currentTimeStamp;
}
}
newSample.setSteps(getNullableInt(cursor, colSteps, ActivitySample.NOT_MEASURED)); newSample.setSteps(getNullableInt(cursor, colSteps, ActivitySample.NOT_MEASURED));
if (colCustomShort > -1) { if (colCustomShort > -1) {
newSample.setHeartRate(getNullableInt(cursor, colCustomShort, ActivitySample.NOT_MEASURED)); newSample.setHeartRate(getNullableInt(cursor, colCustomShort, ActivitySample.NOT_MEASURED));
@ -435,6 +478,17 @@ public class DBHelper {
if (!newSamples.isEmpty()) { if (!newSamples.isEmpty()) {
sampleProvider.getSampleDao().insertOrReplaceInTx(newSamples, true); sampleProvider.getSampleDao().insertOrReplaceInTx(newSamples, true);
} }
// store the overlay records
if (!overlayList.isEmpty()) {
try (DBHandler dbHandler = GBApplication.acquireDB()) {
DaoSession session = dbHandler.getDaoSession();
PebbleHealthActivityOverlayDao overlayDao = session.getPebbleHealthActivityOverlayDao();
overlayDao.insertOrReplaceInTx(overlayList);
} catch (Exception ex) {
//FIXME: this whole try catch is probably in the wrong place.
}
}
} }
} }