2017-01-02 01:58:37 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @author João Paulo Barraca <jpbarraca@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
2017-01-05 00:41:35 +01:00
|
|
|
import java.util.GregorianCalendar;
|
2017-01-02 01:58:37 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusConstants;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlayDao;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceIoThread;
|
|
|
|
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
class HPlusHandlerThread extends GBDeviceIoThread {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(HPlusHandlerThread.class);
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
private int SYNC_PERIOD = 60 * 10;
|
|
|
|
private int SYNC_RETRY_PERIOD = 6;
|
|
|
|
private int SLEEP_SYNC_PERIOD = 12 * 60 * 60;
|
|
|
|
private int SLEEP_RETRY_PERIOD = 30;
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
private int HELLO_INTERVAL = 60;
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
private boolean mQuit = false;
|
|
|
|
private HPlusSupport mHPlusSupport;
|
|
|
|
private int mLastSlotReceived = 0;
|
|
|
|
private int mLastSlotRequested = 0;
|
|
|
|
|
2017-01-05 00:41:35 +01:00
|
|
|
private Calendar mLastSleepDayReceived = GregorianCalendar.getInstance();
|
|
|
|
private Calendar mHelloTime = GregorianCalendar.getInstance();
|
|
|
|
private Calendar mGetDaySlotsTime = GregorianCalendar.getInstance();
|
|
|
|
private Calendar mGetSleepTime = GregorianCalendar.getInstance();
|
2017-01-02 01:58:37 +01:00
|
|
|
|
2017-01-05 00:41:35 +01:00
|
|
|
private HPlusDataRecordRealtime prevRealTimeRecord = null;
|
2017-01-02 01:58:37 +01:00
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
private final Object waitObject = new Object();
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
public HPlusHandlerThread(GBDevice gbDevice, Context context, HPlusSupport hplusSupport) {
|
|
|
|
super(gbDevice, context);
|
|
|
|
|
|
|
|
mQuit = false;
|
|
|
|
|
|
|
|
mHPlusSupport = hplusSupport;
|
|
|
|
|
|
|
|
mLastSleepDayReceived.setTimeInMillis(0);
|
|
|
|
mGetSleepTime.setTimeInMillis(0);
|
|
|
|
mGetDaySlotsTime.setTimeInMillis(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
mQuit = false;
|
|
|
|
|
|
|
|
sync();
|
|
|
|
|
|
|
|
long waitTime = 0;
|
|
|
|
while (!mQuit) {
|
|
|
|
//LOG.debug("Waiting " + (waitTime));
|
|
|
|
if (waitTime > 0) {
|
|
|
|
synchronized (waitObject) {
|
|
|
|
try {
|
|
|
|
waitObject.wait(waitTime);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mQuit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-05 00:41:35 +01:00
|
|
|
Calendar now = GregorianCalendar.getInstance();
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
if (now.compareTo(mHelloTime) > 0) {
|
|
|
|
sendHello();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now.compareTo(mGetDaySlotsTime) > 0) {
|
|
|
|
requestNextDaySlots();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now.compareTo(mGetSleepTime) > 0) {
|
|
|
|
requestNextSleepData();
|
|
|
|
}
|
|
|
|
|
2017-01-05 00:41:35 +01:00
|
|
|
now = GregorianCalendar.getInstance();
|
2017-01-02 01:58:37 +01:00
|
|
|
waitTime = Math.min(Math.min(mGetDaySlotsTime.getTimeInMillis(), mGetSleepTime.getTimeInMillis()), mHelloTime.getTimeInMillis()) - now.getTimeInMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void quit() {
|
|
|
|
mQuit = true;
|
|
|
|
synchronized (waitObject) {
|
|
|
|
waitObject.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sync() {
|
|
|
|
mGetSleepTime.setTimeInMillis(0);
|
|
|
|
mGetDaySlotsTime.setTimeInMillis(0);
|
|
|
|
|
|
|
|
TransactionBuilder builder = new TransactionBuilder("startSyncDayStats");
|
|
|
|
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_SLEEP});
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_DAY_DATA});
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_ACTIVE_DAY});
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_DEVICE_ID});
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_VERSION});
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_CURR_DATA});
|
|
|
|
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SET_ALLDAY_HRM, HPlusConstants.ARG_HEARTRATE_ALLDAY_ON});
|
|
|
|
|
|
|
|
builder.queue(mHPlusSupport.getQueue());
|
|
|
|
|
|
|
|
synchronized (waitObject) {
|
|
|
|
waitObject.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
private void sendHello() {
|
2017-01-02 01:58:37 +01:00
|
|
|
TransactionBuilder builder = new TransactionBuilder("hello");
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, HPlusConstants.CMD_ACTION_HELLO);
|
|
|
|
|
|
|
|
builder.queue(mHPlusSupport.getQueue());
|
2017-01-04 02:46:24 +01:00
|
|
|
scheduleHello();
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
public void scheduleHello(){
|
2017-01-05 00:41:35 +01:00
|
|
|
mHelloTime = GregorianCalendar.getInstance();
|
2017-01-04 02:46:24 +01:00
|
|
|
mHelloTime.add(Calendar.SECOND, HELLO_INTERVAL);
|
|
|
|
}
|
2017-01-02 01:58:37 +01:00
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
public boolean processIncomingDaySlotData(byte[] data) {
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
HPlusDataRecordDay record;
|
|
|
|
try{
|
|
|
|
record = new HPlusDataRecordDay(data);
|
|
|
|
} catch(IllegalArgumentException e){
|
|
|
|
LOG.debug((e.getMessage()));
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((record.slot == 0 && mLastSlotReceived == 0) || (record.slot == mLastSlotReceived + 1)) {
|
|
|
|
mLastSlotReceived = record.slot;
|
|
|
|
|
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
|
|
|
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
|
|
|
|
|
|
|
|
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
|
|
|
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
|
|
|
HPlusHealthActivitySample sample = new HPlusHealthActivitySample(
|
|
|
|
record.timestamp, // ts
|
|
|
|
deviceId, userId, // User id
|
|
|
|
record.getRawData(), // Raw Data
|
|
|
|
ActivityKind.TYPE_UNKNOWN,
|
2017-01-04 02:46:24 +01:00
|
|
|
0, // Intensity
|
2017-01-02 01:58:37 +01:00
|
|
|
record.steps, // Steps
|
|
|
|
record.heartRate, // HR
|
|
|
|
ActivitySample.NOT_MEASURED, // Distance
|
|
|
|
ActivitySample.NOT_MEASURED // Calories
|
|
|
|
);
|
|
|
|
sample.setProvider(provider);
|
|
|
|
|
|
|
|
provider.addGBActivitySample(sample);
|
|
|
|
|
|
|
|
} catch (GBException ex) {
|
|
|
|
LOG.debug((ex.getMessage()));
|
|
|
|
} catch (Exception ex) {
|
|
|
|
LOG.debug(ex.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (record.slot >= mLastSlotRequested) {
|
|
|
|
synchronized (waitObject) {
|
|
|
|
mGetDaySlotsTime.setTimeInMillis(0);
|
|
|
|
waitObject.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void requestNextDaySlots() {
|
|
|
|
//Sync Day Stats
|
|
|
|
byte hour = (byte) ((mLastSlotReceived) / 6);
|
|
|
|
byte nextHour = (byte) (hour + 1);
|
|
|
|
|
|
|
|
byte nextMinute = 0;
|
|
|
|
|
2017-01-05 00:41:35 +01:00
|
|
|
if (nextHour == (byte) GregorianCalendar.getInstance().get(Calendar.HOUR_OF_DAY)) {
|
|
|
|
nextMinute = (byte) GregorianCalendar.getInstance().get(Calendar.MINUTE);
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
byte minute = (byte) ((mLastSlotReceived % 6) * 10);
|
|
|
|
|
|
|
|
mLastSlotRequested = (nextHour) * 6 + Math.round(nextMinute / 10);
|
|
|
|
|
|
|
|
if (nextHour >= 24 && nextMinute > 0) { // 24 * 6
|
|
|
|
LOG.debug("Reached End of the Day");
|
|
|
|
mLastSlotRequested = 0;
|
|
|
|
mLastSlotReceived = 0;
|
2017-01-05 00:41:35 +01:00
|
|
|
mGetDaySlotsTime = GregorianCalendar.getInstance();
|
2017-01-02 01:58:37 +01:00
|
|
|
mGetDaySlotsTime.add(Calendar.SECOND, SYNC_PERIOD);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-05 00:41:35 +01:00
|
|
|
if (nextHour > GregorianCalendar.getInstance().get(GregorianCalendar.HOUR_OF_DAY)) {
|
2017-01-02 01:58:37 +01:00
|
|
|
LOG.debug("Day data is up to date");
|
2017-01-05 00:41:35 +01:00
|
|
|
mGetDaySlotsTime = GregorianCalendar.getInstance();
|
2017-01-02 01:58:37 +01:00
|
|
|
mGetDaySlotsTime.add(Calendar.SECOND, SYNC_PERIOD);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
//LOG.debug("Making new Request From " + hour + ":" + minute + " to " + nextHour + ":" + nextMinute);
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
byte[] msg = new byte[]{39, hour, minute, nextHour, nextMinute}; //Request the entire day
|
|
|
|
TransactionBuilder builder = new TransactionBuilder("getNextDaySlot");
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, msg);
|
|
|
|
builder.queue(mHPlusSupport.getQueue());
|
|
|
|
|
2017-01-05 00:41:35 +01:00
|
|
|
mGetDaySlotsTime = GregorianCalendar.getInstance();
|
2017-01-02 01:58:37 +01:00
|
|
|
mGetDaySlotsTime.add(Calendar.SECOND, SYNC_RETRY_PERIOD);
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
public boolean processIncomingSleepData(byte[] data){
|
2017-01-02 01:58:37 +01:00
|
|
|
HPlusDataRecordSleep record;
|
|
|
|
|
|
|
|
try{
|
|
|
|
record = new HPlusDataRecordSleep(data);
|
|
|
|
} catch(IllegalArgumentException e){
|
|
|
|
LOG.debug((e.getMessage()));
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mLastSleepDayReceived.setTimeInMillis(record.bedTimeStart * 1000L);
|
|
|
|
|
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
|
|
|
DaoSession session = dbHandler.getDaoSession();
|
|
|
|
Long userId = DBHelper.getUser(session).getId();
|
|
|
|
Long deviceId = DBHelper.getDevice(getDevice(), session).getId();
|
|
|
|
|
|
|
|
HPlusHealthActivityOverlayDao overlayDao = session.getHPlusHealthActivityOverlayDao();
|
|
|
|
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
|
|
|
|
|
|
|
|
//Insert the Overlays
|
|
|
|
List<HPlusHealthActivityOverlay> overlayList = new ArrayList<>();
|
|
|
|
List<HPlusDataRecord.RecordInterval> intervals = record.getIntervals();
|
|
|
|
for(HPlusDataRecord.RecordInterval interval : intervals){
|
|
|
|
overlayList.add(new HPlusHealthActivityOverlay(interval.timestampFrom, interval.timestampTo, interval.activityKind, deviceId, userId, null));
|
|
|
|
}
|
|
|
|
|
|
|
|
overlayDao.insertOrReplaceInTx(overlayList);
|
|
|
|
|
|
|
|
//Store the data
|
|
|
|
HPlusHealthActivitySample sample = new HPlusHealthActivitySample(
|
|
|
|
record.timestamp, // ts
|
|
|
|
deviceId, userId, // User id
|
|
|
|
record.getRawData(), // Raw Data
|
|
|
|
record.activityKind,
|
2017-01-04 02:46:24 +01:00
|
|
|
0, // Intensity
|
2017-01-02 01:58:37 +01:00
|
|
|
ActivitySample.NOT_MEASURED, // Steps
|
|
|
|
ActivitySample.NOT_MEASURED, // HR
|
|
|
|
ActivitySample.NOT_MEASURED, // Distance
|
|
|
|
ActivitySample.NOT_MEASURED // Calories
|
|
|
|
);
|
|
|
|
|
|
|
|
sample.setProvider(provider);
|
|
|
|
|
|
|
|
provider.addGBActivitySample(sample);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
LOG.debug(ex.getMessage());
|
|
|
|
}
|
|
|
|
|
2017-01-05 00:41:35 +01:00
|
|
|
mGetSleepTime = GregorianCalendar.getInstance();
|
|
|
|
mGetSleepTime.add(GregorianCalendar.SECOND, SLEEP_SYNC_PERIOD);
|
2017-01-02 01:58:37 +01:00
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void requestNextSleepData() {
|
2017-01-05 00:41:35 +01:00
|
|
|
mGetSleepTime = GregorianCalendar.getInstance();
|
|
|
|
mGetSleepTime.add(GregorianCalendar.SECOND, SLEEP_RETRY_PERIOD);
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
TransactionBuilder builder = new TransactionBuilder("requestSleepStats");
|
|
|
|
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_SLEEP});
|
|
|
|
builder.queue(mHPlusSupport.getQueue());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
public boolean processRealtimeStats(byte[] data) {
|
2017-01-02 01:58:37 +01:00
|
|
|
HPlusDataRecordRealtime record;
|
|
|
|
|
|
|
|
try{
|
|
|
|
record = new HPlusDataRecordRealtime(data);
|
|
|
|
} catch(IllegalArgumentException e){
|
|
|
|
LOG.debug((e.getMessage()));
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(record.same(prevRealTimeRecord))
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
prevRealTimeRecord = record;
|
|
|
|
|
|
|
|
getDevice().setBatteryLevel(record.battery);
|
|
|
|
getDevice().sendDeviceUpdateIntent(getContext());
|
|
|
|
|
|
|
|
//Skip when measuring
|
|
|
|
if(record.heartRate == 255) {
|
|
|
|
getDevice().setFirmwareVersion2("---");
|
|
|
|
getDevice().sendDeviceUpdateIntent(getContext());
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
getDevice().setFirmwareVersion2("" + record.heartRate);
|
2017-01-02 01:58:37 +01:00
|
|
|
getDevice().sendDeviceUpdateIntent(getContext());
|
|
|
|
|
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
|
|
|
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
|
|
|
|
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
|
|
|
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
|
|
|
|
|
|
|
HPlusHealthActivitySample sample = new HPlusHealthActivitySample(
|
|
|
|
record.timestamp, // ts
|
|
|
|
deviceId, userId, // User id
|
|
|
|
record.getRawData(), // Raw Data
|
|
|
|
record.activityKind,
|
2017-01-04 02:46:24 +01:00
|
|
|
record.intensity, // Intensity
|
2017-01-02 01:58:37 +01:00
|
|
|
ActivitySample.NOT_MEASURED, // Steps
|
|
|
|
record.heartRate, // HR
|
|
|
|
record.distance, // Distance
|
|
|
|
record.calories // Calories
|
|
|
|
);
|
|
|
|
|
|
|
|
sample.setProvider(provider);
|
|
|
|
provider.addGBActivitySample(sample);
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
//TODO: Handle Active Time. With Overlay?
|
2017-01-02 01:58:37 +01:00
|
|
|
|
|
|
|
} catch (GBException ex) {
|
|
|
|
LOG.debug((ex.getMessage()));
|
|
|
|
} catch (Exception ex) {
|
|
|
|
LOG.debug(ex.getMessage());
|
|
|
|
}
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-04 02:46:24 +01:00
|
|
|
public boolean processStepStats(byte[] data) {
|
2017-01-02 01:58:37 +01:00
|
|
|
HPlusDataRecordSteps record;
|
|
|
|
|
|
|
|
try{
|
|
|
|
record = new HPlusDataRecordSteps(data);
|
|
|
|
} catch(IllegalArgumentException e){
|
|
|
|
LOG.debug((e.getMessage()));
|
2017-01-04 02:46:24 +01:00
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
|
|
|
HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
|
|
|
|
|
|
|
|
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
|
|
|
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
2017-01-04 02:46:24 +01:00
|
|
|
|
|
|
|
//Hugly (?) fix.
|
|
|
|
//This message returns the day summary, but the DB already has some detailed entries with steps and distance.
|
|
|
|
//However DB data is probably incomplete as some update messages could be missing
|
|
|
|
//Proposed fix: Calculate the total steps and distance and store a new sample with the remaining data
|
|
|
|
//Existing data will reflect user activity with the issue of a potencially large number of steps at midnight.
|
|
|
|
//Steps counters by day will be OK with this
|
|
|
|
|
|
|
|
List<HPlusHealthActivitySample> samples = provider.getActivitySamples(record.timestamp - 3600 * 24 + 1, record.timestamp);
|
|
|
|
|
|
|
|
int missingDistance = record.distance;
|
|
|
|
int missingSteps = record.steps;
|
|
|
|
|
|
|
|
for(HPlusHealthActivitySample sample : samples){
|
|
|
|
if(sample.getSteps() > 0) {
|
|
|
|
missingSteps -= sample.getSteps();
|
|
|
|
}
|
|
|
|
if(sample.getDistance() > 0){
|
|
|
|
missingDistance -= sample.getDistance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:58:37 +01:00
|
|
|
HPlusHealthActivitySample sample = new HPlusHealthActivitySample(
|
|
|
|
record.timestamp, // ts
|
|
|
|
deviceId, userId, // User id
|
|
|
|
record.getRawData(), // Raw Data
|
|
|
|
ActivityKind.TYPE_UNKNOWN,
|
2017-01-04 02:46:24 +01:00
|
|
|
0, // Intensity
|
|
|
|
Math.max( missingSteps, 0), // Steps
|
2017-01-02 01:58:37 +01:00
|
|
|
ActivitySample.NOT_MEASURED, // HR
|
2017-01-04 02:46:24 +01:00
|
|
|
Math.max( missingDistance, 0), // Distance
|
2017-01-02 01:58:37 +01:00
|
|
|
ActivitySample.NOT_MEASURED // Calories
|
|
|
|
);
|
2017-01-04 02:46:24 +01:00
|
|
|
|
2017-01-02 01:58:37 +01:00
|
|
|
sample.setProvider(provider);
|
|
|
|
provider.addGBActivitySample(sample);
|
|
|
|
} catch (GBException ex) {
|
|
|
|
LOG.debug((ex.getMessage()));
|
|
|
|
} catch (Exception ex) {
|
|
|
|
LOG.debug(ex.getMessage());
|
|
|
|
}
|
2017-01-04 02:46:24 +01:00
|
|
|
|
|
|
|
return true;
|
2017-01-02 01:58:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean processVersion(byte[] data) {
|
|
|
|
int major = data[2] & 0xFF;
|
|
|
|
int minor = data[1] & 0xFF;
|
|
|
|
|
|
|
|
getDevice().setFirmwareVersion(major + "." + minor);
|
|
|
|
|
|
|
|
getDevice().sendDeviceUpdateIntent(getContext());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|