Search in sources :

Example 1 with HPlusHealthActivityOverlay

use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay 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;
}
Also used : HPlusHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) HPlusHealthActivityOverlay(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay) NonNull(android.support.annotation.NonNull)

Example 2 with HPlusHealthActivityOverlay

use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay 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;
}
Also used : HPlusHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample) ArrayList(java.util.ArrayList) HPlusHealthSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider) HPlusHealthActivityOverlay(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) HPlusHealthActivityOverlayDao(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlayDao) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Aggregations

HPlusHealthActivityOverlay (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay)2 HPlusHealthActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)2 NonNull (android.support.annotation.NonNull)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)1 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)1 HPlusHealthSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider)1 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)1 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)1 HPlusHealthActivityOverlayDao (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlayDao)1 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1