use of nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider 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.devices.hplus.HPlusHealthSampleProvider 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.devices.hplus.HPlusHealthSampleProvider in project Gadgetbridge by Freeyourgadget.
the class HPlusHandlerThread method processRealtimeStats.
/**
* Process a message containing real time information
*
* @param data the message from the device
* @return boolean indicating success or fail
*/
public boolean processRealtimeStats(byte[] data) {
HPlusDataRecordRealtime record;
try {
record = new HPlusDataRecordRealtime(data);
} catch (IllegalArgumentException e) {
LOG.debug((e.getMessage()));
return false;
}
//This can be used to detect the user is moving (not sleeping)
if (prevRealTimeRecord != null && record.same(prevRealTimeRecord))
return true;
prevRealTimeRecord = record;
getDevice().setBatteryLevel(record.battery);
//Because a message with a valid Heart Rate will be provided, this loss very limited
if (record.heartRate == ActivityKind.TYPE_NOT_MEASURED) {
getDevice().setFirmwareVersion2("---");
getDevice().sendDeviceUpdateIntent(getContext());
} else {
getDevice().setFirmwareVersion2("" + record.heartRate);
getDevice().sendDeviceUpdateIntent(getContext());
}
try (DBHandler dbHandler = GBApplication.acquireDB()) {
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
HPlusHealthActivitySample sample = createSample(dbHandler, record.timestamp);
sample.setRawKind(record.type);
sample.setRawIntensity(record.intensity);
sample.setHeartRate(record.heartRate);
sample.setDistance(record.distance);
sample.setCalories(record.calories);
sample.setSteps(record.steps);
sample.setRawHPlusHealthData(record.getRawData());
sample.setProvider(provider);
provider.addGBActivitySample(sample);
sample.setSteps(sample.getSteps() - prevRealTimeRecord.steps);
Intent intent = new Intent(DeviceService.ACTION_REALTIME_SAMPLES).putExtra(DeviceService.EXTRA_REALTIME_SAMPLE, sample).putExtra(DeviceService.EXTRA_TIMESTAMP, System.currentTimeMillis());
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
//TODO: Handle Active Time. With Overlay?
} catch (GBException ex) {
LOG.debug((ex.getMessage()));
} catch (Exception ex) {
LOG.debug(ex.getMessage());
}
return true;
}
use of nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider in project Gadgetbridge by Freeyourgadget.
the class HPlusHandlerThread method processIncomingDaySlotData.
/**
* Process a message containing information regarding a day slot
* A slot summarizes 10 minutes of data
*
* @param data the message from the device
* @return boolean indicating success or fail
*/
public boolean processIncomingDaySlotData(byte[] data) {
HPlusDataRecordDaySlot record;
try {
record = new HPlusDataRecordDaySlot(data);
} catch (IllegalArgumentException e) {
LOG.debug((e.getMessage()));
return false;
}
Calendar now = GregorianCalendar.getInstance();
int nowSlot = now.get(Calendar.HOUR_OF_DAY) * 6 + (now.get(Calendar.MINUTE) / 10);
if (record.slot == nowSlot) {
if (mCurrentDaySlot != null && mCurrentDaySlot != record) {
mCurrentDaySlot.accumulate(record);
mDaySlotRecords.add(mCurrentDaySlot);
mCurrentDaySlot = null;
} else {
//Store it to a temp variable as this is an intermediate value
mCurrentDaySlot = record;
if (!mSlotsInitialSync)
return true;
}
}
if (mSlotsInitialSync) {
//Subtract a day of seconds
if (record.slot > nowSlot) {
record.timestamp -= 3600 * 24;
}
if (record.slot == mLastSlotReceived + 1) {
mLastSlotReceived = record.slot;
}
//Ignore the current slot as it is incomplete
if (record.slot != nowSlot)
mDaySlotRecords.add(record);
//Still fetching ring buffer. Request the next slots
if (record.slot == mLastSlotRequested) {
mGetDaySlotsTime.clear();
synchronized (waitObject) {
waitObject.notify();
}
}
//Keep buffering
if (record.slot != 143)
return true;
} else {
mGetDaySlotsTime = GregorianCalendar.getInstance();
mGetDaySlotsTime.add(Calendar.DAY_OF_MONTH, 1);
}
if (mDaySlotRecords.size() > 0) {
//Sort the samples
Collections.sort(mDaySlotRecords, new Comparator<HPlusDataRecordDaySlot>() {
public int compare(HPlusDataRecordDaySlot one, HPlusDataRecordDaySlot other) {
return one.timestamp - other.timestamp;
}
});
try (DBHandler dbHandler = GBApplication.acquireDB()) {
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
List<HPlusHealthActivitySample> samples = new ArrayList<>();
for (HPlusDataRecordDaySlot storedRecord : mDaySlotRecords) {
HPlusHealthActivitySample sample = createSample(dbHandler, storedRecord.timestamp);
sample.setRawHPlusHealthData(storedRecord.getRawData());
sample.setSteps(storedRecord.steps);
sample.setHeartRate(storedRecord.heartRate);
sample.setRawKind(storedRecord.type);
sample.setProvider(provider);
samples.add(sample);
}
provider.getSampleDao().insertOrReplaceInTx(samples);
mDaySlotRecords.clear();
} catch (GBException ex) {
LOG.debug((ex.getMessage()));
} catch (Exception ex) {
LOG.debug(ex.getMessage());
}
}
return true;
}
Aggregations