From 26ecc3dd6053915529ff211c8ab114bac0d9ae43 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 22:59:25 +0100 Subject: [PATCH] isAMAModeEnabled reactor & tests --- .../androidaps/data/ConstraintChecker.java | 11 ++++++---- .../interfaces/ConstraintsInterface.java | 2 +- .../ObjectivesPlugin.java | 6 ++++-- .../ConstraintsSafety/SafetyPlugin.java | 7 +++++-- .../DetermineBasalAdapterAMAJS.java | 2 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 2 +- .../DetermineBasalAdapterSMBJS.java | 2 +- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 2 +- .../plugins/PumpCombo/ComboPlugin.java | 4 ++-- .../PumpDanaR/AbstractDanaRPlugin.java | 4 ++-- .../plugins/PumpDanaRS/DanaRSPlugin.java | 4 ++-- .../PumpInsight/InsightPumpPlugin.java | 4 ++-- app/src/main/res/values/strings.xml | 1 + .../interfaces/ConstraintsCheckerTest.java | 20 +++++++++++++++++++ 14 files changed, 50 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java index 33be6328fa..7254221985 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -33,6 +33,10 @@ public class ConstraintChecker implements ConstraintsInterface { return isAutosensModeEnabled(new Constraint<>(true)); } + public Constraint isAMAModeEnabled() { + return isAMAModeEnabled(new Constraint<>(true)); + } + @Override public Constraint isLoopInvokationAllowed(Constraint value) { @@ -70,16 +74,15 @@ public class ConstraintChecker implements ConstraintsInterface { } @Override - public boolean isAMAModeEnabled() { - boolean result = SP.getBoolean("openapsama_useautosens", false); + public Constraint isAMAModeEnabled(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - result = result && constrain.isAMAModeEnabled(); + constrain.isAMAModeEnabled(value); } - return result; + return value; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java index fa72199646..6a112ab59c 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -11,7 +11,7 @@ public interface ConstraintsInterface { Constraint isAutosensModeEnabled(Constraint value); - boolean isAMAModeEnabled(); + Constraint isAMAModeEnabled(Constraint value); boolean isSMBModeEnabled(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPlugin.java index 6bb4bf5a30..230acb24d9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPlugin.java @@ -320,8 +320,10 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public boolean isAMAModeEnabled() { - return objectives.get(6).started.getTime() > 0; + public Constraint isAMAModeEnabled(Constraint value) { + if (objectives.get(6).started.getTime() == 0) + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 7)); + return value; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java index 45936c2e6e..78e3464167 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java @@ -119,8 +119,11 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public boolean isAMAModeEnabled() { - return true; + public Constraint isAMAModeEnabled(Constraint value) { + boolean enabled = SP.getBoolean("openapsama_useautosens", false); + if (!enabled) + value.set(false, MainApp.gs(R.string.amadisabledinpreferences)); + return value; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java index 7aef2bb0ec..914afe256f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalAdapterAMAJS.java @@ -250,7 +250,7 @@ public class DetermineBasalAdapterAMAJS { mMealData.put("boluses", mealData.boluses); mMealData.put("mealCOB", mealData.mealCOB); - if (MainApp.getConstraintChecker().isAMAModeEnabled()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled().get()) { mAutosensData = new JSONObject(); mAutosensData.put("ratio", autosensDataRatio); } else { 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 f51bbd2352..b6581a2353 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 @@ -223,7 +223,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { return; startPart = new Date(); - if (MainApp.getConstraintChecker().isAMAModeEnabled()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled().get()) { lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); 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 745ae3bb53..3d93f132bc 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 @@ -303,7 +303,7 @@ public class DetermineBasalAdapterSMBJS { mMealData.put("lastCarbTime", mealData.lastCarbTime); - if (MainApp.getConstraintChecker().isAMAModeEnabled()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled().get()) { mAutosensData = new JSONObject(); mAutosensData.put("ratio", autosensDataRatio); } else { 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 09ff62a324..2eb38d785f 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 @@ -225,7 +225,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { if (!checkOnlyHardLimits(pump.getBaseBasalRate(), "current_basal", 0.01, HardLimits.maxBasal())) return; startPart = new Date(); - if (MainApp.getConstraintChecker().isAMAModeEnabled()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled().get()) { lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 097c53bb18..5fca42e5c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -1443,8 +1443,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public boolean isAMAModeEnabled() { - return true; + public Constraint isAMAModeEnabled(Constraint value) { + return value; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java index 755b8e9dd1..282f3e4806 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java @@ -449,8 +449,8 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, } @Override - public boolean isAMAModeEnabled() { - return true; + public Constraint isAMAModeEnabled(Constraint value) { + return value; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java index b2c81efe1e..0fdb238e53 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java @@ -289,8 +289,8 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public boolean isAMAModeEnabled() { - return true; + public Constraint isAMAModeEnabled(Constraint value) { + return value; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java index 9570b7fdef..52ff326f1f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java @@ -1095,8 +1095,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } @Override - public boolean isAMAModeEnabled() { - return true; + public Constraint isAMAModeEnabled(Constraint value) { + return value; } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c962481eaf..56875c6ee6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -966,5 +966,6 @@ Pump is not temp basal capable No valid basal rate read from pump Closed loop mode disabled in preferences + AMA disabled in preferences diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java index 16301bd63f..2a176c9e60 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -117,6 +117,25 @@ public class ConstraintsCheckerTest { Assert.assertEquals(Boolean.FALSE, c.get()); } + // isAMAModeEnabled tests + @Test + public void notEnabledAMAInPreferencesDisablesAMA() throws Exception { + when(SP.getBoolean("openapsama_useautosens", false)).thenReturn(false); + + Constraint c = constraintChecker.isAMAModeEnabled(); + Assert.assertEquals(true, c.getReasons().contains("AMA disabled in preferences")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + + @Test + public void notStartedObjective7ShouldLimitAMAMode() throws Exception { + objectivesPlugin.objectives.get(6).setStarted(new Date(0)); + + Constraint c = constraintChecker.isAMAModeEnabled(); + Assert.assertEquals(true, c.getReasons().contains("Objective 7 not started")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + @Before public void prepareMock() throws Exception { PowerMockito.mockStatic(ConfigBuilderPlugin.class); @@ -137,6 +156,7 @@ public class ConstraintsCheckerTest { when(MainApp.gs(R.string.closedmodedisabledinpreferences)).thenReturn("Closed loop mode disabled in preferences"); when(MainApp.gs(R.string.objectivenotstarted)).thenReturn("Objective %d not started"); when(MainApp.gs(R.string.novalidbasalrate)).thenReturn("No valid basal rate read from pump"); + when(MainApp.gs(R.string.amadisabledinpreferences)).thenReturn("AMA disabled in preferences"); safetyPlugin = SafetyPlugin.getPlugin(); objectivesPlugin = ObjectivesPlugin.getPlugin();