synchronized connectifnotconnected

This commit is contained in:
Milos Kozak 2016-07-13 14:46:08 +02:00
parent a10a819a9d
commit b818027b02

View file

@ -68,6 +68,8 @@ public class DanaConnection {
PowerManager.WakeLock mWakeLock; PowerManager.WakeLock mWakeLock;
private Treatment bolusingTreatment = null; private Treatment bolusingTreatment = null;
private static Object connectionInProgress = new Object();
private DanaRFragment danaRFragment; private DanaRFragment danaRFragment;
private DanaRPump danaRPump; private DanaRPump danaRPump;
@ -77,7 +79,7 @@ public class DanaConnection {
private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
// TODO: keepalive kills active connection // TODO: keepalive kills active connection
public DanaConnection(Bus bus) { public DanaConnection(Bus bus) {
danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class); danaRFragment = (DanaRFragment) MainActivity.getSpecificPlugin(DanaRFragment.class);
danaRFragment.setDanaConnection(this); danaRFragment.setDanaConnection(this);
@ -159,37 +161,39 @@ public class DanaConnection {
} }
public synchronized void connectIfNotConnected(String callerName) { public synchronized void connectIfNotConnected(String callerName) {
mWakeLock.acquire(); synchronized (connectionInProgress) {
long startTime = System.currentTimeMillis(); mWakeLock.acquire();
short connectionAttemptCount = 0; long startTime = System.currentTimeMillis();
if (!(isConnected())) { short connectionAttemptCount = 0;
long timeToConnectTimeSoFar = 0; if (!(isConnected())) {
while (!(isConnected())) { long timeToConnectTimeSoFar = 0;
timeToConnectTimeSoFar = (System.currentTimeMillis() - startTime) / 1000; while (!(isConnected())) {
mBus.post(new EventDanaRConnectionStatus(true, false, connectionAttemptCount)); timeToConnectTimeSoFar = (System.currentTimeMillis() - startTime) / 1000;
connectBT(); mBus.post(new EventDanaRConnectionStatus(true, false, connectionAttemptCount));
if (isConnected()) { connectBT();
mBus.post(new EventDanaRConnectionStatus(false, true, 0)); if (isConnected()) {
break; mBus.post(new EventDanaRConnectionStatus(false, true, 0));
break;
}
if (Config.logDanaBTComm)
log.debug("connectIfNotConnected waiting " + timeToConnectTimeSoFar + "s attempts:" + connectionAttemptCount + " caller:" + callerName);
connectionAttemptCount++;
if (timeToConnectTimeSoFar / 60 > 15 || connectionAttemptCount > 180) {
Intent alarmServiceIntent = new Intent(MainApp.instance().getApplicationContext(), AlertService.class);
alarmServiceIntent.putExtra("alarmText", MainApp.sResources.getString(R.string.connectionerror));
MainApp.instance().getApplicationContext().startService(alarmServiceIntent);
}
waitMsec(1000);
} }
if (Config.logDanaBTComm) if (Config.logDanaBTComm)
log.debug("connectIfNotConnected waiting " + timeToConnectTimeSoFar + "s attempts:" + connectionAttemptCount + " caller:" + callerName); log.debug("connectIfNotConnected took " + timeToConnectTimeSoFar + "s attempts:" + connectionAttemptCount);
connectionAttemptCount++; pingStatus();
} else {
if (timeToConnectTimeSoFar / 60 > 15 || connectionAttemptCount > 180) { mBus.post(new EventDanaRConnectionStatus(false, true, 0));
Intent alarmServiceIntent = new Intent(MainApp.instance().getApplicationContext(), AlertService.class);
alarmServiceIntent.putExtra("alarmText", MainApp.sResources.getString(R.string.connectionerror));
MainApp.instance().getApplicationContext().startService(alarmServiceIntent);
}
waitMsec(1000);
} }
if (Config.logDanaBTComm) mWakeLock.release();
log.debug("connectIfNotConnected took " + timeToConnectTimeSoFar + "s attempts:" + connectionAttemptCount);
pingStatus();
} else {
mBus.post(new EventDanaRConnectionStatus(false, true, 0));
} }
mWakeLock.release();
} }
private synchronized void connectBT() { private synchronized void connectBT() {