1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 21:06:50 +01:00

Mi2: more work on activity data #323

This commit is contained in:
cpfeiffer 2016-11-20 23:33:46 +01:00
parent 82cd06f4c1
commit 0c51f86afc
6 changed files with 134 additions and 45 deletions

View File

@ -22,6 +22,7 @@ public interface SampleProvider<T extends AbstractActivitySample> {
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

View File

@ -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<MiBandActivitySample> {
// 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<MiBandActivitySample, ?> 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();
}
}

View File

@ -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;
}
}
}

View File

@ -80,7 +80,7 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator {
@Override
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(GBDevice device, DaoSession session) {
return new MiBandSampleProvider(device, session);
return new MiBand2SampleProvider(device, session);
}
@Override

View File

@ -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<MiBandActivitySample> {
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<MiBandActivityS
// public static final byte TYPE_USER = 100;
// public static final byte TYPE_WALKING = 1;
// maybe this should be configurable 256 seems way off, though.
private final float movementDivisor = 180.0f; //256.0f;
public MiBandSampleProvider(GBDevice device, DaoSession session) {
super(device, session);
}
@Override
public int getID() {
return SampleProvider.PROVIDER_MIBAND;
}
@Override
public int normalizeType(int rawType) {
switch (rawType) {
@ -68,39 +65,4 @@ public class MiBandSampleProvider extends AbstractSampleProvider<MiBandActivityS
return TYPE_UNKNOWN;
}
}
@Override
public float normalizeIntensity(int rawIntensity) {
return rawIntensity / movementDivisor;
}
@Override
public int getID() {
return SampleProvider.PROVIDER_MIBAND;
}
@Override
public AbstractDao<MiBandActivitySample, ?> 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();
}
}

View File

@ -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 {