Search in sources :

Example 1 with Device

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

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

the class EntitiesTest method testDBHelper.

@Test
public void testDBHelper() {
    GBDevice dummyGBDevice = createDummyGDevice("00:00:00:00:01");
    Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
    assertNotNull(device);
    assertEquals("00:00:00:00:01", device.getIdentifier());
    assertEquals("Testie", device.getName());
    assertEquals("4.0", device.getModel());
    assertEquals(DeviceType.TEST.getKey(), device.getType());
    DeviceAttributes attributes = device.getDeviceAttributesList().get(0);
    assertNotNull(attributes);
    assertEquals("1.2.3", attributes.getFirmwareVersion1());
}
Also used : GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) DeviceAttributes(nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Test(org.junit.Test)

Example 3 with Device

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

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

the class PebbleHealthSampleProvider method getAllActivitySamples.

@Override
public List<PebbleHealthActivitySample> getAllActivitySamples(int timestamp_from, int timestamp_to) {
    List<PebbleHealthActivitySample> samples = super.getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL);
    Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
    if (dbDevice == null) {
        // no device, no samples
        return Collections.emptyList();
    }
    QueryBuilder<PebbleHealthActivityOverlay> qb = getSession().getPebbleHealthActivityOverlayDao().queryBuilder();
    // I assume it returns the records by id ascending ... (last overlay is dominant)
    qb.where(PebbleHealthActivityOverlayDao.Properties.DeviceId.eq(dbDevice.getId()), PebbleHealthActivityOverlayDao.Properties.TimestampTo.ge(timestamp_from)).where(PebbleHealthActivityOverlayDao.Properties.TimestampFrom.le(timestamp_to));
    List<PebbleHealthActivityOverlay> overlayRecords = qb.build().list();
    for (PebbleHealthActivityOverlay overlay : overlayRecords) {
        for (PebbleHealthActivitySample sample : samples) {
            if (overlay.getTimestampFrom() <= sample.getTimestamp() && sample.getTimestamp() < overlay.getTimestampTo()) {
                // patch in the raw kind
                sample.setRawKind(overlay.getRawKind());
            }
        }
    }
    detachFromSession();
    return samples;
}
Also used : GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) PebbleHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivitySample) PebbleHealthActivityOverlay(nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay)

Example 5 with Device

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

the class DBHelper method createDevice.

private static Device createDevice(GBDevice gbDevice, DaoSession session) {
    Device device = new Device();
    ensureDeviceUpToDate(device, gbDevice, session);
    return device;
}
Also used : Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)

Aggregations

Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)14 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)12 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)4 MiBandSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider)3 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)3 MiBandActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample)3 User (nodomain.freeyourgadget.gadgetbridge.entities.User)3 Test (org.junit.Test)3 BluetoothDevice (android.bluetooth.BluetoothDevice)2 Property (de.greenrobot.dao.Property)2 IOException (java.io.IOException)2 GregorianCalendar (java.util.GregorianCalendar)2 DeviceAttributes (nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes)2 DeviceDao (nodomain.freeyourgadget.gadgetbridge.entities.DeviceDao)2 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 Calendar (java.util.Calendar)1 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)1 HPlusHealthActivityOverlay (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay)1 HPlusHealthActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)1