diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightFragment.java index a79fdad542..34a9597fb2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/LocalInsightFragment.java @@ -27,6 +27,7 @@ import info.nightscout.androidaps.plugins.PumpInsightLocal.descriptors.ActiveBas import info.nightscout.androidaps.plugins.PumpInsightLocal.descriptors.ActiveBolus; import info.nightscout.androidaps.plugins.PumpInsightLocal.descriptors.ActiveTBR; import info.nightscout.androidaps.plugins.PumpInsightLocal.descriptors.CartridgeStatus; +import info.nightscout.androidaps.plugins.PumpInsightLocal.descriptors.InsightState; import info.nightscout.androidaps.plugins.PumpInsightLocal.descriptors.TotalDailyDose; import info.nightscout.androidaps.queue.Callback; import info.nightscout.utils.DateUtil; @@ -169,7 +170,8 @@ public class LocalInsightFragment extends SubscriberFragment implements View.OnC private void getConnectionStatusItem(List statusItems) { int string = 0; - switch (LocalInsightPlugin.getInstance().getConnectionService().getState()) { + InsightState state = LocalInsightPlugin.getInstance().getConnectionService().getState(); + switch (state) { case NOT_PAIRED: string = R.string.not_paired; break; @@ -199,6 +201,9 @@ public class LocalInsightFragment extends SubscriberFragment implements View.OnC break; } statusItems.add(getStatusItem(MainApp.gs(R.string.insight_status), MainApp.gs(string))); + if (state == InsightState.RECOVERING) { + statusItems.add(getStatusItem(MainApp.gs(R.string.recovery_duration), LocalInsightPlugin.getInstance().getConnectionService().getRecoveryDuration() / 1000 + "s")); + } } private void getLastConnectedItem(List statusItems) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/connection_service/InsightConnectionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/connection_service/InsightConnectionService.java index 57dc2cfcc0..903a85aaac 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/connection_service/InsightConnectionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsightLocal/connection_service/InsightConnectionService.java @@ -116,6 +116,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab private List activatedServices = new ArrayList<>(); private long lastDataTime; private long lastConnected; + private long recoveryDuration = 0; KeyPair getKeyPair() { if (keyPair == null) keyPair = Cryptograph.generateRSAKey(); @@ -130,6 +131,22 @@ public class InsightConnectionService extends Service implements ConnectionEstab return randomBytes; } + public synchronized long getRecoveryDuration() { + return recoveryDuration; + } + + private void increaseRecoveryDuration() { + long maxRecoveryDuration = SP.getInt("insight_max_recovery_duration", 20); + maxRecoveryDuration = Math.min(maxRecoveryDuration, 20); + maxRecoveryDuration = Math.max(maxRecoveryDuration, 0); + long minRecoveryDuration = SP.getInt("insight_min_recovery_duration", 5); + minRecoveryDuration = Math.min(minRecoveryDuration, 20); + minRecoveryDuration = Math.max(minRecoveryDuration, 0); + recoveryDuration += 1000; + recoveryDuration = Math.max(recoveryDuration, minRecoveryDuration * 1000); + recoveryDuration = Math.min(recoveryDuration, maxRecoveryDuration * 1000); + } + public long getLastConnected() { return lastConnected; } @@ -255,7 +272,10 @@ public class InsightConnectionService extends Service implements ConnectionEstab disconnectTimer.interrupt(); disconnectTimer = null; } - if (state == InsightState.DISCONNECTED && pairingDataStorage.isPaired()) connect(); + if (state == InsightState.DISCONNECTED && pairingDataStorage.isPaired()) { + recoveryDuration = 0; + connect(); + } } public synchronized void withdrawConnectionRequest(Object lock) { @@ -271,7 +291,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab long disconnectTimeout = SP.getInt("insight_disconnect_delay", 5); disconnectTimeout = Math.min(disconnectTimeout, 15); disconnectTimeout = Math.max(disconnectTimeout, 0); - log.info("Last connection lock released, will disconnect " + disconnectTimeout + " seconds"); + log.info("Last connection lock released, will disconnect in " + disconnectTimeout + " seconds"); disconnectTimer = DelayedActionThread.runDelayed("Disconnect Timer", disconnectTimeout * 1000, this::disconnect); } } @@ -342,15 +362,16 @@ public class InsightConnectionService extends Service implements ConnectionEstab if (!(e instanceof ConnectionFailedException)) { connect(); } else { - int recoveryDuration = SP.getInt("insight_recovery_duration", 5); - recoveryDuration = Math.min(recoveryDuration, 20); - recoveryDuration = Math.max(recoveryDuration, 0); - recoveryTimer = DelayedActionThread.runDelayed("RecoveryTimer", recoveryDuration * 1000, () -> { - recoveryTimer = null; - synchronized (InsightConnectionService.this) { - if (!Thread.currentThread().isInterrupted()) connect(); - } - }); + increaseRecoveryDuration(); + if (recoveryDuration == 0) connect(); + else { + recoveryTimer = DelayedActionThread.runDelayed("RecoveryTimer", recoveryDuration, () -> { + recoveryTimer = null; + synchronized (InsightConnectionService.this) { + if (!Thread.currentThread().isInterrupted()) connect(); + } + }); + } } } } else { @@ -402,6 +423,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab @Override public synchronized void onConnectionSucceed() { try { + recoveryDuration = 0; inputStreamReader = new InputStreamReader(bluetoothSocket.getInputStream(), this); outputStreamWriter = new OutputStreamWriter(bluetoothSocket.getOutputStream(), this); inputStreamReader.start(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 60da39318b..b3cf7b8b47 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1230,7 +1230,6 @@ Log alerts Enable TBR emulation Use extended boluses instead of TBRs to bypass the 250% limit - Recovery duration [s] Disconnect delay [s] Serial number Release software version @@ -1273,6 +1272,9 @@ TDD: %1$.2f Reser.: %1$.2fU Batt.: %1$d%% + Max. recovery duration [s] + Min. recovery duration [s] + Recovery duration %1$d day diff --git a/app/src/main/res/xml/pref_insight_local.xml b/app/src/main/res/xml/pref_insight_local.xml index f1ec6ad94c..58dee3b182 100644 --- a/app/src/main/res/xml/pref_insight_local.xml +++ b/app/src/main/res/xml/pref_insight_local.xml @@ -40,8 +40,14 @@ + android:key="insight_min_recovery_duration" + android:title="@string/min_recovery_duration" /> + +