Search in sources :

Example 1 with MiBandSampleProvider

use of nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider 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 MiBandSampleProvider

use of nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider in project Gadgetbridge by Freeyourgadget.

the class SampleProviderTest method testBasics.

@Test
public void testBasics() {
    MiBandSampleProvider sampleProvider = new MiBandSampleProvider(dummyGBDevice, daoSession);
    assertNotNull(sampleProvider.getDevice());
    assertSame(dummyGBDevice, sampleProvider.getDevice());
    assertNotNull(sampleProvider.getSampleDao());
    assertNotNull(sampleProvider.createActivitySample());
    float intensity = sampleProvider.normalizeIntensity(50);
    assertTrue(intensity > 0);
    assertTrue(intensity < 1);
}
Also used : MiBandSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider) Test(org.junit.Test)

Example 3 with MiBandSampleProvider

use of nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider in project Gadgetbridge by Freeyourgadget.

the class SampleProviderTest method testNoSamples.

@Test
public void testNoSamples() {
    MiBandSampleProvider sampleProvider = new MiBandSampleProvider(dummyGBDevice, daoSession);
    List<MiBandActivitySample> samples = sampleProvider.getAllActivitySamples(0, 0);
    assertEquals(0, samples.size());
    samples = sampleProvider.getAllActivitySamples(-1, 1);
    assertEquals(0, samples.size());
    samples = sampleProvider.getAllActivitySamples(1, -1);
    assertEquals(0, samples.size());
    // now specific activity kinds
    samples = sampleProvider.getActivitySamples(0, 0);
    assertEquals(0, samples.size());
    samples = sampleProvider.getActivitySamples(-1, 1);
    assertEquals(0, samples.size());
    samples = sampleProvider.getActivitySamples(1, -1);
    assertEquals(0, samples.size());
    // and sleep
    samples = sampleProvider.getSleepSamples(0, 0);
    assertEquals(0, samples.size());
    samples = sampleProvider.getSleepSamples(-1, 1);
    assertEquals(0, samples.size());
    samples = sampleProvider.getSleepSamples(1, -1);
    assertEquals(0, samples.size());
}
Also used : MiBandSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider) MiBandActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample) Test(org.junit.Test)

Example 4 with MiBandSampleProvider

use of nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider in project Gadgetbridge by Freeyourgadget.

the class FetchActivityOperation method saveSamples.

private GregorianCalendar 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()));
            return timestamp;
        } catch (Exception ex) {
            GB.toast(getContext(), "Error saving activity samples", Toast.LENGTH_LONG, GB.ERROR);
        } finally {
            samples.clear();
        }
    }
    return null;
}
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 5 with MiBandSampleProvider

use of nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider in project Gadgetbridge by Freeyourgadget.

the class SampleProviderTest method testSamples.

@Test
public void testSamples() {
    MiBandSampleProvider sampleProvider = new MiBandSampleProvider(dummyGBDevice, daoSession);
    User user = DBHelper.getUser(daoSession);
    assertNotNull(user);
    assertNotNull(user.getId());
    Device device = DBHelper.getDevice(dummyGBDevice, daoSession);
    assertNotNull(device);
    MiBandActivitySample s1 = createSample(sampleProvider, MiBandSampleProvider.TYPE_ACTIVITY, 100, 10, 70, 1000, user, device);
    sampleProvider.addGBActivitySample(s1);
    // add again, should not throw or fail
    sampleProvider.addGBActivitySample(s1);
    MiBandActivitySample s2 = createSample(sampleProvider, MiBandSampleProvider.TYPE_ACTIVITY, 200, 20, 80, 1030, user, device);
    sampleProvider.addGBActivitySample(s2);
    MiBandActivitySample s3 = createSample(sampleProvider, MiBandSampleProvider.TYPE_DEEP_SLEEP, 1200, 10, 62, 4030, user, device);
    MiBandActivitySample s4 = createSample(sampleProvider, MiBandSampleProvider.TYPE_LIGHT_SLEEP, 2000, 10, 60, 4030, user, device);
    sampleProvider.addGBActivitySamples(new MiBandActivitySample[] { s3, s4 });
    // first checks for irrelevant timestamps => no samples
    List<MiBandActivitySample> samples = sampleProvider.getAllActivitySamples(0, 0);
    assertEquals(0, samples.size());
    samples = sampleProvider.getAllActivitySamples(-1, 1);
    assertEquals(0, samples.size());
    samples = sampleProvider.getAllActivitySamples(1, -1);
    assertEquals(0, samples.size());
    // now specific activity kinds
    samples = sampleProvider.getActivitySamples(0, 0);
    assertEquals(0, samples.size());
    samples = sampleProvider.getActivitySamples(-1, 1);
    assertEquals(0, samples.size());
    samples = sampleProvider.getActivitySamples(1, -1);
    assertEquals(0, samples.size());
    // and sleep
    samples = sampleProvider.getSleepSamples(0, 0);
    assertEquals(0, samples.size());
    samples = sampleProvider.getSleepSamples(-1, 1);
    assertEquals(0, samples.size());
    samples = sampleProvider.getSleepSamples(1, -1);
    assertEquals(0, samples.size());
    // finally checks for existing timestamps
    List<MiBandActivitySample> allSamples = sampleProvider.getAllActivitySamples(0, 10000);
    assertEquals(4, allSamples.size());
    List<MiBandActivitySample> activitySamples = sampleProvider.getActivitySamples(0, 10000);
    assertEquals(2, activitySamples.size());
    List<MiBandActivitySample> sleepSamples = sampleProvider.getSleepSamples(0, 10000);
    assertEquals(2, sleepSamples.size());
    // now with more strict time ranges
    allSamples = sampleProvider.getAllActivitySamples(0, 1300);
    assertEquals(3, allSamples.size());
    activitySamples = sampleProvider.getActivitySamples(10, 150);
    assertEquals(1, activitySamples.size());
    sleepSamples = sampleProvider.getSleepSamples(1500, 2500);
    assertEquals(1, sleepSamples.size());
}
Also used : User(nodomain.freeyourgadget.gadgetbridge.entities.User) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) MiBandSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider) MiBandActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample) Test(org.junit.Test)

Aggregations

MiBandSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider)7 MiBandActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample)5 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)4 User (nodomain.freeyourgadget.gadgetbridge.entities.User)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)3 GregorianCalendar (java.util.GregorianCalendar)2 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)2 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1