From d421ceffa61999dba1f08707d24f01d42c3eb3f2 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Thu, 25 Jan 2018 20:55:55 +0000 Subject: [PATCH] Insight automatic reconnector --- .../PumpInsight/connector/Connector.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/Connector.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/Connector.java index 1d91eead62..857d59dd82 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/Connector.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/connector/Connector.java @@ -28,10 +28,13 @@ public class Connector { private volatile Status lastStatus = null; private volatile long lastStatusTime = -1; private boolean companionAppInstalled = false; + private int serviceReconnects = 0; + private StatusCallback statusCallback = new StatusCallback() { @Override public void onStatusChange(Status status) { + synchronized (this) { log("Status change: " + status); lastStatus = status; @@ -55,6 +58,15 @@ public class Connector { @Override public void onServiceDisconnected() { log("Disconnected from service"); + if (Helpers.ratelimit("insight-automatic-reconnect", 30)) { + log("Scheduling automatic service reconnection"); + Helpers.runOnUiThreadDelayed(new Runnable() { + @Override + public void run() { + init(); + } + }, 20000); + } } }; @@ -84,12 +96,12 @@ public class Connector { } private static void log(String msg) { - android.util.Log.e("PUMPPUMP", msg); + android.util.Log.e("INSIGHTPUMP", msg); } public synchronized void init() { - log("init"); + log("Connector::init()"); if (serviceConnector == null) { companionAppInstalled = isCompanionAppInstalled(); if (companionAppInstalled) { @@ -103,8 +115,16 @@ public class Connector { } } else { if (!serviceConnector.isConnectedToService()) { - log("Trying to reconnect to service"); - serviceConnector.connectToService(); + if (serviceReconnects > 2) { + serviceConnector = null; + init(); + } else { + log("Trying to reconnect to service (" + serviceReconnects + ")"); + serviceConnector.connectToService(); + serviceReconnects++; + } + } else { + serviceReconnects = 0; // everything ok } } }