Search in sources :

Example 6 with HPlusHealthActivitySample

use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample 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 7 with HPlusHealthActivitySample

use of nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample 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

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