From a6a4a8837dfd2ed0e3eab40c4d86aa5048294f6b Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Wed, 25 Apr 2018 01:18:40 +0200 Subject: [PATCH] safety run if SMB fails --- .../androidaps/interfaces/APSInterface.java | 2 +- .../androidaps/plugins/Loop/LoopPlugin.java | 37 +++++++++++++------ .../OpenAPSAMA/OpenAPSAMAFragment.java | 2 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 4 +- .../plugins/OpenAPSMA/OpenAPSMAFragment.java | 2 +- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 4 +- .../DetermineBasalAdapterSMBJS.java | 10 ++--- .../OpenAPSSMB/OpenAPSSMBFragment.java | 2 +- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 14 +++---- 9 files changed, 45 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/APSInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/APSInterface.java index 11c01112ba..6c47dd9d6e 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/APSInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/APSInterface.java @@ -11,5 +11,5 @@ public interface APSInterface { public APSResult getLastAPSResult(); public Date getLastAPSRun(); - public void invoke(String initiator); + public void invoke(String initiator, boolean safety); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java index a4cf0c60ac..9806df37a0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java @@ -9,6 +9,7 @@ import android.app.TaskStackBuilder; import android.content.Context; import android.content.Intent; import android.os.Build; +import android.os.SystemClock; import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; @@ -253,7 +254,11 @@ public class LoopPlugin extends PluginBase { return isDisconnected; } - public void invoke(String initiator, boolean allowNotification) { + public void invoke(String initiator, boolean allowNotification){ + invoke(initiator, allowNotification, false); + } + + public void invoke(String initiator, boolean allowNotification, boolean safety) { try { if (Config.logFunctionCalls) log.debug("invoke from " + initiator); @@ -284,7 +289,7 @@ public class LoopPlugin extends PluginBase { APSInterface usedAPS = ConfigBuilderPlugin.getActiveAPS(); if (usedAPS != null && ((PluginBase) usedAPS).isEnabled(PluginType.APS)) { - usedAPS.invoke(initiator); + usedAPS.invoke(initiator, safety); result = usedAPS.getLastAPSResult(); } @@ -348,16 +353,24 @@ public class LoopPlugin extends PluginBase { if (result.enacted || result.success) { lastRun.tbrSetByPump = result; lastRun.lastEnact = lastRun.lastAPSRun; - } - MainApp.bus().post(new EventLoopUpdateGui()); - } - }); - MainApp.getConfigBuilder().applySMBRequest(resultAfterConstraints, new Callback() { - @Override - public void run() { - if (result.enacted || result.success) { - lastRun.smbSetByPump = result; - lastRun.lastEnact = lastRun.lastAPSRun; + + MainApp.getConfigBuilder().applySMBRequest(resultAfterConstraints, new Callback() { + @Override + public void run() { + //Callback is only called if a bolus was acutally requested + if (result.enacted || result.success) { + lastRun.smbSetByPump = result; + lastRun.lastEnact = lastRun.lastAPSRun; + } else { + new Thread(() -> { + SystemClock.sleep(1000); + LoopPlugin.getPlugin().invoke("safety", allowNotification, true); + }).start(); + FabricPrivacy.getInstance().logCustom(new CustomEvent("Loop_Run_Safety")); + } + MainApp.bus().post(new EventLoopUpdateGui()); + } + }); } MainApp.bus().post(new EventLoopUpdateGui()); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAFragment.java index 9c9ad7d222..2e6bb277e6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAFragment.java @@ -72,7 +72,7 @@ public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnCli public void onClick(View view) { switch (view.getId()) { case R.id.openapsma_run: - OpenAPSAMAPlugin.getPlugin().invoke("OpenAPSAMA button"); + OpenAPSAMAPlugin.getPlugin().invoke("OpenAPSAMA button", false); FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_AMA_Run")); break; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java index 2789c75252..49973d6cd5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java @@ -88,8 +88,8 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface { } @Override - public void invoke(String initiator) { - log.debug("invoke from " + initiator); + public void invoke(String initiator, boolean safety) { + log.debug("invoke from " + initiator + " safety: " + safety); lastAPSResult = null; DetermineBasalAdapterAMAJS determineBasalAdapterAMAJS; try { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java index 6b8cb9d0aa..7360bb8b2a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java @@ -66,7 +66,7 @@ public class OpenAPSMAFragment extends SubscriberFragment implements View.OnClic public void onClick(View view) { switch (view.getId()) { case R.id.openapsma_run: - OpenAPSMAPlugin.getPlugin().invoke("OpenAPSMA button"); + OpenAPSMAPlugin.getPlugin().invoke("OpenAPSMA button", false); FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_MA_Run")); break; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java index 0d9895bad7..45a8ea470d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java @@ -88,8 +88,8 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface { } @Override - public void invoke(String initiator) { - log.debug("invoke from " + initiator); + public void invoke(String initiator, boolean safety) { + log.debug("invoke from " + initiator + " safety: " + safety); lastAPSResult = null; DetermineBasalAdapterMAJS determineBasalAdapterMAJS = null; try { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/DetermineBasalAdapterSMBJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/DetermineBasalAdapterSMBJS.java index 0d1cf1cca8..da96241e61 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/DetermineBasalAdapterSMBJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/DetermineBasalAdapterSMBJS.java @@ -214,7 +214,7 @@ public class DetermineBasalAdapterSMBJS { double autosensDataRatio, boolean tempTargetSet, boolean microBolusAllowed, - boolean smbAlwaysAllowed + boolean advancedFiltering ) throws JSONException { String units = profile.getUnits(); @@ -250,13 +250,13 @@ public class DetermineBasalAdapterSMBJS { mProfile.put("min_5m_carbimpact", SP.getDouble(R.string.key_openapsama_min_5m_carbimpact, SMBDefaults.min_5m_carbimpact)); } mProfile.put("remainingCarbsCap", SMBDefaults.remainingCarbsCap); - mProfile.put("enableUAM", SP.getBoolean(R.string.key_use_uam, false)); + mProfile.put("enableUAM", SP.getBoolean(R.string.key_use_uam, false)&& advancedFiltering); mProfile.put("A52_risk_enable", SMBDefaults.A52_risk_enable); mProfile.put("enableSMB_with_COB", SP.getBoolean(R.string.key_enableSMB_with_COB, false)); mProfile.put("enableSMB_with_temptarget", SP.getBoolean(R.string.key_enableSMB_with_temptarget, false)); mProfile.put("allowSMB_with_high_temptarget", SP.getBoolean(R.string.key_allowSMB_with_high_temptarget, false)); - mProfile.put("enableSMB_always", SP.getBoolean(R.string.key_enableSMB_always, false) && smbAlwaysAllowed); - mProfile.put("enableSMB_after_carbs", SP.getBoolean(R.string.key_enableSMB_after_carbs, false) && smbAlwaysAllowed); + mProfile.put("enableSMB_always", SP.getBoolean(R.string.key_enableSMB_always, false) && advancedFiltering); + mProfile.put("enableSMB_after_carbs", SP.getBoolean(R.string.key_enableSMB_after_carbs, false) && advancedFiltering); mProfile.put("maxSMBBasalMinutes", SP.getInt("key_smbmaxminutes", SMBDefaults.maxSMBBasalMinutes)); mProfile.put("carbsReqThreshold", SMBDefaults.carbsReqThreshold); @@ -315,7 +315,7 @@ public class DetermineBasalAdapterSMBJS { mAutosensData.put("ratio", 1.0); } mMicrobolusAllowed = microBolusAllowed; - mSMBAlwaysAllowed = smbAlwaysAllowed; + mSMBAlwaysAllowed = advancedFiltering; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBFragment.java index c35e229354..7780fcc9b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBFragment.java @@ -66,7 +66,7 @@ public class OpenAPSSMBFragment extends SubscriberFragment { @OnClick(R.id.openapsma_run) public void onRunClick() { - OpenAPSSMBPlugin.getPlugin().invoke("OpenAPSSMB button"); + OpenAPSSMBPlugin.getPlugin().invoke("OpenAPSSMB button", false); FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_SMB_Run")); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java index 60b516fc63..11fe65a082 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java @@ -91,8 +91,8 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { } @Override - public void invoke(String initiator) { - log.debug("invoke from " + initiator); + public void invoke(String initiator, boolean safety) { + log.debug("invoke from " + initiator + " safety: " + safety); lastAPSResult = null; DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS = null; try { @@ -184,13 +184,13 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { lastAutosensResult = new AutosensResult(); } - Constraint smbAllowed = new Constraint<>(true); + Constraint smbAllowed = new Constraint<>(!safety); MainApp.getConstraintChecker().isSMBModeEnabled(smbAllowed); inputConstraints.copyReasons(smbAllowed); - Constraint smbAlwaysEnabled = new Constraint<>(true); - MainApp.getConstraintChecker().isAdvancedFilteringEnabled(smbAlwaysEnabled); - inputConstraints.copyReasons(smbAlwaysEnabled); + Constraint advancedFiltering = new Constraint<>(!safety); + MainApp.getConstraintChecker().isAdvancedFilteringEnabled(advancedFiltering); + inputConstraints.copyReasons(advancedFiltering); Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart); Profiler.log(log, "SMB data gathering", start); @@ -201,7 +201,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { lastAutosensResult.ratio, //autosensDataRatio isTempTarget, smbAllowed.value(), - smbAlwaysEnabled.value() + advancedFiltering.value() ); } catch (JSONException e) { log.error(e.getMessage());