Mi2/Bip: Various sleep fixes

- Long standing bug fixed in determinePreviousValidActivityType()
  it always got the very fist valid sample, not the nearest in the past
  (for me that was light sleep, so every timespan queried began with light sleep)
- Support type 2 (running) and 12 (wakeup(?))
- Support new firmwares (Mi2 and Bip) by masking only the last 4 bits (0xf),
  takes some ugly query hack in determinePreviousValidActivityType() since
  greenDAO does not support the & operator (even though sqlite does)
This commit is contained in:
Andreas Shimokawa 2017-09-29 17:21:16 +02:00
parent 0ffa2ce45a
commit 9f05aff11b
1 changed files with 12 additions and 5 deletions

View File

@ -50,13 +50,13 @@ public class MiBand2SampleProvider extends AbstractMiBandSampleProvider {
public static final int TYPE_UNSET = -1; public static final int TYPE_UNSET = -1;
public static final int TYPE_NO_CHANGE = 0; public static final int TYPE_NO_CHANGE = 0;
public static final int TYPE_ACTIVITY = 1; public static final int TYPE_ACTIVITY = 1;
public static final int TYPE_RUNNING = 2;
public static final int TYPE_NONWEAR = 3; public static final int TYPE_NONWEAR = 3;
public static final int TYPE_CHARGING = 6; public static final int TYPE_CHARGING = 6;
public static final int TYPE_LIGHT_SLEEP = 9; public static final int TYPE_LIGHT_SLEEP = 9;
public static final int TYPE_IGNORE = 10;
public static final int TYPE_DEEP_SLEEP = 11; public static final int TYPE_DEEP_SLEEP = 11;
public static final int TYPE_WAKE_UP = 12; public static final int TYPE_WAKE_UP = 12;
// appears to be a measurement problem resulting in type = 10 and intensity = 20, at least with fw 1.0.0.39
public static final int TYPE_IGNORE = 10;
public MiBand2SampleProvider(GBDevice device, DaoSession session) { public MiBand2SampleProvider(GBDevice device, DaoSession session) {
super(device, session); super(device, session);
@ -81,6 +81,11 @@ public class MiBand2SampleProvider extends AbstractMiBandSampleProvider {
int lastValidKind = determinePreviousValidActivityType(samples.get(0)); int lastValidKind = determinePreviousValidActivityType(samples.get(0));
for (MiBandActivitySample sample : samples) { for (MiBandActivitySample sample : samples) {
int rawKind = sample.getRawKind(); int rawKind = sample.getRawKind();
if (rawKind != TYPE_UNSET) {
rawKind &= 0xf;
sample.setRawKind(rawKind);
}
switch (rawKind) { switch (rawKind) {
case TYPE_IGNORE: case TYPE_IGNORE:
case TYPE_NO_CHANGE: case TYPE_NO_CHANGE:
@ -100,11 +105,12 @@ public class MiBand2SampleProvider extends AbstractMiBandSampleProvider {
qb.where(MiBandActivitySampleDao.Properties.DeviceId.eq(sample.getDeviceId()), qb.where(MiBandActivitySampleDao.Properties.DeviceId.eq(sample.getDeviceId()),
MiBandActivitySampleDao.Properties.UserId.eq(sample.getUserId()), MiBandActivitySampleDao.Properties.UserId.eq(sample.getUserId()),
MiBandActivitySampleDao.Properties.Timestamp.lt(sample.getTimestamp()), MiBandActivitySampleDao.Properties.Timestamp.lt(sample.getTimestamp()),
MiBandActivitySampleDao.Properties.RawKind.notIn(TYPE_IGNORE, TYPE_NO_CHANGE)); MiBandActivitySampleDao.Properties.RawKind.notIn(TYPE_NO_CHANGE, TYPE_IGNORE, TYPE_UNSET, 16, 80, 96, 112)); // all I ever had that are 0 when doing &=0xf
qb.orderDesc(MiBandActivitySampleDao.Properties.Timestamp);
qb.limit(1); qb.limit(1);
List<MiBandActivitySample> result = qb.build().list(); List<MiBandActivitySample> result = qb.build().list();
if (result.size() > 0) { if (result.size() > 0) {
return result.get(0).getRawKind(); return result.get(0).getRawKind() & 0xf;
} }
return TYPE_UNSET; return TYPE_UNSET;
} }
@ -117,12 +123,13 @@ public class MiBand2SampleProvider extends AbstractMiBandSampleProvider {
case TYPE_LIGHT_SLEEP: case TYPE_LIGHT_SLEEP:
return ActivityKind.TYPE_LIGHT_SLEEP; return ActivityKind.TYPE_LIGHT_SLEEP;
case TYPE_ACTIVITY: case TYPE_ACTIVITY:
case TYPE_RUNNING:
case TYPE_WAKE_UP:
return ActivityKind.TYPE_ACTIVITY; return ActivityKind.TYPE_ACTIVITY;
case TYPE_NONWEAR: case TYPE_NONWEAR:
return ActivityKind.TYPE_NOT_WORN; return ActivityKind.TYPE_NOT_WORN;
case TYPE_CHARGING: case TYPE_CHARGING:
return ActivityKind.TYPE_NOT_WORN; //I believe it's a safe assumption return ActivityKind.TYPE_NOT_WORN; //I believe it's a safe assumption
case TYPE_IGNORE:
default: default:
case TYPE_UNSET: // fall through case TYPE_UNSET: // fall through
return ActivityKind.TYPE_UNKNOWN; return ActivityKind.TYPE_UNKNOWN;