Check remote protocol version

This commit is contained in:
Jamorham 2018-01-31 00:33:10 +00:00
parent c3b07d2a0f
commit ce0650a495
No known key found for this signature in database
GPG key ID: 0BC5C3E0AAD64DF9

View file

@ -15,6 +15,7 @@ import sugar.free.sightparser.pipeline.Status;
import static sugar.free.sightparser.handling.HistoryBroadcast.ACTION_START_RESYNC; import static sugar.free.sightparser.handling.HistoryBroadcast.ACTION_START_RESYNC;
import static sugar.free.sightparser.handling.HistoryBroadcast.ACTION_START_SYNC; import static sugar.free.sightparser.handling.HistoryBroadcast.ACTION_START_SYNC;
import static sugar.free.sightparser.handling.SightService.COMPATIBILITY_VERSION;
/** /**
* Created by jamorham on 23/01/2018. * Created by jamorham on 23/01/2018.
@ -37,6 +38,7 @@ public class Connector {
private static volatile HistoryReceiver historyReceiver; private static volatile HistoryReceiver historyReceiver;
private volatile SightServiceConnector serviceConnector; private volatile SightServiceConnector serviceConnector;
private volatile Status lastStatus = null; private volatile Status lastStatus = null;
private String compatabilityMessage = null;
private volatile long lastStatusTime = -1; private volatile long lastStatusTime = -1;
private boolean companionAppInstalled = false; private boolean companionAppInstalled = false;
private int serviceReconnects = 0; private int serviceReconnects = 0;
@ -58,7 +60,16 @@ public class Connector {
public synchronized void onServiceConnected() { public synchronized void onServiceConnected() {
log("On service connected"); log("On service connected");
try { try {
final String remoteVersion = serviceConnector.getRemoteVersion();
if (remoteVersion.equals(COMPATIBILITY_VERSION)) {
serviceConnector.connect(); serviceConnector.connect();
} else {
log("PROTOCOL VERSION MISMATCH! local: " + COMPATIBILITY_VERSION + " remote: " + remoteVersion);
statusCallback.onStatusChange(Status.INCOMPATIBLE);
compatabilityMessage = "Incompatible companion app, we need version " + getLocalVersion();
serviceConnector.disconnectFromService();
}
} catch (NullPointerException e) { } catch (NullPointerException e) {
log("ERROR: null pointer when trying to connect to pump"); log("ERROR: null pointer when trying to connect to pump");
} }
@ -110,6 +121,10 @@ public class Connector {
android.util.Log.e("INSIGHTPUMP", msg); android.util.Log.e("INSIGHTPUMP", msg);
} }
static String getLocalVersion() {
return COMPATIBILITY_VERSION;
}
@SuppressWarnings("AccessStaticViaInstance") @SuppressWarnings("AccessStaticViaInstance")
private synchronized void initializeHistoryReceiver() { private synchronized void initializeHistoryReceiver() {
if (historyReceiver == null) { if (historyReceiver == null) {
@ -159,8 +174,12 @@ public class Connector {
} }
public Status safeGetStatus() { public Status safeGetStatus() {
try {
if (isConnected()) return serviceConnector.getStatus(); if (isConnected()) return serviceConnector.getStatus();
return Status.DISCONNECTED; return Status.DISCONNECTED;
} catch (IllegalArgumentException e) {
return Status.INCOMPATIBLE;
}
} }
public Status getLastStatus() { public Status getLastStatus() {
@ -186,23 +205,32 @@ public class Connector {
if (Helpers.ratelimit("insight-app-not-connected", 5)) { if (Helpers.ratelimit("insight-app-not-connected", 5)) {
init(); init();
} }
if ((lastStatus == null) || (lastStatus != Status.INCOMPATIBLE)) {
if (compatabilityMessage != null) {
// if disconnected but previous state was incompatible
return compatabilityMessage;
} else {
return "Not connected to companion app!"; return "Not connected to companion app!";
} }
}
}
if (lastStatus == null) { if (lastStatus == null) {
return "Unknown"; return "Unknown";
} }
switch (lastStatus) { switch (lastStatus) {
case CONNECTED: case CONNECTED:
if (lastStatusTime < 1) { if (lastStatusTime < 1) {
tryToGetPumpStatusAgain(); tryToGetPumpStatusAgain();
} }
// TODO other refresh?
default: break;
return lastStatus.toString(); case INCOMPATIBLE:
return lastStatus.toString() + " needs " + getLocalVersion();
} }
return lastStatus.toString();
} }
public String getNiceLastStatusTime() { public String getNiceLastStatusTime() {