HPlus: Start detecting band not worn

This commit is contained in:
João Paulo Barraca 2017-05-22 23:19:43 +01:00
parent 013cbf139a
commit bd754b4130
4 changed files with 99 additions and 51 deletions

View File

@ -79,7 +79,6 @@ public final class HPlusConstants {
public static final byte ARG_FINDME_ON = 0x01;
public static final byte ARG_FINDME_OFF = 0x02;
public static final byte CMD_GET_VERSION = 0x17;
public static final byte CMD_SET_END = 0x4f;
public static final byte CMD_SET_INCOMING_CALL_NUMBER = 0x23;
public static final byte CMD_SET_ALLDAY_HRM = 0x35;
@ -89,7 +88,8 @@ public final class HPlusConstants {
public static final byte CMD_SET_SIT_INTERVAL = 0x51;
public static final byte CMD_SET_HEARTRATE_STATE = 0x32;
//Actions to device
//GET messages
public static final byte CMD_GET_VERSION = 0x17;
public static final byte CMD_GET_ACTIVE_DAY = 0x27;
public static final byte CMD_GET_DAY_DATA = 0x15;
public static final byte CMD_GET_SLEEP = 0x19;
@ -122,7 +122,7 @@ public final class HPlusConstants {
public static final byte DATA_SLEEP = 0x1A;
public static final byte DATA_VERSION = 0x18;
public static final byte DATA_VERSION1 = 0x2E;
public static final byte DATA_DAY_UNKNOWN = 0x52; //To be defined
public static final byte DATA_UNKNOWN = 0x4d;
public static final String PREF_HPLUS_SCREENTIME = "hplus_screentime";

View File

@ -21,6 +21,7 @@ package nodomain.freeyourgadget.gadgetbridge.devices.hplus;
*/
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.Calendar;
import java.util.Collections;
@ -168,7 +169,7 @@ public class HPlusHealthSampleProvider extends AbstractSampleProvider<HPlusHealt
for (HPlusHealthActivitySample sample : samples) {
if (sample.getTimestamp() >= overlay.getTimestampFrom() && sample.getTimestamp() < overlay.getTimestampTo()) {
if(overlay.getRawKind() == ActivityKind.TYPE_LIGHT_SLEEP || overlay.getRawKind() == ActivityKind.TYPE_DEEP_SLEEP) {
if (overlay.getRawKind() == ActivityKind.TYPE_NOT_WORN || overlay.getRawKind() == ActivityKind.TYPE_LIGHT_SLEEP || overlay.getRawKind() == ActivityKind.TYPE_DEEP_SLEEP) {
if (sample.getRawKind() == HPlusDataRecord.TYPE_DAY_SLOT && sample.getSteps() > 0){
nonSleepTimeEnd = sample.getTimestamp() + 10 * 60; // 10 minutes
continue;
@ -176,7 +177,12 @@ public class HPlusHealthSampleProvider extends AbstractSampleProvider<HPlusHealt
continue;
}
if (overlay.getRawKind() == ActivityKind.TYPE_NOT_WORN)
sample.setHeartRate(0);
if (sample.getRawKind() != ActivityKind.TYPE_NOT_WORN)
sample.setRawKind(overlay.getRawKind());
sample.setRawIntensity(10);
}
}

View File

@ -232,6 +232,8 @@ class HPlusHandlerThread extends GBDeviceIoThread {
return false;
}
LOG.info("SLOT: " + record);
Calendar now = GregorianCalendar.getInstance();
int nowSlot = now.get(Calendar.HOUR_OF_DAY) * 6 + (now.get(Calendar.MINUTE) / 10);
if (record.slot == nowSlot){
@ -287,6 +289,8 @@ class HPlusHandlerThread extends GBDeviceIoThread {
}
});
List<Integer> notWornSlots = new ArrayList<>();
try (DBHandler dbHandler = GBApplication.acquireDB()) {
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
List<HPlusHealthActivitySample> samples = new ArrayList<>();
@ -301,16 +305,55 @@ class HPlusHandlerThread extends GBDeviceIoThread {
sample.setRawHPlusHealthData(storedRecord.getRawData());
sample.setSteps(storedRecord.steps);
sample.setRawIntensity(storedRecord.intensity);
sample.setHeartRate(storedRecord.heartRate);
sample.setRawKind(storedRecord.type);
sample.setProvider(provider);
samples.add(sample);
if (HPlusCoordinator.getAllDayHR(gbDevice.getAddress()) == HPlusConstants.ARG_HEARTRATE_ALLDAY_ON && storedRecord.heartRate == ActivitySample.NOT_MEASURED && storedRecord.steps <= 0) {
notWornSlots.add(sample.getTimestamp());
notWornSlots.add(sample.getTimestamp() + 10 * 60);
}
}
provider.getSampleDao().insertOrReplaceInTx(samples);
mDaySlotRecords.clear();
//Create an overlay with unused slots
if (notWornSlots.size() > 0) {
DaoSession session = dbHandler.getDaoSession();
Long userId = DBHelper.getUser(session).getId();
Long deviceId = DBHelper.getDevice(getDevice(), session).getId();
HPlusHealthActivityOverlayDao overlayDao = session.getHPlusHealthActivityOverlayDao();
List<HPlusHealthActivityOverlay> overlayList = new ArrayList<>();
int firstSlotTimestamp = notWornSlots.get(0);
int lastSlotTimestamp = notWornSlots.get(0);
int i = 1;
for (Integer timestamp : notWornSlots) {
//If it is the last of the samples or of the interruption period
if (timestamp - lastSlotTimestamp > 10 * 60) {
overlayList.add(new HPlusHealthActivityOverlay(firstSlotTimestamp, lastSlotTimestamp, ActivityKind.TYPE_NOT_WORN, deviceId, userId, null));
firstSlotTimestamp = timestamp;
}
lastSlotTimestamp = timestamp;
}
if (firstSlotTimestamp != lastSlotTimestamp)
overlayList.add(new HPlusHealthActivityOverlay(firstSlotTimestamp, lastSlotTimestamp, ActivityKind.TYPE_NOT_WORN, deviceId, userId, null));
overlayDao.insertOrReplaceInTx(overlayList);
}
} catch (GBException ex) {
LOG.info((ex.getMessage()));
} catch (Exception ex) {
@ -393,7 +436,7 @@ class HPlusHandlerThread extends GBDeviceIoThread {
LOG.info((e.getMessage()));
return false;
}
LOG.info("RealTime: " + record);
//Skip duplicated messages as the device seems to send the same record multiple times
//This can be used to detect the user is moving (not sleeping)
if (prevRealTimeRecord != null && record.same(prevRealTimeRecord))
@ -507,7 +550,7 @@ class HPlusHandlerThread extends GBDeviceIoThread {
int hwMinor = data[1] & 0xFF;
getDevice().setFirmwareVersion2(hwMajor + "." + hwMinor);
mHPlusSupport.setUnicodeSupport((data[3] != 0));
mHPlusSupport.setUnicodeSupport(data[3] != 0);
} else {
major = data[2] & 0xFF;
minor = data[1] & 0xFF;

View File

@ -819,7 +819,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
cs = HPlusConstants.transliterateMap.get(c);
} else {
try {
if(HPlusCoordinator.getUnicodeSupport(this.gbDevice.getAddress()))
if(unicode)
cs = c.toString().getBytes("Unicode");
else