mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-25 18:15:49 +01:00
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.
This commit is contained in:
parent
5cfddbb7e9
commit
4a7a34f461
@ -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<PebbleHealthActivityOverlay> 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 {
|
||||
|
@ -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<PebbleHealthActivityOverlay> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user