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

HPlus: Support reconnection when messages are to be sent.

This commit is contained in:
João Paulo Barraca 2017-04-21 18:58:31 +01:00
parent 33c9752fde
commit 70663af35a
2 changed files with 61 additions and 58 deletions

View File

@ -24,6 +24,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.hplus;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -65,7 +66,7 @@ class HPlusHandlerThread extends GBDeviceIoThread {
private int DAY_SUMMARY_SYNC_PERIOD = 24 * 60 * 60;
private int DAY_SUMMARY_SYNC_RETRY_PERIOD = 30;
private int HELLO_PERIOD = 60;
private int HELLO_PERIOD = 60 * 2;
private boolean mQuit = false;
private HPlusSupport mHPlusSupport;
@ -122,11 +123,6 @@ class HPlusHandlerThread extends GBDeviceIoThread {
break;
}
if(!mHPlusSupport.getDevice().isConnected()){
quit();
break;
}
Calendar now = GregorianCalendar.getInstance();
if (now.compareTo(mGetDaySlotsTime) > 0) {
@ -142,22 +138,29 @@ class HPlusHandlerThread extends GBDeviceIoThread {
}
if(now.compareTo(mHelloTime) > 0){
LOG.info("Sending hello");
sendHello();
}
now = GregorianCalendar.getInstance();
waitTime = Math.min(mGetDaySummaryTime.getTimeInMillis(), Math.min(mGetDaySlotsTime.getTimeInMillis(), mGetSleepTime.getTimeInMillis())) - now.getTimeInMillis();
waitTime = Math.min(mGetDaySummaryTime.getTimeInMillis(), Math.min(mGetDaySlotsTime.getTimeInMillis(), Math.min(mHelloTime.getTimeInMillis(), mGetSleepTime.getTimeInMillis()))) - now.getTimeInMillis();
}
}
@Override
public void quit() {
LOG.info("HPlus: Quit Handler Thread");
mQuit = true;
synchronized (waitObject) {
waitObject.notify();
}
StackTraceElement l[] = Thread.currentThread().getStackTrace();
for(StackTraceElement e: l){
LOG.warn(e.toString());
}
}
public void sync() {
LOG.info("HPlus: Starting data synchronization");
@ -185,9 +188,9 @@ class HPlusHandlerThread extends GBDeviceIoThread {
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_VERSION});
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_CURR_DATA});
builder.queue(mHPlusSupport.getQueue());
mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){
LOG.warn("HPlus: Synchronization exception: " + e);
}
synchronized (waitObject) {
@ -197,18 +200,19 @@ class HPlusHandlerThread extends GBDeviceIoThread {
public void sendHello(){
try {
if(!mHPlusSupport.isConnected())
mHPlusSupport.connect();
TransactionBuilder builder = new TransactionBuilder("hello");
builder.write(mHPlusSupport.ctrlCharacteristic, HPlusConstants.CMD_ACTION_HELLO);
builder.queue(mHPlusSupport.getQueue());
mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){
}
mHelloTime = GregorianCalendar.getInstance();
mHelloTime.add(Calendar.SECOND, HELLO_PERIOD);
synchronized (waitObject) {
waitObject.notify();
}
}
/**
* Process a message containing information regarding a day slot
@ -508,12 +512,9 @@ class HPlusHandlerThread extends GBDeviceIoThread {
*/
private void requestNextSleepData() {
try {
if(!mHPlusSupport.isConnected())
mHPlusSupport.connect();
TransactionBuilder builder = new TransactionBuilder("requestSleepStats");
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_SLEEP});
builder.queue(mHPlusSupport.getQueue());
mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){
}
@ -565,12 +566,10 @@ class HPlusHandlerThread extends GBDeviceIoThread {
byte[] msg = new byte[]{HPlusConstants.CMD_GET_ACTIVE_DAY, hour, minute, nextHour, nextMinute};
try {
if(!mHPlusSupport.isConnected())
mHPlusSupport.connect();
TransactionBuilder builder = new TransactionBuilder("getNextDaySlot");
builder.write(mHPlusSupport.ctrlCharacteristic, msg);
builder.queue(mHPlusSupport.getQueue());
mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){
}
@ -580,12 +579,9 @@ class HPlusHandlerThread extends GBDeviceIoThread {
*/
public void requestDaySummaryData(){
try {
if(!mHPlusSupport.isConnected())
mHPlusSupport.connect();
TransactionBuilder builder = new TransactionBuilder("startSyncDaySummary");
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_DAY_DATA});
builder.queue(mHPlusSupport.getQueue());
mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){
}
@ -617,4 +613,8 @@ class HPlusHandlerThread extends GBDeviceIoThread {
return sample;
}
public void setHPlusSupport(HPlusSupport HPlusSupport) {
LOG.info("Updating HPlusSupport object");
this.mHPlusSupport = HPlusSupport;
}
}

View File

@ -122,24 +122,29 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
measureCharacteristic = getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_MEASURE);
ctrlCharacteristic = getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_CONTROL);
getDevice().setFirmwareVersion("N/A");
getDevice().setFirmwareVersion2("N/A");
syncHelper = new HPlusHandlerThread(getDevice(), getContext(), this);
//Initialize device
sendUserInfo(builder); //Sync preferences
requestDeviceInfo(builder);
setInitialized(builder);
syncHelper.start();
builder.notify(getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_MEASURE), true);
builder.setGattCallback(this);
builder.notify(measureCharacteristic, true);
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext()));
if(syncHelper != null){
syncHelper.setHPlusSupport(this);
}else {
syncHelper = new HPlusHandlerThread(getDevice(), getContext(), this);
syncHelper.start();
}
syncHelper.sync();
getDevice().setFirmwareVersion("N/A");
getDevice().setFirmwareVersion2("N/A");
requestDeviceInfo(builder);
LOG.info("Initialization Done");
return builder;
}
@ -405,11 +410,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
return this;
}
private void setInitialized(TransactionBuilder builder) {
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext()));
}
@Override
public boolean useAutoConnect() {
return true;
@ -433,13 +433,13 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override
public void onSetTime() {
try {
TransactionBuilder builder = performInitialized("time");
setCurrentDate(builder);
setCurrentTime(builder);
builder.queue(getQueue());
performConnected(builder.getTransaction());
}catch(IOException e){
}
@ -447,6 +447,8 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
try {
TransactionBuilder builder = performInitialized("alarm");
@ -468,7 +470,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
}
setAlarm(builder, null);
builder.queue(getQueue());
performConnected(builder.getTransaction());
GB.toast(getContext(), getContext().getString(R.string.user_feedback_all_alarms_disabled), Toast.LENGTH_SHORT, GB.INFO);
}catch(Exception e){}
@ -494,7 +496,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override
public void onSetMusicState(MusicStateSpec stateSpec) {
}
@Override
@ -539,7 +540,12 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override
public void onFetchActivityData() {
if (syncHelper != null)
if (syncHelper == null){
syncHelper = new HPlusHandlerThread(gbDevice, getContext(), this);
syncHelper.start();
}
syncHelper.sync();
}
@ -550,7 +556,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
TransactionBuilder builder = performInitialized("Shutdown");
builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SHUTDOWN, HPlusConstants.ARG_SHUTDOWN_EN});
builder.queue(getQueue());
performConnected(builder.getTransaction());
}catch(Exception e){
}
@ -563,7 +569,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
TransactionBuilder builder = performInitialized("HeartRateTest");
builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SET_HEARTRATE_STATE, HPlusConstants.ARG_HEARTRATE_MEASURE_ON}); //Set Real Time... ?
builder.queue(getQueue());
performConnected(builder.getTransaction());
}catch(Exception e){
}
@ -571,7 +577,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override
public void onEnableRealtimeHeartRateMeasurement(boolean enable) {
getQueue().clear();
try {
TransactionBuilder builder = performInitialized("realTimeHeartMeasurement");
byte state;
@ -582,7 +587,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
state = HPlusConstants.ARG_HEARTRATE_ALLDAY_OFF;
builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SET_ALLDAY_HRM, state});
builder.queue(getQueue());
performConnected(builder.getTransaction());
}catch(Exception e){
}
@ -594,7 +599,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
TransactionBuilder builder = performInitialized("findMe");
setFindMe(builder, start);
builder.queue(getQueue());
performConnected(builder.getTransaction());
} catch (IOException e) {
GB.toast(getContext(), "Error toggling Find Me: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
}
@ -603,8 +608,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override
public void onSetConstantVibration(int intensity) {
getQueue().clear();
try {
TransactionBuilder builder = performInitialized("vibration");
@ -615,7 +618,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
msg[i + 1] = (byte) "GadgetBridge".charAt(i);
builder.write(ctrlCharacteristic, msg);
builder.queue(getQueue());
performConnected(builder.getTransaction());
} catch (IOException e) {
GB.toast(getContext(), "Error setting Vibration: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
}
@ -710,7 +713,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
builder.write(ctrlCharacteristic, msg);
builder.queue(getQueue());
performConnected(builder.getTransaction());
} catch (IOException e) {
GB.toast(getContext(), "Error showing incoming call: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
@ -718,7 +721,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
}
private void showText(String title, String body) {
try {
TransactionBuilder builder = performInitialized("notification");
@ -772,7 +774,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
msg[2] = (byte) remaining;
builder.write(ctrlCharacteristic, msg);
builder.queue(getQueue());
performConnected(builder.getTransaction());
} catch (IOException e) {
GB.toast(getContext(), "Error showing device Notification: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
@ -782,6 +784,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
private void close() {
if (syncHelper != null) {
syncHelper.quit();
syncHelper.interrupt();
syncHelper = null;
}
}