1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 03:16:51 +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.Context;
import android.content.Intent; import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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_PERIOD = 24 * 60 * 60;
private int DAY_SUMMARY_SYNC_RETRY_PERIOD = 30; private int DAY_SUMMARY_SYNC_RETRY_PERIOD = 30;
private int HELLO_PERIOD = 60; private int HELLO_PERIOD = 60 * 2;
private boolean mQuit = false; private boolean mQuit = false;
private HPlusSupport mHPlusSupport; private HPlusSupport mHPlusSupport;
@ -122,11 +123,6 @@ class HPlusHandlerThread extends GBDeviceIoThread {
break; break;
} }
if(!mHPlusSupport.getDevice().isConnected()){
quit();
break;
}
Calendar now = GregorianCalendar.getInstance(); Calendar now = GregorianCalendar.getInstance();
if (now.compareTo(mGetDaySlotsTime) > 0) { if (now.compareTo(mGetDaySlotsTime) > 0) {
@ -142,23 +138,30 @@ class HPlusHandlerThread extends GBDeviceIoThread {
} }
if(now.compareTo(mHelloTime) > 0){ if(now.compareTo(mHelloTime) > 0){
LOG.info("Sending hello");
sendHello(); sendHello();
} }
now = GregorianCalendar.getInstance(); 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 @Override
public void quit() { public void quit() {
LOG.info("HPlus: Quit Handler Thread");
mQuit = true; mQuit = true;
synchronized (waitObject) { synchronized (waitObject) {
waitObject.notify(); waitObject.notify();
} }
StackTraceElement l[] = Thread.currentThread().getStackTrace();
for(StackTraceElement e: l){
LOG.warn(e.toString());
}
} }
public void sync() { public void sync() {
LOG.info("HPlus: Starting data synchronization"); 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_VERSION});
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_CURR_DATA}); builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_CURR_DATA});
builder.queue(mHPlusSupport.getQueue()); mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){ }catch(Exception e){
LOG.warn("HPlus: Synchronization exception: " + e);
} }
synchronized (waitObject) { synchronized (waitObject) {
@ -197,18 +200,19 @@ class HPlusHandlerThread extends GBDeviceIoThread {
public void sendHello(){ public void sendHello(){
try { try {
if(!mHPlusSupport.isConnected())
mHPlusSupport.connect();
TransactionBuilder builder = new TransactionBuilder("hello"); TransactionBuilder builder = new TransactionBuilder("hello");
builder.write(mHPlusSupport.ctrlCharacteristic, HPlusConstants.CMD_ACTION_HELLO); builder.write(mHPlusSupport.ctrlCharacteristic, HPlusConstants.CMD_ACTION_HELLO);
builder.queue(mHPlusSupport.getQueue()); mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){ }catch(Exception e){
} }
mHelloTime = GregorianCalendar.getInstance(); mHelloTime = GregorianCalendar.getInstance();
mHelloTime.add(Calendar.SECOND, HELLO_PERIOD); mHelloTime.add(Calendar.SECOND, HELLO_PERIOD);
synchronized (waitObject) {
waitObject.notify();
}
} }
/** /**
* Process a message containing information regarding a day slot * Process a message containing information regarding a day slot
@ -508,12 +512,9 @@ class HPlusHandlerThread extends GBDeviceIoThread {
*/ */
private void requestNextSleepData() { private void requestNextSleepData() {
try { try {
if(!mHPlusSupport.isConnected())
mHPlusSupport.connect();
TransactionBuilder builder = new TransactionBuilder("requestSleepStats"); TransactionBuilder builder = new TransactionBuilder("requestSleepStats");
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_SLEEP}); builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_SLEEP});
builder.queue(mHPlusSupport.getQueue()); mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){ }catch(Exception e){
} }
@ -565,12 +566,10 @@ class HPlusHandlerThread extends GBDeviceIoThread {
byte[] msg = new byte[]{HPlusConstants.CMD_GET_ACTIVE_DAY, hour, minute, nextHour, nextMinute}; byte[] msg = new byte[]{HPlusConstants.CMD_GET_ACTIVE_DAY, hour, minute, nextHour, nextMinute};
try { try {
if(!mHPlusSupport.isConnected())
mHPlusSupport.connect();
TransactionBuilder builder = new TransactionBuilder("getNextDaySlot"); TransactionBuilder builder = new TransactionBuilder("getNextDaySlot");
builder.write(mHPlusSupport.ctrlCharacteristic, msg); builder.write(mHPlusSupport.ctrlCharacteristic, msg);
builder.queue(mHPlusSupport.getQueue()); mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){ }catch(Exception e){
} }
@ -580,12 +579,9 @@ class HPlusHandlerThread extends GBDeviceIoThread {
*/ */
public void requestDaySummaryData(){ public void requestDaySummaryData(){
try { try {
if(!mHPlusSupport.isConnected())
mHPlusSupport.connect();
TransactionBuilder builder = new TransactionBuilder("startSyncDaySummary"); TransactionBuilder builder = new TransactionBuilder("startSyncDaySummary");
builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_DAY_DATA}); builder.write(mHPlusSupport.ctrlCharacteristic, new byte[]{HPlusConstants.CMD_GET_DAY_DATA});
builder.queue(mHPlusSupport.getQueue()); mHPlusSupport.performConnected(builder.getTransaction());
}catch(Exception e){ }catch(Exception e){
} }
@ -617,4 +613,8 @@ class HPlusHandlerThread extends GBDeviceIoThread {
return sample; 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); measureCharacteristic = getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_MEASURE);
ctrlCharacteristic = getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_CONTROL); ctrlCharacteristic = getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_CONTROL);
getDevice().setFirmwareVersion("N/A");
getDevice().setFirmwareVersion2("N/A");
syncHelper = new HPlusHandlerThread(getDevice(), getContext(), this);
//Initialize device //Initialize device
sendUserInfo(builder); //Sync preferences sendUserInfo(builder); //Sync preferences
requestDeviceInfo(builder);
setInitialized(builder);
syncHelper.start();
builder.notify(getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_MEASURE), true); builder.notify(getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_MEASURE), true);
builder.setGattCallback(this); builder.setGattCallback(this);
builder.notify(measureCharacteristic, true); 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; return builder;
} }
@ -405,11 +410,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
return this; return this;
} }
private void setInitialized(TransactionBuilder builder) {
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext()));
}
@Override @Override
public boolean useAutoConnect() { public boolean useAutoConnect() {
return true; return true;
@ -433,13 +433,13 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onSetTime() { public void onSetTime() {
try { try {
TransactionBuilder builder = performInitialized("time"); TransactionBuilder builder = performInitialized("time");
setCurrentDate(builder); setCurrentDate(builder);
setCurrentTime(builder); setCurrentTime(builder);
performConnected(builder.getTransaction());
builder.queue(getQueue());
}catch(IOException e){ }catch(IOException e){
} }
@ -447,6 +447,8 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) { public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
try { try {
TransactionBuilder builder = performInitialized("alarm"); TransactionBuilder builder = performInitialized("alarm");
@ -468,7 +470,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
} }
setAlarm(builder, null); 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); GB.toast(getContext(), getContext().getString(R.string.user_feedback_all_alarms_disabled), Toast.LENGTH_SHORT, GB.INFO);
}catch(Exception e){} }catch(Exception e){}
@ -494,7 +496,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onSetMusicState(MusicStateSpec stateSpec) { public void onSetMusicState(MusicStateSpec stateSpec) {
} }
@Override @Override
@ -539,8 +540,13 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onFetchActivityData() { public void onFetchActivityData() {
if (syncHelper != null)
syncHelper.sync(); if (syncHelper == null){
syncHelper = new HPlusHandlerThread(gbDevice, getContext(), this);
syncHelper.start();
}
syncHelper.sync();
} }
@Override @Override
@ -550,7 +556,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
TransactionBuilder builder = performInitialized("Shutdown"); TransactionBuilder builder = performInitialized("Shutdown");
builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SHUTDOWN, HPlusConstants.ARG_SHUTDOWN_EN}); builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SHUTDOWN, HPlusConstants.ARG_SHUTDOWN_EN});
builder.queue(getQueue()); performConnected(builder.getTransaction());
}catch(Exception e){ }catch(Exception e){
} }
@ -563,7 +569,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
TransactionBuilder builder = performInitialized("HeartRateTest"); TransactionBuilder builder = performInitialized("HeartRateTest");
builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SET_HEARTRATE_STATE, HPlusConstants.ARG_HEARTRATE_MEASURE_ON}); //Set Real Time... ? 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){ }catch(Exception e){
} }
@ -571,7 +577,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onEnableRealtimeHeartRateMeasurement(boolean enable) { public void onEnableRealtimeHeartRateMeasurement(boolean enable) {
getQueue().clear();
try { try {
TransactionBuilder builder = performInitialized("realTimeHeartMeasurement"); TransactionBuilder builder = performInitialized("realTimeHeartMeasurement");
byte state; byte state;
@ -582,7 +587,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
state = HPlusConstants.ARG_HEARTRATE_ALLDAY_OFF; state = HPlusConstants.ARG_HEARTRATE_ALLDAY_OFF;
builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SET_ALLDAY_HRM, state}); builder.write(ctrlCharacteristic, new byte[]{HPlusConstants.CMD_SET_ALLDAY_HRM, state});
builder.queue(getQueue()); performConnected(builder.getTransaction());
}catch(Exception e){ }catch(Exception e){
} }
@ -594,7 +599,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
TransactionBuilder builder = performInitialized("findMe"); TransactionBuilder builder = performInitialized("findMe");
setFindMe(builder, start); setFindMe(builder, start);
builder.queue(getQueue()); performConnected(builder.getTransaction());
} catch (IOException e) { } catch (IOException e) {
GB.toast(getContext(), "Error toggling Find Me: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "Error toggling Find Me: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
} }
@ -603,8 +608,6 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onSetConstantVibration(int intensity) { public void onSetConstantVibration(int intensity) {
getQueue().clear();
try { try {
TransactionBuilder builder = performInitialized("vibration"); TransactionBuilder builder = performInitialized("vibration");
@ -615,7 +618,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
msg[i + 1] = (byte) "GadgetBridge".charAt(i); msg[i + 1] = (byte) "GadgetBridge".charAt(i);
builder.write(ctrlCharacteristic, msg); builder.write(ctrlCharacteristic, msg);
builder.queue(getQueue()); performConnected(builder.getTransaction());
} catch (IOException e) { } catch (IOException e) {
GB.toast(getContext(), "Error setting Vibration: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); 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.write(ctrlCharacteristic, msg);
builder.queue(getQueue()); performConnected(builder.getTransaction());
} catch (IOException e) { } catch (IOException e) {
GB.toast(getContext(), "Error showing incoming call: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); 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) { private void showText(String title, String body) {
try { try {
TransactionBuilder builder = performInitialized("notification"); TransactionBuilder builder = performInitialized("notification");
@ -772,7 +774,7 @@ public class HPlusSupport extends AbstractBTLEDeviceSupport {
msg[2] = (byte) remaining; msg[2] = (byte) remaining;
builder.write(ctrlCharacteristic, msg); builder.write(ctrlCharacteristic, msg);
builder.queue(getQueue()); performConnected(builder.getTransaction());
} catch (IOException e) { } catch (IOException e) {
GB.toast(getContext(), "Error showing device Notification: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); 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() { private void close() {
if (syncHelper != null) { if (syncHelper != null) {
syncHelper.quit(); syncHelper.quit();
syncHelper.interrupt();
syncHelper = null; syncHelper = null;
} }
} }