Search in sources :

Example 1 with HPlusHealthSampleProvider

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;
}
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 2 with HPlusHealthSampleProvider

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

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

Example 4 with HPlusHealthSampleProvider

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

Aggregations

GBException (nodomain.freeyourgadget.gadgetbridge.GBException)4 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)4 HPlusHealthSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider)4 HPlusHealthActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)4 ArrayList (java.util.ArrayList)2 Intent (android.content.Intent)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)1 HPlusHealthActivityOverlay (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay)1 HPlusHealthActivityOverlayDao (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlayDao)1