diff --git a/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java b/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java index 24dc8261e..1c5d7fc87 100644 --- a/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java +++ b/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java @@ -167,22 +167,31 @@ public class GBDaoGenerator { "intensity, are device specific. Normalized values can be retrieved through the\n" + "corresponding {@link SampleProvider}."); activitySample.addIdProperty(); + activitySample.addIntProperty("timestamp").notNull(); } private static void addCommonActivitySampleProperties2(Entity activitySample, Entity user, Entity device) { - Property timestamp = activitySample.addIntProperty("timestamp").notNull().getProperty(); Property userId = activitySample.addLongProperty("userId").getProperty(); activitySample.addToOne(user, userId); Property deviceId = activitySample.addLongProperty("deviceId").getProperty(); activitySample.addToOne(device, deviceId); Index indexUnique = new Index(); - indexUnique.addProperty(timestamp); + indexUnique.addProperty(findProperty(activitySample, "timestamp")); indexUnique.addProperty(deviceId); indexUnique.makeUnique(); activitySample.addIndex(indexUnique); } + private static Property findProperty(Entity entity, String propertyName) { + for (Property prop : entity.getProperties()) { + if (propertyName.equals(prop.getPropertyName())) { + return prop; + } + } + throw new IllegalArgumentException("Property " + propertyName + " not found in Entity " + entity.getClassName()); + } + private static void addDefaultActivitySampleAttributes(Entity activitySample) { activitySample.addIntProperty("rawIntensity").notNull(); activitySample.addIntProperty("steps").notNull(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHelper.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHelper.java index 8144d4ab6..8369bcfb7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHelper.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHelper.java @@ -29,6 +29,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.User; import nodomain.freeyourgadget.gadgetbridge.entities.UserAttributes; import nodomain.freeyourgadget.gadgetbridge.entities.UserDao; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; import nodomain.freeyourgadget.gadgetbridge.model.ValidByDate; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; @@ -373,19 +374,28 @@ public class DBHelper { List newSamples = new ArrayList<>(cursor.getCount()); while (cursor.moveToNext()) { T newSample = sampleProvider.createActivitySample(); + newSample.setProvider(sampleProvider); newSample.setUserId(userId); newSample.setDeviceId(deviceId); newSample.setTimestamp(cursor.getInt(colTimeStamp)); - newSample.setRawKind(cursor.getInt(colType)); - newSample.setProvider(sampleProvider); - newSample.setRawIntensity(cursor.getInt(colIntensity)); - newSample.setSteps(cursor.getInt(colSteps)); - - int hrValue = cursor.getInt(colCustomShort); - newSample.setHeartRate(hrValue); + newSample.setRawKind(getNullableInt(cursor, colType, ActivitySample.NOT_MEASURED)); + newSample.setRawIntensity(getNullableInt(cursor, colIntensity, ActivitySample.NOT_MEASURED)); + newSample.setSteps(getNullableInt(cursor, colSteps, ActivitySample.NOT_MEASURED)); + if (colCustomShort > -1) { + newSample.setHeartRate(getNullableInt(cursor, colCustomShort, ActivitySample.NOT_MEASURED)); + } else { + newSample.setHeartRate(ActivitySample.NOT_MEASURED); + } newSamples.add(newSample); } sampleProvider.getSampleDao().insertOrReplaceInTx(newSamples, true); } } + + private int getNullableInt(Cursor cursor, int columnIndex, int defaultValue) { + if (cursor.isNull(columnIndex)) { + return defaultValue; + } + return cursor.getInt(columnIndex); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMisfit.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMisfit.java index acfd123ee..ad74f3a06 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMisfit.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMisfit.java @@ -83,7 +83,7 @@ public class AppMessageHandlerMisfit extends AppMessageHandler { Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId(); for (int i = 0; i < samples; i++) { short sample = buf.getShort(); - misfitSamples[i] = new PebbleMisfitSample(null, sample & 0xffff, timestamp + i * 60, userId, deviceId); + misfitSamples[i] = new PebbleMisfitSample(null, timestamp + i * 60, sample & 0xffff, userId, deviceId); misfitSamples[i].setProvider(sampleProvider); int steps = misfitSamples[i].getSteps(); totalSteps += steps;