Search in sources :

Example 1 with HPlusHealthActivitySample

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;
}
Also used : HPlusHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)

Example 2 with HPlusHealthActivitySample

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;
}
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 3 with HPlusHealthActivitySample

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;
}
Also used : HPlusHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) HPlusHealthSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Example 4 with HPlusHealthActivitySample

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;
}
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)

Example 5 with HPlusHealthActivitySample

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;
}
Also used : HPlusHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)

Aggregations

HPlusHealthActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)7 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)4 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)4 HPlusHealthSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider)4 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 HPlusHealthActivityOverlay (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay)2 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)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