From 6b2565e4c9dd9fa46da0f3c57bd0a9ea35ea9b57 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sun, 7 Aug 2016 01:47:15 +0200 Subject: [PATCH] DB refactoring: remove activity type from Morpheuz database, determinate it in PebbleMorpheuzSampleProvider instead --- .../gadgetbridge/daogen/GBDaoGenerator.java | 3 +- .../devices/AbstractSampleProvider.java | 4 ++ .../pebble/PebbleMorpheuzSampleProvider.java | 40 +++++++++++++++++-- .../pebble/AppMessageHandlerMorpheuz.java | 8 +--- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java b/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java index 306134615..64a3a8d2d 100644 --- a/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java +++ b/GBDaoGenerator/src/nodomain/freeyourgadget/gadgetbridge/daogen/GBDaoGenerator.java @@ -34,7 +34,7 @@ public class GBDaoGenerator { private static final String VALID_BY_DATE = MODEL_PACKAGE + ".ValidByDate"; public static void main(String[] args) throws Exception { - Schema schema = new Schema(10, MAIN_PACKAGE + ".entities"); + Schema schema = new Schema(11, MAIN_PACKAGE + ".entities"); addActivityDescription(schema); @@ -164,7 +164,6 @@ public class GBDaoGenerator { Entity activitySample = addEntity(schema, "PebbleMorpheuzSample"); addCommonActivitySampleProperties("AbstractActivitySample", activitySample, user, device); activitySample.addIntProperty("rawIntensity").notNull(); - activitySample.addIntProperty("rawKind").notNull(); addCommonActivitySampleProperties2(activitySample, user, device); return activitySample; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java index 227543b5c..3721e74c3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java @@ -91,6 +91,10 @@ public abstract class AbstractSampleProvider i } protected List getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) { + if (getRawKindSampleProperty() == null && activityType != ActivityKind.TYPE_ALL) { + // if we do not have a raw kind property we cannot query anything else then TYPE_ALL + return Collections.emptyList(); + } QueryBuilder qb = getSampleDao().queryBuilder(); Property timestampProperty = getTimestampSampleProperty(); Device dbDevice = DBHelper.findDevice(getmDevice(), getSession()); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleMorpheuzSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleMorpheuzSampleProvider.java index 8cd3223e2..c43a58c9c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleMorpheuzSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleMorpheuzSampleProvider.java @@ -1,5 +1,8 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pebble; +import java.util.ArrayList; +import java.util.List; + import de.greenrobot.dao.AbstractDao; import de.greenrobot.dao.Property; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider; @@ -15,7 +18,7 @@ public class PebbleMorpheuzSampleProvider extends AbstractSampleProvider getActivitySamples(int timestamp_from, int timestamp_to) { + List samples = getAllActivitySamples(timestamp_from, timestamp_to); + List filteredSamples = new ArrayList<>(); + for (PebbleMorpheuzSample sample : samples) { + if (sample.getRawIntensity() > 1000) { + sample.setRawKind(ActivityKind.TYPE_ACTIVITY); + filteredSamples.add(sample); + } + } + + return filteredSamples; + } + + @Override + public List getSleepSamples(int timestamp_from, int timestamp_to) { + List samples = getAllActivitySamples(timestamp_from, timestamp_to); + List filteredSamples = new ArrayList<>(); + for (PebbleMorpheuzSample sample : samples) { + if (sample.getRawIntensity() < 1000) { + if (sample.getRawIntensity() <= 120) { + sample.setRawKind(ActivityKind.TYPE_DEEP_SLEEP); + } else { + sample.setRawKind(ActivityKind.TYPE_LIGHT_SLEEP); + } + filteredSamples.add(sample); + } + } + + return filteredSamples; + } + @Override public int getID() { return SampleProvider.PROVIDER_PEBBLE_MORPHEUZ; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMorpheuz.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMorpheuz.java index b81b345d4..2ec80438a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMorpheuz.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMorpheuz.java @@ -109,18 +109,12 @@ public class AppMessageHandlerMorpheuz extends AppMessageHandler { int index = ((int) pair.second >> 16); int intensity = ((int) pair.second & 0xffff); LOG.info("got point:" + index + " " + intensity); - int type = PebbleMorpheuzSampleProvider.TYPE_ACTIVITY; - if (intensity <= 120) { - type = PebbleMorpheuzSampleProvider.TYPE_DEEP_SLEEP; - } else if (intensity <= 1000) { - type = PebbleMorpheuzSampleProvider.TYPE_LIGHT_SLEEP; - } if (index >= 0) { try (DBHandler db = GBApplication.acquireDB()) { Long userId = DBHelper.getUser(db.getDaoSession()).getId(); Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId(); PebbleMorpheuzSampleProvider sampleProvider = new PebbleMorpheuzSampleProvider(getDevice(), db.getDaoSession()); - PebbleMorpheuzSample sample = new PebbleMorpheuzSample(null, recording_base_timestamp + index * 600, intensity, type, userId, deviceId); + PebbleMorpheuzSample sample = new PebbleMorpheuzSample(null, recording_base_timestamp + index * 600, intensity, userId, deviceId); sample.setProvider(sampleProvider); sampleProvider.addGBActivitySample(sample); } catch (Exception e) {