Search in sources :

Example 1 with DaoSession

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

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

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

Example 4 with DaoSession

use of nodomain.freeyourgadget.gadgetbridge.entities.DaoSession in project Gadgetbridge by Freeyourgadget.

the class AbstractDeviceCoordinator method deleteDevice.

@Override
public void deleteDevice(final GBDevice gbDevice) throws GBException {
    LOG.info("will try to delete device: " + gbDevice.getName());
    if (gbDevice.isConnected() || gbDevice.isConnecting()) {
        GBApplication.deviceService().disconnect();
    }
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        DaoSession session = dbHandler.getDaoSession();
        Device device = DBHelper.findDevice(gbDevice, session);
        if (device != null) {
            deleteDevice(gbDevice, device, session);
            QueryBuilder<?> qb = session.getDeviceAttributesDao().queryBuilder();
            qb.where(DeviceAttributesDao.Properties.DeviceId.eq(device.getId())).buildDelete().executeDeleteWithoutDetachingEntities();
            session.getDeviceDao().delete(device);
        } else {
            LOG.info("device to delete not found in db: " + gbDevice);
        }
    } catch (Exception e) {
        throw new GBException("Error deleting device: " + e.getMessage(), e);
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) BluetoothDevice(android.bluetooth.BluetoothDevice) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Example 5 with DaoSession

use of nodomain.freeyourgadget.gadgetbridge.entities.DaoSession in project Gadgetbridge by Freeyourgadget.

the class DatalogSessionHealthSleep method store.

private void store(SleepRecord[] sleepRecords) {
    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 (SleepRecord sleepRecord : sleepRecords) {
            //TODO: check the firmware version and don't use the sleep record if overlay is available?
            overlayList.add(new PebbleHealthActivityOverlay(sleepRecord.bedTimeStart, sleepRecord.bedTimeEnd, sleepRecord.type, deviceId, userId, sleepRecord.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)

Aggregations

DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)7 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)6 ArrayList (java.util.ArrayList)3 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)3 BluetoothDevice (android.bluetooth.BluetoothDevice)2 IOException (java.io.IOException)2 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)2 PebbleHealthActivityOverlay (nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay)2 PebbleHealthActivityOverlayDao (nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao)2 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)2 GregorianCalendar (java.util.GregorianCalendar)1 HPlusHealthSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider)1 MiBandSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider)1 DeviceDao (nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao)1 HPlusHealthActivityOverlay (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay)1 HPlusHealthActivityOverlayDao (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlayDao)1 HPlusHealthActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)1 MiBandActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample)1 User (nodomain.freeyourgadget.gadgetbridge.entities.User)1 DeviceHelper (nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper)1