From 0c51f86afc470d88997adb924b3702e29fb43659 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 20 Nov 2016 23:33:46 +0100 Subject: [PATCH] Mi2: more work on activity data #323 --- .../gadgetbridge/devices/SampleProvider.java | 1 + .../miband/AbstractMiBandSampleProvider.java | 59 +++++++++++++++++ .../devices/miband/MiBand2SampleProvider.java | 64 +++++++++++++++++++ .../devices/miband/MiBandCoordinator.java | 2 +- .../devices/miband/MiBandSampleProvider.java | 50 ++------------- .../operations/FetchActivityOperation.java | 3 + 6 files changed, 134 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/AbstractMiBandSampleProvider.java create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2SampleProvider.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java index df91696e9..54f809dd4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java @@ -22,6 +22,7 @@ public interface SampleProvider { int PROVIDER_PEBBLE_GADGETBRIDGE = 2; // removed int PROVIDER_PEBBLE_MISFIT = 3; int PROVIDER_PEBBLE_HEALTH = 4; + int PROVIDER_MIBAND2 = 5; int PROVIDER_UNKNOWN = 100; // TODO: can also be removed diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/AbstractMiBandSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/AbstractMiBandSampleProvider.java new file mode 100644 index 000000000..8bed55449 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/AbstractMiBandSampleProvider.java @@ -0,0 +1,59 @@ +package nodomain.freeyourgadget.gadgetbridge.devices.miband; + +import android.support.annotation.NonNull; + +import de.greenrobot.dao.AbstractDao; +import de.greenrobot.dao.Property; +import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider; +import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; +import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample; +import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; + +/** + * Base class for Mi1 and Mi2 sample providers. At the moment they both share the + * same activity sample class. + */ +public abstract class AbstractMiBandSampleProvider extends AbstractSampleProvider { + + // maybe this should be configurable 256 seems way off, though. + private final float movementDivisor = 180.0f; //256.0f; + + public AbstractMiBandSampleProvider(GBDevice device, DaoSession session) { + super(device, session); + } + + @Override + public float normalizeIntensity(int rawIntensity) { + return rawIntensity / movementDivisor; + } + + @Override + public AbstractDao getSampleDao() { + return getSession().getMiBandActivitySampleDao(); + } + + @NonNull + @Override + protected Property getTimestampSampleProperty() { + return MiBandActivitySampleDao.Properties.Timestamp; + } + + @NonNull + @Override + protected Property getDeviceIdentifierSampleProperty() { + return MiBandActivitySampleDao.Properties.DeviceId; + } + + @Override + protected Property getRawKindSampleProperty() { + return MiBandActivitySampleDao.Properties.RawKind; + } + + @Override + public MiBandActivitySample createActivitySample() { + return new MiBandActivitySample(); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2SampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2SampleProvider.java new file mode 100644 index 000000000..9b7b2c66d --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2SampleProvider.java @@ -0,0 +1,64 @@ +package nodomain.freeyourgadget.gadgetbridge.devices.miband; + +import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; +import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; + +public class MiBand2SampleProvider extends AbstractMiBandSampleProvider { + // these are all bogus atm (come from Mi1) + public static final int TYPE_DEEP_SLEEP = 11; + public static final int TYPE_LIGHT_SLEEP = 5; + public static final int TYPE_ACTIVITY = -1; + public static final int TYPE_UNKNOWN = -1; + public static final int TYPE_NONWEAR = 3; + public static final int TYPE_CHARGING = 6; + + // observed the following values so far: + // 00 01 02 09 0a 0b 0c 10 11 + + public MiBand2SampleProvider(GBDevice device, DaoSession session) { + super(device, session); + } + + @Override + public int getID() { + return SampleProvider.PROVIDER_MIBAND2; + } + + @Override + public int normalizeType(int rawType) { + switch (rawType) { + case TYPE_DEEP_SLEEP: + return ActivityKind.TYPE_DEEP_SLEEP; + case TYPE_LIGHT_SLEEP: + return ActivityKind.TYPE_LIGHT_SLEEP; + case TYPE_ACTIVITY: + return ActivityKind.TYPE_ACTIVITY; + case TYPE_NONWEAR: + return ActivityKind.TYPE_NOT_WORN; + case TYPE_CHARGING: + return ActivityKind.TYPE_NOT_WORN; //I believe it's a safe assumption + default: +// case TYPE_UNKNOWN: // fall through + return ActivityKind.TYPE_UNKNOWN; + } + } + + @Override + public int toRawActivityKind(int activityKind) { + switch (activityKind) { + case ActivityKind.TYPE_ACTIVITY: + return TYPE_ACTIVITY; + case ActivityKind.TYPE_DEEP_SLEEP: + return TYPE_DEEP_SLEEP; + case ActivityKind.TYPE_LIGHT_SLEEP: + return TYPE_LIGHT_SLEEP; + case ActivityKind.TYPE_NOT_WORN: + return TYPE_NONWEAR; + case ActivityKind.TYPE_UNKNOWN: // fall through + default: + return TYPE_UNKNOWN; + } + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java index 069c9ba16..eee28a3e7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandCoordinator.java @@ -80,7 +80,7 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator { @Override public SampleProvider getSampleProvider(GBDevice device, DaoSession session) { - return new MiBandSampleProvider(device, session); + return new MiBand2SampleProvider(device, session); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandSampleProvider.java index 13f77224f..8f2a95c7d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandSampleProvider.java @@ -1,16 +1,11 @@ package nodomain.freeyourgadget.gadgetbridge.devices.miband; -import de.greenrobot.dao.AbstractDao; -import de.greenrobot.dao.Property; -import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; -import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample; -import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; -public class MiBandSampleProvider extends AbstractSampleProvider { +public class MiBandSampleProvider extends AbstractMiBandSampleProvider { public static final int TYPE_DEEP_SLEEP = 4; public static final int TYPE_LIGHT_SLEEP = 5; public static final int TYPE_ACTIVITY = -1; @@ -26,13 +21,15 @@ public class MiBandSampleProvider extends AbstractSampleProvider getSampleDao() { - return getSession().getMiBandActivitySampleDao(); - } - - @Override - protected Property getTimestampSampleProperty() { - return MiBandActivitySampleDao.Properties.Timestamp; - } - - @Override - protected Property getDeviceIdentifierSampleProperty() { - return MiBandActivitySampleDao.Properties.DeviceId; - } - - @Override - protected Property getRawKindSampleProperty() { - return MiBandActivitySampleDao.Properties.RawKind; - } - - @Override - public MiBandActivitySample createActivitySample() { - return new MiBandActivitySample(); - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java index bb4b87bce..fa5facb7c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java @@ -35,6 +35,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WaitAction; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBand2Support; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.AbstractMiBand2Operation; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; +import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; /** @@ -133,6 +134,8 @@ public class FetchActivityOperation extends AbstractMiBand2Operation { } sampleProvider.addGBActivitySamples(samples.toArray(new MiBandActivitySample[0])); + 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 {