From 4a7a34f4619546fbae5417ab36574bda233f5f98 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Tue, 9 Aug 2016 18:05:24 +0200 Subject: [PATCH] Use only the overlay table for storing overlay data. This removes the need to wait to have minute samples to store the overlay data, hence store has been made void instead of boolean. --- .../DatalogSessionHealthOverlayData.java | 25 ++--------- .../pebble/DatalogSessionHealthSleep.java | 44 +++++++++++-------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthOverlayData.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthOverlayData.java index 40be306a4..677060a57 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthOverlayData.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthOverlayData.java @@ -11,13 +11,10 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; -import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; -import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleHealthSampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay; import nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; -import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.util.GB; class DatalogSessionHealthOverlayData extends DatalogSessionPebbleHealth { @@ -61,10 +58,11 @@ class DatalogSessionHealthOverlayData extends DatalogSessionPebbleHealth { overlayRecords[recordIdx] = new OverlayRecord(recordType, datalogMessage.getInt(), datalogMessage.getInt(), datalogMessage.getInt()); } - return store(overlayRecords);//NACK if we cannot store the data yet, the watch will send the overlay records again. + store(overlayRecords); + return true; } - private boolean store(OverlayRecord[] overlayRecords) { + private void store(OverlayRecord[] overlayRecords) { try (DBHandler dbHandler = GBApplication.acquireDB()) { DaoSession session = dbHandler.getDaoSession(); Long userId = DBHelper.getUser(session).getId(); @@ -72,29 +70,14 @@ class DatalogSessionHealthOverlayData extends DatalogSessionPebbleHealth { PebbleHealthActivityOverlayDao overlayDao = session.getPebbleHealthActivityOverlayDao(); - SampleProvider sampleProvider = new PebbleHealthSampleProvider(getDevice(), session); List overlayList = new ArrayList<>(); - int latestTimestamp = sampleProvider.fetchLatestTimestamp(); for (OverlayRecord overlayRecord : overlayRecords) { - if (latestTimestamp < (overlayRecord.timestampStart + overlayRecord.durationSeconds)) - return false; - switch (overlayRecord.type) { - case 1: - sampleProvider.changeStoredSamplesType(overlayRecord.timestampStart, (overlayRecord.timestampStart + overlayRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_ACTIVITY), sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP)); - break; - case 2: - sampleProvider.changeStoredSamplesType(overlayRecord.timestampStart, (overlayRecord.timestampStart + overlayRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_DEEP_SLEEP)); - break; - default: - //TODO: other values refer to unknown activity types. - } - overlayList.add(new PebbleHealthActivityOverlay(null, overlayRecord.timestampStart, overlayRecord.timestampStart + overlayRecord.durationSeconds - 1, overlayRecord.type, userId, deviceId)); + overlayList.add(new PebbleHealthActivityOverlay(null, overlayRecord.timestampStart, overlayRecord.timestampStart + overlayRecord.durationSeconds - 1, overlayRecord.type, userId, deviceId)); //TODO: consider if "-1" is what we really want } overlayDao.insertOrReplaceInTx(overlayList); } catch (Exception ex) { LOG.debug(ex.getMessage()); } - return true; } private class OverlayRecord { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSleep.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSleep.java index 377c4fa09..762ba7062 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSleep.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSleep.java @@ -4,14 +4,17 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; -import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; -import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleHealthSampleProvider; +import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; +import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay; +import nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; -import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.util.GB; class DatalogSessionHealthSleep extends DatalogSessionPebbleHealth { @@ -54,25 +57,30 @@ class DatalogSessionHealthSleep extends DatalogSessionPebbleHealth { datalogMessage.getInt()); } - return store(sleepRecords);//NACK if we cannot store the data yet, the watch will send the sleep records again. - } - - private boolean store(SleepRecord[] sleepRecords) { - try (DBHandler dbHandler = GBApplication.acquireDB()) { - SampleProvider sampleProvider = new PebbleHealthSampleProvider(getDevice(), dbHandler.getDaoSession()); - int latestTimestamp = sampleProvider.fetchLatestTimestamp(); - for (SleepRecord sleepRecord : sleepRecords) { - if (latestTimestamp < sleepRecord.bedTimeEnd) - return false; - sampleProvider.changeStoredSamplesType(sleepRecord.bedTimeStart, sleepRecord.bedTimeEnd, sampleProvider.toRawActivityKind(ActivityKind.TYPE_ACTIVITY), sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP)); - } - } catch (Exception ex) { - LOG.debug(ex.getMessage()); - } + store(sleepRecords); return true; } + private void store(SleepRecord[] sleepRecords) { + try (DBHandler dbHandler = GBApplication.acquireDB()) { + DaoSession session = dbHandler.getDaoSession(); + Long userId = DBHelper.getUser(session).getId(); + Long deviceId = DBHelper.getDevice(getDevice(), session).getId(); + + PebbleHealthActivityOverlayDao overlayDao = session.getPebbleHealthActivityOverlayDao(); + + List overlayList = new ArrayList<>(); + for (SleepRecord sleepRecord : sleepRecords) { + overlayList.add(new PebbleHealthActivityOverlay(null, sleepRecord.bedTimeStart, sleepRecord.bedTimeEnd - 1, sleepRecord.type, userId, deviceId)); //TODO: consider if "-1" is what we really want + } + overlayDao.insertOrReplaceInTx(overlayList); + } catch (Exception ex) { + LOG.debug(ex.getMessage()); + } + } + private class SleepRecord { + int type = 1; //sleep, hardcoded as we don't get other info int offsetUTC; //probably int bedTimeStart; int bedTimeEnd;