use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample in project Gadgetbridge by Freeyourgadget.
the class HPlusHealthSampleProvider method insertVirtualItem.
private List<HPlusHealthActivitySample> insertVirtualItem(List<HPlusHealthActivitySample> samples, int timestamp, long deviceId, long userId) {
HPlusHealthActivitySample sample = new HPlusHealthActivitySample(// ts
timestamp, deviceId, // User id
userId, // Raw Data
null, ActivityKind.TYPE_UNKNOWN, // Intensity
1, // Steps
ActivitySample.NOT_MEASURED, // HR
ActivitySample.NOT_MEASURED, // Distance
ActivitySample.NOT_MEASURED, // Calories
ActivitySample.NOT_MEASURED);
sample.setProvider(this);
samples.add(sample);
return samples;
}
use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample in project Gadgetbridge by Freeyourgadget.
the class HPlusHealthSampleProvider method getAllActivitySamples.
@NonNull
@Override
public List<HPlusHealthActivitySample> getAllActivitySamples(int timestamp_from, int timestamp_to) {
List<HPlusHealthActivitySample> samples = super.getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL);
Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
if (dbDevice == null) {
return Collections.emptyList();
}
QueryBuilder<HPlusHealthActivityOverlay> qb = getSession().getHPlusHealthActivityOverlayDao().queryBuilder();
qb.where(HPlusHealthActivityOverlayDao.Properties.DeviceId.eq(dbDevice.getId()), HPlusHealthActivityOverlayDao.Properties.TimestampFrom.ge(timestamp_from - 3600 * 24), HPlusHealthActivityOverlayDao.Properties.TimestampTo.le(timestamp_to), HPlusHealthActivityOverlayDao.Properties.TimestampTo.ge(timestamp_from));
List<HPlusHealthActivityOverlay> overlayRecords = qb.build().list();
//Todays sample steps will come from the Day Slots messages
//Historical steps will be provided by Day Summaries messages
//This will allow both week and current day results to be consistent
Calendar today = GregorianCalendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
int stepsTodayMax = 0;
int stepsTodayCount = 0;
HPlusHealthActivitySample lastSample = null;
for (HPlusHealthActivitySample sample : samples) {
if (sample.getTimestamp() >= today.getTimeInMillis() / 1000) {
if (sample.getRawKind() == HPlusDataRecord.TYPE_REALTIME) {
stepsTodayMax = Math.max(stepsTodayMax, sample.getSteps());
} else if (sample.getRawKind() == HPlusDataRecord.TYPE_DAY_SLOT) {
stepsTodayCount += sample.getSteps();
}
sample.setSteps(ActivitySample.NOT_MEASURED);
lastSample = sample;
} else {
if (sample.getRawKind() != HPlusDataRecord.TYPE_DAY_SUMMARY) {
sample.setSteps(ActivitySample.NOT_MEASURED);
}
}
}
if (lastSample != null)
lastSample.setSteps(Math.max(stepsTodayCount, stepsTodayMax));
for (HPlusHealthActivityOverlay overlay : overlayRecords) {
//Create fake events to improve activity counters if there are no events around the overlay
//timestamp boundaries
//Insert one before, one at the beginning, one at the end, and one 1s after.
insertVirtualItem(samples, Math.max(overlay.getTimestampFrom() - 1, timestamp_from), overlay.getDeviceId(), overlay.getUserId());
insertVirtualItem(samples, Math.max(overlay.getTimestampFrom(), timestamp_from), overlay.getDeviceId(), overlay.getUserId());
insertVirtualItem(samples, Math.min(overlay.getTimestampTo() - 1, timestamp_to - 1), overlay.getDeviceId(), overlay.getUserId());
insertVirtualItem(samples, Math.min(overlay.getTimestampTo(), timestamp_to), overlay.getDeviceId(), overlay.getUserId());
for (HPlusHealthActivitySample sample : samples) {
if (sample.getTimestamp() >= overlay.getTimestampFrom() && sample.getTimestamp() < overlay.getTimestampTo()) {
sample.setRawKind(overlay.getRawKind());
}
}
}
detachFromSession();
Collections.sort(samples, new Comparator<HPlusHealthActivitySample>() {
public int compare(HPlusHealthActivitySample one, HPlusHealthActivitySample other) {
return one.getTimestamp() - other.getTimestamp();
}
});
return samples;
}
use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample in project Gadgetbridge by Freeyourgadget.
the class HPlusHandlerThread method processDaySummary.
/**
* Process a day summary message
* This message includes aggregates regarding an entire day
*
* @param data the message from the device
* @return boolean indicating success or fail
*/
public boolean processDaySummary(byte[] data) {
HPlusDataRecordDaySummary record;
try {
record = new HPlusDataRecordDaySummary(data);
} catch (IllegalArgumentException e) {
LOG.debug((e.getMessage()));
return false;
}
try (DBHandler dbHandler = GBApplication.acquireDB()) {
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
HPlusHealthActivitySample sample = createSample(dbHandler, record.timestamp);
sample.setRawKind(record.type);
sample.setSteps(record.steps);
sample.setDistance(record.distance);
sample.setCalories(record.calories);
sample.setDistance(record.distance);
//TODO: Find an alternative approach for Day Summary Heart Rate
sample.setHeartRate((record.maxHeartRate - record.minHeartRate) / 2);
sample.setRawHPlusHealthData(record.getRawData());
sample.setProvider(provider);
provider.addGBActivitySample(sample);
} catch (GBException ex) {
LOG.debug((ex.getMessage()));
} catch (Exception ex) {
LOG.debug(ex.getMessage());
}
mGetDaySummaryTime = GregorianCalendar.getInstance();
mGetDaySummaryTime.add(Calendar.SECOND, DAY_SUMMARY_SYNC_PERIOD);
return true;
}
use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample in project Gadgetbridge by Freeyourgadget.
the class HPlusHandlerThread method processIncomingSleepData.
/**
* Process sleep data from the device
* Devices send a single sleep message for each sleep period
* This message contains the duration of the sub-intervals (rem, deep, etc...)
*
* @param data the message from the device
* @return boolean indicating success or fail
*/
public boolean processIncomingSleepData(byte[] data) {
HPlusDataRecordSleep record;
try {
record = new HPlusDataRecordSleep(data);
} catch (IllegalArgumentException e) {
LOG.debug((e.getMessage()));
return false;
}
mLastSleepDayReceived.setTimeInMillis(record.bedTimeStart * 1000L);
try (DBHandler dbHandler = GBApplication.acquireDB()) {
DaoSession session = dbHandler.getDaoSession();
Long userId = DBHelper.getUser(session).getId();
Long deviceId = DBHelper.getDevice(getDevice(), session).getId();
HPlusHealthActivityOverlayDao overlayDao = session.getHPlusHealthActivityOverlayDao();
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
//Get the individual Sleep overlays and insert them
List<HPlusHealthActivityOverlay> overlayList = new ArrayList<>();
List<HPlusDataRecord.RecordInterval> intervals = record.getIntervals();
for (HPlusDataRecord.RecordInterval interval : intervals) {
overlayList.add(new HPlusHealthActivityOverlay(interval.timestampFrom, interval.timestampTo, interval.activityKind, deviceId, userId, null));
}
overlayDao.insertOrReplaceInTx(overlayList);
//Store the data
HPlusHealthActivitySample sample = createSample(dbHandler, record.timestamp);
sample.setRawHPlusHealthData(record.getRawData());
sample.setRawKind(record.activityKind);
sample.setProvider(provider);
provider.addGBActivitySample(sample);
} catch (Exception ex) {
LOG.debug(ex.getMessage());
}
mGetSleepTime = GregorianCalendar.getInstance();
mGetSleepTime.add(GregorianCalendar.SECOND, SLEEP_SYNC_PERIOD);
return true;
}
use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample in project Gadgetbridge by Freeyourgadget.
the class HPlusHandlerThread method createSample.
/**
* Helper function to create a sample
* @param dbHandler The database handler
* @param timestamp The sample timestamp
* @return The sample just created
*/
private HPlusHealthActivitySample createSample(DBHandler dbHandler, int timestamp) {
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
HPlusHealthActivitySample sample = new HPlusHealthActivitySample(// ts
timestamp, // User id
deviceId, // User id
userId, // Raw Data
null, ActivityKind.TYPE_UNKNOWN, // Intensity
0, // Steps
ActivitySample.NOT_MEASURED, // HR
ActivitySample.NOT_MEASURED, // Distance
ActivitySample.NOT_MEASURED, // Calories
ActivitySample.NOT_MEASURED);
return sample;
}
Aggregations