Attempt to eliminate duplicate connectors

This commit is contained in:
Jamorham 2018-01-26 22:27:04 +00:00
parent 3150e7d00f
commit f036ca4547
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9

View file

@ -40,9 +40,8 @@ public class Connector {
private StatusCallback statusCallback = new StatusCallback() { private StatusCallback statusCallback = new StatusCallback() {
@Override @Override
public void onStatusChange(Status status) { public synchronized void onStatusChange(Status status) {
synchronized (this) {
log("Status change: " + status); log("Status change: " + status);
lastStatus = status; lastStatus = status;
lastStatusTime = Helpers.tsl(); lastStatusTime = Helpers.tsl();
@ -51,19 +50,23 @@ public class Connector {
} }
MainApp.bus().post(new EventInsightPumpUpdateGui()); MainApp.bus().post(new EventInsightPumpUpdateGui());
} }
}
}; };
private ServiceConnectionCallback connectionCallback = new ServiceConnectionCallback() { private ServiceConnectionCallback connectionCallback = new ServiceConnectionCallback() {
@Override @Override
public void onServiceConnected() { public synchronized void onServiceConnected() {
log("On service connected"); log("On service connected");
try {
serviceConnector.connect(); serviceConnector.connect();
} catch (NullPointerException e) {
log("ERROR: null pointer when trying to connect to pump");
}
statusCallback.onStatusChange(safeGetStatus()); statusCallback.onStatusChange(safeGetStatus());
} }
@Override @Override
public void onServiceDisconnected() { public synchronized void onServiceDisconnected() {
log("Disconnected from service"); log("Disconnected from service");
if (Helpers.ratelimit("insight-automatic-reconnect", 30)) { if (Helpers.ratelimit("insight-automatic-reconnect", 30)) {
log("Scheduling automatic service reconnection"); log("Scheduling automatic service reconnection");
@ -119,7 +122,7 @@ public class Connector {
get().getServiceConnector().connect(); get().getServiceConnector().connect();
} }
private static void log(String msg) { static void log(String msg) {
android.util.Log.e("INSIGHTPUMP", msg); android.util.Log.e("INSIGHTPUMP", msg);
} }
@ -130,6 +133,7 @@ public class Connector {
companionAppInstalled = isCompanionAppInstalled(); companionAppInstalled = isCompanionAppInstalled();
if (companionAppInstalled) { if (companionAppInstalled) {
serviceConnector = new SightServiceConnector(MainApp.instance()); serviceConnector = new SightServiceConnector(MainApp.instance());
serviceConnector.removeStatusCallback(statusCallback);
serviceConnector.addStatusCallback(statusCallback); serviceConnector.addStatusCallback(statusCallback);
serviceConnector.setConnectionCallback(connectionCallback); serviceConnector.setConnectionCallback(connectionCallback);
serviceConnector.connectToService(); serviceConnector.connectToService();