Search in sources :

Example 1 with DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler 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 DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler 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 DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler in project Gadgetbridge by Freeyourgadget.

the class DatalogSessionHealthOverlayData method store.

private void store(OverlayRecord[] overlayRecords) {
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        DaoSession session = dbHandler.getDaoSession();
        Long userId = DBHelper.getUser(session).getId();
        Long deviceId = DBHelper.getDevice(getDevice(), session).getId();
        PebbleHealthActivityOverlayDao overlayDao = session.getPebbleHealthActivityOverlayDao();
        List<PebbleHealthActivityOverlay> overlayList = new ArrayList<>();
        for (OverlayRecord overlayRecord : overlayRecords) {
            overlayList.add(new PebbleHealthActivityOverlay(overlayRecord.timestampStart, overlayRecord.timestampStart + overlayRecord.durationSeconds, overlayRecord.type, deviceId, userId, overlayRecord.getRawData()));
        }
        overlayDao.insertOrReplaceInTx(overlayList);
    } catch (Exception ex) {
        LOG.debug(ex.getMessage());
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) ArrayList(java.util.ArrayList) PebbleHealthActivityOverlayDao(nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao) PebbleHealthActivityOverlay(nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Example 4 with DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler in project Gadgetbridge by Freeyourgadget.

the class DatalogSessionHealthSteps method store.

private void store(StepsRecord[] stepsRecords) {
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        PebbleHealthSampleProvider sampleProvider = new PebbleHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
        PebbleHealthActivitySample[] samples = new PebbleHealthActivitySample[stepsRecords.length];
        // TODO: user and device
        Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
        Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
        for (int j = 0; j < stepsRecords.length; j++) {
            StepsRecord stepsRecord = stepsRecords[j];
            samples[j] = new PebbleHealthActivitySample(stepsRecord.timestamp, deviceId, userId, stepsRecord.getRawData(), stepsRecord.intensity, stepsRecord.steps, stepsRecord.heart_rate);
            samples[j].setProvider(sampleProvider);
        }
        sampleProvider.addGBActivitySamples(samples);
    } catch (Exception ex) {
        LOG.debug(ex.getMessage());
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) PebbleHealthSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleHealthSampleProvider) PebbleHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivitySample)

Example 5 with DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler in project Gadgetbridge by Freeyourgadget.

the class FetchActivityOperation method saveSamples.

private void saveSamples() {
    if (samples.size() > 0) {
        // save all the samples that we got
        try (DBHandler handler = GBApplication.acquireDB()) {
            DaoSession session = handler.getDaoSession();
            SampleProvider<MiBandActivitySample> sampleProvider = new MiBandSampleProvider(getDevice(), session);
            Device device = DBHelper.getDevice(getDevice(), session);
            User user = DBHelper.getUser(session);
            GregorianCalendar timestamp = (GregorianCalendar) startTimestamp.clone();
            for (MiBandActivitySample sample : samples) {
                sample.setDevice(device);
                sample.setUser(user);
                sample.setTimestamp((int) (timestamp.getTimeInMillis() / 1000));
                sample.setProvider(sampleProvider);
                if (LOG.isDebugEnabled()) {
                //                        LOG.debug("sample: " + sample);
                }
                timestamp.add(Calendar.MINUTE, 1);
            }
            sampleProvider.addGBActivitySamples(samples.toArray(new MiBandActivitySample[0]));
            saveLastSyncTimestamp(timestamp);
            LOG.info("Mi2 activity data: last sample timestamp: " + DateTimeUtils.formatDateTime(timestamp.getTime()));
        } catch (Exception ex) {
            GB.toast(getContext(), "Error saving activity samples", Toast.LENGTH_LONG, GB.ERROR);
        } finally {
            samples.clear();
        }
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) User(nodomain.freeyourgadget.gadgetbridge.entities.User) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GregorianCalendar(java.util.GregorianCalendar) MiBandSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider) MiBandActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample) IOException(java.io.IOException) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Aggregations

DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)14 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)6 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)5 ArrayList (java.util.ArrayList)4 HPlusHealthSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider)4 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)4 HPlusHealthActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)4 IOException (java.io.IOException)3 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)3 BluetoothDevice (android.bluetooth.BluetoothDevice)2 GregorianCalendar (java.util.GregorianCalendar)2 GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)2 GBDeviceEventSendBytes (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)2 MiBandSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider)2 MiBandActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample)2 PebbleHealthActivityOverlay (nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay)2 PebbleHealthActivityOverlayDao (nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao)2 User (nodomain.freeyourgadget.gadgetbridge.entities.User)2 Intent (android.content.Intent)1 File (java.io.File)1