Search in sources :

Example 6 with MiBandSampleProvider

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

the class SampleProviderTest method testActivityKind.

@Test
public void testActivityKind() {
    MiBandSampleProvider sampleProvider = new MiBandSampleProvider(dummyGBDevice, daoSession);
    int type = sampleProvider.normalizeType(MiBandSampleProvider.TYPE_ACTIVITY);
    assertEquals(ActivityKind.TYPE_ACTIVITY, type);
    type = sampleProvider.normalizeType(MiBandSampleProvider.TYPE_DEEP_SLEEP);
    assertEquals(ActivityKind.TYPE_DEEP_SLEEP, type);
    type = sampleProvider.normalizeType(MiBandSampleProvider.TYPE_LIGHT_SLEEP);
    assertEquals(ActivityKind.TYPE_LIGHT_SLEEP, type);
    type = sampleProvider.normalizeType(MiBandSampleProvider.TYPE_NONWEAR);
    assertEquals(ActivityKind.TYPE_NOT_WORN, type);
}
Also used : MiBandSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider) Test(org.junit.Test)

Example 7 with MiBandSampleProvider

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

the class FetchActivityOperation method flushActivityDataHolder.

/**
 * empty the local buffer for activity data, arrange the values received in groups of three and
 * store them in the DB
 */
private void flushActivityDataHolder() {
    if (activityStruct == null) {
        LOG.debug("nothing to flush, struct is already null");
        return;
    }
    int bpm = getBytesPerMinuteOfActivityData();
    LOG.debug("flushing activity data samples: " + activityStruct.activityDataHolderProgress / bpm);
    byte category, intensity, steps, heartrate = 0;
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        MiBandSampleProvider provider = new MiBandSampleProvider(getDevice(), dbHandler.getDaoSession());
        User user = DBHelper.getUser(dbHandler.getDaoSession());
        Device device = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession());
        int minutes = 0;
        try {
            int timestampInSeconds = (int) (activityStruct.activityDataTimestampProgress.getTimeInMillis() / 1000);
            if ((activityStruct.activityDataHolderProgress % bpm) != 0) {
                throw new IllegalStateException("Unexpected data, progress should be multiple of " + bpm + ": " + activityStruct.activityDataHolderProgress);
            }
            int numSamples = activityStruct.activityDataHolderProgress / bpm;
            MiBandActivitySample[] samples = new MiBandActivitySample[numSamples];
            for (int i = 0; i < activityStruct.activityDataHolderProgress; i += bpm) {
                category = activityStruct.activityDataHolder[i];
                intensity = activityStruct.activityDataHolder[i + 1];
                steps = activityStruct.activityDataHolder[i + 2];
                if (hasExtendedActivityData) {
                    heartrate = activityStruct.activityDataHolder[i + 3];
                // LOG.debug("heartrate received: " + (heartrate & 0xff));
                }
                MiBandActivitySample sample = getSupport().createActivitySample(device, user, timestampInSeconds, provider);
                sample.setRawIntensity(intensity & 0xff);
                sample.setSteps(steps & 0xff);
                sample.setRawKind(category & 0xff);
                sample.setHeartRate(heartrate & 0xff);
                samples[minutes] = sample;
                if (LOG.isDebugEnabled()) {
                    LOG.debug("sample: " + samples[minutes]);
                }
                // next minute
                minutes++;
                timestampInSeconds += 60;
            }
            provider.addGBActivitySamples(samples);
        } finally {
            activityStruct.bufferFlushed(minutes);
        }
    } catch (Exception ex) {
        GB.toast(getContext(), ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex);
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) User(nodomain.freeyourgadget.gadgetbridge.entities.User) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) MiBandSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider) MiBandActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample) IOException(java.io.IOException)

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