From b0a9f678f0cca0ed6a59f2e10a63a39a48dcedc5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 13:11:25 +0100 Subject: [PATCH 01/28] limitRunningLoop constraint --- .../interfaces/ConstraintsInterface.java | 4 +-- .../constrains/BooleanConstraint.java | 29 +++++++++++++++ .../interfaces/constrains/Constraint.java | 27 ++++++++++++++ .../constrains/DoubleConstraint.java | 29 +++++++++++++++ .../constrains/IntegerConstraint.java | 28 +++++++++++++++ .../ConfigBuilder/ConfigBuilderPlugin.java | 10 ++---- .../ObjectivesPlugin.java | 7 ++-- .../ConstraintsSafety/SafetyPlugin.java | 6 ++-- .../androidaps/plugins/Loop/LoopPlugin.java | 20 ++++++----- .../plugins/PumpCombo/ComboPlugin.java | 7 ++-- .../PumpDanaR/AbstractDanaRPlugin.java | 4 +-- .../plugins/PumpDanaRS/DanaRSPlugin.java | 4 +-- .../PumpInsight/InsightPumpPlugin.java | 10 ++---- app/src/main/res/values/strings.xml | 3 ++ .../contraints/BooleanConstraintTest.java | 32 +++++++++++++++++ .../contraints/DoubleConstraintTest.java | 36 +++++++++++++++++++ .../contraints/IntegerConstraintTest.java | 34 ++++++++++++++++++ 17 files changed, 252 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/interfaces/constrains/BooleanConstraint.java create mode 100644 app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java create mode 100644 app/src/main/java/info/nightscout/androidaps/interfaces/constrains/DoubleConstraint.java create mode 100644 app/src/main/java/info/nightscout/androidaps/interfaces/constrains/IntegerConstraint.java create mode 100644 app/src/test/java/info/nightscout/androidaps/interfaces/contraints/BooleanConstraintTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/interfaces/contraints/DoubleConstraintTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/interfaces/contraints/IntegerConstraintTest.java 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 aac7918bdc..471b7735cf 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -1,13 +1,13 @@ package info.nightscout.androidaps.interfaces; -import info.nightscout.androidaps.plugins.Loop.APSResult; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; /** * Created by mike on 15.06.2016. */ public interface ConstraintsInterface { - boolean isLoopEnabled(); + void limitRunningLoop(BooleanConstraint value); boolean isClosedModeEnabled(); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/BooleanConstraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/BooleanConstraint.java new file mode 100644 index 0000000000..eda25b9d68 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/BooleanConstraint.java @@ -0,0 +1,29 @@ +package info.nightscout.androidaps.interfaces.constrains; + +/** + * Created by mike on 19.03.2018. + */ + +public class BooleanConstraint extends Constraint { + boolean value; + + public BooleanConstraint(boolean value) { + this.value = value; + } + + public boolean get() { + return value; + } + + public BooleanConstraint set(boolean value) { + this.value = value; + return this; + } + + public BooleanConstraint set(boolean value, String reason) { + this.value = value; + reason(reason); + return this; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java new file mode 100644 index 0000000000..4cc4ef7c79 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java @@ -0,0 +1,27 @@ +package info.nightscout.androidaps.interfaces.constrains; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by mike on 19.03.2018. + */ + +public class Constraint { + List reasons = new ArrayList<>(); + + public Constraint reason(String reason) { + reasons.add(reason); + return this; + } + + public String getReasons() { + StringBuilder sb = new StringBuilder(); + int count = 0; + for (String r: reasons) { + if (count++ != 0) sb.append("\n"); + sb.append(r); + } + return sb.toString(); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/DoubleConstraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/DoubleConstraint.java new file mode 100644 index 0000000000..211a545219 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/DoubleConstraint.java @@ -0,0 +1,29 @@ +package info.nightscout.androidaps.interfaces.constrains; + +/** + * Created by mike on 19.03.2018. + */ + +public class DoubleConstraint extends Constraint{ + double value; + + public DoubleConstraint(double value) { + this.value = value; + } + + public Double getDouble() { + return value; + } + + public DoubleConstraint set(double value) { + this.value = value; + return this; + } + + public DoubleConstraint set(double value, String reason) { + this.value = value; + reason(reason); + return this; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/IntegerConstraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/IntegerConstraint.java new file mode 100644 index 0000000000..31d2a76e82 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/IntegerConstraint.java @@ -0,0 +1,28 @@ +package info.nightscout.androidaps.interfaces.constrains; + +/** + * Created by mike on 19.03.2018. + */ + +public class IntegerConstraint extends Constraint { + int value; + + public IntegerConstraint(int value) { + this.value = value; + } + + public int getInteger() { + return value; + } + + public IntegerConstraint set(int value) { + this.value = value; + return this; + } + + public IntegerConstraint set(int value, String reason) { + this.value = value; + reason(reason); + return this; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index e0a8155ffa..3f5283ec48 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -4,8 +4,6 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.annotation.Nullable; -import org.json.JSONException; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,6 +31,7 @@ import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppInitialized; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; @@ -43,7 +42,6 @@ import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; -import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; import info.nightscout.androidaps.queue.Callback; @@ -451,16 +449,14 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr * Constraints interface **/ @Override - public boolean isLoopEnabled() { - boolean result = true; + public void limitRunningLoop(BooleanConstraint value) { ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - result = result && constrain.isLoopEnabled(); + constrain.limitRunningLoop(value); } - return result; } @Override 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 3b0891680d..0516cdda51 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 @@ -10,12 +10,12 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.APSInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -294,8 +294,9 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { * Constraints interface **/ @Override - public boolean isLoopEnabled() { - return objectives.get(0).started.getTime() > 0; + public void limitRunningLoop(BooleanConstraint value) { + if (objectives.get(0).started.getTime() == 0) + value.set(false, MainApp.gs(R.string.objective1notstarted)); } @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 41a7c1eb00..4062836c48 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 @@ -11,6 +11,7 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -93,8 +94,9 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public boolean isLoopEnabled() { - return ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; + public void limitRunningLoop(BooleanConstraint value) { + if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable) + value.set(false, MainApp.gs(R.string.pumpisnottempbasalcapable)); } /** 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 5846140b1e..a1ca643adc 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 @@ -23,11 +23,10 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; -import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.APSInterface; -import info.nightscout.androidaps.interfaces.ConstraintsInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -260,10 +259,13 @@ public class LoopPlugin implements PluginBase { try { if (Config.logFunctionCalls) log.debug("invoke from " + initiator); - ConstraintsInterface constraintsInterface = MainApp.getConfigBuilder(); - if (!constraintsInterface.isLoopEnabled()) { - log.debug(MainApp.sResources.getString(R.string.loopdisabled)); - MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.sResources.getString(R.string.loopdisabled))); + BooleanConstraint loopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitRunningLoop(loopEnabled); + + if (!loopEnabled.get()) { + String message = MainApp.sResources.getString(R.string.loopdisabled) + "\n" + loopEnabled.getReasons(); + log.debug(message); + MainApp.bus().post(new EventLoopSetLastRunGui(message)); return; } final PumpInterface pump = ConfigBuilderPlugin.getActivePump(); @@ -297,8 +299,8 @@ public class LoopPlugin implements PluginBase { // check rate for constrais final APSResult resultAfterConstraints = result.clone(); - resultAfterConstraints.rate = constraintsInterface.applyBasalConstraints(resultAfterConstraints.rate); - resultAfterConstraints.smb = constraintsInterface.applyBolusConstraints(resultAfterConstraints.smb); + resultAfterConstraints.rate = MainApp.getConfigBuilder().applyBasalConstraints(resultAfterConstraints.rate); + resultAfterConstraints.smb = MainApp.getConfigBuilder().applyBolusConstraints(resultAfterConstraints.smb); // safety check for multiple SMBs long lastBolusTime = TreatmentsPlugin.getPlugin().getLastBolusTime(); @@ -329,7 +331,7 @@ public class LoopPlugin implements PluginBase { return; } - if (constraintsInterface.isClosedModeEnabled()) { + if (MainApp.getConfigBuilder().isClosedModeEnabled()) { if (result.isChangeRequested()) { final PumpEnactResult waiting = new PumpEnactResult(); waiting.queued = true; 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 ee3a32ad7e..5d60498b32 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 @@ -4,7 +4,6 @@ import android.os.SystemClock; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import com.crashlytics.android.answers.Answers; import com.crashlytics.android.answers.CustomEvent; import org.json.JSONObject; @@ -29,6 +28,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.events.EventRefreshOverview; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; @@ -1407,8 +1407,9 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf private boolean validBasalRateProfileSelectedOnPump = true; @Override - public boolean isLoopEnabled() { - return validBasalRateProfileSelectedOnPump; + public void limitRunningLoop(BooleanConstraint value) { + if (!validBasalRateProfileSelectedOnPump) + value.set(false, MainApp.gs(R.string.novalidbasalrate)); } @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 b57b8543f4..111919c47d 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 @@ -19,6 +19,7 @@ import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.DanaRInterface; import info.nightscout.androidaps.interfaces.PluginBase; @@ -436,8 +437,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, */ @Override - public boolean isLoopEnabled() { - return true; + public void limitRunningLoop(BooleanConstraint 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 48838117c6..1bc785352d 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 @@ -30,6 +30,7 @@ import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.DanaRInterface; import info.nightscout.androidaps.interfaces.PluginBase; @@ -274,8 +275,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, // Constraints interface @Override - public boolean isLoopEnabled() { - return true; + public void limitRunningLoop(BooleanConstraint 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 a7c7cb52f4..9889c9723c 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 @@ -1,10 +1,5 @@ package info.nightscout.androidaps.plugins.PumpInsight; -import android.os.Handler; -import android.util.Log; - -import com.j256.ormlite.stmt.query.In; - import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -27,11 +22,11 @@ import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.Overview.events.EventDismissBolusprogressIfRunning; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; @@ -1085,8 +1080,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints // Constraints @Override - public boolean isLoopEnabled() { - return true; + public void limitRunningLoop(BooleanConstraint value){ } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6c1a0b774a..b6c7783ece 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1011,5 +1011,8 @@ ProfileSwitch missing. Please do a profile switch or press \"Activate Profile\" in the LocalProfile. Bolus count TBR count + Objective 1 not started + Pump is not temp basal capable + No valid basal rate read from pump diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/BooleanConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/BooleanConstraintTest.java new file mode 100644 index 0000000000..610f3f5519 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/BooleanConstraintTest.java @@ -0,0 +1,32 @@ +package info.nightscout.androidaps.interfaces.contraints; + +import junit.framework.Assert; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; + +/** + * Created by mike on 19.03.2018. + */ + +@RunWith(PowerMockRunner.class) +public class BooleanConstraintTest { + + @Test + public void doTests() throws Exception { + BooleanConstraint c; + + c = new BooleanConstraint(true); + Assert.assertEquals(true, c.get()); + Assert.assertEquals("", c.getReasons()); + c.set(false); + Assert.assertEquals(false, c.get()); + Assert.assertEquals("", c.getReasons()); + c.set(true, "Set true"); + Assert.assertEquals(true, c.get()); + Assert.assertEquals("Set true", c.getReasons()); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/DoubleConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/DoubleConstraintTest.java new file mode 100644 index 0000000000..b99376b033 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/DoubleConstraintTest.java @@ -0,0 +1,36 @@ +package info.nightscout.androidaps.interfaces.contraints; + +import junit.framework.Assert; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.nightscout.androidaps.interfaces.constrains.DoubleConstraint; + +/** + * Created by mike on 19.03.2018. + */ + +@RunWith(PowerMockRunner.class) +public class DoubleConstraintTest { + + @Test + public void doTests() throws Exception { + DoubleConstraint c; + + c = new DoubleConstraint(10d); + Assert.assertEquals(10d, c.getDouble()); + Assert.assertEquals("", c.getReasons()); + c.set(11d); + Assert.assertEquals(11d, c.getDouble()); + Assert.assertEquals("", c.getReasons()); + c.set(9d, "Set 9d"); + Assert.assertEquals(9d, c.getDouble()); + Assert.assertEquals("Set 9d", c.getReasons()); + c.set(8d, "Set 8d"); + Assert.assertEquals(8d, c.getDouble()); + Assert.assertEquals("Set 9d\nSet 8d", c.getReasons()); + + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/IntegerConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/IntegerConstraintTest.java new file mode 100644 index 0000000000..910021e305 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/IntegerConstraintTest.java @@ -0,0 +1,34 @@ +package info.nightscout.androidaps.interfaces.contraints; + +import junit.framework.Assert; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.IntegerConstraint; + +/** + * Created by mike on 19.03.2018. + */ + +@RunWith(PowerMockRunner.class) +public class IntegerConstraintTest { + + @Test + public void doTests() throws Exception { + IntegerConstraint c; + + c = new IntegerConstraint(10); + Assert.assertEquals(10, c.getInteger()); + Assert.assertEquals("", c.getReasons()); + c.set(11); + Assert.assertEquals(11, c.getInteger()); + Assert.assertEquals("", c.getReasons()); + c.set(9, "Set 9"); + Assert.assertEquals(9, c.getInteger()); + Assert.assertEquals("Set 9", c.getReasons()); + + } +} From 326229ebbb0667ef3ce7844ada58d6ffcb6af1a2 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 17:06:22 +0100 Subject: [PATCH 02/28] limitClosedLoop & remove openloop flavor --- app/build.gradle | 18 ------------------ .../info/nightscout/androidaps/MainApp.java | 6 +++++- .../interfaces/ConstraintsInterface.java | 2 +- .../ConfigBuilder/ConfigBuilderPlugin.java | 12 +++++------- .../ObjectivesPlugin.java | 14 ++++++++------ .../ConstraintsSafety/SafetyPlugin.java | 10 ++++++---- .../androidaps/plugins/Loop/LoopPlugin.java | 9 ++++++--- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 5 ++++- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 5 ++++- .../OpenAPSSMB/DetermineBasalAdapterSMBJS.java | 8 +++++--- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 5 ++++- .../plugins/Overview/OverviewFragment.java | 8 ++++++-- .../plugins/PumpCombo/ComboPlugin.java | 3 +-- .../plugins/PumpDanaR/AbstractDanaRPlugin.java | 3 +-- .../plugins/PumpDanaRS/DanaRSPlugin.java | 3 +-- .../plugins/PumpInsight/InsightPumpPlugin.java | 3 +-- .../plugins/Wear/ActionStringHandler.java | 7 ++++--- app/src/main/res/values/strings.xml | 3 ++- 18 files changed, 64 insertions(+), 60 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 00c78fe077..e6efea8288 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -97,21 +97,6 @@ android { buildConfigField "boolean", "APS", "true" buildConfigField "boolean", "PUMPDRIVERS", "true" buildConfigField "boolean", "NSCLIENTOLNY", "false" - buildConfigField "boolean", "CLOSEDLOOP", "true" - buildConfigField "boolean", "G5UPLOADER", "false" - buildConfigField "boolean", "PUMPCONTROL", "false" - } - openloop { - dimension "standard" - resValue "string", "app_name", "AndroidAPS" - versionName version - manifestPlaceholders = [ - appIcon: "@mipmap/blueowl" - ] - buildConfigField "boolean", "APS", "true" - buildConfigField "boolean", "PUMPDRIVERS", "true" - buildConfigField "boolean", "NSCLIENTOLNY", "false" - buildConfigField "boolean", "CLOSEDLOOP", "false" buildConfigField "boolean", "G5UPLOADER", "false" buildConfigField "boolean", "PUMPCONTROL", "false" } @@ -125,7 +110,6 @@ android { buildConfigField "boolean", "APS", "false" buildConfigField "boolean", "PUMPDRIVERS", "true" buildConfigField "boolean", "NSCLIENTOLNY", "false" - buildConfigField "boolean", "CLOSEDLOOP", "false" buildConfigField "boolean", "G5UPLOADER", "false" buildConfigField "boolean", "PUMPCONTROL", "true" } @@ -139,7 +123,6 @@ android { buildConfigField "boolean", "APS", "false" buildConfigField "boolean", "PUMPDRIVERS", "false" buildConfigField "boolean", "NSCLIENTOLNY", "true" - buildConfigField "boolean", "CLOSEDLOOP", "false" buildConfigField "boolean", "G5UPLOADER", "false" buildConfigField "boolean", "PUMPCONTROL", "false" } @@ -153,7 +136,6 @@ android { buildConfigField "boolean", "APS", "false" buildConfigField "boolean", "PUMPDRIVERS", "false" buildConfigField "boolean", "NSCLIENTOLNY", "false" - buildConfigField "boolean", "CLOSEDLOOP", "false" buildConfigField "boolean", "G5UPLOADER", "true" buildConfigField "boolean", "PUMPCONTROL", "false" } diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 5087fefefa..9b30f48ea2 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -28,6 +28,7 @@ import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.Actions.ActionsFragment; import info.nightscout.androidaps.plugins.Careportal.CareportalPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; @@ -191,13 +192,16 @@ public class MainApp extends Application { MainApp.getConfigBuilder().initialize(); } NSUpload.uploadAppStart(); + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + if (Config.NSCLIENT) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-NSClient")); else if (Config.G5UPLOADER) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-G5Uploader")); else if (Config.PUMPCONTROL) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-PumpControl")); - else if (MainApp.getConfigBuilder().isClosedModeEnabled()) + else if (closedLoopEnabled.get()) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop")); else FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-OpenLoop")); 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 471b7735cf..442bf914b0 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -9,7 +9,7 @@ public interface ConstraintsInterface { void limitRunningLoop(BooleanConstraint value); - boolean isClosedModeEnabled(); + void limitClosedLoop(BooleanConstraint value); boolean isAutosensModeEnabled(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 3f5283ec48..c88b369b25 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -453,23 +453,21 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; + ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - constrain.limitRunningLoop(value); + constraint.limitRunningLoop(value); } } @Override - public boolean isClosedModeEnabled() { - boolean result = true; + public void limitClosedLoop(BooleanConstraint value) { ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; + ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - result = result && constrain.isClosedModeEnabled(); + constraint.limitClosedLoop(value); } - return result; } @Override 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 0516cdda51..cd80cffebf 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 @@ -15,9 +15,9 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.APSInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; @@ -183,8 +183,9 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { case 2: return new RequirementResult(true, ""); case 3: - boolean closedModeEnabled = SafetyPlugin.getPlugin().isClosedModeEnabled(); - return new RequirementResult(closedModeEnabled, MainApp.sResources.getString(R.string.closedmodeenabled) + ": " + yesOrNo(closedModeEnabled)); + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + SafetyPlugin.getPlugin().limitClosedLoop(closedLoopEnabled); + return new RequirementResult(closedLoopEnabled.get(), MainApp.sResources.getString(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get())); case 4: double maxIOB = MainApp.getConfigBuilder().applyMaxIOBConstraints(1000d); boolean maxIobSet = maxIOB > 0; @@ -296,12 +297,13 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { @Override public void limitRunningLoop(BooleanConstraint value) { if (objectives.get(0).started.getTime() == 0) - value.set(false, MainApp.gs(R.string.objective1notstarted)); + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1)); } @Override - public boolean isClosedModeEnabled() { - return objectives.get(3).started.getTime() > 0; + public void limitClosedLoop(BooleanConstraint value) { + if (objectives.get(3).started.getTime() == 0) + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4)); } @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 4062836c48..1b39a8bc9c 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 @@ -5,7 +5,6 @@ import org.slf4j.LoggerFactory; import java.util.Objects; -import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; @@ -103,10 +102,13 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { * Constraints interface **/ @Override - public boolean isClosedModeEnabled() { - if (!MainApp.isEngineeringModeOrRelease()) return false; + public void limitClosedLoop(BooleanConstraint value) { + if (!MainApp.isEngineeringModeOrRelease()) + value.set(false, MainApp.gs(R.string.closed_loop_disabled_on_dev_branch)); + String mode = SP.getString("aps_mode", "open"); - return mode.equals("closed") && BuildConfig.CLOSEDLOOP; + if (!mode.equals("closed")) + value.set(false, MainApp.gs(R.string.closedmodedisabledinpreferences)); } @Override 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 a1ca643adc..3452a2ff3f 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 @@ -26,9 +26,9 @@ import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.APSInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui; @@ -152,7 +152,7 @@ public class LoopPlugin implements PluginBase { @Subscribe public void onStatusEvent(final EventTreatmentChange ev) { - if (ev.treatment == null || !ev.treatment.isSMB){ + if (ev.treatment == null || !ev.treatment.isSMB) { invoke("EventTreatmentChange", true); } } @@ -331,7 +331,10 @@ public class LoopPlugin implements PluginBase { return; } - if (MainApp.getConfigBuilder().isClosedModeEnabled()) { + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + + if (closedLoopEnabled.get()) { if (result.isChangeRequested()) { final PumpEnactResult waiting = new PumpEnactResult(); waiting.queued = true; 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 dfab195125..97fedba4a0 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 @@ -19,6 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -248,7 +249,9 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultAMA.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConfigBuilder().isClosedModeEnabled()) { + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + if (!closedLoopEnabled.get()) { long now = System.currentTimeMillis(); TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) { 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 76df63e752..3d9cf3ea4d 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 @@ -19,6 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.ScriptReader; @@ -237,7 +238,9 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultMA.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConfigBuilder().isClosedModeEnabled()) { + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + if (!closedLoopEnabled.get()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) { // going to cancel 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 9df54b34d3..5de8676973 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 @@ -27,12 +27,11 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.TemporaryBasal; -import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.ScriptReader; import info.nightscout.androidaps.plugins.OpenAPSMA.LoggerCallback; -import info.nightscout.androidaps.plugins.SourceDexcomG5.SourceDexcomG5Plugin; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; @@ -217,6 +216,9 @@ public class DetermineBasalAdapterSMBJS { String units = profile.getUnits(); + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + mProfile = new JSONObject(); mProfile.put("max_iob", maxIob); @@ -246,7 +248,7 @@ public class DetermineBasalAdapterSMBJS { mProfile.put("remainingCarbsCap", SMBDefaults.remainingCarbsCap); mProfile.put("enableUAM", SP.getBoolean(R.string.key_use_uam, false)); mProfile.put("A52_risk_enable", SMBDefaults.A52_risk_enable); - boolean SMBEnabled = SP.getBoolean(R.string.key_use_smb, false) && MainApp.getConfigBuilder().isClosedModeEnabled(); + boolean SMBEnabled = SP.getBoolean(R.string.key_use_smb, false) && closedLoopEnabled.get(); mProfile.put("enableSMB_with_COB", SMBEnabled && SP.getBoolean(R.string.key_enableSMB_with_COB, false)); mProfile.put("enableSMB_with_temptarget", SMBEnabled && SP.getBoolean(R.string.key_enableSMB_with_temptarget, false)); mProfile.put("allowSMB_with_high_temptarget", SMBEnabled && SP.getBoolean(R.string.key_allowSMB_with_high_temptarget, false)); 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 f10e6cb0be..3c76698289 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 @@ -19,6 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -253,7 +254,9 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultSMB.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConfigBuilder().isClosedModeEnabled()) { + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + if (!closedLoopEnabled.get()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) { // going to cancel diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index e90a77c524..c4b2bb97d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -87,6 +87,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; @@ -1016,6 +1017,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } } + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + // open loop mode final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun; if (Config.APS && pump.getPumpDescription().isTempBasalCapable) { @@ -1036,7 +1040,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, apsModeView.setText(MainApp.sResources.getString(R.string.pumpsuspended)); apsModeView.setTextColor(Color.WHITE); } else if (activeloop != null && activeloop.isEnabled(activeloop.getType())) { - if (MainApp.getConfigBuilder().isClosedModeEnabled()) { + if (closedLoopEnabled.get()) { apsModeView.setText(MainApp.sResources.getString(R.string.closedloop)); } else { apsModeView.setText(MainApp.sResources.getString(R.string.openloop)); @@ -1066,7 +1070,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, // **** Temp button **** if (acceptTempLayout != null) { - boolean showAcceptButton = !MainApp.getConfigBuilder().isClosedModeEnabled(); // Open mode needed + boolean showAcceptButton = !closedLoopEnabled.get(); // Open mode needed showAcceptButton = showAcceptButton && finalLastRun != null && finalLastRun.lastAPSRun != null; // aps result must exist showAcceptButton = showAcceptButton && (finalLastRun.lastOpenModeAccept == null || finalLastRun.lastOpenModeAccept.getTime() < finalLastRun.lastAPSRun.getTime()); // never accepted or before last result showAcceptButton = showAcceptButton && finalLastRun.constraintsProcessed.isChangeRequested(); // change is requested 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 85a2dbbf55..9a8293f091 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 @@ -1428,8 +1428,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public boolean isClosedModeEnabled() { - return true; + public void limitClosedLoop(BooleanConstraint 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 111919c47d..70eddecf85 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 @@ -441,8 +441,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, } @Override - public boolean isClosedModeEnabled() { - return true; + public void limitClosedLoop(BooleanConstraint 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 1bc785352d..2604c4e1ed 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 @@ -279,8 +279,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public boolean isClosedModeEnabled() { - return true; + public void limitClosedLoop(BooleanConstraint 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 9889c9723c..6dd70ba38e 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 @@ -1084,8 +1084,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } @Override - public boolean isClosedModeEnabled() { - return true; + public void limitClosedLoop(BooleanConstraint value) { } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index b304490e22..d912c2a8d6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.Wear; -import android.Manifest; import android.os.HandlerThread; import android.support.annotation.NonNull; @@ -27,8 +26,8 @@ import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -436,7 +435,9 @@ public class ActionStringHandler { // decide if enabled/disabled closed/open; what Plugin as APS? final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop(); if (activeloop != null && activeloop.isEnabled(activeloop.getType())) { - if (MainApp.getConfigBuilder().isClosedModeEnabled()) { + BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + if (closedLoopEnabled.get()) { ret += "CLOSED LOOP\n"; } else { ret += "OPEN LOOP\n"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1eff2b796e..c962481eaf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -962,8 +962,9 @@ ProfileSwitch missing. Please do a profile switch or press \"Activate Profile\" in the LocalProfile. Bolus count TBR count - Objective 1 not started + Objective %d not started Pump is not temp basal capable No valid basal rate read from pump + Closed loop mode disabled in preferences From 52ffcba047bdc5ea6a52ab49abf71abc8c00d2b3 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 17:38:48 +0100 Subject: [PATCH 03/28] generic Constraint type --- .../info/nightscout/androidaps/MainApp.java | 7 ++-- .../interfaces/ConstraintsInterface.java | 6 ++-- .../constrains/BooleanConstraint.java | 29 --------------- .../interfaces/constrains/Constraint.java | 22 +++++++++++- .../constrains/DoubleConstraint.java | 29 --------------- .../constrains/IntegerConstraint.java | 28 --------------- .../ConfigBuilder/ConfigBuilderPlugin.java | 8 +++-- .../ObjectivesPlugin.java | 10 +++--- .../ConstraintsSafety/SafetyPlugin.java | 18 +++++----- .../androidaps/plugins/Loop/LoopPlugin.java | 6 ++-- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 8 ++--- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 6 ++-- .../DetermineBasalAdapterSMBJS.java | 4 +-- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 6 ++-- .../plugins/Overview/OverviewFragment.java | 4 +-- .../plugins/PumpCombo/ComboPlugin.java | 8 +++-- .../PumpDanaR/AbstractDanaRPlugin.java | 8 +++-- .../plugins/PumpDanaRS/DanaRSPlugin.java | 10 +++--- .../PumpInsight/InsightPumpPlugin.java | 8 +++-- .../plugins/Wear/ActionStringHandler.java | 6 ++-- ...onstraintTest.java => ConstraintTest.java} | 17 +++++---- .../contraints/DoubleConstraintTest.java | 36 ------------------- .../contraints/IntegerConstraintTest.java | 34 ------------------ 23 files changed, 94 insertions(+), 224 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/interfaces/constrains/BooleanConstraint.java delete mode 100644 app/src/main/java/info/nightscout/androidaps/interfaces/constrains/DoubleConstraint.java delete mode 100644 app/src/main/java/info/nightscout/androidaps/interfaces/constrains/IntegerConstraint.java rename app/src/test/java/info/nightscout/androidaps/interfaces/contraints/{BooleanConstraintTest.java => ConstraintTest.java} (53%) delete mode 100644 app/src/test/java/info/nightscout/androidaps/interfaces/contraints/DoubleConstraintTest.java delete mode 100644 app/src/test/java/info/nightscout/androidaps/interfaces/contraints/IntegerConstraintTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 9b30f48ea2..b62ba501a1 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -28,7 +28,7 @@ import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.Actions.ActionsFragment; import info.nightscout.androidaps.plugins.Careportal.CareportalPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; @@ -80,7 +80,6 @@ import info.nightscout.androidaps.receivers.KeepAliveReceiver; import info.nightscout.androidaps.receivers.NSAlarmReceiver; import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.NSUpload; -import info.nightscout.utils.SP; import io.fabric.sdk.android.Fabric; @@ -192,8 +191,6 @@ public class MainApp extends Application { MainApp.getConfigBuilder().initialize(); } NSUpload.uploadAppStart(); - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); - MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); if (Config.NSCLIENT) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-NSClient")); @@ -201,7 +198,7 @@ public class MainApp extends Application { FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-G5Uploader")); else if (Config.PUMPCONTROL) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-PumpControl")); - else if (closedLoopEnabled.get()) + else if (MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop")); else FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-OpenLoop")); 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 442bf914b0..50b5080a59 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -1,15 +1,15 @@ package info.nightscout.androidaps.interfaces; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; /** * Created by mike on 15.06.2016. */ public interface ConstraintsInterface { - void limitRunningLoop(BooleanConstraint value); + Constraint limitRunningLoop(Constraint value); - void limitClosedLoop(BooleanConstraint value); + Constraint limitClosedLoop(Constraint value); boolean isAutosensModeEnabled(); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/BooleanConstraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/BooleanConstraint.java deleted file mode 100644 index eda25b9d68..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/BooleanConstraint.java +++ /dev/null @@ -1,29 +0,0 @@ -package info.nightscout.androidaps.interfaces.constrains; - -/** - * Created by mike on 19.03.2018. - */ - -public class BooleanConstraint extends Constraint { - boolean value; - - public BooleanConstraint(boolean value) { - this.value = value; - } - - public boolean get() { - return value; - } - - public BooleanConstraint set(boolean value) { - this.value = value; - return this; - } - - public BooleanConstraint set(boolean value, String reason) { - this.value = value; - reason(reason); - return this; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java index 4cc4ef7c79..73a15f8bc7 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java @@ -7,9 +7,29 @@ import java.util.List; * Created by mike on 19.03.2018. */ -public class Constraint { +public class Constraint { + T value; List reasons = new ArrayList<>(); + public Constraint(T value) { + this.value = value; + } + + public T get() { + return value; + } + + public Constraint set(T value) { + this.value = value; + return this; + } + + public Constraint set(T value, String reason) { + this.value = value; + reason(reason); + return this; + } + public Constraint reason(String reason) { reasons.add(reason); return this; diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/DoubleConstraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/DoubleConstraint.java deleted file mode 100644 index 211a545219..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/DoubleConstraint.java +++ /dev/null @@ -1,29 +0,0 @@ -package info.nightscout.androidaps.interfaces.constrains; - -/** - * Created by mike on 19.03.2018. - */ - -public class DoubleConstraint extends Constraint{ - double value; - - public DoubleConstraint(double value) { - this.value = value; - } - - public Double getDouble() { - return value; - } - - public DoubleConstraint set(double value) { - this.value = value; - return this; - } - - public DoubleConstraint set(double value, String reason) { - this.value = value; - reason(reason); - return this; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/IntegerConstraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/IntegerConstraint.java deleted file mode 100644 index 31d2a76e82..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/IntegerConstraint.java +++ /dev/null @@ -1,28 +0,0 @@ -package info.nightscout.androidaps.interfaces.constrains; - -/** - * Created by mike on 19.03.2018. - */ - -public class IntegerConstraint extends Constraint { - int value; - - public IntegerConstraint(int value) { - this.value = value; - } - - public int getInteger() { - return value; - } - - public IntegerConstraint set(int value) { - this.value = value; - return this; - } - - public IntegerConstraint set(int value, String reason) { - this.value = value; - reason(reason); - return this; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index c88b369b25..466df7911d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -31,7 +31,6 @@ import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppInitialized; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; @@ -39,6 +38,7 @@ import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; @@ -449,7 +449,7 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr * Constraints interface **/ @Override - public void limitRunningLoop(BooleanConstraint value) { + public Constraint limitRunningLoop(Constraint value) { ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { @@ -457,10 +457,11 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; constraint.limitRunningLoop(value); } + return value; } @Override - public void limitClosedLoop(BooleanConstraint value) { + public Constraint limitClosedLoop(Constraint value) { ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { @@ -468,6 +469,7 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; constraint.limitClosedLoop(value); } + return value; } @Override 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 cd80cffebf..3ce466e0b8 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 @@ -17,7 +17,7 @@ import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; @@ -183,7 +183,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { case 2: return new RequirementResult(true, ""); case 3: - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + Constraint closedLoopEnabled = new Constraint<>(true); SafetyPlugin.getPlugin().limitClosedLoop(closedLoopEnabled); return new RequirementResult(closedLoopEnabled.get(), MainApp.sResources.getString(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get())); case 4: @@ -295,15 +295,17 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { * Constraints interface **/ @Override - public void limitRunningLoop(BooleanConstraint value) { + public Constraint limitRunningLoop(Constraint value) { if (objectives.get(0).started.getTime() == 0) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1)); + return value; } @Override - public void limitClosedLoop(BooleanConstraint value) { + public Constraint limitClosedLoop(Constraint value) { if (objectives.get(3).started.getTime() == 0) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4)); + 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 1b39a8bc9c..68fe73f991 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 @@ -10,9 +10,9 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.utils.HardLimits; import info.nightscout.utils.Round; @@ -92,23 +92,25 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { return R.xml.pref_safety; } - @Override - public void limitRunningLoop(BooleanConstraint value) { - if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable) - value.set(false, MainApp.gs(R.string.pumpisnottempbasalcapable)); - } - /** * Constraints interface **/ @Override - public void limitClosedLoop(BooleanConstraint value) { + public Constraint limitRunningLoop(Constraint value) { + if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable) + value.set(false, MainApp.gs(R.string.pumpisnottempbasalcapable)); + return value; + } + + @Override + public Constraint limitClosedLoop(Constraint value) { if (!MainApp.isEngineeringModeOrRelease()) value.set(false, MainApp.gs(R.string.closed_loop_disabled_on_dev_branch)); String mode = SP.getString("aps_mode", "open"); if (!mode.equals("closed")) value.set(false, MainApp.gs(R.string.closedmodedisabledinpreferences)); + return value; } @Override 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 3452a2ff3f..b947330fa4 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 @@ -28,7 +28,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui; @@ -259,7 +259,7 @@ public class LoopPlugin implements PluginBase { try { if (Config.logFunctionCalls) log.debug("invoke from " + initiator); - BooleanConstraint loopEnabled = new BooleanConstraint(true); + Constraint loopEnabled = new Constraint<>(true); MainApp.getConfigBuilder().limitRunningLoop(loopEnabled); if (!loopEnabled.get()) { @@ -331,7 +331,7 @@ public class LoopPlugin implements PluginBase { return; } - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + Constraint closedLoopEnabled = new Constraint<>(true); MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); if (closedLoopEnabled.get()) { 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 97fedba4a0..df34cf6316 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 @@ -19,7 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -249,12 +249,10 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultAMA.tempBasalReqested = false; // limit requests on openloop mode - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); - MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); - if (!closedLoopEnabled.get()) { + if (!MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) { long now = System.currentTimeMillis(); TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); - if (activeTemp != null && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) { + if (activeTemp != null && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) { // going to cancel } else if (activeTemp != null && Math.abs(determineBasalResultAMA.rate - activeTemp.tempBasalConvertedToAbsolute(now, profile)) < 0.1) { determineBasalResultAMA.tempBasalReqested = false; 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 3d9cf3ea4d..ca1c7608b0 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 @@ -19,7 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.ScriptReader; @@ -238,9 +238,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultMA.tempBasalReqested = false; // limit requests on openloop mode - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); - MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); - if (!closedLoopEnabled.get()) { + if (!MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) { // going to cancel 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 5de8676973..acd134d654 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 @@ -27,7 +27,7 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.TemporaryBasal; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.ScriptReader; @@ -216,7 +216,7 @@ public class DetermineBasalAdapterSMBJS { String units = profile.getUnits(); - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + Constraint closedLoopEnabled = new Constraint<>(true); MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); mProfile = new JSONObject(); 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 3c76698289..f27eabd239 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 @@ -19,7 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -254,9 +254,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultSMB.tempBasalReqested = false; // limit requests on openloop mode - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); - MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); - if (!closedLoopEnabled.get()) { + if (!MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) { // going to cancel diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index c4b2bb97d4..c439e274d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -87,7 +87,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; @@ -1017,7 +1017,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } } - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); + Constraint closedLoopEnabled = new Constraint<>(true); MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); // open loop mode 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 9a8293f091..2cea72a5af 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 @@ -28,11 +28,11 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.events.EventRefreshOverview; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; @@ -1422,13 +1422,15 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf private boolean validBasalRateProfileSelectedOnPump = true; @Override - public void limitRunningLoop(BooleanConstraint value) { + public Constraint limitRunningLoop(Constraint value) { if (!validBasalRateProfileSelectedOnPump) value.set(false, MainApp.gs(R.string.novalidbasalrate)); + return value; } @Override - public void limitClosedLoop(BooleanConstraint value) { + public Constraint limitClosedLoop(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 70eddecf85..c727fc2223 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 @@ -19,13 +19,13 @@ import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.DanaRInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; @@ -437,11 +437,13 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, */ @Override - public void limitRunningLoop(BooleanConstraint value) { + public Constraint limitRunningLoop(Constraint value) { + return value; } @Override - public void limitClosedLoop(BooleanConstraint value) { + public Constraint limitClosedLoop(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 2604c4e1ed..47ed05157a 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 @@ -30,18 +30,18 @@ import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.DanaRInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.DetailedBolusInfoStorage; -import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRFragment; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; @@ -275,11 +275,13 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, // Constraints interface @Override - public void limitRunningLoop(BooleanConstraint value) { + public Constraint limitRunningLoop(Constraint value) { + return value; } @Override - public void limitClosedLoop(BooleanConstraint value) { + public Constraint limitClosedLoop(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 6dd70ba38e..ee5bef5200 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 @@ -22,11 +22,11 @@ import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; @@ -1080,11 +1080,13 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints // Constraints @Override - public void limitRunningLoop(BooleanConstraint value){ + public Constraint limitRunningLoop(Constraint value) { + return value; } @Override - public void limitClosedLoop(BooleanConstraint value) { + public Constraint limitClosedLoop(Constraint value) { + return value; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index d912c2a8d6..e443bbf161 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -27,7 +27,7 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -435,9 +435,7 @@ public class ActionStringHandler { // decide if enabled/disabled closed/open; what Plugin as APS? final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop(); if (activeloop != null && activeloop.isEnabled(activeloop.getType())) { - BooleanConstraint closedLoopEnabled = new BooleanConstraint(true); - MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); - if (closedLoopEnabled.get()) { + if (MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) { ret += "CLOSED LOOP\n"; } else { ret += "OPEN LOOP\n"; diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/BooleanConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/ConstraintTest.java similarity index 53% rename from app/src/test/java/info/nightscout/androidaps/interfaces/contraints/BooleanConstraintTest.java rename to app/src/test/java/info/nightscout/androidaps/interfaces/contraints/ConstraintTest.java index 610f3f5519..c97d3e4750 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/BooleanConstraintTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/ConstraintTest.java @@ -6,27 +6,30 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; +import info.nightscout.androidaps.interfaces.constrains.Constraint; /** * Created by mike on 19.03.2018. */ @RunWith(PowerMockRunner.class) -public class BooleanConstraintTest { +public class ConstraintTest { @Test public void doTests() throws Exception { - BooleanConstraint c; + Constraint c; - c = new BooleanConstraint(true); - Assert.assertEquals(true, c.get()); + c = new Constraint(true); + Assert.assertEquals(Boolean.TRUE, c.get()); Assert.assertEquals("", c.getReasons()); c.set(false); - Assert.assertEquals(false, c.get()); + Assert.assertEquals(Boolean.FALSE, c.get()); Assert.assertEquals("", c.getReasons()); c.set(true, "Set true"); - Assert.assertEquals(true, c.get()); + Assert.assertEquals(Boolean.TRUE, c.get()); Assert.assertEquals("Set true", c.getReasons()); + c.set(false, "Set false"); + Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals("Set true\nSet false", c.getReasons()); } } diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/DoubleConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/DoubleConstraintTest.java deleted file mode 100644 index b99376b033..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/DoubleConstraintTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package info.nightscout.androidaps.interfaces.contraints; - -import junit.framework.Assert; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.nightscout.androidaps.interfaces.constrains.DoubleConstraint; - -/** - * Created by mike on 19.03.2018. - */ - -@RunWith(PowerMockRunner.class) -public class DoubleConstraintTest { - - @Test - public void doTests() throws Exception { - DoubleConstraint c; - - c = new DoubleConstraint(10d); - Assert.assertEquals(10d, c.getDouble()); - Assert.assertEquals("", c.getReasons()); - c.set(11d); - Assert.assertEquals(11d, c.getDouble()); - Assert.assertEquals("", c.getReasons()); - c.set(9d, "Set 9d"); - Assert.assertEquals(9d, c.getDouble()); - Assert.assertEquals("Set 9d", c.getReasons()); - c.set(8d, "Set 8d"); - Assert.assertEquals(8d, c.getDouble()); - Assert.assertEquals("Set 9d\nSet 8d", c.getReasons()); - - } -} diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/IntegerConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/IntegerConstraintTest.java deleted file mode 100644 index 910021e305..0000000000 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/IntegerConstraintTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package info.nightscout.androidaps.interfaces.contraints; - -import junit.framework.Assert; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.modules.junit4.PowerMockRunner; - -import info.nightscout.androidaps.interfaces.constrains.BooleanConstraint; -import info.nightscout.androidaps.interfaces.constrains.IntegerConstraint; - -/** - * Created by mike on 19.03.2018. - */ - -@RunWith(PowerMockRunner.class) -public class IntegerConstraintTest { - - @Test - public void doTests() throws Exception { - IntegerConstraint c; - - c = new IntegerConstraint(10); - Assert.assertEquals(10, c.getInteger()); - Assert.assertEquals("", c.getReasons()); - c.set(11); - Assert.assertEquals(11, c.getInteger()); - Assert.assertEquals("", c.getReasons()); - c.set(9, "Set 9"); - Assert.assertEquals(9, c.getInteger()); - Assert.assertEquals("Set 9", c.getReasons()); - - } -} From ce6365509780f11309f74050b5b6329d227e8c0f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 18:21:02 +0100 Subject: [PATCH 04/28] move constraints interface functions from ConfigBuilder to ConstraintsChecker class --- .../info/nightscout/androidaps/MainApp.java | 11 +- .../androidaps/data/ConstraintChecker.java | 147 ++++++++++++++++++ .../{constrains => }/Constraint.java | 2 +- .../interfaces/ConstraintsInterface.java | 2 - .../plugins/Actions/dialogs/FillDialog.java | 4 +- .../dialogs/NewExtendedBolusDialog.java | 4 +- .../Actions/dialogs/NewTempBasalDialog.java | 4 +- .../Dialogs/NewNSTreatmentDialog.java | 11 +- .../ConfigBuilder/ConfigBuilderPlugin.java | 133 +--------------- .../ObjectivesPlugin.java | 4 +- .../ConstraintsSafety/SafetyPlugin.java | 2 +- .../androidaps/plugins/Loop/LoopPlugin.java | 10 +- .../DetermineBasalAdapterAMAJS.java | 2 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 8 +- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 6 +- .../DetermineBasalAdapterSMBJS.java | 6 +- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 8 +- .../Overview/Dialogs/NewCarbsDialog.java | 4 +- .../Overview/Dialogs/NewInsulinDialog.java | 4 +- .../Overview/Dialogs/NewTreatmentDialog.java | 8 +- .../Overview/Dialogs/WizardDialog.java | 14 +- .../plugins/Overview/OverviewFragment.java | 8 +- .../plugins/PumpCombo/ComboPlugin.java | 2 +- .../PumpDanaR/AbstractDanaRPlugin.java | 9 +- .../plugins/PumpDanaR/DanaRPlugin.java | 9 +- .../plugins/PumpDanaR/comm/MsgBolusStart.java | 3 +- .../comm/MsgBolusStartWithSpeed.java | 2 +- .../comm/MsgSetExtendedBolusStart.java | 3 +- .../PumpDanaRKorean/DanaRKoreanPlugin.java | 9 +- .../plugins/PumpDanaRS/DanaRSPlugin.java | 15 +- ...aRS_Packet_Bolus_Set_Step_Bolus_Start.java | 5 +- .../plugins/PumpDanaRv2/DanaRv2Plugin.java | 10 +- .../PumpInsight/InsightPumpPlugin.java | 2 +- .../SmsCommunicatorPlugin.java | 6 +- .../plugins/Wear/ActionStringHandler.java | 18 +-- .../androidaps/queue/CommandQueue.java | 12 +- .../{contraints => }/ConstraintTest.java | 4 +- 37 files changed, 257 insertions(+), 254 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java rename app/src/main/java/info/nightscout/androidaps/interfaces/{constrains => }/Constraint.java (93%) rename app/src/test/java/info/nightscout/androidaps/interfaces/{contraints => }/ConstraintTest.java (87%) diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index b62ba501a1..6b9754a246 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -25,10 +25,11 @@ import java.util.ArrayList; import ch.qos.logback.classic.LoggerContext; import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.data.ConstraintChecker; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Actions.ActionsFragment; import info.nightscout.androidaps.plugins.Careportal.CareportalPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; @@ -93,6 +94,7 @@ public class MainApp extends Application { private static DatabaseHelper sDatabaseHelper = null; private static ConfigBuilderPlugin sConfigBuilder = null; + private static ConstraintChecker sConstraintsChecker = null; private static ArrayList pluginsList = null; @@ -109,6 +111,7 @@ public class MainApp extends Application { super.onCreate(); sInstance = this; sResources = getResources(); + sConstraintsChecker = new ConstraintChecker(this); try { if (FabricPrivacy.fabricEnabled()) { @@ -198,7 +201,7 @@ public class MainApp extends Application { FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-G5Uploader")); else if (Config.PUMPCONTROL) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-PumpControl")); - else if (MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) + else if (MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop")); else FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-OpenLoop")); @@ -294,6 +297,10 @@ public class MainApp extends Application { return sConfigBuilder; } + public static ConstraintChecker getConstraintChecker() { + return sConstraintsChecker; + } + public static ArrayList getPluginsList() { return pluginsList; } diff --git a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java new file mode 100644 index 0000000000..1a17880cf5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -0,0 +1,147 @@ +package info.nightscout.androidaps.data; + +import java.util.ArrayList; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.ConstraintsInterface; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.Constraint; +import info.nightscout.utils.SP; + +/** + * Created by mike on 19.03.2018. + */ + +public class ConstraintChecker implements ConstraintsInterface { + + private MainApp mainApp; + + public ConstraintChecker(MainApp mainApp) { + this.mainApp = mainApp; + } + + @Override + public Constraint limitRunningLoop(Constraint value) { + + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constraint = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + constraint.limitRunningLoop(value); + } + return value; + } + + @Override + public Constraint limitClosedLoop(Constraint value) { + + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constraint = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + constraint.limitClosedLoop(value); + } + return value; + } + + @Override + public boolean isAutosensModeEnabled() { + boolean result = true; + + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constrain = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + result = result && constrain.isAutosensModeEnabled(); + } + return result; + } + + @Override + public boolean isAMAModeEnabled() { + boolean result = SP.getBoolean("openapsama_useautosens", false); + + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constrain = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + result = result && constrain.isAMAModeEnabled(); + } + return result; + } + + @Override + public boolean isSMBModeEnabled() { + boolean result = true; // TODO update for SMB // SP.getBoolean("openapsama_useautosens", false); + + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constrain = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + result = result && constrain.isSMBModeEnabled(); + } + return result; + } + + @Override + public Double applyBasalConstraints(Double absoluteRate) { + Double rateAfterConstrain = absoluteRate; + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constrain = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + rateAfterConstrain = Math.min(constrain.applyBasalConstraints(absoluteRate), rateAfterConstrain); + } + return rateAfterConstrain; + } + + @Override + public Integer applyBasalConstraints(Integer percentRate) { + Integer rateAfterConstrain = percentRate; + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constrain = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + rateAfterConstrain = Math.min(constrain.applyBasalConstraints(percentRate), rateAfterConstrain); + } + return rateAfterConstrain; + } + + @Override + public Double applyBolusConstraints(Double insulin) { + Double insulinAfterConstrain = insulin; + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constrain = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + insulinAfterConstrain = Math.min(constrain.applyBolusConstraints(insulin), insulinAfterConstrain); + } + return insulinAfterConstrain; + } + + @Override + public Integer applyCarbsConstraints(Integer carbs) { + Integer carbsAfterConstrain = carbs; + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constrain = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + carbsAfterConstrain = Math.min(constrain.applyCarbsConstraints(carbs), carbsAfterConstrain); + } + return carbsAfterConstrain; + } + + @Override + public Double applyMaxIOBConstraints(Double maxIob) { + Double maxIobAfterConstrain = maxIob; + ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + for (PluginBase p : constraintsPlugins) { + ConstraintsInterface constrain = (ConstraintsInterface) p; + if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; + maxIobAfterConstrain = Math.min(constrain.applyMaxIOBConstraints(maxIob), maxIobAfterConstrain); + } + return maxIobAfterConstrain; + } + + +} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java similarity index 93% rename from app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java rename to app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java index 73a15f8bc7..83e7c2119d 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/constrains/Constraint.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.interfaces.constrains; +package info.nightscout.androidaps.interfaces; import java.util.ArrayList; import java.util.List; 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 50b5080a59..495039bf9b 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -1,7 +1,5 @@ package info.nightscout.androidaps.interfaces; -import info.nightscout.androidaps.interfaces.constrains.Constraint; - /** * Created by mike on 15.06.2016. */ diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java index 8297e28412..b50e4f01a2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -60,7 +60,7 @@ public class FillDialog extends DialogFragment implements OnClickListener { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); double bolusstep = ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep; editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_insulinamount); editInsulin.setParams(0d, 0d, maxInsulin, bolusstep, new DecimalFormat("0.00"), false); @@ -138,7 +138,7 @@ public class FillDialog extends DialogFragment implements OnClickListener { String confirmMessage = getString(R.string.fillwarning) + "\n"; - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); confirmMessage += getString(R.string.bolus) + ": " + insulinAfterConstraints + "U"; if (insulinAfterConstraints - insulin != 0) confirmMessage += "\n" + getString(R.string.constraintapllied); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java index e2bcbe2acd..36da617fc5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java @@ -43,7 +43,7 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli View view = inflater.inflate(R.layout.overview_newextendedbolus_dialog, container, false); - Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); editInsulin = (NumberPicker) view.findViewById(R.id.overview_newextendedbolus_insulin); editInsulin.setParams(0d, 0d, maxInsulin, 0.1d, new DecimalFormat("0.00"), false); @@ -70,7 +70,7 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli String confirmMessage = getString(R.string.setextendedbolusquestion); - Double insulinAfterConstraint = MainApp.getConfigBuilder().applyBolusConstraints(insulin); + Double insulinAfterConstraint = MainApp.getConstraintChecker().applyBolusConstraints(insulin); confirmMessage += " " + insulinAfterConstraint + " U "; confirmMessage += getString(R.string.duration) + " " + durationInMinutes + "min ?"; if (insulinAfterConstraint - insulin != 0d) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java index 8e5c1737c9..837c176e85 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java @@ -120,14 +120,14 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi String confirmMessage = getString(R.string.setbasalquestion); if (setAsPercent) { int basalPercentInput = SafeParse.stringToInt(basalPercent.getText()); - percent = MainApp.getConfigBuilder().applyBasalConstraints(basalPercentInput); + percent = MainApp.getConstraintChecker().applyBasalConstraints(basalPercentInput); confirmMessage += "\n" + percent + "% "; confirmMessage += "\n" + getString(R.string.duration) + " " + durationInMinutes + "min ?"; if (percent != basalPercentInput) confirmMessage += "\n" + getString(R.string.constraintapllied); } else { Double basalAbsoluteInput = SafeParse.stringToDouble(basalAbsolute.getText()); - absolute = MainApp.getConfigBuilder().applyBasalConstraints(basalAbsoluteInput); + absolute = MainApp.getConstraintChecker().applyBasalConstraints(basalAbsoluteInput); confirmMessage += "\n" + absolute + " U/h "; confirmMessage += "\n" + getString(R.string.duration) + " " + durationInMinutes + "min ?"; if (absolute - basalAbsoluteInput != 0d) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index 99fc44341b..fcd366dad8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -55,11 +55,10 @@ import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity; import info.nightscout.androidaps.queue.Callback; -import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.DateUtil; +import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.NSUpload; import info.nightscout.utils.NumberPicker; -import info.nightscout.utils.OKDialog; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.Translator; @@ -272,11 +271,11 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }); - Integer maxCarbs = MainApp.getConfigBuilder().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); + Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); editCarbs = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbsinput); editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false); - Double maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); editInsulin = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_insulininput); editInsulin.setParams(0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false); @@ -303,7 +302,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }; - Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit); + Integer maxPercent = MainApp.getConstraintChecker().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit); editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput); editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true, percentTextWatcher); @@ -325,7 +324,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }; - Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit); + Double maxAbsolute = MainApp.getConstraintChecker().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit); editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput); editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true, absoluteTextWatcher); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 466df7911d..dca42d81cf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -31,14 +31,12 @@ import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppInitialized; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface; -import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; @@ -47,13 +45,12 @@ import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.CommandQueue; import info.nightscout.utils.NSUpload; -import info.nightscout.utils.SP; import info.nightscout.utils.ToastUtils; /** * Created by mike on 05.08.2016. */ -public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, TreatmentsInterface { +public class ConfigBuilderPlugin implements PluginBase, TreatmentsInterface { private static Logger log = LoggerFactory.getLogger(ConfigBuilderPlugin.class); private static BgSourceInterface activeBgSource; @@ -354,7 +351,7 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr */ public void applyTBRRequest(APSResult request, Profile profile, Callback callback) { PumpInterface pump = getActivePump(); - request.rate = applyBasalConstraints(request.rate); + request.rate = MainApp.getConstraintChecker().applyBasalConstraints(request.rate); long now = System.currentTimeMillis(); @@ -445,132 +442,6 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr } } - /** - * Constraints interface - **/ - @Override - public Constraint limitRunningLoop(Constraint value) { - - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constraint = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - constraint.limitRunningLoop(value); - } - return value; - } - - @Override - public Constraint limitClosedLoop(Constraint value) { - - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constraint = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - constraint.limitClosedLoop(value); - } - return value; - } - - @Override - public boolean isAutosensModeEnabled() { - boolean result = true; - - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - result = result && constrain.isAutosensModeEnabled(); - } - return result; - } - - @Override - public boolean isAMAModeEnabled() { - boolean result = SP.getBoolean("openapsama_useautosens", false); - - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - result = result && constrain.isAMAModeEnabled(); - } - return result; - } - - @Override - public boolean isSMBModeEnabled() { - boolean result = true; // TODO update for SMB // SP.getBoolean("openapsama_useautosens", false); - - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - result = result && constrain.isSMBModeEnabled(); - } - return result; - } - - @Override - public Double applyBasalConstraints(Double absoluteRate) { - Double rateAfterConstrain = absoluteRate; - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - rateAfterConstrain = Math.min(constrain.applyBasalConstraints(absoluteRate), rateAfterConstrain); - } - return rateAfterConstrain; - } - - @Override - public Integer applyBasalConstraints(Integer percentRate) { - Integer rateAfterConstrain = percentRate; - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - rateAfterConstrain = Math.min(constrain.applyBasalConstraints(percentRate), rateAfterConstrain); - } - return rateAfterConstrain; - } - - @Override - public Double applyBolusConstraints(Double insulin) { - Double insulinAfterConstrain = insulin; - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - insulinAfterConstrain = Math.min(constrain.applyBolusConstraints(insulin), insulinAfterConstrain); - } - return insulinAfterConstrain; - } - - @Override - public Integer applyCarbsConstraints(Integer carbs) { - Integer carbsAfterConstrain = carbs; - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - carbsAfterConstrain = Math.min(constrain.applyCarbsConstraints(carbs), carbsAfterConstrain); - } - return carbsAfterConstrain; - } - - @Override - public Double applyMaxIOBConstraints(Double maxIob) { - Double maxIobAfterConstrain = maxIob; - ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; - if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - maxIobAfterConstrain = Math.min(constrain.applyMaxIOBConstraints(maxIob), maxIobAfterConstrain); - } - return maxIobAfterConstrain; - } - // ****** Treatments interface ***** @Override public void updateTotalIOBTreatments() { 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 3ce466e0b8..ccee2f00ee 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 @@ -17,7 +17,7 @@ import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; @@ -187,7 +187,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { SafetyPlugin.getPlugin().limitClosedLoop(closedLoopEnabled); return new RequirementResult(closedLoopEnabled.get(), MainApp.sResources.getString(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get())); case 4: - double maxIOB = MainApp.getConfigBuilder().applyMaxIOBConstraints(1000d); + double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(1000d); boolean maxIobSet = maxIOB > 0; return new RequirementResult(maxIobSet, MainApp.sResources.getString(R.string.maxiobset) + ": " + yesOrNo(maxIobSet)); default: 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 68fe73f991..6292925f06 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 @@ -12,7 +12,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.utils.HardLimits; import info.nightscout.utils.Round; 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 b947330fa4..5cd16d3eb4 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 @@ -28,7 +28,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui; @@ -260,7 +260,7 @@ public class LoopPlugin implements PluginBase { if (Config.logFunctionCalls) log.debug("invoke from " + initiator); Constraint loopEnabled = new Constraint<>(true); - MainApp.getConfigBuilder().limitRunningLoop(loopEnabled); + MainApp.getConstraintChecker().limitRunningLoop(loopEnabled); if (!loopEnabled.get()) { String message = MainApp.sResources.getString(R.string.loopdisabled) + "\n" + loopEnabled.getReasons(); @@ -299,8 +299,8 @@ public class LoopPlugin implements PluginBase { // check rate for constrais final APSResult resultAfterConstraints = result.clone(); - resultAfterConstraints.rate = MainApp.getConfigBuilder().applyBasalConstraints(resultAfterConstraints.rate); - resultAfterConstraints.smb = MainApp.getConfigBuilder().applyBolusConstraints(resultAfterConstraints.smb); + resultAfterConstraints.rate = MainApp.getConstraintChecker().applyBasalConstraints(resultAfterConstraints.rate); + resultAfterConstraints.smb = MainApp.getConstraintChecker().applyBolusConstraints(resultAfterConstraints.smb); // safety check for multiple SMBs long lastBolusTime = TreatmentsPlugin.getPlugin().getLastBolusTime(); @@ -332,7 +332,7 @@ public class LoopPlugin implements PluginBase { } Constraint closedLoopEnabled = new Constraint<>(true); - MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled); if (closedLoopEnabled.get()) { if (result.isChangeRequested()) { 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 5fc7809f4e..7aef2bb0ec 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.getConfigBuilder().isAMAModeEnabled()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled()) { 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 df34cf6316..3df9534f49 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 @@ -19,7 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -192,7 +192,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); Profiler.log(log, "getMealData()", startPart); - maxIob = MainApp.getConfigBuilder().applyMaxIOBConstraints(maxIob); + maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(maxIob); minBg = HardLimits.verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); maxBg = HardLimits.verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]); @@ -223,7 +223,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { return; startPart = new Date(); - if (MainApp.getConfigBuilder().isAMAModeEnabled()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled()) { lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); @@ -249,7 +249,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultAMA.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) { + if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { long now = System.currentTimeMillis(); TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) { 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 ca1c7608b0..d72e9d27a9 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 @@ -19,7 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.ScriptReader; @@ -194,7 +194,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); - maxIob = MainApp.getConfigBuilder().applyMaxIOBConstraints(maxIob); + maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(maxIob); Profiler.log(log, "MA data gathering", start); minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); @@ -238,7 +238,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultMA.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) { + if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) { // going to cancel 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 acd134d654..c0983fc09d 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 @@ -27,7 +27,7 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.TemporaryBasal; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.ScriptReader; @@ -217,7 +217,7 @@ public class DetermineBasalAdapterSMBJS { String units = profile.getUnits(); Constraint closedLoopEnabled = new Constraint<>(true); - MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled); mProfile = new JSONObject(); @@ -304,7 +304,7 @@ public class DetermineBasalAdapterSMBJS { mMealData.put("lastCarbTime", mealData.lastCarbTime); - if (MainApp.getConfigBuilder().isAMAModeEnabled()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled()) { 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 f27eabd239..6b6a0ee1db 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 @@ -19,7 +19,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -197,7 +197,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); Profiler.log(log, "getMealData()", startPart); - maxIob = MainApp.getConfigBuilder().applyMaxIOBConstraints(maxIob); + maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(maxIob); minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); maxBg = verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]); @@ -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.getConfigBuilder().isAMAModeEnabled()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled()) { lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); @@ -254,7 +254,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultSMB.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) { + if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) { // going to cancel diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index 3c068b79d7..dcb20bdb70 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -120,7 +120,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxCarbs = MainApp.getConfigBuilder().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); + maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); editCarbs = view.findViewById(R.id.newcarb_carbsamount); @@ -303,7 +303,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D okClicked = true; try { final Integer carbs = SafeParse.stringToInt(editCarbs.getText()); - Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(carbs); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbs); List actions = new LinkedList<>(); if (carbs > 0) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java index f72e1c1e87..140719a31e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java @@ -119,7 +119,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newinsulin_amount); @@ -223,7 +223,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, try { Double insulin = SafeParse.stringToDouble(editInsulin.getText()); - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); List actions = new LinkedList<>(); if (insulin > 0) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index d17dfdd0b8..70404a416c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -95,8 +95,8 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxCarbs = MainApp.getConfigBuilder().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); - maxInsulin = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); + maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); editCarbs = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_carbsamount); editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_insulinamount); @@ -128,8 +128,8 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene String confirmMessage = MainApp.gs(R.string.entertreatmentquestion) + "
"; - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin); - Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(carbs); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbs); if (insulin > 0) { confirmMessage += MainApp.gs(R.string.bolus) + ": " + "" + insulinAfterConstraints + "U" + ""; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index 0d59983796..09f123909b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -58,10 +58,10 @@ import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugi import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.queue.Callback; -import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.NumberPicker; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; @@ -238,8 +238,8 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com superbolusCheckbox.setVisibility(SP.getBoolean(R.string.key_usesuperbolus, false) ? View.VISIBLE : View.GONE); - Integer maxCarbs = MainApp.getConfigBuilder().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); - Double maxCorrection = MainApp.getConfigBuilder().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); + Double maxCorrection = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); editBg.setParams(0d, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false, textWatcher); editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false, textWatcher); @@ -303,8 +303,8 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com String confirmMessage = getString(R.string.entertreatmentquestion); - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(calculatedTotalInsulin); - Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(calculatedCarbs); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(calculatedTotalInsulin); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(calculatedCarbs); confirmMessage += "
" + getString(R.string.bolus) + ": " + "" + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U" + ""; confirmMessage += "
" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; @@ -460,13 +460,13 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com Double c_bg = SafeParse.stringToDouble(editBg.getText()); Integer c_carbs = SafeParse.stringToInt(editCarbs.getText()); Double c_correction = SafeParse.stringToDouble(editCorr.getText()); - Double corrAfterConstraint = MainApp.getConfigBuilder().applyBolusConstraints(c_correction); + Double corrAfterConstraint = MainApp.getConstraintChecker().applyBolusConstraints(c_correction); if (c_correction - corrAfterConstraint != 0) { // c_correction != corrAfterConstraint doesn't work editCorr.setValue(0d); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.bolusconstraintapplied)); return; } - Integer carbsAfterConstraint = MainApp.getConfigBuilder().applyCarbsConstraints(c_carbs); + Integer carbsAfterConstraint = MainApp.getConstraintChecker().applyCarbsConstraints(c_carbs); if (c_carbs - carbsAfterConstraint != 0) { editCarbs.setValue(0d); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.carbsconstraintapplied)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index c439e274d4..f79c5af3be 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -87,7 +87,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; @@ -724,8 +724,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); String confirmMessage = getString(R.string.entertreatmentquestion); - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(wizard.calculatedTotalInsulin); - Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(quickWizardEntry.carbs()); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(wizard.calculatedTotalInsulin); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(quickWizardEntry.carbs()); confirmMessage += "\n" + getString(R.string.bolus) + ": " + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U"; confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; @@ -1018,7 +1018,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } Constraint closedLoopEnabled = new Constraint<>(true); - MainApp.getConfigBuilder().limitClosedLoop(closedLoopEnabled); + MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled); // open loop mode final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun; 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 2cea72a5af..41b1462eb3 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 @@ -32,7 +32,7 @@ import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; 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 c727fc2223..652b11de53 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 @@ -25,8 +25,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; @@ -217,8 +216,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, @Override public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - percent = configBuilderPlugin.applyBasalConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalConstraints(percent); if (percent < 0) { result.isTempCancel = false; result.enacted = false; @@ -266,8 +264,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, @Override public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - insulin = configBuilderPlugin.applyBolusConstraints(insulin); + insulin = MainApp.getConstraintChecker().applyBolusConstraints(insulin); // needs to be rounded int durationInHalfHours = Math.max(durationInMinutes / 30, 1); insulin = Round.roundTo(insulin, getPumpDescription().extendedBolusStep); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java index 7be12e0cb5..866df4dc54 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java @@ -21,7 +21,6 @@ import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PumpDescription; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.services.DanaRExecutionService; import info.nightscout.utils.Round; import info.nightscout.utils.SP; @@ -131,8 +130,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { Treatment t = new Treatment(); t.isSMB = detailedBolusInfo.isSMB; @@ -171,8 +169,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { PumpEnactResult result = new PumpEnactResult(); - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - absoluteRate = configBuilderPlugin.applyBasalConstraints(absoluteRate); + absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; final boolean doLowTemp = absoluteRate < getBaseBasalRate(); @@ -269,7 +266,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { Integer durationInHalfHours = Math.max(durationInMinutes / 30, 1); // We keep current basal running so need to sub current basal Double extendedRateToSet = absoluteRate - getBaseBasalRate(); - extendedRateToSet = configBuilderPlugin.applyBasalConstraints(extendedRateToSet); + extendedRateToSet = MainApp.getConstraintChecker().applyBasalConstraints(extendedRateToSet); // needs to be rounded to 0.1 extendedRateToSet = Round.roundTo(extendedRateToSet, pumpDescription.extendedBolusStep * 2); // *2 because of halfhours diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStart.java index 0e6a3e814a..0c3e7dce70 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStart.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStart.java @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.PumpDanaR.comm; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.utils.HardLimits; @@ -19,7 +18,7 @@ public class MsgBolusStart extends MessageBase { this(); // HARDCODED LIMIT - amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); + amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); if (amount < 0) amount = 0d; if (amount > HardLimits.maxBolus()) amount = HardLimits.maxBolus(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStartWithSpeed.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStartWithSpeed.java index b04d501ef0..9a9c1140aa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStartWithSpeed.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStartWithSpeed.java @@ -18,7 +18,7 @@ public class MsgBolusStartWithSpeed extends MessageBase { this(); // HARDCODED LIMIT - amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); + amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); if (amount < 0) amount = 0d; if (amount > HardLimits.maxBolus()) amount = HardLimits.maxBolus(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetExtendedBolusStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetExtendedBolusStart.java index 484078825e..d27623d757 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetExtendedBolusStart.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetExtendedBolusStart.java @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.PumpDanaR.comm; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.utils.HardLimits; @@ -21,7 +20,7 @@ public class MsgSetExtendedBolusStart extends MessageBase { // HARDCODED LIMITS if (halfhours < 1) halfhours = 1; if (halfhours > 16) halfhours = 16; - amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); + amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); if (amount < 0d) amount = 0d; if (amount > HardLimits.maxBolus()) amount = HardLimits.maxBolus(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java index 0a35d56a17..3b4a5ce7d7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java @@ -21,7 +21,6 @@ import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.PumpDescription; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.AbstractDanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.services.DanaRKoreanExecutionService; import info.nightscout.utils.Round; @@ -132,8 +131,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { Treatment t = new Treatment(); t.isSMB = detailedBolusInfo.isSMB; @@ -172,8 +170,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { PumpEnactResult result = new PumpEnactResult(); - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - absoluteRate = configBuilderPlugin.applyBasalConstraints(absoluteRate); + absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; final boolean doLowTemp = absoluteRate < getBaseBasalRate(); @@ -270,7 +267,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { Integer durationInHalfHours = Math.max(durationInMinutes / 30, 1); // We keep current basal running so need to sub current basal Double extendedRateToSet = absoluteRate - getBaseBasalRate(); - extendedRateToSet = configBuilderPlugin.applyBasalConstraints(extendedRateToSet); + extendedRateToSet = MainApp.getConstraintChecker().applyBasalConstraints(extendedRateToSet); // needs to be rounded to 0.1 extendedRateToSet = Round.roundTo(extendedRateToSet, pumpDescription.extendedBolusStep * 2); // *2 because of halfhours 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 47ed05157a..b415014b4b 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 @@ -36,8 +36,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; @@ -451,8 +450,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public synchronized PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); int speed = 12; @@ -524,8 +522,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, PumpEnactResult result = new PumpEnactResult(); - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - absoluteRate = configBuilderPlugin.applyBasalConstraints(absoluteRate); + absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; final boolean doLowTemp = absoluteRate < getBaseBasalRate(); @@ -597,8 +594,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public synchronized PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - percent = configBuilderPlugin.applyBasalConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalConstraints(percent); if (percent < 0) { result.isTempCancel = false; result.enacted = false; @@ -673,8 +669,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public synchronized PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - insulin = configBuilderPlugin.applyBolusConstraints(insulin); + insulin = MainApp.getConstraintChecker().applyBolusConstraints(insulin); // needs to be rounded int durationInHalfHours = Math.max(durationInMinutes / 30, 1); insulin = Round.roundTo(insulin, getPumpDescription().extendedBolusStep); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java index 4c5cc23ef3..864e528e57 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java @@ -1,11 +1,12 @@ package info.nightscout.androidaps.plugins.PumpDanaRS.comm; +import com.cozmo.danar.util.BleCommandUtil; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; -import com.cozmo.danar.util.BleCommandUtil; import info.nightscout.utils.HardLimits; public class DanaRS_Packet_Bolus_Set_Step_Bolus_Start extends DanaRS_Packet { @@ -26,7 +27,7 @@ public class DanaRS_Packet_Bolus_Set_Step_Bolus_Start extends DanaRS_Packet { this(); // HARDCODED LIMIT - amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); + amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); if (amount < 0) amount = 0d; if (amount > HardLimits.maxBolus()) amount = HardLimits.maxBolus(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java index c1a6bb54da..594db71ece 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java @@ -19,7 +19,6 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.interfaces.PumpDescription; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.PumpDanaR.AbstractDanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaRv2.services.DanaRv2ExecutionService; @@ -121,8 +120,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { // Pump interface @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { // v2 stores end time for bolus, we need to adjust time // default delivery speed is 12 sec/U @@ -194,8 +192,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { PumpEnactResult result = new PumpEnactResult(); - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - absoluteRate = configBuilderPlugin.applyBasalConstraints(absoluteRate); + absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; final boolean doLowTemp = absoluteRate < getBaseBasalRate(); @@ -265,8 +262,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { @Override public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - percent = configBuilderPlugin.applyBasalConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalConstraints(percent); if (percent < 0) { result.isTempCancel = false; result.enacted = false; 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 ee5bef5200..4c982a44a2 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 @@ -26,7 +26,7 @@ import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 01f883e6d6..e545d7922e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -32,8 +32,8 @@ import info.nightscout.androidaps.events.EventRefreshOverview; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; -import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventNewSMS; @@ -418,7 +418,7 @@ public class SmsCommunicatorPlugin implements PluginBase { } } else { tempBasal = SafeParse.stringToDouble(splited[1]); - tempBasal = MainApp.getConfigBuilder().applyBasalConstraints(tempBasal); + tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(tempBasal); if (remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode); @@ -443,7 +443,7 @@ public class SmsCommunicatorPlugin implements PluginBase { sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } else if (splited.length > 1) { amount = SafeParse.stringToDouble(splited[1]); - amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); + amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); if (amount > 0d && remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusreplywithcode), amount, passCode); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index e443bbf161..20684891a5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -27,7 +27,7 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -93,7 +93,7 @@ public class ActionStringHandler { } else { return; } - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(amount); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(amount); rMessage += MainApp.instance().getString(R.string.primefill) + ": " + insulinAfterConstraints + "U"; if (insulinAfterConstraints - amount != 0) rMessage += "\n" + MainApp.instance().getString(R.string.constraintapllied); @@ -104,7 +104,7 @@ public class ActionStringHandler { ////////////////////////////////////////////// PRIME/FILL double amount = SafeParse.stringToDouble(act[1]); - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(amount); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(amount); rMessage += MainApp.instance().getString(R.string.primefill) + ": " + insulinAfterConstraints + "U"; if (insulinAfterConstraints - amount != 0) rMessage += "\n" + MainApp.instance().getString(R.string.constraintapllied); @@ -115,8 +115,8 @@ public class ActionStringHandler { ////////////////////////////////////////////// BOLUS double insulin = SafeParse.stringToDouble(act[1]); int carbs = SafeParse.stringToInt(act[2]); - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin); - Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(carbs); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbs); rMessage += MainApp.instance().getString(R.string.bolus) + ": " + insulinAfterConstraints + "U\n"; rMessage += MainApp.instance().getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; @@ -180,7 +180,7 @@ public class ActionStringHandler { } else if ("wizard".equals(act[0])) { ////////////////////////////////////////////// WIZARD Integer carbsBeforeConstraints = SafeParse.stringToInt(act[1]); - Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(carbsBeforeConstraints); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbsBeforeConstraints); if (carbsAfterConstraints - carbsBeforeConstraints != 0) { sendError("Carb constraint violation!"); @@ -208,7 +208,7 @@ public class ActionStringHandler { BolusWizard bolusWizard = new BolusWizard(); bolusWizard.doCalc(profile, null, carbsAfterConstraints, 0d, useBG ? bgReading.valueToUnits(profile.getUnits()) : 0d, 0d, percentage, useBolusIOB, useBasalIOB, false, false); - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(bolusWizard.calculatedTotalInsulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(bolusWizard.calculatedTotalInsulin); if (insulinAfterConstraints - bolusWizard.calculatedTotalInsulin != 0) { sendError("Insulin contraint violation!" + "\nCannot deliver " + format.format(bolusWizard.calculatedTotalInsulin) + "!"); @@ -435,7 +435,7 @@ public class ActionStringHandler { // decide if enabled/disabled closed/open; what Plugin as APS? final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop(); if (activeloop != null && activeloop.isEnabled(activeloop.getType())) { - if (MainApp.getConfigBuilder().limitClosedLoop(new Constraint<>(true)).get()) { + if (MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { ret += "CLOSED LOOP\n"; } else { ret += "OPEN LOOP\n"; @@ -535,7 +535,7 @@ public class ActionStringHandler { if ("fill".equals(act[0])) { Double amount = SafeParse.stringToDouble(act[1]); - Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(amount); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(amount); if (amount - insulinAfterConstraints != 0) { ToastUtils.showToastInUiThread(MainApp.instance(), "aborting: previously applied constraint changed"); sendError("aborting: previously applied constraint changed"); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index 748ada61f6..949551f8e5 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -22,9 +22,9 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressHelperActivity; -import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.androidaps.queue.commands.CommandBolus; import info.nightscout.androidaps.queue.commands.CommandCancelExtendedBolus; @@ -165,8 +165,8 @@ public class CommandQueue { removeAll(type); // apply constraints - detailedBolusInfo.insulin = MainApp.getConfigBuilder().applyBolusConstraints(detailedBolusInfo.insulin); - detailedBolusInfo.carbs = MainApp.getConfigBuilder().applyCarbsConstraints((int) detailedBolusInfo.carbs); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.carbs = MainApp.getConstraintChecker().applyCarbsConstraints((int) detailedBolusInfo.carbs); // add new command to queue if (detailedBolusInfo.isSMB) { @@ -196,7 +196,7 @@ public class CommandQueue { // remove all unfinished removeAll(Command.CommandType.TEMPBASAL); - Double rateAfterConstraints = MainApp.getConfigBuilder().applyBasalConstraints(absoluteRate); + Double rateAfterConstraints = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); // add new command to queue add(new CommandTempBasalAbsolute(rateAfterConstraints, durationInMinutes, enforceNew, callback)); @@ -217,7 +217,7 @@ public class CommandQueue { // remove all unfinished removeAll(Command.CommandType.TEMPBASAL); - Integer percentAfterConstraints = MainApp.getConfigBuilder().applyBasalConstraints(percent); + Integer percentAfterConstraints = MainApp.getConstraintChecker().applyBasalConstraints(percent); // add new command to queue add(new CommandTempBasalPercent(percentAfterConstraints, durationInMinutes, enforceNew, callback)); @@ -235,7 +235,7 @@ public class CommandQueue { return false; } - Double rateAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin); + Double rateAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); // remove all unfinished removeAll(Command.CommandType.EXTENDEDBOLUS); diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/ConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java similarity index 87% rename from app/src/test/java/info/nightscout/androidaps/interfaces/contraints/ConstraintTest.java rename to app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java index c97d3e4750..088c6c4459 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/contraints/ConstraintTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.interfaces.contraints; +package info.nightscout.androidaps.interfaces; import junit.framework.Assert; @@ -6,7 +6,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; -import info.nightscout.androidaps.interfaces.constrains.Constraint; +import info.nightscout.androidaps.interfaces.Constraint; /** * Created by mike on 19.03.2018. From 7178e1573a1f9cb7f7ccac339be1172c61d50c41 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 18:44:11 +0100 Subject: [PATCH 05/28] fix CommandQueueTest --- .../info/nightscout/androidaps/queue/CommandQueueTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java index 8f9cd75f51..cb221eb93c 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java @@ -16,6 +16,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.data.ConstraintChecker; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.interfaces.PumpInterface; @@ -105,8 +106,7 @@ public class CommandQueueTest extends CommandQueue { private void prepareMock(Double insulin, Integer carbs) throws Exception { ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); - when(configBuilderPlugin.applyBolusConstraints(insulin)).thenReturn(insulin); - when(configBuilderPlugin.applyCarbsConstraints(carbs)).thenReturn(carbs); + ConstraintChecker constraintChecker = mock(ConstraintChecker.class); PowerMockito.mockStatic(ConfigBuilderPlugin.class); PumpInterface pump = MDIPlugin.getPlugin(); @@ -115,8 +115,11 @@ public class CommandQueueTest extends CommandQueue { PowerMockito.mockStatic(MainApp.class); MainApp mainApp = mock(MainApp.class); when(MainApp.getConfigBuilder()).thenReturn(configBuilderPlugin); + when(MainApp.getConstraintChecker()).thenReturn(constraintChecker); when(MainApp.isEngineeringModeOrRelease()).thenReturn(true); when(MainApp.instance()).thenReturn(mainApp); + when(MainApp.getConstraintChecker().applyBolusConstraints(insulin)).thenReturn(insulin); + when(MainApp.getConstraintChecker().applyCarbsConstraints(carbs)).thenReturn(carbs); PowerMockito.mockStatic(ToastUtils.class); Context context = mock(Context.class); From 01a9e7682750908bb7adf5f315eb6e810088e312 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 19 Mar 2018 18:45:23 +0100 Subject: [PATCH 06/28] refactor a bit --- .../info/nightscout/androidaps/MainApp.java | 2 +- .../androidaps/data/ConstraintChecker.java | 17 +++++++++++++---- .../interfaces/ConstraintsInterface.java | 4 ++-- .../ConstraintsObjectives/ObjectivesPlugin.java | 6 +++--- .../plugins/ConstraintsSafety/SafetyPlugin.java | 4 ++-- .../androidaps/plugins/Loop/LoopPlugin.java | 6 ++---- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 2 +- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 2 +- .../OpenAPSSMB/DetermineBasalAdapterSMBJS.java | 3 +-- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 2 +- .../plugins/Overview/OverviewFragment.java | 3 +-- .../plugins/PumpCombo/ComboPlugin.java | 4 ++-- .../plugins/PumpDanaR/AbstractDanaRPlugin.java | 4 ++-- .../plugins/PumpDanaRS/DanaRSPlugin.java | 4 ++-- .../plugins/PumpInsight/InsightPumpPlugin.java | 4 ++-- .../plugins/Wear/ActionStringHandler.java | 2 +- 16 files changed, 37 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 6b9754a246..2e48d5328f 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -201,7 +201,7 @@ public class MainApp extends Application { FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-G5Uploader")); else if (Config.PUMPCONTROL) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-PumpControl")); - else if (MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) + else if (MainApp.getConstraintChecker().isClosedLoopAllowed().get()) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop")); else FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-OpenLoop")); 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 1a17880cf5..a4cccd772c 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -20,26 +20,35 @@ public class ConstraintChecker implements ConstraintsInterface { this.mainApp = mainApp; } + + public Constraint isLoopInvokationAllowed() { + return isLoopInvokationAllowed(new Constraint<>(true)); + } + + public Constraint isClosedLoopAllowed() { + return isClosedLoopAllowed(new Constraint<>(true)); + } + @Override - public Constraint limitRunningLoop(Constraint value) { + public Constraint isLoopInvokationAllowed(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - constraint.limitRunningLoop(value); + constraint.isLoopInvokationAllowed(value); } return value; } @Override - public Constraint limitClosedLoop(Constraint value) { + public Constraint isClosedLoopAllowed(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - constraint.limitClosedLoop(value); + constraint.isClosedLoopAllowed(value); } return value; } 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 495039bf9b..95a49d38ae 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -5,9 +5,9 @@ package info.nightscout.androidaps.interfaces; */ public interface ConstraintsInterface { - Constraint limitRunningLoop(Constraint value); + Constraint isLoopInvokationAllowed(Constraint value); - Constraint limitClosedLoop(Constraint value); + Constraint isClosedLoopAllowed(Constraint value); boolean isAutosensModeEnabled(); 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 ccee2f00ee..d9e60bc61c 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 @@ -184,7 +184,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { return new RequirementResult(true, ""); case 3: Constraint closedLoopEnabled = new Constraint<>(true); - SafetyPlugin.getPlugin().limitClosedLoop(closedLoopEnabled); + SafetyPlugin.getPlugin().isClosedLoopAllowed(closedLoopEnabled); return new RequirementResult(closedLoopEnabled.get(), MainApp.sResources.getString(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get())); case 4: double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(1000d); @@ -295,14 +295,14 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { * Constraints interface **/ @Override - public Constraint limitRunningLoop(Constraint value) { + public Constraint isLoopInvokationAllowed(Constraint value) { if (objectives.get(0).started.getTime() == 0) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1)); return value; } @Override - public Constraint limitClosedLoop(Constraint value) { + public Constraint isClosedLoopAllowed(Constraint value) { if (objectives.get(3).started.getTime() == 0) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4)); return value; 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 6292925f06..863c899856 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 @@ -96,14 +96,14 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { * Constraints interface **/ @Override - public Constraint limitRunningLoop(Constraint value) { + public Constraint isLoopInvokationAllowed(Constraint value) { if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable) value.set(false, MainApp.gs(R.string.pumpisnottempbasalcapable)); return value; } @Override - public Constraint limitClosedLoop(Constraint value) { + public Constraint isClosedLoopAllowed(Constraint value) { if (!MainApp.isEngineeringModeOrRelease()) value.set(false, MainApp.gs(R.string.closed_loop_disabled_on_dev_branch)); 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 5cd16d3eb4..ba3d88b76a 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 @@ -259,8 +259,7 @@ public class LoopPlugin implements PluginBase { try { if (Config.logFunctionCalls) log.debug("invoke from " + initiator); - Constraint loopEnabled = new Constraint<>(true); - MainApp.getConstraintChecker().limitRunningLoop(loopEnabled); + Constraint loopEnabled = MainApp.getConstraintChecker().isLoopInvokationAllowed(); if (!loopEnabled.get()) { String message = MainApp.sResources.getString(R.string.loopdisabled) + "\n" + loopEnabled.getReasons(); @@ -331,8 +330,7 @@ public class LoopPlugin implements PluginBase { return; } - Constraint closedLoopEnabled = new Constraint<>(true); - MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled); + Constraint closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed(); if (closedLoopEnabled.get()) { if (result.isChangeRequested()) { 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 3df9534f49..f51bbd2352 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 @@ -249,7 +249,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultAMA.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { + if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) { long now = System.currentTimeMillis(); TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) { 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 d72e9d27a9..4258ae6714 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 @@ -238,7 +238,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultMA.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { + if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) { // going to cancel 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 c0983fc09d..745ae3bb53 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 @@ -216,8 +216,7 @@ public class DetermineBasalAdapterSMBJS { String units = profile.getUnits(); - Constraint closedLoopEnabled = new Constraint<>(true); - MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled); + Constraint closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed(); mProfile = new JSONObject(); 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 6b6a0ee1db..09ff62a324 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 @@ -254,7 +254,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultSMB.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { + if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) { // going to cancel diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index f79c5af3be..5736c0a715 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -1017,8 +1017,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } } - Constraint closedLoopEnabled = new Constraint<>(true); - MainApp.getConstraintChecker().limitClosedLoop(closedLoopEnabled); + Constraint closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed(); // open loop mode final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun; 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 41b1462eb3..c68815e2fa 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 @@ -1422,14 +1422,14 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf private boolean validBasalRateProfileSelectedOnPump = true; @Override - public Constraint limitRunningLoop(Constraint value) { + public Constraint isLoopInvokationAllowed(Constraint value) { if (!validBasalRateProfileSelectedOnPump) value.set(false, MainApp.gs(R.string.novalidbasalrate)); return value; } @Override - public Constraint limitClosedLoop(Constraint value) { + public Constraint isClosedLoopAllowed(Constraint value) { return value; } 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 652b11de53..2f225e9894 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 @@ -434,12 +434,12 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, */ @Override - public Constraint limitRunningLoop(Constraint value) { + public Constraint isLoopInvokationAllowed(Constraint value) { return value; } @Override - public Constraint limitClosedLoop(Constraint value) { + public Constraint isClosedLoopAllowed(Constraint value) { return value; } 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 b415014b4b..4cd2bfb99b 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 @@ -274,12 +274,12 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, // Constraints interface @Override - public Constraint limitRunningLoop(Constraint value) { + public Constraint isLoopInvokationAllowed(Constraint value) { return value; } @Override - public Constraint limitClosedLoop(Constraint value) { + public Constraint isClosedLoopAllowed(Constraint value) { return value; } 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 4c982a44a2..ce5be1a2a9 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 @@ -1080,12 +1080,12 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints // Constraints @Override - public Constraint limitRunningLoop(Constraint value) { + public Constraint isLoopInvokationAllowed(Constraint value) { return value; } @Override - public Constraint limitClosedLoop(Constraint value) { + public Constraint isClosedLoopAllowed(Constraint value) { return value; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index 20684891a5..9323393abd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -435,7 +435,7 @@ public class ActionStringHandler { // decide if enabled/disabled closed/open; what Plugin as APS? final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop(); if (activeloop != null && activeloop.isEnabled(activeloop.getType())) { - if (MainApp.getConstraintChecker().limitClosedLoop(new Constraint<>(true)).get()) { + if (MainApp.getConstraintChecker().isClosedLoopAllowed().get()) { ret += "CLOSED LOOP\n"; } else { ret += "OPEN LOOP\n"; From f6fdef79867e97bc1fa0ac26da5e7ec3ae4c8739 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 22:20:42 +0100 Subject: [PATCH 07/28] isClosedLoopAllowed & isLoopInvokationAllowed tests --- .../androidaps/data/ConstraintChecker.java | 2 +- .../ObjectivesPlugin.java | 58 +++---- .../plugins/PumpCombo/ComboPlugin.java | 8 +- .../interfaces/ConstraintsCheckerTest.java | 151 ++++++++++++++++++ 4 files changed, 189 insertions(+), 30 deletions(-) create mode 100644 app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java 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 a4cccd772c..358110cc7c 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -3,9 +3,9 @@ package info.nightscout.androidaps.data; import java.util.ArrayList; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.utils.SP; /** 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 d9e60bc61c..1a93c15c1b 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 @@ -15,9 +15,9 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.APSInterface; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; @@ -67,7 +67,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { @Override public String getNameShort() { - String name = MainApp.sResources.getString(R.string.objectives_shortname); + String name = MainApp.gs(R.string.objectives_shortname); if (!name.trim().isEmpty()) { //only if translation exists return name; @@ -115,7 +115,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { return -1; } - class Objective { + public class Objective { Integer num; String objective; String gate; @@ -131,6 +131,10 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { this.durationInDays = durationInDays; this.accomplished = accomplished; } + + public void setStarted(Date started) { + this.started = started; + } } // Objective 0 @@ -169,27 +173,27 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { apsEnabled = true; return new RequirementResult(hasBGData && bgIsAvailableInNS && pumpStatusIsAvailableInNS && NSClientInternalPlugin.getPlugin().hasWritePermission() && LoopPlugin.getPlugin().isEnabled(PluginBase.LOOP) && apsEnabled && vpUploadNeeded, - MainApp.sResources.getString(R.string.objectives_bgavailableinns) + ": " + yesOrNo(bgIsAvailableInNS) - + "\n" + MainApp.sResources.getString(R.string.nsclienthaswritepermission) + ": " + yesOrNo(NSClientInternalPlugin.getPlugin().hasWritePermission()) - + (isVirtualPump ? "\n" + MainApp.sResources.getString(R.string.virtualpump_uploadstatus_title) + ": " + yesOrNo(vpUploadEnabled) : "") - + "\n" + MainApp.sResources.getString(R.string.objectives_pumpstatusavailableinns) + ": " + yesOrNo(pumpStatusIsAvailableInNS) - + "\n" + MainApp.sResources.getString(R.string.hasbgdata) + ": " + yesOrNo(hasBGData) - + "\n" + MainApp.sResources.getString(R.string.loopenabled) + ": " + yesOrNo(LoopPlugin.getPlugin().isEnabled(PluginBase.LOOP)) - + "\n" + MainApp.sResources.getString(R.string.apsselected) + ": " + yesOrNo(apsEnabled) + MainApp.gs(R.string.objectives_bgavailableinns) + ": " + yesOrNo(bgIsAvailableInNS) + + "\n" + MainApp.gs(R.string.nsclienthaswritepermission) + ": " + yesOrNo(NSClientInternalPlugin.getPlugin().hasWritePermission()) + + (isVirtualPump ? "\n" + MainApp.gs(R.string.virtualpump_uploadstatus_title) + ": " + yesOrNo(vpUploadEnabled) : "") + + "\n" + MainApp.gs(R.string.objectives_pumpstatusavailableinns) + ": " + yesOrNo(pumpStatusIsAvailableInNS) + + "\n" + MainApp.gs(R.string.hasbgdata) + ": " + yesOrNo(hasBGData) + + "\n" + MainApp.gs(R.string.loopenabled) + ": " + yesOrNo(LoopPlugin.getPlugin().isEnabled(PluginBase.LOOP)) + + "\n" + MainApp.gs(R.string.apsselected) + ": " + yesOrNo(apsEnabled) ); case 1: return new RequirementResult(manualEnacts >= manualEnactsNeeded, - MainApp.sResources.getString(R.string.objectives_manualenacts) + ": " + manualEnacts + "/" + manualEnactsNeeded); + MainApp.gs(R.string.objectives_manualenacts) + ": " + manualEnacts + "/" + manualEnactsNeeded); case 2: return new RequirementResult(true, ""); case 3: Constraint closedLoopEnabled = new Constraint<>(true); SafetyPlugin.getPlugin().isClosedLoopAllowed(closedLoopEnabled); - return new RequirementResult(closedLoopEnabled.get(), MainApp.sResources.getString(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get())); + return new RequirementResult(closedLoopEnabled.get(), MainApp.gs(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get())); case 4: double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(1000d); boolean maxIobSet = maxIOB > 0; - return new RequirementResult(maxIobSet, MainApp.sResources.getString(R.string.maxiobset) + ": " + yesOrNo(maxIobSet)); + return new RequirementResult(maxIobSet, MainApp.gs(R.string.maxiobset) + ": " + yesOrNo(maxIobSet)); default: return new RequirementResult(true, ""); } @@ -203,49 +207,49 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { objectives = new ArrayList<>(); objectives.add(new Objective(0, - MainApp.sResources.getString(R.string.objectives_0_objective), - MainApp.sResources.getString(R.string.objectives_0_gate), + MainApp.gs(R.string.objectives_0_objective), + MainApp.gs(R.string.objectives_0_gate), new Date(0), 0, // 0 day new Date(0))); objectives.add(new Objective(1, - MainApp.sResources.getString(R.string.objectives_1_objective), - MainApp.sResources.getString(R.string.objectives_1_gate), + MainApp.gs(R.string.objectives_1_objective), + MainApp.gs(R.string.objectives_1_gate), new Date(0), 7, // 7 days new Date(0))); objectives.add(new Objective(2, - MainApp.sResources.getString(R.string.objectives_2_objective), - MainApp.sResources.getString(R.string.objectives_2_gate), + MainApp.gs(R.string.objectives_2_objective), + MainApp.gs(R.string.objectives_2_gate), new Date(0), 0, // 0 days new Date(0))); objectives.add(new Objective(3, - MainApp.sResources.getString(R.string.objectives_3_objective), - MainApp.sResources.getString(R.string.objectives_3_gate), + MainApp.gs(R.string.objectives_3_objective), + MainApp.gs(R.string.objectives_3_gate), new Date(0), 5, // 5 days new Date(0))); objectives.add(new Objective(4, - MainApp.sResources.getString(R.string.objectives_4_objective), - MainApp.sResources.getString(R.string.objectives_4_gate), + MainApp.gs(R.string.objectives_4_objective), + MainApp.gs(R.string.objectives_4_gate), new Date(0), 1, new Date(0))); objectives.add(new Objective(5, - MainApp.sResources.getString(R.string.objectives_5_objective), - MainApp.sResources.getString(R.string.objectives_5_gate), + MainApp.gs(R.string.objectives_5_objective), + MainApp.gs(R.string.objectives_5_gate), new Date(0), 7, new Date(0))); objectives.add(new Objective(6, - MainApp.sResources.getString(R.string.objectives_6_objective), + MainApp.gs(R.string.objectives_6_objective), "", new Date(0), 28, new Date(0))); objectives.add(new Objective(7, - MainApp.sResources.getString(R.string.objectives_7_objective), + MainApp.gs(R.string.objectives_7_objective), "", new Date(0), 28, 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 c68815e2fa..a10752df47 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 @@ -424,7 +424,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf return; } pump.basalProfile = readBasalResult.basalProfile; - validBasalRateProfileSelectedOnPump = true; + setValidBasalRateProfileSelectedOnPump(true); pump.initialized = true; MainApp.bus().post(new EventInitializationChanged()); @@ -925,7 +925,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf if (commandResult.success) { pump.lastSuccessfulCmdTime = System.currentTimeMillis(); if (validBasalRateProfileSelectedOnPump && commandResult.state.unsafeUsageDetected == PumpState.UNSUPPORTED_BASAL_RATE_PROFILE) { - validBasalRateProfileSelectedOnPump = false; + setValidBasalRateProfileSelectedOnPump(false); Notification n = new Notification(Notification.COMBO_PUMP_ALARM, MainApp.gs(R.string.combo_force_disabled_notification), Notification.URGENT); @@ -945,6 +945,10 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf return commandResult; } + public void setValidBasalRateProfileSelectedOnPump(boolean value) { + validBasalRateProfileSelectedOnPump = value; + } + /** * Returns the command result of running ReadPumpState if it wasn't successful, indicating * an error condition. Returns null otherwise. diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java new file mode 100644 index 0000000000..7ec2eaeabd --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -0,0 +1,151 @@ +package info.nightscout.androidaps.interfaces; + +import com.squareup.otto.Bus; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.ArrayList; +import java.util.Date; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.ConstraintChecker; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; +import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; +import info.nightscout.androidaps.plugins.PumpCombo.ComboPlugin; +import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; +import info.nightscout.utils.FabricPrivacy; +import info.nightscout.utils.SP; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Created by mike on 18.03.2018. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, FabricPrivacy.class, SP.class}) +public class ConstraintsCheckerTest { + + PumpInterface pump = new VirtualPumpPlugin(); + ConstraintChecker constraintChecker; + + ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); + MainApp mainApp = mock(MainApp.class); + MockedBus bus = new MockedBus(); + //PumpDescription pumpDescription = new PumpDescription(); + + SafetyPlugin safetyPlugin; + ObjectivesPlugin objectivesPlugin; + ComboPlugin comboPlugin; + + boolean notificationSent = false; + + // isLoopInvokationAllowed tests + @Test + public void pumpDescriptionShouldLimitLoopInvokation() throws Exception { + pump.getPumpDescription().isTempBasalCapable = false; + + Constraint c = constraintChecker.isLoopInvokationAllowed(); + Assert.assertEquals(true, c.getReasons().contains("Pump is not temp basal capable")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + + @Test + public void notStartedObjectivesShouldLimitLoopInvokation() throws Exception { + objectivesPlugin.objectives.get(0).setStarted(new Date(0)); + + Constraint c = constraintChecker.isLoopInvokationAllowed(); + Assert.assertEquals(true, c.getReasons().contains("Objective 1 not started")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + + @Test + public void invalidBasalRateOnComboPumpShouldLimitLoopInvokation() throws Exception { + comboPlugin.setFragmentEnabled(PluginBase.PUMP, true); + comboPlugin.setValidBasalRateProfileSelectedOnPump(false); + + Constraint c = constraintChecker.isLoopInvokationAllowed(); + Assert.assertEquals(true, c.getReasons().contains("No valid basal rate read from pump")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + + // isClosedLoopAllowed tests + @Test + public void disabledEngineeringModeShouldLimitClosedLoop() throws Exception { + when(SP.getString("aps_mode", "open")).thenReturn("closed"); + when(MainApp.isEngineeringModeOrRelease()).thenReturn(false); + + Constraint c = constraintChecker.isClosedLoopAllowed(); + Assert.assertEquals(true, c.getReasons().contains("Running dev version. Closed loop is disabled.")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + + @Test + public void setOpenLoopInPreferencesShouldLimitClosedLoop() throws Exception { + when(SP.getString("aps_mode", "open")).thenReturn("open"); + + Constraint c = constraintChecker.isClosedLoopAllowed(); + Assert.assertEquals(true, c.getReasons().contains("Closed loop mode disabled in preferences")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + + @Test + public void notStartedObjective4ShouldLimitClosedLoop() throws Exception { + when(SP.getString("aps_mode", "open")).thenReturn("closed"); + objectivesPlugin.objectives.get(3).setStarted(new Date(0)); + + Constraint c = constraintChecker.isClosedLoopAllowed(); + Assert.assertEquals(true, c.getReasons().contains("Objective 4 not started")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + + @Before + public void prepareMock() throws Exception { + PowerMockito.mockStatic(ConfigBuilderPlugin.class); + + PowerMockito.mockStatic(MainApp.class); + when(MainApp.instance()).thenReturn(mainApp); + when(MainApp.getConfigBuilder()).thenReturn(configBuilderPlugin); + when(MainApp.getConfigBuilder().getActivePump()).thenReturn(pump); + + constraintChecker = new ConstraintChecker(mainApp); + + PowerMockito.mockStatic(FabricPrivacy.class); + + when(MainApp.bus()).thenReturn(bus); + + when(MainApp.gs(R.string.pumpisnottempbasalcapable)).thenReturn("Pump is not temp basal capable"); + when(MainApp.gs(R.string.closed_loop_disabled_on_dev_branch)).thenReturn("Running dev version. Closed loop is disabled."); + 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"); + + safetyPlugin = SafetyPlugin.getPlugin(); + objectivesPlugin = ObjectivesPlugin.getPlugin(); + comboPlugin = ComboPlugin.getPlugin(); + ArrayList constraintsPluginsList = new ArrayList<>(); + constraintsPluginsList.add(safetyPlugin); + constraintsPluginsList.add(objectivesPlugin); + constraintsPluginsList.add(comboPlugin); + when(mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class)).thenReturn(constraintsPluginsList); + + PowerMockito.mockStatic(SP.class); + } + + class MockedBus extends Bus { + @Override + public void post(Object event) { + notificationSent = true; + } + } + +} From f8a3b7aee74d772aada60766b7d1728b6e107fff Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 22:36:08 +0100 Subject: [PATCH 08/28] isAutosensModeEnabled reactor & tests --- .../androidaps/data/ConstraintChecker.java | 13 ++++++++----- .../androidaps/interfaces/ConstraintsInterface.java | 2 +- .../ConstraintsObjectives/ObjectivesPlugin.java | 6 ++++-- .../plugins/ConstraintsSafety/SafetyPlugin.java | 4 ++-- .../androidaps/plugins/PumpCombo/ComboPlugin.java | 4 ++-- .../plugins/PumpDanaR/AbstractDanaRPlugin.java | 4 ++-- .../androidaps/plugins/PumpDanaRS/DanaRSPlugin.java | 4 ++-- .../plugins/PumpInsight/InsightPumpPlugin.java | 4 ++-- .../interfaces/ConstraintsCheckerTest.java | 11 ++++++++++- 9 files changed, 33 insertions(+), 19 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 358110cc7c..33be6328fa 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -29,6 +29,10 @@ public class ConstraintChecker implements ConstraintsInterface { return isClosedLoopAllowed(new Constraint<>(true)); } + public Constraint isAutosensModeEnabled() { + return isAutosensModeEnabled(new Constraint<>(true)); + } + @Override public Constraint isLoopInvokationAllowed(Constraint value) { @@ -54,16 +58,15 @@ public class ConstraintChecker implements ConstraintsInterface { } @Override - public boolean isAutosensModeEnabled() { - boolean result = true; + public Constraint isAutosensModeEnabled(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; + ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - result = result && constrain.isAutosensModeEnabled(); + constraint.isAutosensModeEnabled(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 95a49d38ae..fa72199646 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -9,7 +9,7 @@ public interface ConstraintsInterface { Constraint isClosedLoopAllowed(Constraint value); - boolean isAutosensModeEnabled(); + Constraint isAutosensModeEnabled(Constraint value); boolean isAMAModeEnabled(); 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 1a93c15c1b..6bb4bf5a30 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 @@ -313,8 +313,10 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public boolean isAutosensModeEnabled() { - return objectives.get(5).started.getTime() > 0; + public Constraint isAutosensModeEnabled(Constraint value) { + if (objectives.get(5).started.getTime() == 0) + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 6)); + 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 863c899856..45936c2e6e 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 @@ -114,8 +114,8 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public boolean isAutosensModeEnabled() { - return true; + public Constraint isAutosensModeEnabled(Constraint value) { + return value; } @Override 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 a10752df47..097c53bb18 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 @@ -1438,8 +1438,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public boolean isAutosensModeEnabled() { - return true; + public Constraint isAutosensModeEnabled(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 2f225e9894..755b8e9dd1 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 @@ -444,8 +444,8 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, } @Override - public boolean isAutosensModeEnabled() { - return true; + public Constraint isAutosensModeEnabled(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 4cd2bfb99b..b2c81efe1e 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 @@ -284,8 +284,8 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public boolean isAutosensModeEnabled() { - return true; + public Constraint isAutosensModeEnabled(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 ce5be1a2a9..9570b7fdef 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 @@ -1090,8 +1090,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } @Override - public boolean isAutosensModeEnabled() { - return true; + public Constraint isAutosensModeEnabled(Constraint value) { + return value; } @Override 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 7ec2eaeabd..16301bd63f 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -41,7 +41,6 @@ public class ConstraintsCheckerTest { ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); MainApp mainApp = mock(MainApp.class); MockedBus bus = new MockedBus(); - //PumpDescription pumpDescription = new PumpDescription(); SafetyPlugin safetyPlugin; ObjectivesPlugin objectivesPlugin; @@ -108,6 +107,16 @@ public class ConstraintsCheckerTest { Assert.assertEquals(Boolean.FALSE, c.get()); } + // isAutosensModeEnabled tests + @Test + public void notStartedObjective6ShouldLimitAutosensMode() throws Exception { + objectivesPlugin.objectives.get(5).setStarted(new Date(0)); + + Constraint c = constraintChecker.isAutosensModeEnabled(); + Assert.assertEquals(true, c.getReasons().contains("Objective 6 not started")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + @Before public void prepareMock() throws Exception { PowerMockito.mockStatic(ConfigBuilderPlugin.class); From 26ecc3dd6053915529ff211c8ab114bac0d9ae43 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 22:59:25 +0100 Subject: [PATCH 09/28] 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(); From c2cfe4e7ea3e73b86344364cae5c12d61933f30f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 19 Mar 2018 23:14:04 +0100 Subject: [PATCH 10/28] isSMBModeEnabled reactor & tests --- .../androidaps/data/ConstraintChecker.java | 13 +++++++----- .../interfaces/ConstraintsInterface.java | 2 +- .../ObjectivesPlugin.java | 6 ++++-- .../ConstraintsSafety/SafetyPlugin.java | 7 +++++-- .../DetermineBasalAdapterSMBJS.java | 4 +--- .../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 +++++++++++++++++++ 11 files changed, 48 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 7254221985..3a01b1d4e0 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -37,6 +37,10 @@ public class ConstraintChecker implements ConstraintsInterface { return isAMAModeEnabled(new Constraint<>(true)); } + public Constraint isSMBModeEnabled() { + return isSMBModeEnabled(new Constraint<>(true)); + } + @Override public Constraint isLoopInvokationAllowed(Constraint value) { @@ -86,16 +90,15 @@ public class ConstraintChecker implements ConstraintsInterface { } @Override - public boolean isSMBModeEnabled() { - boolean result = true; // TODO update for SMB // SP.getBoolean("openapsama_useautosens", false); + public Constraint isSMBModeEnabled(Constraint value) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; + ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - result = result && constrain.isSMBModeEnabled(); + constraint.isSMBModeEnabled(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 6a112ab59c..9a05a2cb8b 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -13,7 +13,7 @@ public interface ConstraintsInterface { Constraint isAMAModeEnabled(Constraint value); - boolean isSMBModeEnabled(); + Constraint isSMBModeEnabled(Constraint value); Double applyBasalConstraints(Double absoluteRate); 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 230acb24d9..de2b549d87 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 @@ -327,8 +327,10 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public boolean isSMBModeEnabled() { - return objectives.get(7).started.getTime() > 0; + public Constraint isSMBModeEnabled(Constraint value) { + if (objectives.get(7).started.getTime() == 0) + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 8)); + 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 78e3464167..52f6b9e4ef 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 @@ -127,8 +127,11 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public boolean isSMBModeEnabled() { - return true; + public Constraint isSMBModeEnabled(Constraint value) { + boolean enabled = SP.getBoolean(R.string.key_use_smb, false); + if (!enabled) + value.set(false, MainApp.gs(R.string.smbdisabledinpreferences)); + return value; } @Override 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 3d93f132bc..c9178b4ee9 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 @@ -216,8 +216,6 @@ public class DetermineBasalAdapterSMBJS { String units = profile.getUnits(); - Constraint closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed(); - mProfile = new JSONObject(); mProfile.put("max_iob", maxIob); @@ -247,7 +245,7 @@ public class DetermineBasalAdapterSMBJS { mProfile.put("remainingCarbsCap", SMBDefaults.remainingCarbsCap); mProfile.put("enableUAM", SP.getBoolean(R.string.key_use_uam, false)); mProfile.put("A52_risk_enable", SMBDefaults.A52_risk_enable); - boolean SMBEnabled = SP.getBoolean(R.string.key_use_smb, false) && closedLoopEnabled.get(); + boolean SMBEnabled = MainApp.getConstraintChecker().isSMBModeEnabled().get() && MainApp.getConstraintChecker().isClosedLoopAllowed().get(); mProfile.put("enableSMB_with_COB", SMBEnabled && SP.getBoolean(R.string.key_enableSMB_with_COB, false)); mProfile.put("enableSMB_with_temptarget", SMBEnabled && SP.getBoolean(R.string.key_enableSMB_with_temptarget, false)); mProfile.put("allowSMB_with_high_temptarget", SMBEnabled && SP.getBoolean(R.string.key_allowSMB_with_high_temptarget, false)); 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 5fca42e5c5..f8708dbd39 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 @@ -1448,8 +1448,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public boolean isSMBModeEnabled() { - return true; + public Constraint isSMBModeEnabled(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 282f3e4806..2f8ea81957 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 @@ -454,8 +454,8 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, } @Override - public boolean isSMBModeEnabled() { - return true; + public Constraint isSMBModeEnabled(Constraint value) { + return value; } @SuppressWarnings("PointlessBooleanExpression") 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 0fdb238e53..3d0e266397 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 @@ -294,8 +294,8 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public boolean isSMBModeEnabled() { - return true; + public Constraint isSMBModeEnabled(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 52ff326f1f..c6e4a76913 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 @@ -1100,8 +1100,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } @Override - public boolean isSMBModeEnabled() { - return true; + public Constraint isSMBModeEnabled(Constraint value) { + return value; } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 56875c6ee6..053786dab2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -967,5 +967,6 @@ No valid basal rate read from pump Closed loop mode disabled in preferences AMA disabled in preferences + SMB 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 2a176c9e60..cb755291f0 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -136,6 +136,25 @@ public class ConstraintsCheckerTest { Assert.assertEquals(Boolean.FALSE, c.get()); } + // isSMBModeEnabled tests + @Test + public void notEnabledSMBInPreferencesDisablesSMB() throws Exception { + when(SP.getBoolean(R.string.key_use_smb, false)).thenReturn(false); + + Constraint c = constraintChecker.isSMBModeEnabled(); + Assert.assertEquals(true, c.getReasons().contains("SMB disabled in preferences")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + + @Test + public void notStartedObjective8ShouldLimitSMBMode() throws Exception { + objectivesPlugin.objectives.get(7).setStarted(new Date(0)); + + Constraint c = constraintChecker.isSMBModeEnabled(); + Assert.assertEquals(true, c.getReasons().contains("Objective 8 not started")); + Assert.assertEquals(Boolean.FALSE, c.get()); + } + @Before public void prepareMock() throws Exception { PowerMockito.mockStatic(ConfigBuilderPlugin.class); @@ -157,6 +176,7 @@ public class ConstraintsCheckerTest { 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"); + when(MainApp.gs(R.string.smbdisabledinpreferences)).thenReturn("SMB disabled in preferences"); safetyPlugin = SafetyPlugin.getPlugin(); objectivesPlugin = ObjectivesPlugin.getPlugin(); From 15ebfc11936501b0726c12302eab516842c4e990 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 20 Mar 2018 22:09:22 +0100 Subject: [PATCH 11/28] applyBasalConstraints reactor & tests --- .../info/nightscout/androidaps/Constants.java | 2 +- .../info/nightscout/androidaps/MainApp.java | 3 +- .../androidaps/data/ConstraintChecker.java | 14 +-- .../androidaps/interfaces/Constraint.java | 41 ++++++- .../interfaces/ConstraintsInterface.java | 6 +- .../androidaps/interfaces/PumpInterface.java | 2 +- .../Actions/dialogs/NewTempBasalDialog.java | 11 +- .../Dialogs/NewNSTreatmentDialog.java | 5 +- .../ConfigBuilder/ConfigBuilderPlugin.java | 7 +- .../ObjectivesPlugin.java | 7 +- .../ConstraintsSafety/SafetyPlugin.java | 44 +++----- .../androidaps/plugins/Loop/APSResult.java | 14 ++- .../androidaps/plugins/Loop/LoopPlugin.java | 7 +- .../DetermineBasalAdapterAMAJS.java | 7 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 9 +- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 8 +- .../DetermineBasalAdapterSMBJS.java | 9 +- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 9 +- .../plugins/Overview/OverviewFragment.java | 4 +- .../plugins/PumpCombo/ComboPlugin.java | 6 +- .../PumpDanaR/AbstractDanaRPlugin.java | 17 +-- .../plugins/PumpDanaR/DanaRPlugin.java | 8 +- .../PumpDanaRKorean/DanaRKoreanPlugin.java | 8 +- .../plugins/PumpDanaRS/DanaRSPlugin.java | 20 ++-- .../plugins/PumpDanaRv2/DanaRv2Plugin.java | 8 +- .../PumpInsight/InsightPumpPlugin.java | 17 ++- .../androidaps/plugins/PumpMDI/MDIPlugin.java | 2 +- .../PumpVirtual/VirtualPumpPlugin.java | 2 +- .../SmsCommunicatorPlugin.java | 57 ++++++---- .../plugins/Wear/ActionStringHandler.java | 3 +- .../androidaps/queue/CommandQueue.java | 9 +- .../commands/CommandTempBasalAbsolute.java | 7 +- app/src/main/res/values/strings.xml | 12 +- app/src/main/res/xml/pref_advanced.xml | 8 +- app/src/main/res/xml/pref_openapsama.xml | 4 +- app/src/main/res/xml/pref_openapsma.xml | 4 +- app/src/main/res/xml/pref_openapssmb.xml | 4 +- .../androidaps/interfaces/ConstraintTest.java | 37 ++++--- .../interfaces/ConstraintsCheckerTest.java | 103 +++++++++++++++--- .../androidaps/queue/CommandQueueTest.java | 13 ++- 40 files changed, 353 insertions(+), 205 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 0729e9d4b1..11b8952519 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -12,7 +12,7 @@ public class Constants { public static final double defaultDIA = 3d; - public static final double basalAbsoluteOnlyForCheckLimit = 10101010d; + public static final Double REALLYHIGHBASALRATE = 1111111d; public static final Integer basalPercentOnlyForCheckLimit = 10101010; public static final double bolusOnlyForCheckLimit = 10101010d; public static final Integer carbsOnlyForCheckLimit = 10101010; diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 2e48d5328f..28c3add9ba 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -29,7 +29,6 @@ import info.nightscout.androidaps.data.ConstraintChecker; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Actions.ActionsFragment; import info.nightscout.androidaps.plugins.Careportal.CareportalPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; @@ -201,7 +200,7 @@ public class MainApp extends Application { FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-G5Uploader")); else if (Config.PUMPCONTROL) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-PumpControl")); - else if (MainApp.getConstraintChecker().isClosedLoopAllowed().get()) + else if (MainApp.getConstraintChecker().isClosedLoopAllowed().value()) FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-ClosedLoop")); else FabricPrivacy.getInstance().logCustom(new CustomEvent("AppStart-OpenLoop")); 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 3a01b1d4e0..4063afb75b 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -6,7 +6,6 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.utils.SP; /** * Created by mike on 19.03.2018. @@ -102,25 +101,24 @@ public class ConstraintChecker implements ConstraintsInterface { } @Override - public Double applyBasalConstraints(Double absoluteRate) { - Double rateAfterConstrain = absoluteRate; + public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constrain = (ConstraintsInterface) p; + ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - rateAfterConstrain = Math.min(constrain.applyBasalConstraints(absoluteRate), rateAfterConstrain); + constraint.applyBasalConstraints(absoluteRate, profile); } - return rateAfterConstrain; + return absoluteRate; } @Override - public Integer applyBasalConstraints(Integer percentRate) { + public Integer applyBasalPercentConstraints(Integer percentRate) { Integer rateAfterConstrain = percentRate; ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - rateAfterConstrain = Math.min(constrain.applyBasalConstraints(percentRate), rateAfterConstrain); + rateAfterConstrain = Math.min(constrain.applyBasalPercentConstraints(percentRate), rateAfterConstrain); } return rateAfterConstrain; } diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java index 83e7c2119d..a830bcde73 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java @@ -1,5 +1,8 @@ package info.nightscout.androidaps.interfaces; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.List; @@ -7,20 +10,30 @@ import java.util.List; * Created by mike on 19.03.2018. */ -public class Constraint { +public class Constraint { + private static Logger log = LoggerFactory.getLogger(Constraint.class); + T value; + T originalValue; + List reasons = new ArrayList<>(); public Constraint(T value) { this.value = value; + this.originalValue = value; } - public T get() { + public T value() { return value; } + public T originalValue() { + return originalValue; + } + public Constraint set(T value) { this.value = value; + this.originalValue = value; return this; } @@ -30,6 +43,26 @@ public class Constraint { return this; } + public Constraint setIfSmaller(T value, String reason) { + if (value.compareTo(this.value) < 0) { + this.value = value; + } + if (value.compareTo(this.originalValue) < 0) { + reason(reason); + } + return this; + } + + public Constraint setIfGreater(T value, String reason) { + if (value.compareTo(this.value) > 0) { + this.value = value; + } + if (value.compareTo(this.originalValue) > 0) { + reason(reason); + } + return this; + } + public Constraint reason(String reason) { reasons.add(reason); return this; @@ -38,10 +71,12 @@ public class Constraint { public String getReasons() { StringBuilder sb = new StringBuilder(); int count = 0; - for (String r: reasons) { + for (String r : reasons) { if (count++ != 0) sb.append("\n"); sb.append(r); } + log.debug("Limiting origial value: " + originalValue + " to " + value + ". Reason: " + sb.toString()); return sb.toString(); } + } 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 9a05a2cb8b..d4c1d310a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.interfaces; +import info.nightscout.androidaps.data.Profile; + /** * Created by mike on 15.06.2016. */ @@ -15,9 +17,9 @@ public interface ConstraintsInterface { Constraint isSMBModeEnabled(Constraint value); - Double applyBasalConstraints(Double absoluteRate); + Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile); - Integer applyBasalConstraints(Integer percentRate); + Integer applyBasalPercentConstraints(Integer percentRate); Double applyBolusConstraints(Double insulin); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index 45666cf4ae..1a19fdf6a6 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -35,7 +35,7 @@ public interface PumpInterface { PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo); void stopBolusDelivering(); - PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew); + PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew); PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew); PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes); //some pumps might set a very short temp close to 100% as cancelling a temp can be noisy diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java index 837c176e85..b9feb77679 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java @@ -22,6 +22,7 @@ import java.text.DecimalFormat; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity; @@ -117,17 +118,21 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi final boolean setAsPercent = percentRadio.isChecked(); int durationInMinutes = SafeParse.stringToInt(duration.getText()); + Profile profile = MainApp.getConfigBuilder().getProfile(); + if (profile == null) + return; + String confirmMessage = getString(R.string.setbasalquestion); if (setAsPercent) { int basalPercentInput = SafeParse.stringToInt(basalPercent.getText()); - percent = MainApp.getConstraintChecker().applyBasalConstraints(basalPercentInput); + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(basalPercentInput); confirmMessage += "\n" + percent + "% "; confirmMessage += "\n" + getString(R.string.duration) + " " + durationInMinutes + "min ?"; if (percent != basalPercentInput) confirmMessage += "\n" + getString(R.string.constraintapllied); } else { Double basalAbsoluteInput = SafeParse.stringToDouble(basalAbsolute.getText()); - absolute = MainApp.getConstraintChecker().applyBasalConstraints(basalAbsoluteInput); + absolute = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(basalAbsoluteInput), profile).value(); confirmMessage += "\n" + absolute + " U/h "; confirmMessage += "\n" + getString(R.string.duration) + " " + durationInMinutes + "min ?"; if (absolute - basalAbsoluteInput != 0d) @@ -159,7 +164,7 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi if (setAsPercent) { ConfigBuilderPlugin.getCommandQueue().tempBasalPercent(finalBasalPercent, finalDurationInMinutes, true, callback); } else { - ConfigBuilderPlugin.getCommandQueue().tempBasalAbsolute(finalBasal, finalDurationInMinutes, true, callback); + ConfigBuilderPlugin.getCommandQueue().tempBasalAbsolute(finalBasal, finalDurationInMinutes, true, profile, callback); } FabricPrivacy.getInstance().logCustom(new CustomEvent("TempBasal")); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index fcd366dad8..26ba4c3c21 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -51,6 +51,7 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.events.EventNewBasalProfile; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity; @@ -302,7 +303,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }; - Integer maxPercent = MainApp.getConstraintChecker().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit); + Integer maxPercent = MainApp.getConstraintChecker().applyBasalPercentConstraints(Constants.basalPercentOnlyForCheckLimit); editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput); editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true, percentTextWatcher); @@ -324,7 +325,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }; - Double maxAbsolute = MainApp.getConstraintChecker().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit); + Double maxAbsolute = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile).value(); editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput); editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true, absoluteTextWatcher); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index dca42d81cf..351cbc2191 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -31,6 +31,7 @@ import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppInitialized; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.BgSourceInterface; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; @@ -351,7 +352,9 @@ public class ConfigBuilderPlugin implements PluginBase, TreatmentsInterface { */ public void applyTBRRequest(APSResult request, Profile profile, Callback callback) { PumpInterface pump = getActivePump(); - request.rate = MainApp.getConstraintChecker().applyBasalConstraints(request.rate); + + request.rateConstraint = new Constraint<>(request.rate); + request.rate = MainApp.getConstraintChecker().applyBasalConstraints(request.rateConstraint, profile).value(); long now = System.currentTimeMillis(); @@ -399,7 +402,7 @@ public class ConfigBuilderPlugin implements PluginBase, TreatmentsInterface { } else { if (Config.logCongigBuilderActions) log.debug("applyAPSRequest: setTempBasalAbsolute()"); - getCommandQueue().tempBasalAbsolute(request.rate, request.duration, false, callback); + getCommandQueue().tempBasalAbsolute(request.rate, request.duration, false, profile, callback); } } } 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 de2b549d87..c5cac6bfbb 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 @@ -13,6 +13,7 @@ import java.util.List; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.Constraint; @@ -189,7 +190,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { case 3: Constraint closedLoopEnabled = new Constraint<>(true); SafetyPlugin.getPlugin().isClosedLoopAllowed(closedLoopEnabled); - return new RequirementResult(closedLoopEnabled.get(), MainApp.gs(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.get())); + return new RequirementResult(closedLoopEnabled.value(), MainApp.gs(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.value())); case 4: double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(1000d); boolean maxIobSet = maxIOB > 0; @@ -345,12 +346,12 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public Double applyBasalConstraints(Double absoluteRate) { + public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { return absoluteRate; } @Override - public Integer applyBasalConstraints(Integer percentRate) { + public Integer applyBasalPercentConstraints(Integer percentRate) { return percentRate; } 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 52f6b9e4ef..f11432713b 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 @@ -135,40 +135,28 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public Double applyBasalConstraints(Double absoluteRate) { - Double origAbsoluteRate = absoluteRate; - Double maxBasal = SP.getDouble("openapsma_max_basal", 1d); + public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { - Profile profile = MainApp.getConfigBuilder().getProfile(); - if (profile == null) return absoluteRate; - if (absoluteRate < 0) absoluteRate = 0d; + absoluteRate.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbasalratio), 0d, MainApp.gs(R.string.basalmustbepositivevalue))); + + double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); + absoluteRate.setIfSmaller(maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), maxBasal, MainApp.gs(R.string.maxbasalinpreferences))); - Double maxBasalMult = SP.getDouble("openapsama_current_basal_safety_multiplier", 4d); - Integer maxBasalFromDaily = SP.getInt("openapsama_max_daily_safety_multiplier", 3); // Check percentRate but absolute rate too, because we know real current basal in pump - Double origRate = absoluteRate; - if (absoluteRate > maxBasal) { - absoluteRate = maxBasal; - if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) - log.debug("Limiting rate " + origRate + " by maxBasal preference to " + absoluteRate + "U/h"); - } - if (absoluteRate > maxBasalMult * profile.getBasal()) { - absoluteRate = Math.floor(maxBasalMult * profile.getBasal() * 100) / 100; - if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) - log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h"); - } - if (absoluteRate > profile.getMaxDailyBasal() * maxBasalFromDaily) { - absoluteRate = profile.getMaxDailyBasal() * maxBasalFromDaily; - if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) - log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h"); - } + Double maxBasalMult = SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d); + double maxFromBasalMult = Math.floor(maxBasalMult * profile.getBasal() * 100) / 100; + absoluteRate.setIfSmaller(maxFromBasalMult, String.format(MainApp.gs(R.string.limitingbasalratio), maxFromBasalMult, MainApp.gs(R.string.maxbasalmultiplier))); + + Double maxBasalFromDaily = SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d); + double maxFromDaily = Math.floor(profile.getMaxDailyBasal() * maxBasalFromDaily * 100) / 100; + absoluteRate.setIfSmaller(maxFromDaily, String.format(MainApp.gs(R.string.limitingbasalratio), maxFromDaily, MainApp.gs(R.string.maxdailybasalmultiplier))); return absoluteRate; } @Override - public Integer applyBasalConstraints(Integer percentRate) { + public Integer applyBasalPercentConstraints(Integer percentRate) { Integer origPercentRate = percentRate; - Double maxBasal = SP.getDouble("openapsma_max_basal", 1d); + Double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); Profile profile = MainApp.getConfigBuilder().getProfile(); if (profile == null) return percentRate; @@ -181,8 +169,8 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { if (absoluteRate < 0) absoluteRate = 0d; - Double maxBasalMult = SP.getDouble("openapsama_current_basal_safety_multiplier", 4d); - Integer maxBasalFromDaily = SP.getInt("openapsama_max_daily_safety_multiplier", 3); + Double maxBasalMult = SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d); + Integer maxBasalFromDaily = SP.getInt(R.string.key_openapsama_max_daily_safety_multiplier, 3); // Check percentRate but absolute rate too, because we know real current basal in pump Double origRate = absoluteRate; if (absoluteRate > maxBasal) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java index 8050aa7769..a1588e859f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.utils.DecimalFormatter; @@ -39,6 +40,9 @@ public class APSResult { public double smb = 0d; // super micro bolus in units public long deliverAt = 0; + public Constraint rateConstraint; + public Constraint smbConstraint; + @Override public String toString() { final PumpInterface pump = ConfigBuilderPlugin.getActivePump(); @@ -76,8 +80,8 @@ public class APSResult { ret = MainApp.sResources.getString(R.string.let_temp_basal_run) + "
"; else ret = "" + MainApp.sResources.getString(R.string.rate) + ": " + DecimalFormatter.to2Decimal(rate) + " U/h " + - "(" + DecimalFormatter.to2Decimal(rate / pump.getBaseBasalRate() * 100) + "%)
" + - "" + MainApp.sResources.getString(R.string.duration) + ": " + DecimalFormatter.to2Decimal(duration) + " min
"; + "(" + DecimalFormatter.to2Decimal(rate / pump.getBaseBasalRate() * 100) + "%)
" + + "" + MainApp.sResources.getString(R.string.duration) + ": " + DecimalFormatter.to2Decimal(duration) + " min
"; // smb if (smb != 0) @@ -101,6 +105,12 @@ public class APSResult { newResult.tempBasalReqested = tempBasalReqested; newResult.bolusRequested = bolusRequested; newResult.iob = iob; + newResult.json = json; + newResult.hasPredictions = hasPredictions; + newResult.smb = smb; + newResult.deliverAt = deliverAt; + newResult.rateConstraint = rateConstraint; + newResult.smbConstraint = smbConstraint; return newResult; } 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 ba3d88b76a..c5eee53c80 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 @@ -261,7 +261,7 @@ public class LoopPlugin implements PluginBase { log.debug("invoke from " + initiator); Constraint loopEnabled = MainApp.getConstraintChecker().isLoopInvokationAllowed(); - if (!loopEnabled.get()) { + if (!loopEnabled.value()) { String message = MainApp.sResources.getString(R.string.loopdisabled) + "\n" + loopEnabled.getReasons(); log.debug(message); MainApp.bus().post(new EventLoopSetLastRunGui(message)); @@ -298,7 +298,8 @@ public class LoopPlugin implements PluginBase { // check rate for constrais final APSResult resultAfterConstraints = result.clone(); - resultAfterConstraints.rate = MainApp.getConstraintChecker().applyBasalConstraints(resultAfterConstraints.rate); + resultAfterConstraints.rateConstraint = new Constraint<>(resultAfterConstraints.rate); + resultAfterConstraints.rate = MainApp.getConstraintChecker().applyBasalConstraints(resultAfterConstraints.rateConstraint, profile).value(); resultAfterConstraints.smb = MainApp.getConstraintChecker().applyBolusConstraints(resultAfterConstraints.smb); // safety check for multiple SMBs @@ -332,7 +333,7 @@ public class LoopPlugin implements PluginBase { Constraint closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed(); - if (closedLoopEnabled.get()) { + if (closedLoopEnabled.value()) { if (result.isChangeRequested()) { final PumpEnactResult waiting = new PumpEnactResult(); waiting.queued = true; 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 914afe256f..f71f7483b0 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 @@ -21,6 +21,7 @@ import java.lang.reflect.InvocationTargetException; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; @@ -205,8 +206,8 @@ public class DetermineBasalAdapterAMAJS { mProfile.put("target_bg", targetBg); mProfile.put("carb_ratio", profile.getIc()); mProfile.put("sens", Profile.toMgdl(profile.getIsf(), units)); - mProfile.put("max_daily_safety_multiplier", SP.getInt("openapsama_max_daily_safety_multiplier", 3)); - mProfile.put("current_basal_safety_multiplier", SP.getDouble("openapsama_current_basal_safety_multiplier", 4d)); + mProfile.put("max_daily_safety_multiplier", SP.getInt(R.string.key_openapsama_max_daily_safety_multiplier, 3)); + mProfile.put("current_basal_safety_multiplier", SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)); mProfile.put("skip_neutral_temps", true); mProfile.put("current_basal", basalrate); mProfile.put("temptargetSet", tempTargetSet); @@ -250,7 +251,7 @@ public class DetermineBasalAdapterAMAJS { mMealData.put("boluses", mealData.boluses); mMealData.put("mealCOB", mealData.mealCOB); - if (MainApp.getConstraintChecker().isAMAModeEnabled().get()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled().value()) { 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 b6581a2353..178745e3bc 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 @@ -19,7 +19,6 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -174,8 +173,8 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxIob = SP.getDouble("openapsma_max_iob", 1.5d); - double maxBasal = SP.getDouble("openapsma_max_basal", 1d); + double maxIob = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); + double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); double targetBg = Profile.toMgdl(profile.getTarget(), units); @@ -223,7 +222,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { return; startPart = new Date(); - if (MainApp.getConstraintChecker().isAMAModeEnabled().get()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled().value()) { lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); @@ -249,7 +248,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultAMA.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) { + if (!MainApp.getConstraintChecker().isClosedLoopAllowed().value()) { long now = System.currentTimeMillis(); TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) { 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 4258ae6714..2f947d3975 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 @@ -19,7 +19,6 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.ScriptReader; @@ -30,7 +29,6 @@ import info.nightscout.utils.HardLimits; import info.nightscout.utils.Profiler; import info.nightscout.utils.Round; import info.nightscout.utils.SP; -import info.nightscout.utils.SafeParse; import static info.nightscout.utils.HardLimits.checkOnlyHardLimits; import static info.nightscout.utils.HardLimits.verifyHardLimits; @@ -175,8 +173,8 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxIob = SP.getDouble("openapsma_max_iob", 1.5d); - double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); + double maxIob = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); + double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); double targetBg = Profile.toMgdl(profile.getTarget(), units); @@ -238,7 +236,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultMA.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) { + if (!MainApp.getConstraintChecker().isClosedLoopAllowed().value()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) { // going to cancel 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 c9178b4ee9..e5a555c326 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 @@ -27,7 +27,6 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.TemporaryBasal; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.ScriptReader; @@ -228,8 +227,8 @@ public class DetermineBasalAdapterSMBJS { mProfile.put("target_bg", targetBg); mProfile.put("carb_ratio", profile.getIc()); mProfile.put("sens", Profile.toMgdl(profile.getIsf(), units)); - mProfile.put("max_daily_safety_multiplier", SP.getInt("openapsama_max_daily_safety_multiplier", 3)); - mProfile.put("current_basal_safety_multiplier", SP.getDouble("openapsama_current_basal_safety_multiplier", 4d)); + mProfile.put("max_daily_safety_multiplier", SP.getInt(R.string.key_openapsama_max_daily_safety_multiplier, 3)); + mProfile.put("current_basal_safety_multiplier", SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)); mProfile.put("high_temptarget_raises_sensitivity", SMBDefaults.high_temptarget_raises_sensitivity); mProfile.put("low_temptarget_lowers_sensitivity", SMBDefaults.low_temptarget_lowers_sensitivity); @@ -245,7 +244,7 @@ public class DetermineBasalAdapterSMBJS { mProfile.put("remainingCarbsCap", SMBDefaults.remainingCarbsCap); mProfile.put("enableUAM", SP.getBoolean(R.string.key_use_uam, false)); mProfile.put("A52_risk_enable", SMBDefaults.A52_risk_enable); - boolean SMBEnabled = MainApp.getConstraintChecker().isSMBModeEnabled().get() && MainApp.getConstraintChecker().isClosedLoopAllowed().get(); + boolean SMBEnabled = MainApp.getConstraintChecker().isSMBModeEnabled().value() && MainApp.getConstraintChecker().isClosedLoopAllowed().value(); mProfile.put("enableSMB_with_COB", SMBEnabled && SP.getBoolean(R.string.key_enableSMB_with_COB, false)); mProfile.put("enableSMB_with_temptarget", SMBEnabled && SP.getBoolean(R.string.key_enableSMB_with_temptarget, false)); mProfile.put("allowSMB_with_high_temptarget", SMBEnabled && SP.getBoolean(R.string.key_allowSMB_with_high_temptarget, false)); @@ -301,7 +300,7 @@ public class DetermineBasalAdapterSMBJS { mMealData.put("lastCarbTime", mealData.lastCarbTime); - if (MainApp.getConstraintChecker().isAMAModeEnabled().get()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled().value()) { 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 2eb38d785f..22860f2c27 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 @@ -19,7 +19,6 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -179,8 +178,8 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxIob = SP.getDouble("openapsma_max_iob", 1.5d); - double maxBasal = SP.getDouble("openapsma_max_basal", 1d); + double maxIob = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); + double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); double targetBg = Profile.toMgdl(profile.getTarget(), units); @@ -225,7 +224,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().get()) { + if (MainApp.getConstraintChecker().isAMAModeEnabled().value()) { lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); @@ -254,7 +253,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) determineBasalResultSMB.tempBasalReqested = false; // limit requests on openloop mode - if (!MainApp.getConstraintChecker().isClosedLoopAllowed().get()) { + if (!MainApp.getConstraintChecker().isClosedLoopAllowed().value()) { TemporaryBasal activeTemp = MainApp.getConfigBuilder().getTempBasalFromHistory(now); if (activeTemp != null && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) { // going to cancel diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 5736c0a715..aa4c581479 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -1039,7 +1039,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, apsModeView.setText(MainApp.sResources.getString(R.string.pumpsuspended)); apsModeView.setTextColor(Color.WHITE); } else if (activeloop != null && activeloop.isEnabled(activeloop.getType())) { - if (closedLoopEnabled.get()) { + if (closedLoopEnabled.value()) { apsModeView.setText(MainApp.sResources.getString(R.string.closedloop)); } else { apsModeView.setText(MainApp.sResources.getString(R.string.openloop)); @@ -1069,7 +1069,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, // **** Temp button **** if (acceptTempLayout != null) { - boolean showAcceptButton = !closedLoopEnabled.get(); // Open mode needed + boolean showAcceptButton = !closedLoopEnabled.value(); // Open mode needed showAcceptButton = showAcceptButton && finalLastRun != null && finalLastRun.lastAPSRun != null; // aps result must exist showAcceptButton = showAcceptButton && (finalLastRun.lastOpenModeAccept == null || finalLastRun.lastOpenModeAccept.getTime() < finalLastRun.lastAPSRun.getTime()); // never accepted or before last result showAcceptButton = showAcceptButton && finalLastRun.constraintsProcessed.isChangeRequested(); // change is requested 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 f8708dbd39..5f3ae270cf 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 @@ -746,7 +746,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf * the new value (and thus still has the old duration of e.g. 1 min) expires?) */ @Override - public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean force) { + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean force) { log.debug("setTempBasalAbsolute called with a rate of " + absoluteRate + " for " + durationInMinutes + " min."); int unroundedPercentage = Double.valueOf(absoluteRate / getBaseBasalRate() * 100).intValue(); int roundedPercentage = (int) (Math.round(absoluteRate / getBaseBasalRate() * 10) * 10); @@ -1453,12 +1453,12 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public Double applyBasalConstraints(Double absoluteRate) { + public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { return absoluteRate; } @Override - public Integer applyBasalConstraints(Integer percentRate) { + public Integer applyBasalPercentConstraints(Integer percentRate) { return percentRate; } 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 2f8ea81957..8dc5c2b8b4 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 @@ -216,7 +216,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, @Override public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - percent = MainApp.getConstraintChecker().applyBasalConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(percent); if (percent < 0) { result.isTempCancel = false; result.enacted = false; @@ -458,23 +458,16 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, return value; } - @SuppressWarnings("PointlessBooleanExpression") @Override - public Double applyBasalConstraints(Double absoluteRate) { - double origAbsoluteRate = absoluteRate; - if (pump != null) { - if (absoluteRate > pump.maxBasal) { - absoluteRate = pump.maxBasal; - if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) - log.debug("Limiting rate " + origAbsoluteRate + "U/h by pump constraint to " + absoluteRate + "U/h"); - } - } + public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { + if (pump != null) + absoluteRate.setIfSmaller(pump.maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), pump.maxBasal, MainApp.gs(R.string.pumplimit))); return absoluteRate; } @SuppressWarnings("PointlessBooleanExpression") @Override - public Integer applyBasalConstraints(Integer percentRate) { + public Integer applyBasalPercentConstraints(Integer percentRate) { Integer origPercentRate = percentRate; if (percentRate < 0) percentRate = 0; if (percentRate > getPumpDescription().maxTempPercent) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java index 866df4dc54..224679a23a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java @@ -14,12 +14,14 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.plugins.PumpDanaR.services.DanaRExecutionService; import info.nightscout.utils.Round; @@ -160,7 +162,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { // This is called from APS @Override - public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) { // Recheck pump status if older than 30 min //This should not be needed while using queue because connection should be done before calling this //if (pump.lastConnection.getTime() + 30 * 60 * 1000L < System.currentTimeMillis()) { @@ -169,7 +171,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { PumpEnactResult result = new PumpEnactResult(); - absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); + absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(absoluteRate), profile).value(); final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; final boolean doLowTemp = absoluteRate < getBaseBasalRate(); @@ -266,7 +268,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { Integer durationInHalfHours = Math.max(durationInMinutes / 30, 1); // We keep current basal running so need to sub current basal Double extendedRateToSet = absoluteRate - getBaseBasalRate(); - extendedRateToSet = MainApp.getConstraintChecker().applyBasalConstraints(extendedRateToSet); + extendedRateToSet = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(extendedRateToSet), profile).value(); // needs to be rounded to 0.1 extendedRateToSet = Round.roundTo(extendedRateToSet, pumpDescription.extendedBolusStep * 2); // *2 because of halfhours diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java index 3b4a5ce7d7..fd4f3d960f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java @@ -14,12 +14,14 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.plugins.PumpDanaR.AbstractDanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.services.DanaRKoreanExecutionService; @@ -161,7 +163,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { // This is called from APS @Override - public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) { // Recheck pump status if older than 30 min //This should not be needed while using queue because connection should be done before calling this //if (pump.lastConnection.getTime() + 30 * 60 * 1000L < System.currentTimeMillis()) { @@ -170,7 +172,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { PumpEnactResult result = new PumpEnactResult(); - absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); + absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(absoluteRate), profile).value(); final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; final boolean doLowTemp = absoluteRate < getBaseBasalRate(); @@ -267,7 +269,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { Integer durationInHalfHours = Math.max(durationInMinutes / 30, 1); // We keep current basal running so need to sub current basal Double extendedRateToSet = absoluteRate - getBaseBasalRate(); - extendedRateToSet = MainApp.getConstraintChecker().applyBasalConstraints(extendedRateToSet); + extendedRateToSet = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(extendedRateToSet), profile).value(); // needs to be rounded to 0.1 extendedRateToSet = Round.roundTo(extendedRateToSet, pumpDescription.extendedBolusStep * 2); // *2 because of halfhours 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 3d0e266397..214d002a4e 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 @@ -299,20 +299,14 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public Double applyBasalConstraints(Double absoluteRate) { - double origAbsoluteRate = absoluteRate; - if (pump != null) { - if (absoluteRate > pump.maxBasal) { - absoluteRate = pump.maxBasal; - if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) - log.debug("Limiting rate " + origAbsoluteRate + "U/h by pump constraint to " + absoluteRate + "U/h"); - } - } + public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { + if (pump != null) + absoluteRate.setIfSmaller(pump.maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), pump.maxBasal, MainApp.gs(R.string.pumplimit))); return absoluteRate; } @Override - public Integer applyBasalConstraints(Integer percentRate) { + public Integer applyBasalPercentConstraints(Integer percentRate) { Integer origPercentRate = percentRate; if (percentRate < 0) percentRate = 0; if (percentRate > getPumpDescription().maxTempPercent) @@ -512,7 +506,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, // This is called from APS @Override - public synchronized PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { + public synchronized PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) { // Recheck pump status if older than 30 min //This should not be needed while using queue because connection should be done before calling this @@ -522,7 +516,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, PumpEnactResult result = new PumpEnactResult(); - absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); + absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(absoluteRate), profile).value(); final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; final boolean doLowTemp = absoluteRate < getBaseBasalRate(); @@ -594,7 +588,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public synchronized PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - percent = MainApp.getConstraintChecker().applyBasalConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(percent); if (percent < 0) { result.isTempCancel = false; result.enacted = false; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java index 594db71ece..044cec18a0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java @@ -14,10 +14,12 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.plugins.ConfigBuilder.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.PumpDanaR.AbstractDanaRPlugin; @@ -183,7 +185,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { // This is called from APS @Override - public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) { // Recheck pump status if older than 30 min //This should not be needed while using queue because connection should be done before calling this //if (pump.lastConnection.getTime() + 30 * 60 * 1000L < System.currentTimeMillis()) { @@ -192,7 +194,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { PumpEnactResult result = new PumpEnactResult(); - absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); + absoluteRate = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(absoluteRate), profile).value(); final boolean doTempOff = getBaseBasalRate() - absoluteRate == 0d; final boolean doLowTemp = absoluteRate < getBaseBasalRate(); @@ -262,7 +264,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { @Override public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - percent = MainApp.getConstraintChecker().applyBasalConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(percent); if (percent < 0) { result.isTempCancel = false; result.enacted = false; 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 c6e4a76913..fcf8d42094 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 @@ -355,7 +355,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints Mstatus mstatus = async.busyWaitForCommandResult(uuid, BUSY_WAIT_TIME); if (mstatus.success()) { log("GOT STATUS RESULT!!! PARTY WOOHOO!!!"); - statusResult = (StatusTaskRunner.Result) mstatus.getResponseObject(); + setStatusResult((StatusTaskRunner.Result) mstatus.getResponseObject()); statusResultTime = Helpers.tsl(); processStatusResult(); updateGui(); @@ -375,6 +375,10 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } } + public void setStatusResult(StatusTaskRunner.Result result) { + this.statusResult = result; + } + @Override public PumpEnactResult setNewBasalProfile(Profile profile) { PumpEnactResult result = new PumpEnactResult(); @@ -543,7 +547,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints // Temporary Basals @Override - public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) { absoluteRate = Helpers.roundDouble(absoluteRate, 3); log("Set TBR absolute: " + absoluteRate); final double base_basal = getBaseBasalRate(); @@ -1105,12 +1109,15 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } @Override - public Double applyBasalConstraints(Double absoluteRate) { - return Math.min(absoluteRate, statusResult != null ? statusResult.maximumBasalAmount : 0); + public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { + if (statusResult != null) { + absoluteRate.setIfSmaller(statusResult.maximumBasalAmount, String.format(MainApp.gs(R.string.limitingbasalratio), statusResult.maximumBasalAmount, MainApp.gs(R.string.pumplimit))); + } + return absoluteRate; } @Override - public Integer applyBasalConstraints(Integer percentRate) { + public Integer applyBasalPercentConstraints(Integer percentRate) { return Math.min(percentRate, pumpDescription.maxTempPercent); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java index 0d726b5dcd..f22a0b3a0e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java @@ -194,7 +194,7 @@ public class MDIPlugin implements PluginBase, PumpInterface { } @Override - public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); result.success = false; result.comment = MainApp.instance().getString(R.string.pumperror); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java index 0a38477808..927618b539 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java @@ -289,7 +289,7 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { } @Override - public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) { TreatmentsInterface treatmentsInterface = MainApp.getConfigBuilder(); TemporaryBasal tempBasal = new TemporaryBasal(); tempBasal.date = System.currentTimeMillis(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index e545d7922e..8bdd83bbc3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -29,6 +29,7 @@ import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshOverview; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; @@ -418,18 +419,24 @@ public class SmsCommunicatorPlugin implements PluginBase { } } else { tempBasal = SafeParse.stringToDouble(splited[1]); - tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(tempBasal); - if (remoteCommandsAllowed) { - passCode = generatePasscode(); - reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode); - receivedSms.processed = true; - resetWaitingMessages(); - sendSMS(tempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); - tempBasalWaitingForConfirmation.tempBasal = tempBasal; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Basal")); - } else { - reply = MainApp.sResources.getString(R.string.smscommunicator_remotebasalnotallowed); + Profile profile = MainApp.getConfigBuilder().getProfile(); + if (profile == null) { + reply = MainApp.sResources.getString(R.string.noprofile); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); + } else { + tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(tempBasal), profile).value(); + if (remoteCommandsAllowed) { + passCode = generatePasscode(); + reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode); + receivedSms.processed = true; + resetWaitingMessages(); + sendSMS(tempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); + tempBasalWaitingForConfirmation.tempBasal = tempBasal; + FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Basal")); + } else { + reply = MainApp.sResources.getString(R.string.smscommunicator_remotebasalnotallowed); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); + } } } } @@ -503,20 +510,22 @@ public class SmsCommunicatorPlugin implements PluginBase { } else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed && tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - tempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { tempBasalWaitingForConfirmation.processed = true; - ConfigBuilderPlugin.getCommandQueue().tempBasalAbsolute(tempBasalWaitingForConfirmation.tempBasal, 30, true, new Callback() { - @Override - public void run() { - if (result.success) { - String reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); - reply += "\n" + ConfigBuilderPlugin.getActivePump().shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); - } else { - String reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalfailed); - reply += "\n" + ConfigBuilderPlugin.getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); + Profile profile = MainApp.getConfigBuilder().getProfile(); + if (profile != null) + ConfigBuilderPlugin.getCommandQueue().tempBasalAbsolute(tempBasalWaitingForConfirmation.tempBasal, 30, true, profile, new Callback() { + @Override + public void run() { + if (result.success) { + String reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); + reply += "\n" + ConfigBuilderPlugin.getActivePump().shortStatus(true); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); + } else { + String reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalfailed); + reply += "\n" + ConfigBuilderPlugin.getActivePump().shortStatus(true); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); + } } - } - }); + }); } else if (cancelTempBasalWaitingForConfirmation != null && !cancelTempBasalWaitingForConfirmation.processed && cancelTempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - cancelTempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { cancelTempBasalWaitingForConfirmation.processed = true; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index 9323393abd..0dcdb3b1d9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -27,7 +27,6 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -435,7 +434,7 @@ public class ActionStringHandler { // decide if enabled/disabled closed/open; what Plugin as APS? final LoopPlugin activeloop = MainApp.getConfigBuilder().getActiveLoop(); if (activeloop != null && activeloop.isEnabled(activeloop.getType())) { - if (MainApp.getConstraintChecker().isClosedLoopAllowed().get()) { + if (MainApp.getConstraintChecker().isClosedLoopAllowed().value()) { ret += "CLOSED LOOP\n"; } else { ret += "OPEN LOOP\n"; diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index 949551f8e5..d9b1d98cfb 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -18,6 +18,7 @@ import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.events.EventBolusRequested; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog; @@ -186,7 +187,7 @@ public class CommandQueue { } // returns true if command is queued - public boolean tempBasalAbsolute(double absoluteRate, int durationInMinutes, boolean enforceNew, Callback callback) { + public boolean tempBasalAbsolute(double absoluteRate, int durationInMinutes, boolean enforceNew, Profile profile, Callback callback) { if (isRunning(Command.CommandType.TEMPBASAL)) { if (callback != null) callback.result(executingNowError()).run(); @@ -196,10 +197,10 @@ public class CommandQueue { // remove all unfinished removeAll(Command.CommandType.TEMPBASAL); - Double rateAfterConstraints = MainApp.getConstraintChecker().applyBasalConstraints(absoluteRate); + Double rateAfterConstraints = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(absoluteRate), profile).value(); // add new command to queue - add(new CommandTempBasalAbsolute(rateAfterConstraints, durationInMinutes, enforceNew, callback)); + add(new CommandTempBasalAbsolute(rateAfterConstraints, durationInMinutes, enforceNew, profile, callback)); notifyAboutNewCommand(); @@ -217,7 +218,7 @@ public class CommandQueue { // remove all unfinished removeAll(Command.CommandType.TEMPBASAL); - Integer percentAfterConstraints = MainApp.getConstraintChecker().applyBasalConstraints(percent); + Integer percentAfterConstraints = MainApp.getConstraintChecker().applyBasalPercentConstraints(percent); // add new command to queue add(new CommandTempBasalPercent(percentAfterConstraints, durationInMinutes, enforceNew, callback)); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandTempBasalAbsolute.java b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandTempBasalAbsolute.java index 28dc728174..35c2435558 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandTempBasalAbsolute.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandTempBasalAbsolute.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.queue.Callback; @@ -19,18 +20,20 @@ public class CommandTempBasalAbsolute extends Command { int durationInMinutes; double absoluteRate; boolean enforceNew; + Profile profile; - public CommandTempBasalAbsolute(double absoluteRate, int durationInMinutes, boolean enforceNew, Callback callback) { + public CommandTempBasalAbsolute(double absoluteRate, int durationInMinutes, boolean enforceNew, Profile profile, Callback callback) { commandType = CommandType.TEMPBASAL; this.absoluteRate = absoluteRate; this.durationInMinutes = durationInMinutes; this.enforceNew = enforceNew; + this.profile = profile; this.callback = callback; } @Override public void execute() { - PumpEnactResult r = ConfigBuilderPlugin.getActivePump().setTempBasalAbsolute(absoluteRate, durationInMinutes, enforceNew); + PumpEnactResult r = ConfigBuilderPlugin.getActivePump().setTempBasalAbsolute(absoluteRate, durationInMinutes, profile, enforceNew); if (Config.logCongigBuilderActions) log.debug("setTempBasalAbsolute rate: " + absoluteRate + " durationInMinutes: " + durationInMinutes + " success: " + r.success + " enacted: " + r.enacted); if (callback != null) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 053786dab2..2af8624a21 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -430,9 +430,7 @@ Advanced Settings Model: %02X Protocol: %02X Code: %02X Profile - max_daily_safety_multiplier Default value: 3 This is a key OpenAPS safety cap. What this does is limit your basals to be 3x (in this people) your biggest basal rate. You likely will not need to change this, but you should be aware that’s what is discussed about “3x max daily; 4x current” for safety caps. - current_basal_safety_multiplier Default value: 4 This is the other half of the key OpenAPS safety caps, and the other half of “3x max daily; 4x current” of the safety caps. This means your basal, regardless of max basal set on your pump, cannot be any higher than this number times the current level of your basal. This is to prevent people from getting into dangerous territory by setting excessively high max basals before understanding how the algorithm works. Again, the default is 4x; most people will never need to adjust this and are instead more likely to need to adjust other settings if they feel like they are “running into” this safety cap. autosens_max Default value: 1.2\nThis is a multiplier cap for autosens (and soon autotune) to set a 20% max limit on how high the autosens ratio can be, which in turn determines how high autosens can adjust basals, how low it can adjust ISF, and how low it can set the BG target. @@ -968,5 +966,15 @@ Closed loop mode disabled in preferences AMA disabled in preferences SMB disabled in preferences + Limiting basal rate to %.2f U/h because of %s + pump limit + openapsma_max_basal + openapsama_current_basal_safety_multiplier + openapsama_max_daily_safety_multiplier + basal must be positive value + max basal settings in preferences + max basal multiplier + max daily basal multiplier + openapsma_max_iob diff --git a/app/src/main/res/xml/pref_advanced.xml b/app/src/main/res/xml/pref_advanced.xml index cc0d496943..260e2c5135 100644 --- a/app/src/main/res/xml/pref_advanced.xml +++ b/app/src/main/res/xml/pref_advanced.xml @@ -46,10 +46,10 @@ android:dialogMessage="@string/openapsama_max_daily_safety_multiplier_summary" android:digits="0123456789.," android:inputType="number" - android:key="openapsama_max_daily_safety_multiplier" + android:key="@string/key_openapsama_max_daily_safety_multiplier" android:maxLines="20" android:selectAllOnFocus="true" - android:title="@string/openapsama_max_daily_safety_multiplier" + android:title="@string/key_openapsama_max_daily_safety_multiplier" validate:maxNumber="10" validate:minNumber="1" validate:testType="numericRange" /> @@ -58,11 +58,11 @@ android:dialogMessage="@string/openapsama_current_basal_safety_multiplier_summary" android:digits="0123456789.," android:inputType="number" - android:key="openapsama_current_basal_safety_multiplier" + android:key="@string/key_openapsama_current_basal_safety_multiplier" android:maxLines="20" android:selectAllOnFocus="true" android:singleLine="true" - android:title="@string/openapsama_current_basal_safety_multiplier" + android:title="@string/key_openapsama_current_basal_safety_multiplier" validate:floatmaxNumber="10" validate:floatminNumber="1" validate:testType="floatNumericRange" /> diff --git a/app/src/main/res/xml/pref_openapsama.xml b/app/src/main/res/xml/pref_openapsama.xml index 7dfcf7ad72..ee8a1d80bd 100644 --- a/app/src/main/res/xml/pref_openapsama.xml +++ b/app/src/main/res/xml/pref_openapsama.xml @@ -7,13 +7,13 @@ diff --git a/app/src/main/res/xml/pref_openapsma.xml b/app/src/main/res/xml/pref_openapsma.xml index f091bbce0b..79393f614a 100644 --- a/app/src/main/res/xml/pref_openapsma.xml +++ b/app/src/main/res/xml/pref_openapsma.xml @@ -6,13 +6,13 @@ diff --git a/app/src/main/res/xml/pref_openapssmb.xml b/app/src/main/res/xml/pref_openapssmb.xml index da0b64a91c..098d874eaa 100644 --- a/app/src/main/res/xml/pref_openapssmb.xml +++ b/app/src/main/res/xml/pref_openapssmb.xml @@ -6,13 +6,13 @@ android:title="@string/openapssmb"> diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java index 088c6c4459..b2b86047b8 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java @@ -6,8 +6,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; -import info.nightscout.androidaps.interfaces.Constraint; - /** * Created by mike on 19.03.2018. */ @@ -17,19 +15,28 @@ public class ConstraintTest { @Test public void doTests() throws Exception { - Constraint c; + Constraint b = new Constraint<>(true); + Assert.assertEquals(Boolean.TRUE, b.value()); + Assert.assertEquals("", b.getReasons()); + b.set(false); + Assert.assertEquals(Boolean.FALSE, b.value()); + Assert.assertEquals("", b.getReasons()); + b.set(true, "Set true"); + Assert.assertEquals(Boolean.TRUE, b.value()); + Assert.assertEquals("Set true", b.getReasons()); + b.set(false, "Set false"); + Assert.assertEquals(Boolean.FALSE, b.value()); + Assert.assertEquals("Set true\nSet false", b.getReasons()); - c = new Constraint(true); - Assert.assertEquals(Boolean.TRUE, c.get()); - Assert.assertEquals("", c.getReasons()); - c.set(false); - Assert.assertEquals(Boolean.FALSE, c.get()); - Assert.assertEquals("", c.getReasons()); - c.set(true, "Set true"); - Assert.assertEquals(Boolean.TRUE, c.get()); - Assert.assertEquals("Set true", c.getReasons()); - c.set(false, "Set false"); - Assert.assertEquals(Boolean.FALSE, c.get()); - Assert.assertEquals("Set true\nSet false", c.getReasons()); + Constraint d = new Constraint<>(10d); + d.set(5d, "Set 5d"); + Assert.assertEquals(5d, b.value()); + Assert.assertEquals("Set 5d", b.getReasons()); + d.setIfSmaller(6d, "Set 6d"); + Assert.assertEquals(5d, b.value()); + Assert.assertEquals("Set 5d", b.getReasons()); + d.setIfSmaller(4d, "Set 4d"); + Assert.assertEquals(4d, b.value()); + Assert.assertEquals("Set 5d\nSet 4d", b.getReasons()); } } 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 cb755291f0..3a290f098b 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -1,9 +1,13 @@ package info.nightscout.androidaps.interfaces; +import android.content.Context; + import com.squareup.otto.Bus; import junit.framework.Assert; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -14,13 +18,20 @@ import org.powermock.modules.junit4.PowerMockRunner; import java.util.ArrayList; import java.util.Date; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.ConstraintChecker; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.PumpCombo.ComboPlugin; +import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; +import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin; +import info.nightscout.androidaps.plugins.PumpInsight.InsightPumpPlugin; +import info.nightscout.androidaps.plugins.PumpInsight.connector.StatusTaskRunner; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.SP; @@ -32,7 +43,7 @@ import static org.mockito.Mockito.when; * Created by mike on 18.03.2018. */ @RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, FabricPrivacy.class, SP.class}) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, FabricPrivacy.class, SP.class, Context.class}) public class ConstraintsCheckerTest { PumpInterface pump = new VirtualPumpPlugin(); @@ -42,12 +53,21 @@ public class ConstraintsCheckerTest { MainApp mainApp = mock(MainApp.class); MockedBus bus = new MockedBus(); + String validProfile = "{\"dia\":\"3\",\"carbratio\":[{\"time\":\"00:00\",\"value\":\"30\"}],\"carbs_hr\":\"20\",\"delay\":\"20\",\"sens\":[{\"time\":\"00:00\",\"value\":\"100\"},{\"time\":\"2:00\",\"value\":\"110\"}],\"timezone\":\"UTC\",\"basal\":[{\"time\":\"00:00\",\"value\":\"1\"}],\"target_low\":[{\"time\":\"00:00\",\"value\":\"4\"}],\"target_high\":[{\"time\":\"00:00\",\"value\":\"5\"}],\"startDate\":\"1970-01-01T00:00:00.000Z\",\"units\":\"mmol\"}"; + Profile profile = new Profile(new JSONObject(validProfile), Constants.MGDL); + SafetyPlugin safetyPlugin; ObjectivesPlugin objectivesPlugin; ComboPlugin comboPlugin; + DanaRPlugin danaRPlugin; + DanaRSPlugin danaRSPlugin; + InsightPumpPlugin insightPlugin; boolean notificationSent = false; + public ConstraintsCheckerTest() throws JSONException { + } + // isLoopInvokationAllowed tests @Test public void pumpDescriptionShouldLimitLoopInvokation() throws Exception { @@ -55,7 +75,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isLoopInvokationAllowed(); Assert.assertEquals(true, c.getReasons().contains("Pump is not temp basal capable")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } @Test @@ -64,7 +84,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isLoopInvokationAllowed(); Assert.assertEquals(true, c.getReasons().contains("Objective 1 not started")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } @Test @@ -74,7 +94,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isLoopInvokationAllowed(); Assert.assertEquals(true, c.getReasons().contains("No valid basal rate read from pump")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } // isClosedLoopAllowed tests @@ -85,7 +105,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isClosedLoopAllowed(); Assert.assertEquals(true, c.getReasons().contains("Running dev version. Closed loop is disabled.")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } @Test @@ -94,7 +114,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isClosedLoopAllowed(); Assert.assertEquals(true, c.getReasons().contains("Closed loop mode disabled in preferences")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } @Test @@ -104,7 +124,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isClosedLoopAllowed(); Assert.assertEquals(true, c.getReasons().contains("Objective 4 not started")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } // isAutosensModeEnabled tests @@ -114,7 +134,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isAutosensModeEnabled(); Assert.assertEquals(true, c.getReasons().contains("Objective 6 not started")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } // isAMAModeEnabled tests @@ -124,7 +144,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isAMAModeEnabled(); Assert.assertEquals(true, c.getReasons().contains("AMA disabled in preferences")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } @Test @@ -133,7 +153,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isAMAModeEnabled(); Assert.assertEquals(true, c.getReasons().contains("Objective 7 not started")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } // isSMBModeEnabled tests @@ -143,7 +163,7 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isSMBModeEnabled(); Assert.assertEquals(true, c.getReasons().contains("SMB disabled in preferences")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); } @Test @@ -152,7 +172,46 @@ public class ConstraintsCheckerTest { Constraint c = constraintChecker.isSMBModeEnabled(); Assert.assertEquals(true, c.getReasons().contains("Objective 8 not started")); - Assert.assertEquals(Boolean.FALSE, c.get()); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + // applyBasalConstraints tests + @Test + public void basalRateShouldBeLimited() throws Exception { + // DanaR, RS + danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + DanaRPump.getInstance().maxBasal = 0.8d; + + // Insight + insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + StatusTaskRunner.Result result = new StatusTaskRunner.Result(); + result.maximumBasalAmount = 1.1d; + insightPlugin.setStatusResult(result); + + + // No limit by default + when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); + when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d); + when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d); + + // Negative basal not allowed + Constraint d = new Constraint<>(-0.5d); + constraintChecker.applyBasalConstraints(d, profile); + Assert.assertEquals(0d, d.value()); + Assert.assertEquals("Limiting basal rate to 0,00 U/h because of basal must be positive value", d.getReasons()); + + // Apply all limits + d = new Constraint<>(Constants.REALLYHIGHBASALRATE); + constraintChecker.applyBasalConstraints(d, profile); + Assert.assertEquals(0.8d, d.value()); + Assert.assertEquals("Limiting basal rate to 1,00 U/h because of max basal settings in preferences\n" + + "Limiting basal rate to 4,00 U/h because of max basal multiplier\n" + + "Limiting basal rate to 3,00 U/h because of max daily basal multiplier\n" + + "Limiting basal rate to 0,80 U/h because of pump limit\n" + + "Limiting basal rate to 0,80 U/h because of pump limit\n" + + "Limiting basal rate to 1,10 U/h because of pump limit", d.getReasons()); + } @Before @@ -168,6 +227,9 @@ public class ConstraintsCheckerTest { PowerMockito.mockStatic(FabricPrivacy.class); + Context context = mock(Context.class); + when(MainApp.instance().getApplicationContext()).thenReturn(context); + when(MainApp.bus()).thenReturn(bus); when(MainApp.gs(R.string.pumpisnottempbasalcapable)).thenReturn("Pump is not temp basal capable"); @@ -177,17 +239,32 @@ public class ConstraintsCheckerTest { 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"); when(MainApp.gs(R.string.smbdisabledinpreferences)).thenReturn("SMB disabled in preferences"); + when(MainApp.gs(R.string.limitingbasalratio)).thenReturn("Limiting basal rate to %.2f U/h because of %s"); + when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); + when(MainApp.gs(R.string.basalmustbepositivevalue)).thenReturn("basal must be positive value"); + when(MainApp.gs(R.string.maxbasalinpreferences)).thenReturn("max basal settings in preferences"); + when(MainApp.gs(R.string.maxbasalmultiplier)).thenReturn("max basal multiplier"); + when(MainApp.gs(R.string.maxdailybasalmultiplier)).thenReturn("max daily basal multiplier"); + + PowerMockito.mockStatic(SP.class); + // RS constructor + when(SP.getString(R.string.key_danars_address, "")).thenReturn(""); safetyPlugin = SafetyPlugin.getPlugin(); objectivesPlugin = ObjectivesPlugin.getPlugin(); comboPlugin = ComboPlugin.getPlugin(); + danaRPlugin = DanaRPlugin.getPlugin(); + danaRSPlugin = DanaRSPlugin.getPlugin(); + insightPlugin = InsightPumpPlugin.getPlugin(); ArrayList constraintsPluginsList = new ArrayList<>(); constraintsPluginsList.add(safetyPlugin); constraintsPluginsList.add(objectivesPlugin); constraintsPluginsList.add(comboPlugin); + constraintsPluginsList.add(danaRPlugin); + constraintsPluginsList.add(danaRSPlugin); + constraintsPluginsList.add(insightPlugin); when(mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class)).thenReturn(constraintsPluginsList); - PowerMockito.mockStatic(SP.class); } class MockedBus extends Bus { diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java index cb221eb93c..009a834c86 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java @@ -7,6 +7,7 @@ import com.squareup.otto.ThreadEnforcer; import junit.framework.Assert; +import org.json.JSONException; import org.json.JSONObject; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,7 +37,11 @@ import static org.mockito.Mockito.when; @PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class}) public class CommandQueueTest extends CommandQueue { - String profileJson = "{\"dia\":\"3\",\"carbratio\":[{\"time\":\"00:00\",\"value\":\"30\"}],\"carbs_hr\":\"20\",\"delay\":\"20\",\"sens\":[{\"time\":\"00:00\",\"value\":\"100\"}],\"timezone\":\"UTC\",\"basal\":[{\"time\":\"00:00\",\"value\":\"0.1\"}],\"target_low\":[{\"time\":\"00:00\",\"value\":\"4\"}],\"target_high\":[{\"time\":\"00:00\",\"value\":\"5\"}],\"startDate\":\"1970-01-01T00:00:00.000Z\",\"units\":\"mmol\"}"; + String validProfile = "{\"dia\":\"3\",\"carbratio\":[{\"time\":\"00:00\",\"value\":\"30\"}],\"carbs_hr\":\"20\",\"delay\":\"20\",\"sens\":[{\"time\":\"00:00\",\"value\":\"100\"},{\"time\":\"2:00\",\"value\":\"110\"}],\"timezone\":\"UTC\",\"basal\":[{\"time\":\"00:00\",\"value\":\"0.1\"}],\"target_low\":[{\"time\":\"00:00\",\"value\":\"4\"}],\"target_high\":[{\"time\":\"00:00\",\"value\":\"5\"}],\"startDate\":\"1970-01-01T00:00:00.000Z\",\"units\":\"mmol\"}"; + Profile profile = new Profile(new JSONObject(validProfile), Constants.MGDL); + + public CommandQueueTest() throws JSONException { + } @Test public void doTests() throws Exception { @@ -62,7 +67,7 @@ public class CommandQueueTest extends CommandQueue { Assert.assertEquals(0, size()); // add tempbasal - tempBasalAbsolute(0, 30, true, null); + tempBasalAbsolute(0, 30, true, profile, null); Assert.assertEquals(1, size()); // add tempbasal percent. it should replace previous TEMPBASAL @@ -83,7 +88,7 @@ public class CommandQueueTest extends CommandQueue { // add setProfile (command is not queued before unless a ProfileSwitch exists) // TODO test with profile switch set - setProfile(new Profile(new JSONObject(profileJson), Constants.MGDL), null); + setProfile(profile, null); Assert.assertEquals(2, size()); // add loadHistory @@ -95,7 +100,7 @@ public class CommandQueueTest extends CommandQueue { Assert.assertEquals(4, size()); clear(); - tempBasalAbsolute(0, 30, true, null); + tempBasalAbsolute(0, 30, true, profile, null); pickup(); Assert.assertEquals(0, size()); Assert.assertNotNull(performing); From ad7ec4dacada1a9a2945a053dd7f5b32256ed82a Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 20 Mar 2018 23:47:37 +0100 Subject: [PATCH 12/28] fix tests --- .../androidaps/interfaces/ConstraintTest.java | 12 ++++++------ .../androidaps/queue/CommandQueueTest.java | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java index b2b86047b8..e21e09cfb4 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java @@ -30,13 +30,13 @@ public class ConstraintTest { Constraint d = new Constraint<>(10d); d.set(5d, "Set 5d"); - Assert.assertEquals(5d, b.value()); - Assert.assertEquals("Set 5d", b.getReasons()); + Assert.assertEquals(5d, d.value()); + Assert.assertEquals("Set 5d", d.getReasons()); d.setIfSmaller(6d, "Set 6d"); - Assert.assertEquals(5d, b.value()); - Assert.assertEquals("Set 5d", b.getReasons()); + Assert.assertEquals(5d, d.value()); + Assert.assertEquals("Set 5d\nSet 6d", d.getReasons()); d.setIfSmaller(4d, "Set 4d"); - Assert.assertEquals(4d, b.value()); - Assert.assertEquals("Set 5d\nSet 4d", b.getReasons()); + Assert.assertEquals(4d, d.value()); + Assert.assertEquals("Set 5d\nSet 6d\nSet 4d", d.getReasons()); } } diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java index 009a834c86..14892464d4 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java @@ -20,12 +20,14 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.ConstraintChecker; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.PumpMDI.MDIPlugin; import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.utils.ToastUtils; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -125,6 +127,8 @@ public class CommandQueueTest extends CommandQueue { when(MainApp.instance()).thenReturn(mainApp); when(MainApp.getConstraintChecker().applyBolusConstraints(insulin)).thenReturn(insulin); when(MainApp.getConstraintChecker().applyCarbsConstraints(carbs)).thenReturn(carbs); + Constraint rateConstraint = new Constraint<>(0d); + when(MainApp.getConstraintChecker().applyBasalConstraints(any(), any())).thenReturn(rateConstraint); PowerMockito.mockStatic(ToastUtils.class); Context context = mock(Context.class); From 51ca4cc8c6fbe0ac53fe252cea102057b4eefec4 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 21 Mar 2018 07:41:03 +0100 Subject: [PATCH 13/28] show failed tests at the end of travis log --- app/build.gradle | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index e6efea8288..721bb2dd82 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,6 +47,11 @@ def generateGitBuild = { -> return stringBuilder.toString() } +tasks.matching {it instanceof Test}.all { + testLogging.events = ["failed", "skipped"] + testLogging.exceptionFormat = "full" +} + android { compileSdkVersion 27 buildToolsVersion "${supportLibraryVersion}" From 7a5d8e2ec962145c8a9e120e5b8f2358ccf5087e Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 21 Mar 2018 07:51:27 +0100 Subject: [PATCH 14/28] use US locale in tests --- .../nightscout/androidaps/data/ProfileTest.java | 8 +++++--- .../interfaces/ConstraintsCheckerTest.java | 16 +++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/data/ProfileTest.java b/app/src/test/java/info/nightscout/androidaps/data/ProfileTest.java index ee04a6613a..664515d45d 100644 --- a/app/src/test/java/info/nightscout/androidaps/data/ProfileTest.java +++ b/app/src/test/java/info/nightscout/androidaps/data/ProfileTest.java @@ -13,6 +13,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.skyscreamer.jsonassert.JSONAssert; import java.util.Calendar; +import java.util.Locale; import java.util.TimeZone; import info.nightscout.androidaps.Constants; @@ -133,9 +134,9 @@ public class ProfileTest { // Test timeshift functionality p = new Profile(new JSONObject(validProfile), 100, 1); Assert.assertEquals( - "00:00 110,0 mmol/U\n" + - "01:00 100,0 mmol/U\n" + - "03:00 110,0 mmol/U", p.getIsfList().replace(".", ",")); + "00:00 110.0 mmol/U\n" + + "01:00 100.0 mmol/U\n" + + "03:00 110.0 mmol/U", p.getIsfList()); // Test hour alignment MainApp.getConfigBuilder().getActivePump().getPumpDescription().is30minBasalRatesCapable = false; @@ -146,6 +147,7 @@ public class ProfileTest { } private void prepareMock() throws Exception { + Locale.setDefault(new Locale("en", "US")); ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); PowerMockito.mockStatic(ConfigBuilderPlugin.class); 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 3a290f098b..7040e2d9d0 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -17,6 +17,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import java.util.ArrayList; import java.util.Date; +import java.util.Locale; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; @@ -199,23 +200,24 @@ public class ConstraintsCheckerTest { Constraint d = new Constraint<>(-0.5d); constraintChecker.applyBasalConstraints(d, profile); Assert.assertEquals(0d, d.value()); - Assert.assertEquals("Limiting basal rate to 0,00 U/h because of basal must be positive value", d.getReasons()); + Assert.assertEquals("Limiting basal rate to 0.00 U/h because of basal must be positive value", d.getReasons()); // Apply all limits d = new Constraint<>(Constants.REALLYHIGHBASALRATE); constraintChecker.applyBasalConstraints(d, profile); Assert.assertEquals(0.8d, d.value()); - Assert.assertEquals("Limiting basal rate to 1,00 U/h because of max basal settings in preferences\n" + - "Limiting basal rate to 4,00 U/h because of max basal multiplier\n" + - "Limiting basal rate to 3,00 U/h because of max daily basal multiplier\n" + - "Limiting basal rate to 0,80 U/h because of pump limit\n" + - "Limiting basal rate to 0,80 U/h because of pump limit\n" + - "Limiting basal rate to 1,10 U/h because of pump limit", d.getReasons()); + Assert.assertEquals("Limiting basal rate to 1.00 U/h because of max basal settings in preferences\n" + + "Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + + "Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + + "Limiting basal rate to 0.80 U/h because of pump limit\n" + + "Limiting basal rate to 0.80 U/h because of pump limit\n" + + "Limiting basal rate to 1.10 U/h because of pump limit", d.getReasons()); } @Before public void prepareMock() throws Exception { + Locale.setDefault(new Locale("en", "US")); PowerMockito.mockStatic(ConfigBuilderPlugin.class); PowerMockito.mockStatic(MainApp.class); From e2ea44a8a8e1e4639c94b4c7f35da6aa7fe9fcd4 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 21 Mar 2018 20:24:02 +0100 Subject: [PATCH 15/28] applyBasalPercentConstraints reactor & tests part1 --- .../info/nightscout/androidaps/Constants.java | 2 +- .../androidaps/data/ConstraintChecker.java | 7 ++- .../interfaces/ConstraintsInterface.java | 2 +- .../androidaps/interfaces/PumpInterface.java | 2 +- .../Actions/dialogs/NewTempBasalDialog.java | 4 +- .../Dialogs/NewNSTreatmentDialog.java | 4 +- .../ConfigBuilder/ConfigBuilderPlugin.java | 4 +- .../ObjectivesPlugin.java | 2 +- .../ConstraintsSafety/SafetyPlugin.java | 43 +++++------------- .../Overview/Dialogs/WizardDialog.java | 6 ++- .../plugins/Overview/OverviewFragment.java | 17 ++++--- .../plugins/PumpCombo/ComboPlugin.java | 4 +- .../PumpDanaR/AbstractDanaRPlugin.java | 22 ++++------ .../plugins/PumpDanaR/DanaRPlugin.java | 2 +- .../PumpDanaRKorean/DanaRKoreanPlugin.java | 2 +- .../plugins/PumpDanaRS/DanaRSPlugin.java | 16 +++---- .../plugins/PumpDanaRv2/DanaRv2Plugin.java | 4 +- .../PumpInsight/InsightPumpPlugin.java | 42 +++++++++++------- .../androidaps/plugins/PumpMDI/MDIPlugin.java | 2 +- .../PumpVirtual/VirtualPumpPlugin.java | 2 +- .../androidaps/queue/CommandQueue.java | 6 +-- .../commands/CommandTempBasalPercent.java | 7 ++- app/src/main/res/values/strings.xml | 1 + .../androidaps/interfaces/ConstraintTest.java | 1 + .../interfaces/ConstraintsCheckerTest.java | 44 +++++++++++++++++++ .../androidaps/queue/CommandQueueTest.java | 4 +- 26 files changed, 144 insertions(+), 108 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 11b8952519..a633e42746 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -13,7 +13,7 @@ public class Constants { public static final double defaultDIA = 3d; public static final Double REALLYHIGHBASALRATE = 1111111d; - public static final Integer basalPercentOnlyForCheckLimit = 10101010; + public static final Integer REALLYHIGHPERCENTBASALRATE = 1111111; public static final double bolusOnlyForCheckLimit = 10101010d; public static final Integer carbsOnlyForCheckLimit = 10101010; 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 4063afb75b..94e8293a05 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -112,15 +112,14 @@ public class ConstraintChecker implements ConstraintsInterface { } @Override - public Integer applyBasalPercentConstraints(Integer percentRate) { - Integer rateAfterConstrain = percentRate; + public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - rateAfterConstrain = Math.min(constrain.applyBasalPercentConstraints(percentRate), rateAfterConstrain); + constrain.applyBasalPercentConstraints(percentRate, profile); } - return rateAfterConstrain; + return percentRate; } @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 d4c1d310a4..aada1f89ff 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -19,7 +19,7 @@ public interface ConstraintsInterface { Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile); - Integer applyBasalPercentConstraints(Integer percentRate); + Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile); Double applyBolusConstraints(Double insulin); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index 1a19fdf6a6..10d5c58655 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -36,7 +36,7 @@ public interface PumpInterface { PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo); void stopBolusDelivering(); PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew); - PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew); + PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew); PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes); //some pumps might set a very short temp close to 100% as cancelling a temp can be noisy //when the cancel request is requested by the user (forced), the pump should always do a real cancel diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java index b9feb77679..fb00879ae4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewTempBasalDialog.java @@ -125,7 +125,7 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi String confirmMessage = getString(R.string.setbasalquestion); if (setAsPercent) { int basalPercentInput = SafeParse.stringToInt(basalPercent.getText()); - percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(basalPercentInput); + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(basalPercentInput), profile).value(); confirmMessage += "\n" + percent + "% "; confirmMessage += "\n" + getString(R.string.duration) + " " + durationInMinutes + "min ?"; if (percent != basalPercentInput) @@ -162,7 +162,7 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi } }; if (setAsPercent) { - ConfigBuilderPlugin.getCommandQueue().tempBasalPercent(finalBasalPercent, finalDurationInMinutes, true, callback); + ConfigBuilderPlugin.getCommandQueue().tempBasalPercent(finalBasalPercent, finalDurationInMinutes, true, profile, callback); } else { ConfigBuilderPlugin.getCommandQueue().tempBasalAbsolute(finalBasal, finalDurationInMinutes, true, profile, callback); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index 26ba4c3c21..fbe38d45c7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -303,7 +303,9 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }; - Integer maxPercent = MainApp.getConstraintChecker().applyBasalPercentConstraints(Constants.basalPercentOnlyForCheckLimit); + Integer maxPercent = 200; + if (profile != null) + maxPercent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE), profile).value(); editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput); editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true, percentTextWatcher); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 0b886f8332..f93e67fd50 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -696,9 +696,9 @@ public class ConfigBuilderPlugin implements PluginBase, TreatmentsInterface { return null; } - public void disconnectPump(int durationInMinutes) { + public void disconnectPump(int durationInMinutes, Profile profile) { getActiveLoop().disconnectTo(System.currentTimeMillis() + durationInMinutes * 60 * 1000L); - getCommandQueue().tempBasalPercent(0, durationInMinutes, true, new Callback() { + getCommandQueue().tempBasalPercent(0, durationInMinutes, true, profile, new Callback() { @Override public void run() { if (!result.success) { 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 c5cac6bfbb..10e61545be 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 @@ -351,7 +351,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public Integer applyBasalPercentConstraints(Integer percentRate) { + public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { return percentRate; } 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 f11432713b..4759b11ad7 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 @@ -14,6 +14,7 @@ import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.HardLimits; import info.nightscout.utils.Round; import info.nightscout.utils.SP; @@ -154,49 +155,25 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public Integer applyBasalPercentConstraints(Integer percentRate) { - Integer origPercentRate = percentRate; - Double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); + public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - Profile profile = MainApp.getConfigBuilder().getProfile(); - if (profile == null) return percentRate; Double currentBasal = profile.getBasal(); + Double absoluteRate = currentBasal * ((double) percentRate.originalValue() / 100); - Double absoluteRate = currentBasal * ((double) percentRate / 100); + percentRate.reason("Percent rate " + percentRate.originalValue() + "% recalculated to " + DecimalFormatter.to2Decimal(absoluteRate) + " U/h with current basal " + DecimalFormatter.to2Decimal(currentBasal) + " U/h"); - if (Config.logConstraintsChanges) - log.debug("Percent rate " + percentRate + "% recalculated to " + absoluteRate + "U/h with current basal " + currentBasal + "U/h"); + Constraint absoluteConstraint = new Constraint<>(absoluteRate); + applyBasalConstraints(absoluteConstraint, profile); - if (absoluteRate < 0) absoluteRate = 0d; - Double maxBasalMult = SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d); - Integer maxBasalFromDaily = SP.getInt(R.string.key_openapsama_max_daily_safety_multiplier, 3); - // Check percentRate but absolute rate too, because we know real current basal in pump - Double origRate = absoluteRate; - if (absoluteRate > maxBasal) { - absoluteRate = maxBasal; - if (Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit)) - log.debug("Limiting rate " + origRate + " by maxBasal preference to " + absoluteRate + "U/h"); - } - if (absoluteRate > maxBasalMult * profile.getBasal()) { - absoluteRate = Math.floor(maxBasalMult * profile.getBasal() * 100) / 100; - if (Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit)) - log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h"); - } - if (absoluteRate > profile.getMaxDailyBasal() * maxBasalFromDaily) { - absoluteRate = profile.getMaxDailyBasal() * maxBasalFromDaily; - if (Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit)) - log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h"); - } - - Integer percentRateAfterConst = new Double(absoluteRate / currentBasal * 100).intValue(); + Integer percentRateAfterConst = Double.valueOf(absoluteConstraint.value() / currentBasal * 100).intValue(); if (percentRateAfterConst < 100) percentRateAfterConst = Round.ceilTo((double) percentRateAfterConst, 10d).intValue(); else percentRateAfterConst = Round.floorTo((double) percentRateAfterConst, 10d).intValue(); - if (Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit)) - log.debug("Recalculated percent rate " + percentRate + "% to " + percentRateAfterConst + "%"); - return percentRateAfterConst; + percentRate.set(percentRateAfterConst, String.format(MainApp.gs(R.string.limitingpercentrate), percentRateAfterConst, MainApp.gs(R.string.pumplimit))); + + return percentRate; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index eca35fba08..6b44886a6d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -296,7 +296,9 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com return; } okClicked = true; - if (calculatedTotalInsulin > 0d || calculatedCarbs > 0d) { + final Profile profile = MainApp.getConfigBuilder().getProfile(); + + if (profile != null && (calculatedTotalInsulin > 0d || calculatedCarbs > 0d)) { DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); String confirmMessage = getString(R.string.entertreatmentquestion); @@ -341,7 +343,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com activeloop.superBolusTo(System.currentTimeMillis() + 2 * 60L * 60 * 1000); MainApp.bus().post(new EventRefreshOverview("WizardDialog")); } - ConfigBuilderPlugin.getCommandQueue().tempBasalPercent(0, 120, true, new Callback() { + ConfigBuilderPlugin.getCommandQueue().tempBasalPercent(0, 120, true, profile, new Callback() { @Override public void run() { if (!result.success) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index aa4c581479..bbad3d1833 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -440,7 +440,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, if (v == apsModeView) { final LoopPlugin activeloop = ConfigBuilderPlugin.getActiveLoop(); final PumpDescription pumpDescription = ConfigBuilderPlugin.getActivePump().getPumpDescription(); - if (activeloop == null) + if (activeloop == null || !MainApp.getConfigBuilder().isProfileValid("ContexMenuCreation")) return; menu.setHeaderTitle(MainApp.sResources.getString(R.string.loop)); if (activeloop.isEnabled(PluginBase.LOOP)) { @@ -474,6 +474,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, @Override public boolean onContextItemSelected(MenuItem item) { + final Profile profile = MainApp.getConfigBuilder().getProfile(); + if (profile == null) + return true; final LoopPlugin activeloop = ConfigBuilderPlugin.getActiveLoop(); if (item.getTitle().equals(MainApp.sResources.getString(R.string.disableloop))) { activeloop.setFragmentEnabled(PluginBase.LOOP, false); @@ -527,23 +530,23 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, updateGUI("suspendmenu"); return true; } else if (item.getTitle().equals(MainApp.sResources.getString(R.string.disconnectpumpfor15m))) { - MainApp.getConfigBuilder().disconnectPump(15); + MainApp.getConfigBuilder().disconnectPump(15, profile); updateGUI("suspendmenu"); return true; } else if (item.getTitle().equals(MainApp.sResources.getString(R.string.disconnectpumpfor30m))) { - MainApp.getConfigBuilder().disconnectPump(30); + MainApp.getConfigBuilder().disconnectPump(30, profile); updateGUI("suspendmenu"); return true; } else if (item.getTitle().equals(MainApp.sResources.getString(R.string.disconnectpumpfor1h))) { - MainApp.getConfigBuilder().disconnectPump(60); + MainApp.getConfigBuilder().disconnectPump(60, profile); updateGUI("suspendmenu"); return true; } else if (item.getTitle().equals(MainApp.sResources.getString(R.string.disconnectpumpfor2h))) { - MainApp.getConfigBuilder().disconnectPump(120); + MainApp.getConfigBuilder().disconnectPump(120, profile); updateGUI("suspendmenu"); return true; } else if (item.getTitle().equals(MainApp.sResources.getString(R.string.disconnectpumpfor3h))) { - MainApp.getConfigBuilder().disconnectPump(180); + MainApp.getConfigBuilder().disconnectPump(180, profile); updateGUI("suspendmenu"); return true; } else if (item.getTitle().equals(MainApp.sResources.getString(R.string.careportal_profileswitch))) { @@ -761,7 +764,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, activeloop.superBolusTo(System.currentTimeMillis() + 2 * 60L * 60 * 1000); MainApp.bus().post(new EventRefreshOverview("WizardDialog")); } - ConfigBuilderPlugin.getCommandQueue().tempBasalPercent(0, 120, true, new Callback() { + ConfigBuilderPlugin.getCommandQueue().tempBasalPercent(0, 120, true, profile, new Callback() { @Override public void run() { if (!result.success) { 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 a4eafb3a6f..aaa5c8e165 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 @@ -764,7 +764,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf * is or isn't running at the moment */ @Override - public PumpEnactResult setTempBasalPercent(Integer percent, final Integer durationInMinutes, boolean forceNew) { + public PumpEnactResult setTempBasalPercent(Integer percent, final Integer durationInMinutes, Profile profile, boolean forceNew) { return setTempBasalPercent(percent, durationInMinutes); } @@ -1458,7 +1458,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public Integer applyBasalPercentConstraints(Integer percentRate) { + public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { return percentRate; } 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 8dc5c2b8b4..c236ee6716 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 @@ -214,9 +214,9 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, } @Override - public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value(); if (percent < 0) { result.isTempCancel = false; result.enacted = false; @@ -228,7 +228,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, if (percent > getPumpDescription().maxTempPercent) percent = getPumpDescription().maxTempPercent; long now = System.currentTimeMillis(); - TemporaryBasal runningTB = MainApp.getConfigBuilder().getRealTempBasalFromHistory(now); + TemporaryBasal runningTB = MainApp.getConfigBuilder().getRealTempBasalFromHistory(now); if (runningTB != null && runningTB.percentRate == percent && !enforceNew) { result.enacted = false; result.success = true; @@ -291,7 +291,8 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, result.isTempCancel = false; result.duration = pump.extendedBolusRemainingMinutes; result.absolute = pump.extendedBolusAbsoluteRate; - if (! SP.getBoolean("danar_useextended", false)) result.bolusDelivered = pump.extendedBolusAmount; + if (!SP.getBoolean("danar_useextended", false)) + result.bolusDelivered = pump.extendedBolusAmount; result.isPercent = false; if (Config.logPumpActions) log.debug("setExtendedBolus: OK"); @@ -465,19 +466,14 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, return absoluteRate; } - @SuppressWarnings("PointlessBooleanExpression") @Override - public Integer applyBasalPercentConstraints(Integer percentRate) { - Integer origPercentRate = percentRate; - if (percentRate < 0) percentRate = 0; - if (percentRate > getPumpDescription().maxTempPercent) - percentRate = getPumpDescription().maxTempPercent; - if (!Objects.equals(percentRate, origPercentRate) && Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit)) - log.debug("Limiting percent rate " + origPercentRate + "% to " + percentRate + "%"); + public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue))); + percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit))); + return percentRate; } - @SuppressWarnings("PointlessBooleanExpression") @Override public Double applyBolusConstraints(Double insulin) { double origInsulin = insulin; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java index 224679a23a..ee87fd7f25 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java @@ -249,7 +249,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { // Convert duration from minutes to hours if (Config.logPumpActions) log.debug("setTempBasalAbsolute: Setting temp basal " + percentRate + "% for " + durationInMinutes + " mins (doLowTemp || doHighTemp)"); - return setTempBasalPercent(percentRate, durationInMinutes, false); + return setTempBasalPercent(percentRate, durationInMinutes, profile, false); } if (doExtendedTemp) { // Check if some temp is already in progress diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java index fd4f3d960f..bc07486bff 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java @@ -250,7 +250,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { // Convert duration from minutes to hours if (Config.logPumpActions) log.debug("setTempBasalAbsolute: Setting temp basal " + percentRate + "% for " + durationInMinutes + " mins (doLowTemp || doHighTemp)"); - return setTempBasalPercent(percentRate, durationInMinutes, false); + return setTempBasalPercent(percentRate, durationInMinutes, profile,false); } if (doExtendedTemp) { // Check if some temp is already in progress 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 214d002a4e..93148349af 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 @@ -306,16 +306,14 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public Integer applyBasalPercentConstraints(Integer percentRate) { - Integer origPercentRate = percentRate; - if (percentRate < 0) percentRate = 0; - if (percentRate > getPumpDescription().maxTempPercent) - percentRate = getPumpDescription().maxTempPercent; - if (!Objects.equals(percentRate, origPercentRate) && Config.logConstraintsChanges && !Objects.equals(origPercentRate, Constants.basalPercentOnlyForCheckLimit)) - log.debug("Limiting percent rate " + origPercentRate + "% to " + percentRate + "%"); + public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue))); + percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit))); + return percentRate; } + @Override public Double applyBolusConstraints(Double insulin) { double origInsulin = insulin; @@ -586,9 +584,9 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public synchronized PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { + public synchronized PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value(); if (percent < 0) { result.isTempCancel = false; result.enacted = false; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java index 044cec18a0..b03901ad46 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java @@ -262,9 +262,9 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { } @Override - public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); - percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(percent); + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value(); if (percent < 0) { result.isTempCancel = false; result.enacted = false; 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 fcf8d42094..ea114d1da8 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 @@ -22,11 +22,11 @@ import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress; @@ -65,13 +65,12 @@ import static info.nightscout.androidaps.plugins.PumpInsight.history.PumpIdCache /** * Created by jamorham on 23/01/2018. - * + *

* Connects to SightRemote app service using SightParser library - * + *

* SightRemote and SightParser created by Tebbe Ubben - * + *

* Original proof of concept SightProxy by jamorham - * */ @SuppressWarnings("AccessStaticViaInstance") @@ -394,7 +393,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints for (int i = 0; i < profile.getBasalValues().length; i++) { Profile.BasalValue basalValue = profile.getBasalValues()[i]; Profile.BasalValue nextValue = null; - if (profile.getBasalValues().length > i + 1) nextValue = profile.getBasalValues()[i + 1]; + if (profile.getBasalValues().length > i + 1) + nextValue = profile.getBasalValues()[i + 1]; profileBlocks.add(new BRProfileBlock.ProfileBlock((((nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) / 60), Helpers.roundDouble(basalValue.value, 2))); log("setNewBasalProfile: " + basalValue.value + " for " + Integer.toString(((nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) / 60)); } @@ -424,12 +424,15 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints BRProfileBlock.ProfileBlock profileBlock = profileBlocks.get(i); Profile.BasalValue basalValue = profile.getBasalValues()[i]; Profile.BasalValue nextValue = null; - if (profile.getBasalValues().length > i + 1) nextValue = profile.getBasalValues()[i + 1]; + if (profile.getBasalValues().length > i + 1) + nextValue = profile.getBasalValues()[i + 1]; log("isThisProfileSet - Comparing block: Pump: " + profileBlock.getAmount() + " for " + profileBlock.getDuration() + " Profile: " + basalValue.value + " for " + Integer.toString(((nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) / 60)); - if (profileBlock.getDuration() * 60 != (nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) return false; + if (profileBlock.getDuration() * 60 != (nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) + return false; //Allow a little imprecision due to rounding errors - if (Math.abs(profileBlock.getAmount() - Helpers.roundDouble(basalValue.value, 2)) >= 0.01D) return false; + if (Math.abs(profileBlock.getAmount() - Helpers.roundDouble(basalValue.value, 2)) >= 0.01D) + return false; } return true; } @@ -484,7 +487,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints Treatment t = new Treatment(); t.isSMB = detailedBolusInfo.isSMB; final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.t = t; + bolusingEvent.t = t; bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), 0F); bolusingEvent.bolusId = bolusId; bolusingEvent.percent = 0; @@ -516,9 +519,12 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); ActiveBolusesMessage activeBolusesMessage = (ActiveBolusesMessage) mstatus.getResponseObject(); ActiveBolus activeBolus = null; - if (activeBolusesMessage.getBolus1() != null && activeBolusesMessage.getBolus1().getBolusID() == bolusingEvent.bolusId) activeBolus = activeBolusesMessage.getBolus1(); - else if (activeBolusesMessage.getBolus2() != null && activeBolusesMessage.getBolus2().getBolusID() == bolusingEvent.bolusId) activeBolus = activeBolusesMessage.getBolus2(); - else if (activeBolusesMessage.getBolus3() != null && activeBolusesMessage.getBolus3().getBolusID() == bolusingEvent.bolusId) activeBolus = activeBolusesMessage.getBolus3(); + if (activeBolusesMessage.getBolus1() != null && activeBolusesMessage.getBolus1().getBolusID() == bolusingEvent.bolusId) + activeBolus = activeBolusesMessage.getBolus1(); + else if (activeBolusesMessage.getBolus2() != null && activeBolusesMessage.getBolus2().getBolusID() == bolusingEvent.bolusId) + activeBolus = activeBolusesMessage.getBolus2(); + else if (activeBolusesMessage.getBolus3() != null && activeBolusesMessage.getBolus3().getBolusID() == bolusingEvent.bolusId) + activeBolus = activeBolusesMessage.getBolus3(); if (activeBolus == null) break; else { bolusingEvent.percent = (int) (100D / activeBolus.getInitialAmount() * (activeBolus.getInitialAmount() - activeBolus.getLeftoverAmount())); @@ -566,7 +572,6 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints if (percent_amount > 250) percent_amount = 250; - final SetTBRTaskRunner task = new SetTBRTaskRunner(connector.getServiceConnector(), percent_amount, durationInMinutes); final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m"); @@ -611,7 +616,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints @Override - public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { log("Set TBR %"); percent = (int) Math.round(((double) percent) / 10d) * 10; @@ -1117,8 +1122,11 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } @Override - public Integer applyBasalPercentConstraints(Integer percentRate) { - return Math.min(percentRate, pumpDescription.maxTempPercent); + public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue))); + percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit))); + + return percentRate; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java index f22a0b3a0e..629ffe0d49 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java @@ -204,7 +204,7 @@ public class MDIPlugin implements PluginBase, PumpInterface { } @Override - public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { PumpEnactResult result = new PumpEnactResult(); result.success = false; result.comment = MainApp.instance().getString(R.string.pumperror); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java index 927618b539..debda40e0d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java @@ -313,7 +313,7 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { } @Override - public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) { + public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { TreatmentsInterface treatmentsInterface = MainApp.getConfigBuilder(); PumpEnactResult result = new PumpEnactResult(); if (MainApp.getConfigBuilder().isTempBasalInProgress()) { diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index d9b1d98cfb..e3a3f511a5 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -208,7 +208,7 @@ public class CommandQueue { } // returns true if command is queued - public boolean tempBasalPercent(int percent, int durationInMinutes, boolean enforceNew, Callback callback) { + public boolean tempBasalPercent(Integer percent, int durationInMinutes, boolean enforceNew, Profile profile, Callback callback) { if (isRunning(Command.CommandType.TEMPBASAL)) { if (callback != null) callback.result(executingNowError()).run(); @@ -218,10 +218,10 @@ public class CommandQueue { // remove all unfinished removeAll(Command.CommandType.TEMPBASAL); - Integer percentAfterConstraints = MainApp.getConstraintChecker().applyBasalPercentConstraints(percent); + Integer percentAfterConstraints = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(percent), profile).value(); // add new command to queue - add(new CommandTempBasalPercent(percentAfterConstraints, durationInMinutes, enforceNew, callback)); + add(new CommandTempBasalPercent(percentAfterConstraints, durationInMinutes, enforceNew, profile, callback)); notifyAboutNewCommand(); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandTempBasalPercent.java b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandTempBasalPercent.java index 15f8849cd2..bbb421e128 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandTempBasalPercent.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandTempBasalPercent.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.queue.Callback; @@ -18,18 +19,20 @@ public class CommandTempBasalPercent extends Command { int durationInMinutes; int percent; boolean enforceNew; + Profile profile; - public CommandTempBasalPercent(int percent, int durationInMinutes, boolean enforceNew, Callback callback) { + public CommandTempBasalPercent(int percent, int durationInMinutes, boolean enforceNew, Profile profile, Callback callback) { commandType = CommandType.TEMPBASAL; this.percent = percent; this.durationInMinutes = durationInMinutes; this.enforceNew = enforceNew; + this.profile = profile; this.callback = callback; } @Override public void execute() { - PumpEnactResult r = ConfigBuilderPlugin.getActivePump().setTempBasalPercent(percent, durationInMinutes, enforceNew); + PumpEnactResult r = ConfigBuilderPlugin.getActivePump().setTempBasalPercent(percent, durationInMinutes, profile, enforceNew); if (Config.logCongigBuilderActions) log.debug("setTempBasalPercent percent: " + percent + " durationInMinutes: " + durationInMinutes + " success: " + r.success + " enacted: " + r.enacted); if (callback != null) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 536da84e9d..dd5b01b6e5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -978,5 +978,6 @@ openapsma_max_iob A bolus was delivered within the last 3 minutes, skipping SMB Basal set correctly + Limiting percent rate to %d%% because of %s diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java index e21e09cfb4..36953e9922 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java @@ -38,5 +38,6 @@ public class ConstraintTest { d.setIfSmaller(4d, "Set 4d"); Assert.assertEquals(4d, d.value()); Assert.assertEquals("Set 5d\nSet 6d\nSet 4d", d.getReasons()); + Assert.assertEquals(10d, d.originalValue()); } } 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 7040e2d9d0..a3360333b6 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -215,6 +215,48 @@ public class ConstraintsCheckerTest { } + // applyBasalConstraints tests + @Test + public void percentBasalRateShouldBeLimited() throws Exception { + // DanaR, RS + danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + DanaRPump.getInstance().maxBasal = 0.8d; + + // Insight + insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + StatusTaskRunner.Result result = new StatusTaskRunner.Result(); + result.maximumBasalAmount = 1.1d; + insightPlugin.setStatusResult(result); + + + // No limit by default + when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); + when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d); + when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d); + + // Negative basal not allowed + Constraint i = new Constraint<>(-22); + constraintChecker.applyBasalPercentConstraints(i, profile); + Assert.assertEquals((Integer)0, i.value()); + Assert.assertEquals("Percent rate -22% recalculated to -0.22 U/h with current basal 1.00 U/h\n" + // SafetyPlugin + "Limiting percent rate to 0% because of pump limit\n" + // SafetyPlugin + "Limiting percent rate to 0% because of basal must be positive value\n" + // DanaRPlugin + "Limiting percent rate to 0% because of basal must be positive value\n" + // DanaRSPlugin + "Limiting percent rate to 0% because of basal must be positive value", i.getReasons()); // InsightPlugin + + // Apply all limits + i = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); + constraintChecker.applyBasalPercentConstraints(i, profile); + Assert.assertEquals((Integer)100, i.value()); + Assert.assertEquals("Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h\n" + // SafetyPlugin + "Limiting percent rate to 100% because of pump limit\n" + + "Limiting percent rate to 200% because of pump limit\n" + + "Limiting percent rate to 200% because of pump limit\n" + + "Limiting percent rate to 250% because of pump limit", i.getReasons()); + + } + @Before public void prepareMock() throws Exception { Locale.setDefault(new Locale("en", "US")); @@ -247,6 +289,8 @@ public class ConstraintsCheckerTest { when(MainApp.gs(R.string.maxbasalinpreferences)).thenReturn("max basal settings in preferences"); when(MainApp.gs(R.string.maxbasalmultiplier)).thenReturn("max basal multiplier"); when(MainApp.gs(R.string.maxdailybasalmultiplier)).thenReturn("max daily basal multiplier"); + when(MainApp.gs(R.string.limitingpercentrate)).thenReturn("Limiting percent rate to %d%% because of %s"); + when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); PowerMockito.mockStatic(SP.class); // RS constructor diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java index 14892464d4..b36cd0b0aa 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java @@ -73,7 +73,7 @@ public class CommandQueueTest extends CommandQueue { Assert.assertEquals(1, size()); // add tempbasal percent. it should replace previous TEMPBASAL - tempBasalPercent(0, 30, true, null); + tempBasalPercent(0, 30, true, profile, null); Assert.assertEquals(1, size()); // add extended bolus @@ -129,6 +129,8 @@ public class CommandQueueTest extends CommandQueue { when(MainApp.getConstraintChecker().applyCarbsConstraints(carbs)).thenReturn(carbs); Constraint rateConstraint = new Constraint<>(0d); when(MainApp.getConstraintChecker().applyBasalConstraints(any(), any())).thenReturn(rateConstraint); + Constraint percentageConstraint = new Constraint<>(0); + when(MainApp.getConstraintChecker().applyBasalPercentConstraints(any(), any())).thenReturn(percentageConstraint); PowerMockito.mockStatic(ToastUtils.class); Context context = mock(Context.class); From c68ab9f1ca6c0be573cfacbd4c1db8684d68e951 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 21 Mar 2018 22:06:28 +0100 Subject: [PATCH 16/28] applyBasalPercentConstraints refactor & tests part2 --- .../androidaps/interfaces/Constraint.java | 25 ++++++++---- .../ObjectivesPlugin.java | 10 ++--- .../ConstraintsSafety/SafetyPlugin.java | 30 +++++++-------- .../plugins/PumpCombo/ComboPlugin.java | 2 +- .../PumpDanaR/AbstractDanaRPlugin.java | 9 ++--- .../plugins/PumpDanaRS/DanaRSPlugin.java | 9 ++--- .../PumpInsight/InsightPumpPlugin.java | 6 +-- .../androidaps/interfaces/ConstraintTest.java | 20 +++++----- .../interfaces/ConstraintsCheckerTest.java | 38 ++++++++++--------- 9 files changed, 78 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java index a830bcde73..de3d266d7b 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java @@ -37,34 +37,34 @@ public class Constraint { return this; } - public Constraint set(T value, String reason) { + public Constraint set(T value, String reason, Object from) { this.value = value; - reason(reason); + reason(reason, from); return this; } - public Constraint setIfSmaller(T value, String reason) { + public Constraint setIfSmaller(T value, String reason, Object from) { if (value.compareTo(this.value) < 0) { this.value = value; } if (value.compareTo(this.originalValue) < 0) { - reason(reason); + reason(reason, from); } return this; } - public Constraint setIfGreater(T value, String reason) { + public Constraint setIfGreater(T value, String reason, Object from) { if (value.compareTo(this.value) > 0) { this.value = value; } if (value.compareTo(this.originalValue) > 0) { - reason(reason); + reason(reason, from); } return this; } - public Constraint reason(String reason) { - reasons.add(reason); + public Constraint reason(String reason, Object from) { + reasons.add(from.getClass().getSimpleName() + ": " + reason); return this; } @@ -79,4 +79,13 @@ public class Constraint { return sb.toString(); } + public List getReasonList() { + return reasons; + } + + public void copyReasons(Constraint another) { + for (String s: another.getReasonList()) { + reasons.add(s); + } + } } 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 10e61545be..a442f95c09 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 @@ -302,35 +302,35 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { @Override public Constraint isLoopInvokationAllowed(Constraint value) { if (objectives.get(0).started.getTime() == 0) - value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1)); + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1), this); return value; } @Override public Constraint isClosedLoopAllowed(Constraint value) { if (objectives.get(3).started.getTime() == 0) - value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4)); + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4), this); return value; } @Override public Constraint isAutosensModeEnabled(Constraint value) { if (objectives.get(5).started.getTime() == 0) - value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 6)); + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 6), this); return value; } @Override public Constraint isAMAModeEnabled(Constraint value) { if (objectives.get(6).started.getTime() == 0) - value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 7)); + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 7), this); return value; } @Override public Constraint isSMBModeEnabled(Constraint value) { if (objectives.get(7).started.getTime() == 0) - value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 8)); + value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 8), this); return value; } 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 4759b11ad7..0b3f408498 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 @@ -3,16 +3,12 @@ package info.nightscout.androidaps.plugins.ConstraintsSafety; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Objects; - -import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.HardLimits; @@ -99,18 +95,18 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public Constraint isLoopInvokationAllowed(Constraint value) { if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable) - value.set(false, MainApp.gs(R.string.pumpisnottempbasalcapable)); + value.set(false, MainApp.gs(R.string.pumpisnottempbasalcapable), this); return value; } @Override public Constraint isClosedLoopAllowed(Constraint value) { if (!MainApp.isEngineeringModeOrRelease()) - value.set(false, MainApp.gs(R.string.closed_loop_disabled_on_dev_branch)); + value.set(false, MainApp.gs(R.string.closed_loop_disabled_on_dev_branch), this); String mode = SP.getString("aps_mode", "open"); if (!mode.equals("closed")) - value.set(false, MainApp.gs(R.string.closedmodedisabledinpreferences)); + value.set(false, MainApp.gs(R.string.closedmodedisabledinpreferences), this); return value; } @@ -123,7 +119,7 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { public Constraint isAMAModeEnabled(Constraint value) { boolean enabled = SP.getBoolean("openapsama_useautosens", false); if (!enabled) - value.set(false, MainApp.gs(R.string.amadisabledinpreferences)); + value.set(false, MainApp.gs(R.string.amadisabledinpreferences), this); return value; } @@ -131,26 +127,26 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { public Constraint isSMBModeEnabled(Constraint value) { boolean enabled = SP.getBoolean(R.string.key_use_smb, false); if (!enabled) - value.set(false, MainApp.gs(R.string.smbdisabledinpreferences)); + value.set(false, MainApp.gs(R.string.smbdisabledinpreferences), this); return value; } @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { - absoluteRate.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbasalratio), 0d, MainApp.gs(R.string.basalmustbepositivevalue))); + absoluteRate.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbasalratio), 0d, MainApp.gs(R.string.basalmustbepositivevalue)), this); double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); - absoluteRate.setIfSmaller(maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), maxBasal, MainApp.gs(R.string.maxbasalinpreferences))); + absoluteRate.setIfSmaller(maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), maxBasal, MainApp.gs(R.string.maxbasalinpreferences)), this); // Check percentRate but absolute rate too, because we know real current basal in pump Double maxBasalMult = SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d); double maxFromBasalMult = Math.floor(maxBasalMult * profile.getBasal() * 100) / 100; - absoluteRate.setIfSmaller(maxFromBasalMult, String.format(MainApp.gs(R.string.limitingbasalratio), maxFromBasalMult, MainApp.gs(R.string.maxbasalmultiplier))); + absoluteRate.setIfSmaller(maxFromBasalMult, String.format(MainApp.gs(R.string.limitingbasalratio), maxFromBasalMult, MainApp.gs(R.string.maxbasalmultiplier)), this); Double maxBasalFromDaily = SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d); double maxFromDaily = Math.floor(profile.getMaxDailyBasal() * maxBasalFromDaily * 100) / 100; - absoluteRate.setIfSmaller(maxFromDaily, String.format(MainApp.gs(R.string.limitingbasalratio), maxFromDaily, MainApp.gs(R.string.maxdailybasalmultiplier))); + absoluteRate.setIfSmaller(maxFromDaily, String.format(MainApp.gs(R.string.limitingbasalratio), maxFromDaily, MainApp.gs(R.string.maxdailybasalmultiplier)), this); return absoluteRate; } @@ -160,18 +156,18 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { Double currentBasal = profile.getBasal(); Double absoluteRate = currentBasal * ((double) percentRate.originalValue() / 100); - percentRate.reason("Percent rate " + percentRate.originalValue() + "% recalculated to " + DecimalFormatter.to2Decimal(absoluteRate) + " U/h with current basal " + DecimalFormatter.to2Decimal(currentBasal) + " U/h"); + percentRate.reason("Percent rate " + percentRate.originalValue() + "% recalculated to " + DecimalFormatter.to2Decimal(absoluteRate) + " U/h with current basal " + DecimalFormatter.to2Decimal(currentBasal) + " U/h", this); Constraint absoluteConstraint = new Constraint<>(absoluteRate); applyBasalConstraints(absoluteConstraint, profile); - + percentRate.copyReasons(absoluteConstraint); Integer percentRateAfterConst = Double.valueOf(absoluteConstraint.value() / currentBasal * 100).intValue(); if (percentRateAfterConst < 100) percentRateAfterConst = Round.ceilTo((double) percentRateAfterConst, 10d).intValue(); else percentRateAfterConst = Round.floorTo((double) percentRateAfterConst, 10d).intValue(); - percentRate.set(percentRateAfterConst, String.format(MainApp.gs(R.string.limitingpercentrate), percentRateAfterConst, MainApp.gs(R.string.pumplimit))); + percentRate.set(percentRateAfterConst, String.format(MainApp.gs(R.string.limitingpercentrate), percentRateAfterConst, MainApp.gs(R.string.pumplimit)), this); return percentRate; } 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 aaa5c8e165..c0219f110f 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 @@ -1428,7 +1428,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf @Override public Constraint isLoopInvokationAllowed(Constraint value) { if (!validBasalRateProfileSelectedOnPump) - value.set(false, MainApp.gs(R.string.novalidbasalrate)); + value.set(false, MainApp.gs(R.string.novalidbasalrate), this); return value; } 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 c236ee6716..bd5ea1bb7f 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 @@ -7,7 +7,6 @@ import org.json.JSONObject; import org.slf4j.Logger; import java.util.Date; -import java.util.Objects; import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; @@ -19,13 +18,13 @@ import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.DanaRInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; @@ -462,14 +461,14 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { if (pump != null) - absoluteRate.setIfSmaller(pump.maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), pump.maxBasal, MainApp.gs(R.string.pumplimit))); + absoluteRate.setIfSmaller(pump.maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), pump.maxBasal, MainApp.gs(R.string.pumplimit)), this); return absoluteRate; } @Override public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue))); - percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit))); + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue)), this); + percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit)), this); return percentRate; } 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 93148349af..5dd5352335 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 @@ -15,7 +15,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; -import java.util.Objects; import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; @@ -30,13 +29,13 @@ import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventAppExit; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.DanaRInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; @@ -301,14 +300,14 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { if (pump != null) - absoluteRate.setIfSmaller(pump.maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), pump.maxBasal, MainApp.gs(R.string.pumplimit))); + absoluteRate.setIfSmaller(pump.maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), pump.maxBasal, MainApp.gs(R.string.pumplimit)), this); return absoluteRate; } @Override public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue))); - percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit))); + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue)), this); + percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit)), this); return percentRate; } 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 ea114d1da8..d3dae07d01 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 @@ -1116,15 +1116,15 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { if (statusResult != null) { - absoluteRate.setIfSmaller(statusResult.maximumBasalAmount, String.format(MainApp.gs(R.string.limitingbasalratio), statusResult.maximumBasalAmount, MainApp.gs(R.string.pumplimit))); + absoluteRate.setIfSmaller(statusResult.maximumBasalAmount, String.format(MainApp.gs(R.string.limitingbasalratio), statusResult.maximumBasalAmount, MainApp.gs(R.string.pumplimit)), this); } return absoluteRate; } @Override public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue))); - percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit))); + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue)), this); + percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit)), this); return percentRate; } diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java index 36953e9922..01c3a927f7 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java @@ -21,23 +21,23 @@ public class ConstraintTest { b.set(false); Assert.assertEquals(Boolean.FALSE, b.value()); Assert.assertEquals("", b.getReasons()); - b.set(true, "Set true"); + b.set(true, "Set true", this); Assert.assertEquals(Boolean.TRUE, b.value()); - Assert.assertEquals("Set true", b.getReasons()); - b.set(false, "Set false"); + Assert.assertEquals("ConstraintTest: Set true", b.getReasons()); + b.set(false, "Set false", this); Assert.assertEquals(Boolean.FALSE, b.value()); - Assert.assertEquals("Set true\nSet false", b.getReasons()); + Assert.assertEquals("ConstraintTest: Set true\nConstraintTest: Set false", b.getReasons()); Constraint d = new Constraint<>(10d); - d.set(5d, "Set 5d"); + d.set(5d, "Set 5d", this); Assert.assertEquals(5d, d.value()); - Assert.assertEquals("Set 5d", d.getReasons()); - d.setIfSmaller(6d, "Set 6d"); + Assert.assertEquals("ConstraintTest: Set 5d", d.getReasons()); + d.setIfSmaller(6d, "Set 6d", this); Assert.assertEquals(5d, d.value()); - Assert.assertEquals("Set 5d\nSet 6d", d.getReasons()); - d.setIfSmaller(4d, "Set 4d"); + Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d", d.getReasons()); + d.setIfSmaller(4d, "Set 4d", this); Assert.assertEquals(4d, d.value()); - Assert.assertEquals("Set 5d\nSet 6d\nSet 4d", d.getReasons()); + Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d\nConstraintTest: Set 4d", d.getReasons()); Assert.assertEquals(10d, d.originalValue()); } } 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 a3360333b6..46daf30a79 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -200,18 +200,18 @@ public class ConstraintsCheckerTest { Constraint d = new Constraint<>(-0.5d); constraintChecker.applyBasalConstraints(d, profile); Assert.assertEquals(0d, d.value()); - Assert.assertEquals("Limiting basal rate to 0.00 U/h because of basal must be positive value", d.getReasons()); + Assert.assertEquals("SafetyPlugin: Limiting basal rate to 0.00 U/h because of basal must be positive value", d.getReasons()); // Apply all limits d = new Constraint<>(Constants.REALLYHIGHBASALRATE); constraintChecker.applyBasalConstraints(d, profile); Assert.assertEquals(0.8d, d.value()); - Assert.assertEquals("Limiting basal rate to 1.00 U/h because of max basal settings in preferences\n" + - "Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + - "Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + - "Limiting basal rate to 0.80 U/h because of pump limit\n" + - "Limiting basal rate to 0.80 U/h because of pump limit\n" + - "Limiting basal rate to 1.10 U/h because of pump limit", d.getReasons()); + Assert.assertEquals("SafetyPlugin: Limiting basal rate to 1.00 U/h because of max basal settings in preferences\n" + + "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + + "SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + + "DanaRPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" + + "DanaRSPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" + + "InsightPumpPlugin: Limiting basal rate to 1.10 U/h because of pump limit", d.getReasons()); } @@ -239,21 +239,25 @@ public class ConstraintsCheckerTest { Constraint i = new Constraint<>(-22); constraintChecker.applyBasalPercentConstraints(i, profile); Assert.assertEquals((Integer)0, i.value()); - Assert.assertEquals("Percent rate -22% recalculated to -0.22 U/h with current basal 1.00 U/h\n" + // SafetyPlugin - "Limiting percent rate to 0% because of pump limit\n" + // SafetyPlugin - "Limiting percent rate to 0% because of basal must be positive value\n" + // DanaRPlugin - "Limiting percent rate to 0% because of basal must be positive value\n" + // DanaRSPlugin - "Limiting percent rate to 0% because of basal must be positive value", i.getReasons()); // InsightPlugin + Assert.assertEquals("SafetyPlugin: Percent rate -22% recalculated to -0.22 U/h with current basal 1.00 U/h\n" + + "SafetyPlugin: Limiting basal rate to 0.00 U/h because of basal must be positive value\n" + + "SafetyPlugin: Limiting percent rate to 0% because of pump limit\n" + + "DanaRPlugin: Limiting percent rate to 0% because of basal must be positive value\n" + + "DanaRSPlugin: Limiting percent rate to 0% because of basal must be positive value\n" + + "InsightPumpPlugin: Limiting percent rate to 0% because of basal must be positive value", i.getReasons()); // InsightPlugin // Apply all limits i = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); constraintChecker.applyBasalPercentConstraints(i, profile); Assert.assertEquals((Integer)100, i.value()); - Assert.assertEquals("Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h\n" + // SafetyPlugin - "Limiting percent rate to 100% because of pump limit\n" + - "Limiting percent rate to 200% because of pump limit\n" + - "Limiting percent rate to 200% because of pump limit\n" + - "Limiting percent rate to 250% because of pump limit", i.getReasons()); + Assert.assertEquals("SafetyPlugin: Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h\n" + + "SafetyPlugin: Limiting basal rate to 1.00 U/h because of max basal settings in preferences\n" + + "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + + "SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + + "SafetyPlugin: Limiting percent rate to 100% because of pump limit\n" + + "DanaRPlugin: Limiting percent rate to 200% because of pump limit\n" + + "DanaRSPlugin: Limiting percent rate to 200% because of pump limit\n" + + "InsightPumpPlugin: Limiting percent rate to 250% because of pump limit", i.getReasons()); } From 15158fb5ea639d8740ad83c2ffbb029c8ee06c5f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 21 Mar 2018 23:01:30 +0100 Subject: [PATCH 17/28] applyBolusConstraints refactor & tests --- .../info/nightscout/androidaps/Constants.java | 2 +- .../androidaps/data/ConstraintChecker.java | 7 ++- .../interfaces/ConstraintsInterface.java | 2 +- .../plugins/Actions/dialogs/FillDialog.java | 5 ++- .../dialogs/NewExtendedBolusDialog.java | 5 ++- .../Dialogs/NewNSTreatmentDialog.java | 2 +- .../ObjectivesPlugin.java | 2 +- .../ConstraintsSafety/SafetyPlugin.java | 15 +++---- .../androidaps/plugins/Loop/LoopPlugin.java | 3 +- .../Overview/Dialogs/NewInsulinDialog.java | 5 ++- .../Overview/Dialogs/NewTreatmentDialog.java | 5 ++- .../Overview/Dialogs/WizardDialog.java | 7 +-- .../plugins/Overview/OverviewFragment.java | 2 +- .../plugins/PumpCombo/ComboPlugin.java | 2 +- .../PumpDanaR/AbstractDanaRPlugin.java | 14 ++---- .../plugins/PumpDanaR/DanaRPlugin.java | 2 +- .../plugins/PumpDanaR/comm/MsgBolusStart.java | 5 +-- .../comm/MsgBolusStartWithSpeed.java | 5 +-- .../comm/MsgSetExtendedBolusStart.java | 5 +-- .../PumpDanaRKorean/DanaRKoreanPlugin.java | 2 +- .../plugins/PumpDanaRS/DanaRSPlugin.java | 16 +++---- ...aRS_Packet_Bolus_Set_Step_Bolus_Start.java | 5 +-- .../plugins/PumpDanaRv2/DanaRv2Plugin.java | 2 +- .../PumpInsight/InsightPumpPlugin.java | 6 ++- .../SmsCommunicatorPlugin.java | 2 +- .../plugins/Wear/ActionStringHandler.java | 11 ++--- .../androidaps/queue/CommandQueue.java | 4 +- .../info/nightscout/utils/HardLimits.java | 12 ++--- app/src/main/res/values/strings.xml | 5 +++ app/src/main/res/xml/pref_safety.xml | 2 +- .../interfaces/ConstraintsCheckerTest.java | 44 ++++++++++++++++++- .../androidaps/queue/CommandQueueTest.java | 3 +- 32 files changed, 123 insertions(+), 86 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index a633e42746..7cddc58452 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -14,7 +14,7 @@ public class Constants { public static final Double REALLYHIGHBASALRATE = 1111111d; public static final Integer REALLYHIGHPERCENTBASALRATE = 1111111; - public static final double bolusOnlyForCheckLimit = 10101010d; + public static final double REALLYHIGHBOLUS = 1111111d; public static final Integer carbsOnlyForCheckLimit = 10101010; public static final Integer notificationID = 556677; 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 94e8293a05..5e27373d13 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -123,15 +123,14 @@ public class ConstraintChecker implements ConstraintsInterface { } @Override - public Double applyBolusConstraints(Double insulin) { - Double insulinAfterConstrain = insulin; + public Constraint applyBolusConstraints(Constraint insulin) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - insulinAfterConstrain = Math.min(constrain.applyBolusConstraints(insulin), insulinAfterConstrain); + constrain.applyBolusConstraints(insulin); } - return insulinAfterConstrain; + return insulin; } @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 aada1f89ff..96af468aef 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -21,7 +21,7 @@ public interface ConstraintsInterface { Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile); - Double applyBolusConstraints(Double insulin); + Constraint applyBolusConstraints(Constraint insulin); Integer applyCarbsConstraints(Integer carbs); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java index b50e4f01a2..97bef4c88e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -26,6 +26,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.db.Source; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity; import info.nightscout.androidaps.queue.Callback; @@ -60,7 +61,7 @@ public class FillDialog extends DialogFragment implements OnClickListener { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); double bolusstep = ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep; editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_insulinamount); editInsulin.setParams(0d, 0d, maxInsulin, bolusstep, new DecimalFormat("0.00"), false); @@ -138,7 +139,7 @@ public class FillDialog extends DialogFragment implements OnClickListener { String confirmMessage = getString(R.string.fillwarning) + "\n"; - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); confirmMessage += getString(R.string.bolus) + ": " + insulinAfterConstraints + "U"; if (insulinAfterConstraints - insulin != 0) confirmMessage += "\n" + getString(R.string.constraintapllied); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java index 36da617fc5..db5575fc47 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java @@ -20,6 +20,7 @@ import java.text.DecimalFormat; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity; import info.nightscout.androidaps.queue.Callback; @@ -43,7 +44,7 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli View view = inflater.inflate(R.layout.overview_newextendedbolus_dialog, container, false); - Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); editInsulin = (NumberPicker) view.findViewById(R.id.overview_newextendedbolus_insulin); editInsulin.setParams(0d, 0d, maxInsulin, 0.1d, new DecimalFormat("0.00"), false); @@ -70,7 +71,7 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli String confirmMessage = getString(R.string.setextendedbolusquestion); - Double insulinAfterConstraint = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + Double insulinAfterConstraint = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); confirmMessage += " " + insulinAfterConstraint + " U "; confirmMessage += getString(R.string.duration) + " " + durationInMinutes + "min ?"; if (insulinAfterConstraint - insulin != 0d) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index fbe38d45c7..5b2bacb0ca 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -276,7 +276,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick editCarbs = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbsinput); editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false); - Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); editInsulin = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_insulininput); editInsulin.setParams(0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false); 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 a442f95c09..e203da02df 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 @@ -356,7 +356,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public Double applyBolusConstraints(Double insulin) { + public Constraint applyBolusConstraints(Constraint insulin) { return insulin; } 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 0b3f408498..9ed22e1382 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 @@ -173,16 +173,13 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public Double applyBolusConstraints(Double insulin) { - try { - Double maxBolus = SP.getDouble("treatmentssafety_maxbolus", 3d); + public Constraint applyBolusConstraints(Constraint insulin) { + insulin.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbolus), 0d, MainApp.gs(R.string.bolusmustbepositivevalue)), this); - if (insulin < 0) insulin = 0d; - if (insulin > maxBolus) insulin = maxBolus; - } catch (Exception e) { - insulin = 0d; - } - if (insulin > HardLimits.maxBolus()) insulin = HardLimits.maxBolus(); + Double maxBolus = SP.getDouble(R.string.key_treatmentssafety_maxbolus, 3d); + insulin.setIfSmaller(maxBolus, String.format(MainApp.gs(R.string.limitingbolus), maxBolus, MainApp.gs(R.string.maxvalueinpreferences)), this); + + insulin.setIfSmaller(HardLimits.maxBolus(), String.format(MainApp.gs(R.string.limitingbolus), HardLimits.maxBolus(), MainApp.gs(R.string.hardlimit)), this); return insulin; } 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 5879b28b73..477c2a11c3 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 @@ -296,7 +296,8 @@ public class LoopPlugin implements PluginBase { final APSResult resultAfterConstraints = result.clone(); resultAfterConstraints.rateConstraint = new Constraint<>(resultAfterConstraints.rate); resultAfterConstraints.rate = MainApp.getConstraintChecker().applyBasalConstraints(resultAfterConstraints.rateConstraint, profile).value(); - resultAfterConstraints.smb = MainApp.getConstraintChecker().applyBolusConstraints(resultAfterConstraints.smb); + resultAfterConstraints.smbConstraint = new Constraint<>(resultAfterConstraints.smb); + resultAfterConstraints.smb = MainApp.getConstraintChecker().applyBolusConstraints(resultAfterConstraints.smbConstraint).value(); // safety check for multiple SMBs long lastBolusTime = TreatmentsPlugin.getPlugin().getLastBolusTime(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java index 140719a31e..07eb3289a7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java @@ -44,6 +44,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.utils.DateUtil; @@ -119,7 +120,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newinsulin_amount); @@ -223,7 +224,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, try { Double insulin = SafeParse.stringToDouble(editInsulin.getText()); - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); List actions = new LinkedList<>(); if (insulin > 0) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index 70404a416c..98af776b68 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -31,6 +31,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.Source; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.utils.FabricPrivacy; @@ -96,7 +97,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); - maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); editCarbs = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_carbsamount); editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_insulinamount); @@ -128,7 +129,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene String confirmMessage = MainApp.gs(R.string.entertreatmentquestion) + "
"; - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbs); if (insulin > 0) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index 6b44886a6d..de9adb14e4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -52,6 +52,7 @@ import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.events.EventFeatureRunning; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventRefreshOverview; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; @@ -237,7 +238,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com superbolusCheckbox.setVisibility(SP.getBoolean(R.string.key_usesuperbolus, false) ? View.VISIBLE : View.GONE); Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); - Double maxCorrection = MainApp.getConstraintChecker().applyBolusConstraints(Constants.bolusOnlyForCheckLimit); + Double maxCorrection = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); editBg.setParams(0d, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false, textWatcher); editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false, textWatcher); @@ -303,7 +304,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com String confirmMessage = getString(R.string.entertreatmentquestion); - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(calculatedTotalInsulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(calculatedTotalInsulin)).value(); Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(calculatedCarbs); confirmMessage += "
" + getString(R.string.bolus) + ": " + "" + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U" + ""; @@ -460,7 +461,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com Double c_bg = SafeParse.stringToDouble(editBg.getText()); Integer c_carbs = SafeParse.stringToInt(editCarbs.getText()); Double c_correction = SafeParse.stringToDouble(editCorr.getText()); - Double corrAfterConstraint = MainApp.getConstraintChecker().applyBolusConstraints(c_correction); + Double corrAfterConstraint = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(c_correction)).value(); if (c_correction - corrAfterConstraint != 0) { // c_correction != corrAfterConstraint doesn't work editCorr.setValue(0d); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.bolusconstraintapplied)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index bbad3d1833..0f9e2556d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -727,7 +727,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); String confirmMessage = getString(R.string.entertreatmentquestion); - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(wizard.calculatedTotalInsulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(wizard.calculatedTotalInsulin)).value(); Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(quickWizardEntry.carbs()); confirmMessage += "\n" + getString(R.string.bolus) + ": " + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U"; 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 c0219f110f..ba94dcc5af 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 @@ -1463,7 +1463,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public Double applyBolusConstraints(Double insulin) { + public Constraint applyBolusConstraints(Constraint insulin) { return insulin; } 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 bd5ea1bb7f..f69eb73f9d 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 @@ -263,7 +263,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, @Override public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { - insulin = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); // needs to be rounded int durationInHalfHours = Math.max(durationInMinutes / 30, 1); insulin = Round.roundTo(insulin, getPumpDescription().extendedBolusStep); @@ -474,15 +474,9 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, } @Override - public Double applyBolusConstraints(Double insulin) { - double origInsulin = insulin; - if (pump != null) { - if (insulin > pump.maxBolus) { - insulin = pump.maxBolus; - if (Config.logConstraintsChanges && origInsulin != Constants.bolusOnlyForCheckLimit) - log.debug("Limiting bolus " + origInsulin + "U by pump constraint to " + insulin + "U"); - } - } + public Constraint applyBolusConstraints(Constraint insulin) { + if (pump != null) + insulin.setIfSmaller(pump.maxBolus, String.format(MainApp.gs(R.string.limitingbolus), pump.maxBolus, MainApp.gs(R.string.pumplimit)), this); return insulin; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java index ee87fd7f25..ce1a5c392c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java @@ -132,7 +132,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value(); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { Treatment t = new Treatment(); t.isSMB = detailedBolusInfo.isSMB; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStart.java index 0c3e7dce70..9173794199 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStart.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStart.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.utils.HardLimits; public class MsgBolusStart extends MessageBase { @@ -18,9 +19,7 @@ public class MsgBolusStart extends MessageBase { this(); // HARDCODED LIMIT - amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); - if (amount < 0) amount = 0d; - if (amount > HardLimits.maxBolus()) amount = HardLimits.maxBolus(); + amount = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); AddParamInt((int) (amount * 100)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStartWithSpeed.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStartWithSpeed.java index 9a9c1140aa..3a33e9f866 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStartWithSpeed.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgBolusStartWithSpeed.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.utils.HardLimits; public class MsgBolusStartWithSpeed extends MessageBase { @@ -18,9 +19,7 @@ public class MsgBolusStartWithSpeed extends MessageBase { this(); // HARDCODED LIMIT - amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); - if (amount < 0) amount = 0d; - if (amount > HardLimits.maxBolus()) amount = HardLimits.maxBolus(); + amount = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); AddParamInt((int) (amount * 100)); AddParamByte((byte) speed); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetExtendedBolusStart.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetExtendedBolusStart.java index d27623d757..420f0ccbf9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetExtendedBolusStart.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgSetExtendedBolusStart.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.utils.HardLimits; public class MsgSetExtendedBolusStart extends MessageBase { @@ -20,9 +21,7 @@ public class MsgSetExtendedBolusStart extends MessageBase { // HARDCODED LIMITS if (halfhours < 1) halfhours = 1; if (halfhours > 16) halfhours = 16; - amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); - if (amount < 0d) amount = 0d; - if (amount > HardLimits.maxBolus()) amount = HardLimits.maxBolus(); + amount = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); AddParamInt((int) (amount * 100)); AddParamByte(halfhours); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java index bc07486bff..6462afbe79 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java @@ -133,7 +133,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value(); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { Treatment t = new Treatment(); t.isSMB = detailedBolusInfo.isSMB; 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 5dd5352335..2f651f9ba4 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 @@ -314,15 +314,9 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override - public Double applyBolusConstraints(Double insulin) { - double origInsulin = insulin; - if (pump != null) { - if (insulin > pump.maxBolus) { - insulin = pump.maxBolus; - if (Config.logConstraintsChanges && origInsulin != Constants.bolusOnlyForCheckLimit) - log.debug("Limiting bolus " + origInsulin + "U by pump constraint to " + insulin + "U"); - } - } + public Constraint applyBolusConstraints(Constraint insulin) { + if (pump != null) + insulin.setIfSmaller(pump.maxBolus, String.format(MainApp.gs(R.string.limitingbolus), pump.maxBolus, MainApp.gs(R.string.pumplimit)), this); return insulin; } @@ -441,7 +435,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public synchronized PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value(); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); int speed = 12; @@ -660,7 +654,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public synchronized PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { - insulin = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); // needs to be rounded int durationInHalfHours = Math.max(durationInMinutes / 30, 1); insulin = Round.roundTo(insulin, getPumpDescription().extendedBolusStep); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java index 864e528e57..12f2fa95c6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Start.java @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.utils.HardLimits; public class DanaRS_Packet_Bolus_Set_Step_Bolus_Start extends DanaRS_Packet { @@ -27,9 +28,7 @@ public class DanaRS_Packet_Bolus_Set_Step_Bolus_Start extends DanaRS_Packet { this(); // HARDCODED LIMIT - amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); - if (amount < 0) amount = 0d; - if (amount > HardLimits.maxBolus()) amount = HardLimits.maxBolus(); + amount = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); this.amount = amount; this.speed = speed; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java index b03901ad46..3f372de5db 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java @@ -122,7 +122,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { // Pump interface @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value(); if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { // v2 stores end time for bolus, we need to adjust time // default delivery speed is 12 sec/U 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 d3dae07d01..85ab71accd 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 @@ -1130,8 +1130,10 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } @Override - public Double applyBolusConstraints(Double insulin) { - return Math.min(insulin, statusResult != null ? statusResult.maximumBolusAmount : 0); + public Constraint applyBolusConstraints(Constraint insulin) { + if (statusResult != null) + insulin.setIfSmaller(statusResult.maximumBolusAmount, String.format(MainApp.gs(R.string.limitingbolus), statusResult.maximumBolusAmount, MainApp.gs(R.string.pumplimit)), this); + return insulin; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 8bdd83bbc3..09dfeac901 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -450,7 +450,7 @@ public class SmsCommunicatorPlugin implements PluginBase { sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } else if (splited.length > 1) { amount = SafeParse.stringToDouble(splited[1]); - amount = MainApp.getConstraintChecker().applyBolusConstraints(amount); + amount = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); if (amount > 0d && remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusreplywithcode), amount, passCode); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index 0dcdb3b1d9..097be40015 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -25,6 +25,7 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.interfaces.APSInterface; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; @@ -92,7 +93,7 @@ public class ActionStringHandler { } else { return; } - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(amount); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); rMessage += MainApp.instance().getString(R.string.primefill) + ": " + insulinAfterConstraints + "U"; if (insulinAfterConstraints - amount != 0) rMessage += "\n" + MainApp.instance().getString(R.string.constraintapllied); @@ -103,7 +104,7 @@ public class ActionStringHandler { ////////////////////////////////////////////// PRIME/FILL double amount = SafeParse.stringToDouble(act[1]); - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(amount); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); rMessage += MainApp.instance().getString(R.string.primefill) + ": " + insulinAfterConstraints + "U"; if (insulinAfterConstraints - amount != 0) rMessage += "\n" + MainApp.instance().getString(R.string.constraintapllied); @@ -114,7 +115,7 @@ public class ActionStringHandler { ////////////////////////////////////////////// BOLUS double insulin = SafeParse.stringToDouble(act[1]); int carbs = SafeParse.stringToInt(act[2]); - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbs); rMessage += MainApp.instance().getString(R.string.bolus) + ": " + insulinAfterConstraints + "U\n"; rMessage += MainApp.instance().getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; @@ -207,7 +208,7 @@ public class ActionStringHandler { BolusWizard bolusWizard = new BolusWizard(); bolusWizard.doCalc(profile, null, carbsAfterConstraints, 0d, useBG ? bgReading.valueToUnits(profile.getUnits()) : 0d, 0d, percentage, useBolusIOB, useBasalIOB, false, false); - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(bolusWizard.calculatedTotalInsulin); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(bolusWizard.calculatedTotalInsulin)).value(); if (insulinAfterConstraints - bolusWizard.calculatedTotalInsulin != 0) { sendError("Insulin contraint violation!" + "\nCannot deliver " + format.format(bolusWizard.calculatedTotalInsulin) + "!"); @@ -534,7 +535,7 @@ public class ActionStringHandler { if ("fill".equals(act[0])) { Double amount = SafeParse.stringToDouble(act[1]); - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(amount); + Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); if (amount - insulinAfterConstraints != 0) { ToastUtils.showToastInUiThread(MainApp.instance(), "aborting: previously applied constraint changed"); sendError("aborting: previously applied constraint changed"); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index e3a3f511a5..f9d7a57eb2 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -166,7 +166,7 @@ public class CommandQueue { removeAll(type); // apply constraints - detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value(); detailedBolusInfo.carbs = MainApp.getConstraintChecker().applyCarbsConstraints((int) detailedBolusInfo.carbs); // add new command to queue @@ -236,7 +236,7 @@ public class CommandQueue { return false; } - Double rateAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(insulin); + Double rateAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); // remove all unfinished removeAll(Command.CommandType.EXTENDEDBOLUS); diff --git a/app/src/main/java/info/nightscout/utils/HardLimits.java b/app/src/main/java/info/nightscout/utils/HardLimits.java index a277148d11..8e1a05db0d 100644 --- a/app/src/main/java/info/nightscout/utils/HardLimits.java +++ b/app/src/main/java/info/nightscout/utils/HardLimits.java @@ -49,13 +49,13 @@ public class HardLimits { String sp_age = SP.getString(R.string.key_age, ""); int age; - if (sp_age.equals(MainApp.sResources.getString(R.string.key_child))) + if (sp_age.equals(MainApp.gs(R.string.key_child))) age = CHILD; - else if (sp_age.equals(MainApp.sResources.getString(R.string.key_teenage))) + else if (sp_age.equals(MainApp.gs(R.string.key_teenage))) age = TEENAGE; - else if (sp_age.equals(MainApp.sResources.getString(R.string.key_adult))) + else if (sp_age.equals(MainApp.gs(R.string.key_adult))) age = ADULT; - else if (sp_age.equals(MainApp.sResources.getString(R.string.key_resistantadult))) + else if (sp_age.equals(MainApp.gs(R.string.key_resistantadult))) age = RESISTANTADULT; else age = ADULT; @@ -88,9 +88,9 @@ public class HardLimits { if (newvalue < lowLimit || newvalue > highLimit) { newvalue = Math.max(newvalue, lowLimit); newvalue = Math.min(newvalue, highLimit); - String msg = String.format(MainApp.sResources.getString(R.string.valueoutofrange), valueName); + String msg = String.format(MainApp.gs(R.string.valueoutofrange), valueName); msg += ".\n"; - msg += String.format(MainApp.sResources.getString(R.string.valuelimitedto), value, newvalue); + msg += String.format(MainApp.gs(R.string.valuelimitedto), value, newvalue); log.error(msg); NSUpload.uploadError(msg); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), msg, R.raw.error); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dd5b01b6e5..065bbdd135 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -972,6 +972,7 @@ openapsama_current_basal_safety_multiplier openapsama_max_daily_safety_multiplier basal must be positive value + bolus must be positive value max basal settings in preferences max basal multiplier max daily basal multiplier @@ -979,5 +980,9 @@ A bolus was delivered within the last 3 minutes, skipping SMB Basal set correctly Limiting percent rate to %d%% because of %s + treatmentssafety_maxbolus + Limiting bolus to %.1f U because of %s + max value in preferences + hard limit diff --git a/app/src/main/res/xml/pref_safety.xml b/app/src/main/res/xml/pref_safety.xml index 1cf759487d..36af2f747b 100644 --- a/app/src/main/res/xml/pref_safety.xml +++ b/app/src/main/res/xml/pref_safety.xml @@ -6,7 +6,7 @@ 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 46daf30a79..999f591dbf 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -244,7 +244,7 @@ public class ConstraintsCheckerTest { "SafetyPlugin: Limiting percent rate to 0% because of pump limit\n" + "DanaRPlugin: Limiting percent rate to 0% because of basal must be positive value\n" + "DanaRSPlugin: Limiting percent rate to 0% because of basal must be positive value\n" + - "InsightPumpPlugin: Limiting percent rate to 0% because of basal must be positive value", i.getReasons()); // InsightPlugin + "InsightPumpPlugin: Limiting percent rate to 0% because of basal must be positive value", i.getReasons()); // Apply all limits i = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); @@ -261,6 +261,43 @@ public class ConstraintsCheckerTest { } + // applyBolusConstraints tests + @Test + public void bolusAmountShouldBeLimited() throws Exception { + // DanaR, RS + danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + DanaRPump.getInstance().maxBolus = 6d; + + // Insight + insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + StatusTaskRunner.Result result = new StatusTaskRunner.Result(); + result.maximumBolusAmount = 7d; + insightPlugin.setStatusResult(result); + + + // No limit by default + when(SP.getDouble(R.string.key_treatmentssafety_maxbolus, 3d)).thenReturn(3d); + when(SP.getString(R.string.key_age, "")).thenReturn("child"); + + // Negative basal not allowed + Constraint d = new Constraint<>(-22d); + constraintChecker.applyBolusConstraints(d); + Assert.assertEquals(0d, d.value()); + Assert.assertEquals("SafetyPlugin: Limiting bolus to 0.0 U because of bolus must be positive value", d.getReasons()); + + // Apply all limits + d = new Constraint<>(Constants.REALLYHIGHBOLUS); + constraintChecker.applyBolusConstraints(d); + Assert.assertEquals(3d, d.value()); + Assert.assertEquals("SafetyPlugin: Limiting bolus to 3.0 U because of max value in preferences\n" + + "SafetyPlugin: Limiting bolus to 5.0 U because of hard limit\n" + + "DanaRPlugin: Limiting bolus to 6.0 U because of pump limit\n" + + "DanaRSPlugin: Limiting bolus to 6.0 U because of pump limit\n" + + "InsightPumpPlugin: Limiting bolus to 7.0 U because of pump limit", d.getReasons()); + + } + @Before public void prepareMock() throws Exception { Locale.setDefault(new Locale("en", "US")); @@ -295,6 +332,11 @@ public class ConstraintsCheckerTest { when(MainApp.gs(R.string.maxdailybasalmultiplier)).thenReturn("max daily basal multiplier"); when(MainApp.gs(R.string.limitingpercentrate)).thenReturn("Limiting percent rate to %d%% because of %s"); when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); + when(MainApp.gs(R.string.limitingbolus)).thenReturn("Limiting bolus to %.1f U because of %s"); + when(MainApp.gs(R.string.bolusmustbepositivevalue)).thenReturn("bolus must be positive value"); + when(MainApp.gs(R.string.maxvalueinpreferences)).thenReturn("max value in preferences"); + when(MainApp.gs(R.string.hardlimit)).thenReturn("hard limit"); + when(MainApp.gs(R.string.key_child)).thenReturn("child"); PowerMockito.mockStatic(SP.class); // RS constructor diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java index b36cd0b0aa..f6cf918371 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java @@ -125,7 +125,8 @@ public class CommandQueueTest extends CommandQueue { when(MainApp.getConstraintChecker()).thenReturn(constraintChecker); when(MainApp.isEngineeringModeOrRelease()).thenReturn(true); when(MainApp.instance()).thenReturn(mainApp); - when(MainApp.getConstraintChecker().applyBolusConstraints(insulin)).thenReturn(insulin); + Constraint bolusConstraint = new Constraint<>(0d); + when(MainApp.getConstraintChecker().applyBolusConstraints(any())).thenReturn(bolusConstraint); when(MainApp.getConstraintChecker().applyCarbsConstraints(carbs)).thenReturn(carbs); Constraint rateConstraint = new Constraint<>(0d); when(MainApp.getConstraintChecker().applyBasalConstraints(any(), any())).thenReturn(rateConstraint); From 1aba9a25647e1cfade6e2e0adfffdd5471b83cd5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 22 Mar 2018 10:31:07 +0100 Subject: [PATCH 18/28] applyCarbsConstraints refactor & tests --- .../info/nightscout/androidaps/Constants.java | 2 +- .../androidaps/data/ConstraintChecker.java | 7 ++-- .../interfaces/ConstraintsInterface.java | 2 +- .../Dialogs/NewNSTreatmentDialog.java | 2 +- .../ObjectivesPlugin.java | 2 +- .../ConstraintsSafety/SafetyPlugin.java | 19 ++++----- .../Overview/Dialogs/NewCarbsDialog.java | 5 ++- .../Overview/Dialogs/NewTreatmentDialog.java | 4 +- .../Overview/Dialogs/WizardDialog.java | 6 +-- .../plugins/Overview/OverviewFragment.java | 2 +- .../plugins/PumpCombo/ComboPlugin.java | 2 +- .../PumpDanaR/AbstractDanaRPlugin.java | 4 +- .../plugins/PumpDanaRS/DanaRSPlugin.java | 4 +- .../PumpInsight/InsightPumpPlugin.java | 4 +- .../plugins/Wear/ActionStringHandler.java | 4 +- .../androidaps/queue/CommandQueue.java | 2 +- app/src/main/res/values/strings.xml | 6 +-- app/src/main/res/xml/pref_safety.xml | 2 +- .../interfaces/ConstraintsCheckerTest.java | 42 +++++++++++++------ .../androidaps/queue/CommandQueueTest.java | 3 +- 20 files changed, 70 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 7cddc58452..9aa408d1ed 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -15,7 +15,7 @@ public class Constants { public static final Double REALLYHIGHBASALRATE = 1111111d; public static final Integer REALLYHIGHPERCENTBASALRATE = 1111111; public static final double REALLYHIGHBOLUS = 1111111d; - public static final Integer carbsOnlyForCheckLimit = 10101010; + public static final Integer REALLYHIGHCARBS = 1111111; public static final Integer notificationID = 556677; 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 5e27373d13..5745c802ee 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -134,15 +134,14 @@ public class ConstraintChecker implements ConstraintsInterface { } @Override - public Integer applyCarbsConstraints(Integer carbs) { - Integer carbsAfterConstrain = carbs; + public Constraint applyCarbsConstraints(Constraint carbs) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - carbsAfterConstrain = Math.min(constrain.applyCarbsConstraints(carbs), carbsAfterConstrain); + constrain.applyCarbsConstraints(carbs); } - return carbsAfterConstrain; + return carbs; } @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 96af468aef..a36a67aea9 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -23,7 +23,7 @@ public interface ConstraintsInterface { Constraint applyBolusConstraints(Constraint insulin); - Integer applyCarbsConstraints(Integer carbs); + Constraint applyCarbsConstraints(Constraint carbs); Double applyMaxIOBConstraints(Double maxIob); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index 5b2bacb0ca..e6d1e3d580 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -272,7 +272,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }); - Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); + Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)).value(); editCarbs = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbsinput); editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false); 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 e203da02df..7d2fd1f543 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 @@ -361,7 +361,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public Integer applyCarbsConstraints(Integer carbs) { + public Constraint applyCarbsConstraints(Constraint carbs) { return carbs; } 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 9ed22e1382..5be80b78ba 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 @@ -134,10 +134,10 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { - absoluteRate.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbasalratio), 0d, MainApp.gs(R.string.basalmustbepositivevalue)), this); + absoluteRate.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbasalratio), 0d, MainApp.gs(R.string.itmustbepositivevalue)), this); double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); - absoluteRate.setIfSmaller(maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), maxBasal, MainApp.gs(R.string.maxbasalinpreferences)), this); + absoluteRate.setIfSmaller(maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), maxBasal, MainApp.gs(R.string.maxvalueinpreferences)), this); // Check percentRate but absolute rate too, because we know real current basal in pump Double maxBasalMult = SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d); @@ -174,7 +174,7 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public Constraint applyBolusConstraints(Constraint insulin) { - insulin.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbolus), 0d, MainApp.gs(R.string.bolusmustbepositivevalue)), this); + insulin.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbolus), 0d, MainApp.gs(R.string.itmustbepositivevalue)), this); Double maxBolus = SP.getDouble(R.string.key_treatmentssafety_maxbolus, 3d); insulin.setIfSmaller(maxBolus, String.format(MainApp.gs(R.string.limitingbolus), maxBolus, MainApp.gs(R.string.maxvalueinpreferences)), this); @@ -184,15 +184,12 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public Integer applyCarbsConstraints(Integer carbs) { - try { - Integer maxCarbs = SP.getInt("treatmentssafety_maxcarbs", 48); + public Constraint applyCarbsConstraints(Constraint carbs) { + carbs.setIfGreater(0, String.format(MainApp.gs(R.string.limitingcarbs), 0, MainApp.gs(R.string.itmustbepositivevalue)), this); + + Integer maxCarbs = SP.getInt(R.string.key_treatmentssafety_maxcarbs, 48); + carbs.setIfSmaller(maxCarbs, String.format(MainApp.gs(R.string.limitingcarbs), maxCarbs, MainApp.gs(R.string.maxvalueinpreferences)), this); - if (carbs < 0) carbs = 0; - if (carbs > maxCarbs) carbs = maxCarbs; - } catch (Exception e) { - carbs = 0; - } return carbs; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index dcb20bdb70..2eeb89a597 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -43,6 +43,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.utils.DateUtil; @@ -120,7 +121,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); + maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)).value(); editCarbs = view.findViewById(R.id.newcarb_carbsamount); @@ -303,7 +304,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D okClicked = true; try { final Integer carbs = SafeParse.stringToInt(editCarbs.getText()); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbs); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value(); List actions = new LinkedList<>(); if (carbs > 0) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index 98af776b68..510085d1aa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -96,7 +96,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); + maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)).value(); maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); editCarbs = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_carbsamount); @@ -130,7 +130,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene String confirmMessage = MainApp.gs(R.string.entertreatmentquestion) + "
"; Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbs); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value(); if (insulin > 0) { confirmMessage += MainApp.gs(R.string.bolus) + ": " + "" + insulinAfterConstraints + "U" + ""; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index de9adb14e4..d226f5ac48 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -237,7 +237,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com superbolusCheckbox.setVisibility(SP.getBoolean(R.string.key_usesuperbolus, false) ? View.VISIBLE : View.GONE); - Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(Constants.carbsOnlyForCheckLimit); + Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)).value(); Double maxCorrection = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); editBg.setParams(0d, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false, textWatcher); @@ -305,7 +305,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com String confirmMessage = getString(R.string.entertreatmentquestion); Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(calculatedTotalInsulin)).value(); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(calculatedCarbs); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(calculatedCarbs)).value(); confirmMessage += "
" + getString(R.string.bolus) + ": " + "" + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U" + ""; confirmMessage += "
" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; @@ -467,7 +467,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.bolusconstraintapplied)); return; } - Integer carbsAfterConstraint = MainApp.getConstraintChecker().applyCarbsConstraints(c_carbs); + Integer carbsAfterConstraint = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(c_carbs)).value(); if (c_carbs - carbsAfterConstraint != 0) { editCarbs.setValue(0d); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.carbsconstraintapplied)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 0f9e2556d4..5ecaff8887 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -728,7 +728,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, String confirmMessage = getString(R.string.entertreatmentquestion); Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(wizard.calculatedTotalInsulin)).value(); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(quickWizardEntry.carbs()); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(quickWizardEntry.carbs())).value(); confirmMessage += "\n" + getString(R.string.bolus) + ": " + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U"; confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; 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 ba94dcc5af..042b385320 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 @@ -1468,7 +1468,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public Integer applyCarbsConstraints(Integer carbs) { + public Constraint applyCarbsConstraints(Constraint carbs) { return carbs; } 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 f69eb73f9d..062487d5a8 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 @@ -467,7 +467,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, @Override public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue)), this); + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.itmustbepositivevalue)), this); percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit)), this); return percentRate; @@ -481,7 +481,7 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, } @Override - public Integer applyCarbsConstraints(Integer carbs) { + public Constraint applyCarbsConstraints(Constraint carbs) { return carbs; } 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 2f651f9ba4..12f71bed9a 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 @@ -306,7 +306,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, @Override public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue)), this); + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.itmustbepositivevalue)), this); percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit)), this); return percentRate; @@ -321,7 +321,7 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public Integer applyCarbsConstraints(Integer carbs) { + public Constraint applyCarbsConstraints(Constraint carbs) { return carbs; } 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 85ab71accd..8de88db2a4 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 @@ -1123,7 +1123,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints @Override public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.basalmustbepositivevalue)), this); + percentRate.setIfGreater(0, String.format(MainApp.gs(R.string.limitingpercentrate), 0, MainApp.gs(R.string.itmustbepositivevalue)), this); percentRate.setIfSmaller(getPumpDescription().maxTempPercent, String.format(MainApp.gs(R.string.limitingpercentrate), getPumpDescription().maxTempPercent, MainApp.gs(R.string.pumplimit)), this); return percentRate; @@ -1137,7 +1137,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } @Override - public Integer applyCarbsConstraints(Integer carbs) { + public Constraint applyCarbsConstraints(Constraint carbs) { return carbs; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index 097be40015..190df0a692 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -116,7 +116,7 @@ public class ActionStringHandler { double insulin = SafeParse.stringToDouble(act[1]); int carbs = SafeParse.stringToInt(act[2]); Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbs); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value(); rMessage += MainApp.instance().getString(R.string.bolus) + ": " + insulinAfterConstraints + "U\n"; rMessage += MainApp.instance().getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; @@ -180,7 +180,7 @@ public class ActionStringHandler { } else if ("wizard".equals(act[0])) { ////////////////////////////////////////////// WIZARD Integer carbsBeforeConstraints = SafeParse.stringToInt(act[1]); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(carbsBeforeConstraints); + Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbsBeforeConstraints)).value(); if (carbsAfterConstraints - carbsBeforeConstraints != 0) { sendError("Carb constraint violation!"); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index f9d7a57eb2..2b89113da4 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -167,7 +167,7 @@ public class CommandQueue { // apply constraints detailedBolusInfo.insulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(detailedBolusInfo.insulin)).value(); - detailedBolusInfo.carbs = MainApp.getConstraintChecker().applyCarbsConstraints((int) detailedBolusInfo.carbs); + detailedBolusInfo.carbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>((int) detailedBolusInfo.carbs)).value(); // add new command to queue if (detailedBolusInfo.isSMB) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 065bbdd135..153cc1f0bf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -971,9 +971,7 @@ openapsma_max_basal openapsama_current_basal_safety_multiplier openapsama_max_daily_safety_multiplier - basal must be positive value - bolus must be positive value - max basal settings in preferences + it must be positive value max basal multiplier max daily basal multiplier openapsma_max_iob @@ -982,7 +980,9 @@ Limiting percent rate to %d%% because of %s treatmentssafety_maxbolus Limiting bolus to %.1f U because of %s + Limiting carbs to %d g because of %s max value in preferences hard limit + treatmentssafety_maxcarbs diff --git a/app/src/main/res/xml/pref_safety.xml b/app/src/main/res/xml/pref_safety.xml index 36af2f747b..e906c92608 100644 --- a/app/src/main/res/xml/pref_safety.xml +++ b/app/src/main/res/xml/pref_safety.xml @@ -12,7 +12,7 @@ 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 999f591dbf..1e70e5867c 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -200,13 +200,13 @@ public class ConstraintsCheckerTest { Constraint d = new Constraint<>(-0.5d); constraintChecker.applyBasalConstraints(d, profile); Assert.assertEquals(0d, d.value()); - Assert.assertEquals("SafetyPlugin: Limiting basal rate to 0.00 U/h because of basal must be positive value", d.getReasons()); + Assert.assertEquals("SafetyPlugin: Limiting basal rate to 0.00 U/h because of it must be positive value", d.getReasons()); // Apply all limits d = new Constraint<>(Constants.REALLYHIGHBASALRATE); constraintChecker.applyBasalConstraints(d, profile); Assert.assertEquals(0.8d, d.value()); - Assert.assertEquals("SafetyPlugin: Limiting basal rate to 1.00 U/h because of max basal settings in preferences\n" + + Assert.assertEquals("SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + "SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + "DanaRPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" + @@ -240,18 +240,18 @@ public class ConstraintsCheckerTest { constraintChecker.applyBasalPercentConstraints(i, profile); Assert.assertEquals((Integer)0, i.value()); Assert.assertEquals("SafetyPlugin: Percent rate -22% recalculated to -0.22 U/h with current basal 1.00 U/h\n" + - "SafetyPlugin: Limiting basal rate to 0.00 U/h because of basal must be positive value\n" + + "SafetyPlugin: Limiting basal rate to 0.00 U/h because of it must be positive value\n" + "SafetyPlugin: Limiting percent rate to 0% because of pump limit\n" + - "DanaRPlugin: Limiting percent rate to 0% because of basal must be positive value\n" + - "DanaRSPlugin: Limiting percent rate to 0% because of basal must be positive value\n" + - "InsightPumpPlugin: Limiting percent rate to 0% because of basal must be positive value", i.getReasons()); + "DanaRPlugin: Limiting percent rate to 0% because of it must be positive value\n" + + "DanaRSPlugin: Limiting percent rate to 0% because of it must be positive value\n" + + "InsightPumpPlugin: Limiting percent rate to 0% because of it must be positive value", i.getReasons()); // Apply all limits i = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); constraintChecker.applyBasalPercentConstraints(i, profile); Assert.assertEquals((Integer)100, i.value()); Assert.assertEquals("SafetyPlugin: Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h\n" + - "SafetyPlugin: Limiting basal rate to 1.00 U/h because of max basal settings in preferences\n" + + "SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + "SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + "SafetyPlugin: Limiting percent rate to 100% because of pump limit\n" + @@ -284,7 +284,7 @@ public class ConstraintsCheckerTest { Constraint d = new Constraint<>(-22d); constraintChecker.applyBolusConstraints(d); Assert.assertEquals(0d, d.value()); - Assert.assertEquals("SafetyPlugin: Limiting bolus to 0.0 U because of bolus must be positive value", d.getReasons()); + Assert.assertEquals("SafetyPlugin: Limiting bolus to 0.0 U because of it must be positive value", d.getReasons()); // Apply all limits d = new Constraint<>(Constants.REALLYHIGHBOLUS); @@ -298,6 +298,25 @@ public class ConstraintsCheckerTest { } + // applyCarbsConstraints tests + @Test + public void carbsAmountShouldBeLimited() throws Exception { + // No limit by default + when(SP.getInt(R.string.key_treatmentssafety_maxcarbs, 48)).thenReturn(48); + + // Negative basal not allowed + Constraint i = new Constraint<>(-22); + constraintChecker.applyCarbsConstraints(i); + Assert.assertEquals((Integer) 0, i.value()); + Assert.assertEquals("SafetyPlugin: Limiting carbs to 0 g because of it must be positive value", i.getReasons()); + + // Apply all limits + i = new Constraint<>(Constants.REALLYHIGHCARBS); + constraintChecker.applyCarbsConstraints(i); + Assert.assertEquals((Integer) 48, i.value()); + Assert.assertEquals("SafetyPlugin: Limiting carbs to 48 g because of max value in preferences", i.getReasons()); + } + @Before public void prepareMock() throws Exception { Locale.setDefault(new Locale("en", "US")); @@ -326,17 +345,16 @@ public class ConstraintsCheckerTest { when(MainApp.gs(R.string.smbdisabledinpreferences)).thenReturn("SMB disabled in preferences"); when(MainApp.gs(R.string.limitingbasalratio)).thenReturn("Limiting basal rate to %.2f U/h because of %s"); when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); - when(MainApp.gs(R.string.basalmustbepositivevalue)).thenReturn("basal must be positive value"); - when(MainApp.gs(R.string.maxbasalinpreferences)).thenReturn("max basal settings in preferences"); + when(MainApp.gs(R.string.itmustbepositivevalue)).thenReturn("it must be positive value"); + when(MainApp.gs(R.string.maxvalueinpreferences)).thenReturn("max value in preferences"); when(MainApp.gs(R.string.maxbasalmultiplier)).thenReturn("max basal multiplier"); when(MainApp.gs(R.string.maxdailybasalmultiplier)).thenReturn("max daily basal multiplier"); when(MainApp.gs(R.string.limitingpercentrate)).thenReturn("Limiting percent rate to %d%% because of %s"); when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); when(MainApp.gs(R.string.limitingbolus)).thenReturn("Limiting bolus to %.1f U because of %s"); - when(MainApp.gs(R.string.bolusmustbepositivevalue)).thenReturn("bolus must be positive value"); - when(MainApp.gs(R.string.maxvalueinpreferences)).thenReturn("max value in preferences"); when(MainApp.gs(R.string.hardlimit)).thenReturn("hard limit"); when(MainApp.gs(R.string.key_child)).thenReturn("child"); + when(MainApp.gs(R.string.limitingcarbs)).thenReturn("Limiting carbs to %d g because of %s"); PowerMockito.mockStatic(SP.class); // RS constructor diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java index f6cf918371..9f89fe1ec0 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueTest.java @@ -127,7 +127,8 @@ public class CommandQueueTest extends CommandQueue { when(MainApp.instance()).thenReturn(mainApp); Constraint bolusConstraint = new Constraint<>(0d); when(MainApp.getConstraintChecker().applyBolusConstraints(any())).thenReturn(bolusConstraint); - when(MainApp.getConstraintChecker().applyCarbsConstraints(carbs)).thenReturn(carbs); + Constraint carbsConstraint = new Constraint<>(0); + when(MainApp.getConstraintChecker().applyCarbsConstraints(any())).thenReturn(carbsConstraint); Constraint rateConstraint = new Constraint<>(0d); when(MainApp.getConstraintChecker().applyBasalConstraints(any(), any())).thenReturn(rateConstraint); Constraint percentageConstraint = new Constraint<>(0); From 76ee3c51e4e6c430f49a5f78b5ab670d8ab8dba5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 22 Mar 2018 21:13:26 +0100 Subject: [PATCH 19/28] applyMaxIOBConstraints refactor & tests --- .../info/nightscout/androidaps/Constants.java | 1 + .../androidaps/data/ConstraintChecker.java | 7 ++- .../interfaces/ConstraintsInterface.java | 4 +- .../ObjectivesPlugin.java | 15 +++--- .../ConstraintsSafety/SafetyPlugin.java | 14 +++++- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 10 ++-- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 10 ++-- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 8 ++-- .../plugins/PumpCombo/ComboPlugin.java | 6 ++- .../PumpDanaR/AbstractDanaRPlugin.java | 5 -- .../plugins/PumpDanaRS/DanaRSPlugin.java | 5 -- .../PumpInsight/InsightPumpPlugin.java | 5 -- app/src/main/res/values/strings.xml | 8 +++- .../interfaces/ConstraintsCheckerTest.java | 48 +++++++++++++++++-- 14 files changed, 95 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 9aa408d1ed..1fe9431397 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -16,6 +16,7 @@ public class Constants { public static final Integer REALLYHIGHPERCENTBASALRATE = 1111111; public static final double REALLYHIGHBOLUS = 1111111d; public static final Integer REALLYHIGHCARBS = 1111111; + public static final double REALLYHIGHIOB = 1111111d; public static final Integer notificationID = 556677; 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 5745c802ee..d36c3da52a 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -145,15 +145,14 @@ public class ConstraintChecker implements ConstraintsInterface { } @Override - public Double applyMaxIOBConstraints(Double maxIob) { - Double maxIobAfterConstrain = maxIob; + public Constraint applyMaxIOBConstraints(Constraint maxIob) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constrain = (ConstraintsInterface) p; if (!p.isEnabled(PluginBase.CONSTRAINTS)) continue; - maxIobAfterConstrain = Math.min(constrain.applyMaxIOBConstraints(maxIob), maxIobAfterConstrain); + constrain.applyMaxIOBConstraints(maxIob); } - return maxIobAfterConstrain; + return maxIob; } 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 a36a67aea9..e4255a2157 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -25,6 +25,8 @@ public interface ConstraintsInterface { Constraint applyCarbsConstraints(Constraint carbs); - Double applyMaxIOBConstraints(Double maxIob); + default Constraint applyMaxIOBConstraints(Constraint maxIob) { + return maxIob; + }; } 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 7d2fd1f543..808c8f696d 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 @@ -11,6 +11,7 @@ import java.util.Date; import java.util.List; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; @@ -192,7 +193,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { SafetyPlugin.getPlugin().isClosedLoopAllowed(closedLoopEnabled); return new RequirementResult(closedLoopEnabled.value(), MainApp.gs(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.value())); case 4: - double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(1000d); + double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)).value(); boolean maxIobSet = maxIOB > 0; return new RequirementResult(maxIobSet, MainApp.gs(R.string.maxiobset) + ": " + yesOrNo(maxIobSet)); default: @@ -335,14 +336,10 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public Double applyMaxIOBConstraints(Double maxIob) { - if (objectives.get(3).started.getTime() > 0 && objectives.get(3).accomplished.getTime() == 0) { - if (Config.logConstraintsChanges) - log.debug("Limiting maxIOB " + maxIob + " to " + 0 + "U"); - return 0d; - } else { - return maxIob; - } + public Constraint applyMaxIOBConstraints(Constraint maxIob) { + if (objectives.get(3).started.getTime() > 0&& objectives.get(3).accomplished.getTime() == 0) + maxIob.set(0d, String.format(MainApp.gs(R.string.objectivenotfinished), 4), this); + return maxIob; } @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 5be80b78ba..15f0a13583 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 @@ -10,6 +10,9 @@ import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; +import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAPlugin; +import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.HardLimits; import info.nightscout.utils.Round; @@ -194,7 +197,16 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public Double applyMaxIOBConstraints(Double maxIob) { + public Constraint applyMaxIOBConstraints(Constraint maxIob) { + double maxIobPref = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); + maxIob.setIfSmaller(maxIobPref, String.format(MainApp.gs(R.string.limitingiob), maxIobPref, MainApp.gs(R.string.maxvalueinpreferences)), this); + + if (OpenAPSMAPlugin.getPlugin().isEnabled(PluginBase.APS)) + maxIob.setIfSmaller(HardLimits.maxIobAMA(), String.format(MainApp.gs(R.string.limitingiob), HardLimits.maxIobAMA(), MainApp.gs(R.string.hardlimit)), this); + if (OpenAPSAMAPlugin.getPlugin().isEnabled(PluginBase.APS)) + maxIob.setIfSmaller(HardLimits.maxIobAMA(), String.format(MainApp.gs(R.string.limitingiob), HardLimits.maxIobAMA(), MainApp.gs(R.string.hardlimit)), this); + if (OpenAPSSMBPlugin.getPlugin().isEnabled(PluginBase.APS)) + maxIob.setIfSmaller(HardLimits.maxIobSMB(), String.format(MainApp.gs(R.string.limitingiob), HardLimits.maxIobSMB(), MainApp.gs(R.string.hardlimit)), this); return maxIob; } 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 49dcfdbb6e..879be19b36 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 @@ -8,6 +8,7 @@ import java.io.IOException; import java.util.Date; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.GlucoseStatus; @@ -17,6 +18,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -74,7 +76,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { @Override public boolean isEnabled(int type) { - boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; + boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump() != null && ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; return type == APS && fragmentEnabled && pumpCapable; } @@ -173,7 +175,6 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxIob = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); @@ -191,7 +192,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); Profiler.log(log, "getMealData()", startPart); - maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(maxIob); + double maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)).value(); minBg = HardLimits.verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); maxBg = HardLimits.verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]); @@ -207,8 +208,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { } - maxIob = HardLimits.verifyHardLimits(maxIob, "maxIob", 0, HardLimits.maxIobAMA()); - maxBasal = HardLimits.verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal()); + maxBasal = HardLimits.verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal()); if (!HardLimits.checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) return; 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 9bd5d5189b..d7f9481519 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 @@ -8,6 +8,7 @@ import java.io.IOException; import java.util.Date; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.GlucoseStatus; @@ -17,6 +18,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -74,13 +76,13 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { @Override public boolean isEnabled(int type) { - boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; + boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump() != null && ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; return type == APS && fragmentEnabled && pumpCapable; } @Override public boolean isVisibleInTabs(int type) { - boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; + boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump() != null && ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; return type == APS && fragmentVisible && pumpCapable; } @@ -173,7 +175,6 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxIob = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); @@ -192,7 +193,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); - maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(maxIob); + double maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)).value(); Profiler.log(log, "MA data gathering", start); minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); @@ -206,7 +207,6 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { targetBg = verifyHardLimits(tempTarget.target(), "targetBg", HardLimits.VERY_HARD_LIMIT_TEMP_TARGET_BG[0], HardLimits.VERY_HARD_LIMIT_TEMP_TARGET_BG[1]); } - maxIob = verifyHardLimits(maxIob, "maxIob", 0, HardLimits.maxIobAMA()); maxBasal = verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal()); if (!checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) 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 a16734700b..ee93f000b0 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 @@ -8,6 +8,7 @@ import java.io.IOException; import java.util.Date; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.GlucoseStatus; @@ -17,6 +18,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -79,7 +81,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { @Override public boolean isEnabled(int type) { - boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; + boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump() != null && ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; return type == APS && fragmentEnabled && pumpCapable; } @@ -178,7 +180,6 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxIob = SP.getDouble(R.string.key_openapsma_max_iob, 1.5d); double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); @@ -196,7 +197,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); Profiler.log(log, "getMealData()", startPart); - maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(maxIob); + double maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)).value(); minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); maxBg = verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]); @@ -212,7 +213,6 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { } - maxIob = verifyHardLimits(maxIob, "maxIob", 0, HardLimits.maxIobSMB()); maxBasal = verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal()); if (!checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) return; 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 042b385320..45e9967341 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 @@ -1473,7 +1473,9 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public Double applyMaxIOBConstraints(Double maxIob) { - return lowSuspendOnlyLoopEnforcedUntil < System.currentTimeMillis() ? maxIob : 0; + public Constraint applyMaxIOBConstraints(Constraint maxIob) { + if (lowSuspendOnlyLoopEnforcedUntil > System.currentTimeMillis()) + maxIob.setIfSmaller(0d, String.format(MainApp.gs(R.string.limitingmaxiob), 0d, MainApp.gs(R.string.unsafeusage)), this); + return maxIob; } } 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 062487d5a8..4dec62c8a8 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 @@ -485,11 +485,6 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, return carbs; } - @Override - public Double applyMaxIOBConstraints(Double maxIob) { - return maxIob; - } - @Nullable @Override public ProfileStore getProfile() { 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 12f71bed9a..32d2ea8f9c 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 @@ -325,11 +325,6 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, return carbs; } - @Override - public Double applyMaxIOBConstraints(Double maxIob) { - return maxIob; - } - // Profile interface @Nullable 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 8de88db2a4..2416eb0d4e 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 @@ -1141,10 +1141,5 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints return carbs; } - @Override - public Double applyMaxIOBConstraints(Double maxIob) { - return maxIob; - } - } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 153cc1f0bf..3decf36d10 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -961,6 +961,7 @@ Bolus count TBR count Objective %d not started + Objective %d not finished Pump is not temp basal capable No valid basal rate read from pump Closed loop mode disabled in preferences @@ -979,10 +980,13 @@ Basal set correctly Limiting percent rate to %d%% because of %s treatmentssafety_maxbolus - Limiting bolus to %.1f U because of %s - Limiting carbs to %d g because of %s + Limiting bolus to %.1f U because of %s + Limiting max IOB to %.1f U because of %s + Limiting carbs to %d g because of %s + Limiting IOB to %.1f U because of %s max value in preferences hard limit treatmentssafety_maxcarbs + unsafe usage 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 1e70e5867c..0f8be62ec6 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -27,6 +27,9 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; +import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; +import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAPlugin; +import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.PumpCombo.ComboPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; @@ -44,7 +47,7 @@ import static org.mockito.Mockito.when; * Created by mike on 18.03.2018. */ @RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, FabricPrivacy.class, SP.class, Context.class}) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, FabricPrivacy.class, SP.class, Context.class, OpenAPSMAPlugin.class, OpenAPSAMAPlugin.class, OpenAPSSMBPlugin.class}) public class ConstraintsCheckerTest { PumpInterface pump = new VirtualPumpPlugin(); @@ -280,7 +283,7 @@ public class ConstraintsCheckerTest { when(SP.getDouble(R.string.key_treatmentssafety_maxbolus, 3d)).thenReturn(3d); when(SP.getString(R.string.key_age, "")).thenReturn("child"); - // Negative basal not allowed + // Negative bolus not allowed Constraint d = new Constraint<>(-22d); constraintChecker.applyBolusConstraints(d); Assert.assertEquals(0d, d.value()); @@ -304,7 +307,7 @@ public class ConstraintsCheckerTest { // No limit by default when(SP.getInt(R.string.key_treatmentssafety_maxcarbs, 48)).thenReturn(48); - // Negative basal not allowed + // Negative carbs not allowed Constraint i = new Constraint<>(-22); constraintChecker.applyCarbsConstraints(i); Assert.assertEquals((Integer) 0, i.value()); @@ -317,6 +320,39 @@ public class ConstraintsCheckerTest { Assert.assertEquals("SafetyPlugin: Limiting carbs to 48 g because of max value in preferences", i.getReasons()); } + // applyMaxIOBConstraints tests + @Test + public void iobShouldBeLimited() throws Exception { + // DanaR, RS + danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + DanaRPump.getInstance().maxBolus = 6d; + + // Insight + insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + StatusTaskRunner.Result result = new StatusTaskRunner.Result(); + result.maximumBolusAmount = 7d; + insightPlugin.setStatusResult(result); + + + // No limit by default + when(SP.getDouble(R.string.key_openapsma_max_iob, 1.5d)).thenReturn(1.5d); + when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); + OpenAPSMAPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); + OpenAPSAMAPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); + OpenAPSSMBPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); + + // Apply all limits + Constraint d = new Constraint<>(Constants.REALLYHIGHIOB); + constraintChecker.applyMaxIOBConstraints(d); + Assert.assertEquals(1.5d, d.value()); + Assert.assertEquals("SafetyPlugin: Limiting IOB to 1.5 U because of max value in preferences\n" + + "SafetyPlugin: Limiting IOB to 7.0 U because of hard limit\n" + + "SafetyPlugin: Limiting IOB to 7.0 U because of hard limit\n" + + "SafetyPlugin: Limiting IOB to 12.0 U because of hard limit", d.getReasons()); + + } + @Before public void prepareMock() throws Exception { Locale.setDefault(new Locale("en", "US")); @@ -355,6 +391,12 @@ public class ConstraintsCheckerTest { when(MainApp.gs(R.string.hardlimit)).thenReturn("hard limit"); when(MainApp.gs(R.string.key_child)).thenReturn("child"); when(MainApp.gs(R.string.limitingcarbs)).thenReturn("Limiting carbs to %d g because of %s"); + when(MainApp.gs(R.string.limitingiob)).thenReturn("Limiting IOB to %.1f U because of %s"); + + PowerMockito.mockStatic(SP.class); + //PowerMockito.mock(OpenAPSMAPlugin.class); + //PowerMockito.mock(OpenAPSAMAPlugin.class); + //PowerMockito.mock(OpenAPSSMBPlugin.class); PowerMockito.mockStatic(SP.class); // RS constructor From ae5724183389ad4fda9d841ac3b3d13f34ddecc9 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 22 Mar 2018 21:31:32 +0100 Subject: [PATCH 20/28] use default functions in constraint interface --- .../interfaces/ConstraintsInterface.java | 36 ++++++++++++----- .../plugins/PumpCombo/ComboPlugin.java | 40 ------------------- .../PumpDanaR/AbstractDanaRPlugin.java | 30 -------------- .../plugins/PumpDanaRS/DanaRSPlugin.java | 30 -------------- .../PumpInsight/InsightPumpPlugin.java | 31 -------------- 5 files changed, 27 insertions(+), 140 deletions(-) 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 e4255a2157..4918cbfacf 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -7,23 +7,41 @@ import info.nightscout.androidaps.data.Profile; */ public interface ConstraintsInterface { - Constraint isLoopInvokationAllowed(Constraint value); + default Constraint isLoopInvokationAllowed(Constraint value) { + return value; + } - Constraint isClosedLoopAllowed(Constraint value); + default Constraint isClosedLoopAllowed(Constraint value) { + return value; + } - Constraint isAutosensModeEnabled(Constraint value); + default Constraint isAutosensModeEnabled(Constraint value) { + return value; + } - Constraint isAMAModeEnabled(Constraint value); + default Constraint isAMAModeEnabled(Constraint value) { + return value; + } - Constraint isSMBModeEnabled(Constraint value); + default Constraint isSMBModeEnabled(Constraint value) { + return value; + } - Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile); + default Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { + return absoluteRate; + } - Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile); + default Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { + return percentRate; + } - Constraint applyBolusConstraints(Constraint insulin); + default Constraint applyBolusConstraints(Constraint insulin) { + return insulin; + } - Constraint applyCarbsConstraints(Constraint carbs); + default Constraint applyCarbsConstraints(Constraint carbs) { + return carbs; + } default Constraint applyMaxIOBConstraints(Constraint maxIob) { return maxIob; 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 45e9967341..f9858d4cce 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 @@ -1432,46 +1432,6 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf return value; } - @Override - public Constraint isClosedLoopAllowed(Constraint value) { - return value; - } - - @Override - public Constraint isAutosensModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isAMAModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isSMBModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { - return absoluteRate; - } - - @Override - public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - return percentRate; - } - - @Override - public Constraint applyBolusConstraints(Constraint insulin) { - return insulin; - } - - @Override - public Constraint applyCarbsConstraints(Constraint carbs) { - return carbs; - } - @Override public Constraint applyMaxIOBConstraints(Constraint maxIob) { if (lowSuspendOnlyLoopEnforcedUntil > System.currentTimeMillis()) 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 4dec62c8a8..008cecc590 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 @@ -433,31 +433,6 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, * Constraint interface */ - @Override - public Constraint isLoopInvokationAllowed(Constraint value) { - return value; - } - - @Override - public Constraint isClosedLoopAllowed(Constraint value) { - return value; - } - - @Override - public Constraint isAutosensModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isAMAModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isSMBModeEnabled(Constraint value) { - return value; - } - @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { if (pump != null) @@ -480,11 +455,6 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, return insulin; } - @Override - public Constraint applyCarbsConstraints(Constraint carbs) { - return carbs; - } - @Nullable @Override public ProfileStore getProfile() { 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 32d2ea8f9c..4473f110d4 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 @@ -272,31 +272,6 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, // Constraints interface - @Override - public Constraint isLoopInvokationAllowed(Constraint value) { - return value; - } - - @Override - public Constraint isClosedLoopAllowed(Constraint value) { - return value; - } - - @Override - public Constraint isAutosensModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isAMAModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isSMBModeEnabled(Constraint value) { - return value; - } - @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { if (pump != null) @@ -320,11 +295,6 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, return insulin; } - @Override - public Constraint applyCarbsConstraints(Constraint carbs) { - return carbs; - } - // Profile interface @Nullable 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 2416eb0d4e..9b84f56da6 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 @@ -1088,31 +1088,6 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints // Constraints - @Override - public Constraint isLoopInvokationAllowed(Constraint value) { - return value; - } - - @Override - public Constraint isClosedLoopAllowed(Constraint value) { - return value; - } - - @Override - public Constraint isAutosensModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isAMAModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isSMBModeEnabled(Constraint value) { - return value; - } - @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { if (statusResult != null) { @@ -1136,10 +1111,4 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints return insulin; } - @Override - public Constraint applyCarbsConstraints(Constraint carbs) { - return carbs; - } - - } From ba09538f46872a3f02f74d38461ed156a0fe14a2 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 22 Mar 2018 22:18:40 +0100 Subject: [PATCH 21/28] AMA - Autosens cleanup --- .../androidaps/PreferencesActivity.java | 4 ++-- .../ObjectivesPlugin.java | 21 ------------------- .../ConstraintsSafety/SafetyPlugin.java | 9 ++------ .../DetermineBasalAdapterAMAJS.java | 2 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 2 +- .../DetermineBasalAdapterSMBJS.java | 2 +- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 2 +- app/src/main/res/values/strings.xml | 3 ++- app/src/main/res/xml/pref_openapsama.xml | 2 +- .../interfaces/ConstraintsCheckerTest.java | 14 +++++++------ 10 files changed, 19 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index 0f712c787f..12eb5a1daf 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -14,7 +14,6 @@ import android.text.TextUtils; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.Careportal.CareportalPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.Insulin.InsulinOrefFreePeakPlugin; @@ -22,6 +21,7 @@ import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalPlugin; import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAPlugin; +import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin; @@ -65,7 +65,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre if (key.equals("short_tabtitles")) { MainApp.bus().post(new EventRefreshGui()); } - if (key.equals("openapsama_useautosens") && SP.getBoolean("openapsama_useautosens", false)) { + if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) { OKDialog.show(this, MainApp.sResources.getString(R.string.configbuilder_sensitivity), MainApp.sResources.getString(R.string.sensitivity_warning), null); } updatePrefSummary(myPreferenceFragment.getPreference(key)); 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 808c8f696d..459111ed0d 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 @@ -342,25 +342,4 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { return maxIob; } - @Override - public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { - return absoluteRate; - } - - @Override - public Constraint applyBasalPercentConstraints(Constraint percentRate, Profile profile) { - return percentRate; - } - - @Override - public Constraint applyBolusConstraints(Constraint insulin) { - return insulin; - } - - @Override - public Constraint applyCarbsConstraints(Constraint carbs) { - return carbs; - } - - } 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 15f0a13583..3316bfdc30 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 @@ -115,14 +115,9 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { @Override public Constraint isAutosensModeEnabled(Constraint value) { - return value; - } - - @Override - public Constraint isAMAModeEnabled(Constraint value) { - boolean enabled = SP.getBoolean("openapsama_useautosens", false); + boolean enabled = SP.getBoolean(R.string.key_openapsama_useautosens, false); if (!enabled) - value.set(false, MainApp.gs(R.string.amadisabledinpreferences), this); + value.set(false, MainApp.gs(R.string.autosensdisabledinpreferences), this); return value; } 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 f71f7483b0..0c92df635c 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 @@ -251,7 +251,7 @@ public class DetermineBasalAdapterAMAJS { mMealData.put("boluses", mealData.boluses); mMealData.put("mealCOB", mealData.mealCOB); - if (MainApp.getConstraintChecker().isAMAModeEnabled().value()) { + if (MainApp.getConstraintChecker().isAutosensModeEnabled().value()) { 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 879be19b36..2a51c53811 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 @@ -222,7 +222,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { return; startPart = new Date(); - if (MainApp.getConstraintChecker().isAMAModeEnabled().value()) { + if (MainApp.getConstraintChecker().isAutosensModeEnabled().value()) { 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 e5a555c326..e644b13dc9 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 @@ -300,7 +300,7 @@ public class DetermineBasalAdapterSMBJS { mMealData.put("lastCarbTime", mealData.lastCarbTime); - if (MainApp.getConstraintChecker().isAMAModeEnabled().value()) { + if (MainApp.getConstraintChecker().isAutosensModeEnabled().value()) { 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 ee93f000b0..8b2d17d49f 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 @@ -224,7 +224,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().value()) { + if (MainApp.getConstraintChecker().isAutosensModeEnabled().value()) { lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3decf36d10..9e6bbe311b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -965,7 +965,7 @@ Pump is not temp basal capable No valid basal rate read from pump Closed loop mode disabled in preferences - AMA disabled in preferences + Autosens disabled in preferences SMB disabled in preferences Limiting basal rate to %.2f U/h because of %s pump limit @@ -988,5 +988,6 @@ hard limit treatmentssafety_maxcarbs unsafe usage + openapsama_useautosens diff --git a/app/src/main/res/xml/pref_openapsama.xml b/app/src/main/res/xml/pref_openapsama.xml index ee8a1d80bd..944f057919 100644 --- a/app/src/main/res/xml/pref_openapsama.xml +++ b/app/src/main/res/xml/pref_openapsama.xml @@ -19,7 +19,7 @@ android:title="@string/openapsma_maxiob_title" /> c = constraintChecker.isAMAModeEnabled(); - Assert.assertEquals(true, c.getReasons().contains("AMA disabled in preferences")); + Constraint c = constraintChecker.isAutosensModeEnabled(); + Assert.assertEquals(true, c.getReasons().contains("Autosens disabled in preferences")); + Assert.assertEquals(true, c.getReasons().contains("Objective 6 not started")); Assert.assertEquals(Boolean.FALSE, c.value()); } @@ -377,7 +379,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"); + when(MainApp.gs(R.string.autosensdisabledinpreferences)).thenReturn("Autosens disabled in preferences"); when(MainApp.gs(R.string.smbdisabledinpreferences)).thenReturn("SMB disabled in preferences"); when(MainApp.gs(R.string.limitingbasalratio)).thenReturn("Limiting basal rate to %.2f U/h because of %s"); when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); From 0f531a99546942fc0e5a1c2d287898ea2f4439c2 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 22 Mar 2018 23:05:00 +0100 Subject: [PATCH 22/28] objectives cleanup & basal hard limits to constraints --- .../androidaps/data/ConstraintChecker.java | 5 ++ .../ObjectivesPlugin.java | 20 +++++-- .../ConstraintsSafety/SafetyPlugin.java | 2 + .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 4 +- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 5 +- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 4 +- .../interfaces/ConstraintsCheckerTest.java | 4 ++ .../nightscout/utils/TimeListEditTest.java | 57 ------------------- 8 files changed, 29 insertions(+), 72 deletions(-) delete mode 100644 app/src/test/java/info/nightscout/utils/TimeListEditTest.java 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 d36c3da52a..0a5146f1b5 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.data; import java.util.ArrayList; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.ConstraintsInterface; @@ -40,6 +41,10 @@ public class ConstraintChecker implements ConstraintsInterface { return isSMBModeEnabled(new Constraint<>(true)); } + public Constraint getMaxBasalAllowed(Profile profile) { + return applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile); + } + @Override public Constraint isLoopInvokationAllowed(Constraint value) { 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 459111ed0d..0e4b7476cf 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 @@ -137,6 +137,14 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { public void setStarted(Date started) { this.started = started; } + + boolean isStarted() { + return started.getTime() > 0; + } + + boolean isFinished() { + return accomplished.getTime() != 0; + } } // Objective 0 @@ -302,42 +310,42 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { **/ @Override public Constraint isLoopInvokationAllowed(Constraint value) { - if (objectives.get(0).started.getTime() == 0) + if (!objectives.get(0).isStarted()) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 1), this); return value; } @Override public Constraint isClosedLoopAllowed(Constraint value) { - if (objectives.get(3).started.getTime() == 0) + if (!objectives.get(3).isStarted()) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 4), this); return value; } @Override public Constraint isAutosensModeEnabled(Constraint value) { - if (objectives.get(5).started.getTime() == 0) + if (!objectives.get(5).isStarted()) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 6), this); return value; } @Override public Constraint isAMAModeEnabled(Constraint value) { - if (objectives.get(6).started.getTime() == 0) + if (!objectives.get(6).isStarted()) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 7), this); return value; } @Override public Constraint isSMBModeEnabled(Constraint value) { - if (objectives.get(7).started.getTime() == 0) + if (!objectives.get(7).isStarted()) value.set(false, String.format(MainApp.gs(R.string.objectivenotstarted), 8), this); return value; } @Override public Constraint applyMaxIOBConstraints(Constraint maxIob) { - if (objectives.get(3).started.getTime() > 0&& objectives.get(3).accomplished.getTime() == 0) + if (objectives.get(3).isStarted() && !objectives.get(3).isFinished()) maxIob.set(0d, String.format(MainApp.gs(R.string.objectivenotfinished), 4), this); return maxIob; } 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 3316bfdc30..048e48f8b3 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 @@ -145,6 +145,8 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { Double maxBasalFromDaily = SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d); double maxFromDaily = Math.floor(profile.getMaxDailyBasal() * maxBasalFromDaily * 100) / 100; absoluteRate.setIfSmaller(maxFromDaily, String.format(MainApp.gs(R.string.limitingbasalratio), maxFromDaily, MainApp.gs(R.string.maxdailybasalmultiplier)), this); + + absoluteRate.setIfSmaller(HardLimits.maxBasal(), String.format(MainApp.gs(R.string.limitingbasalratio), HardLimits.maxBasal(), MainApp.gs(R.string.hardlimit)), this); return absoluteRate; } 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 2a51c53811..25f92c17de 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 @@ -175,7 +175,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); + double maxBasal = MainApp.getConstraintChecker().getMaxBasalAllowed(profile).value(); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); double targetBg = Profile.toMgdl(profile.getTarget(), units); @@ -208,8 +208,6 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { } - maxBasal = HardLimits.verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal()); - if (!HardLimits.checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) return; if (!HardLimits.checkOnlyHardLimits(profile.getIcTimeFromMidnight(profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC)) 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 d7f9481519..4d794c4931 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 @@ -175,7 +175,8 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); + double maxBasal = MainApp.getConstraintChecker().getMaxBasalAllowed(profile).value(); + double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); double targetBg = Profile.toMgdl(profile.getTarget(), units); @@ -207,8 +208,6 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { targetBg = verifyHardLimits(tempTarget.target(), "targetBg", HardLimits.VERY_HARD_LIMIT_TEMP_TARGET_BG[0], HardLimits.VERY_HARD_LIMIT_TEMP_TARGET_BG[1]); } - maxBasal = verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal()); - if (!checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) return; if (!checkOnlyHardLimits(profile.getIcTimeFromMidnight(profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC)) 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 8b2d17d49f..a029297cdf 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 @@ -180,7 +180,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); + double maxBasal = MainApp.getConstraintChecker().getMaxBasalAllowed(profile).value(); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); double targetBg = Profile.toMgdl(profile.getTarget(), units); @@ -213,8 +213,6 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { } - maxBasal = verifyHardLimits(maxBasal, "max_basal", 0.1, HardLimits.maxBasal()); - if (!checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) return; if (!checkOnlyHardLimits(profile.getIcTimeFromMidnight(profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC)) return; 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 416374aff6..5c65bfb2fc 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -200,6 +200,7 @@ public class ConstraintsCheckerTest { when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d); when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d); + when(SP.getString(R.string.key_age, "")).thenReturn("child"); // Negative basal not allowed Constraint d = new Constraint<>(-0.5d); @@ -214,6 +215,7 @@ public class ConstraintsCheckerTest { Assert.assertEquals("SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + "SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + + "SafetyPlugin: Limiting basal rate to 2.00 U/h because of hard limit\n" + "DanaRPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" + "DanaRSPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" + "InsightPumpPlugin: Limiting basal rate to 1.10 U/h because of pump limit", d.getReasons()); @@ -239,6 +241,7 @@ public class ConstraintsCheckerTest { when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d); when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d); + when(SP.getString(R.string.key_age, "")).thenReturn("child"); // Negative basal not allowed Constraint i = new Constraint<>(-22); @@ -259,6 +262,7 @@ public class ConstraintsCheckerTest { "SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + "SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + + "SafetyPlugin: Limiting basal rate to 2.00 U/h because of hard limit\n" + "SafetyPlugin: Limiting percent rate to 100% because of pump limit\n" + "DanaRPlugin: Limiting percent rate to 200% because of pump limit\n" + "DanaRSPlugin: Limiting percent rate to 200% because of pump limit\n" + diff --git a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java deleted file mode 100644 index 8fd387eebd..0000000000 --- a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package info.nightscout.utils; - -import android.content.Context; -import android.view.LayoutInflater; - -import org.json.JSONArray; -import org.junit.Test; -import org.mockito.Mock; - -import java.lang.reflect.Method; -import java.text.DecimalFormat; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.when; - -/** - * Created by mike on 30.12.2016. - */ -public class TimeListEditTest { - - /* - JSONArray data = new JSONArray(); - JSONArray data2 = new JSONArray(); - TimeListEdit tle = new TimeListEdit(null, null, 0, "Test1", data, "ic", null, new DecimalFormat("0.00")); - TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00")); - - - @Test - public void doArrayTest() throws Exception { - tle.addItem(0, 0, 0.1, 0); - tle.addItem(1, 60 * 60, 0.2, 0); - assertEquals(2, tle.itemsCount()); - - tle.editItem(0, 2 * 60 * 60, 1, 0); - assertEquals(2, tle.itemsCount()); - assertEquals(1d, tle.value1(0), 0.00001d); - assertEquals( 2 * 60 * 60, tle.secondFromMidnight(0)); - tle.removeItem(0); - assertEquals(0.2d, tle.value1(0), 0.00001d); - assertEquals(0, tle.value2(0), 0.00001d); - assertEquals(60 * 60, tle.secondFromMidnight(0)); - - //System.out.print(tle2.toString()); - assertEquals(0, tle2.itemsCount()); - tle2.addItem(0, 0, 1, 2); - assertEquals(1, tle2.itemsCount()); - assertEquals(0, tle2.secondFromMidnight(0)); - assertEquals(1d, tle2.value1(0), 0.00001d); - assertEquals(2d, tle2.value2(0), 0.00001d); - } - - */ - @Test - public void fakeTest() throws Exception { - } - -} \ No newline at end of file From ad8ff15cdc9a4bb179195c7adfa8174dbdf8c37f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 22 Mar 2018 23:20:10 +0100 Subject: [PATCH 23/28] getMax... constraints functions --- .../androidaps/data/ConstraintChecker.java | 28 +++++++++++++++---- .../plugins/Actions/dialogs/FillDialog.java | 3 +- .../dialogs/NewExtendedBolusDialog.java | 3 +- .../Dialogs/NewNSTreatmentDialog.java | 8 +++--- .../ObjectivesPlugin.java | 2 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 2 +- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 2 +- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 2 +- .../Overview/Dialogs/NewCarbsDialog.java | 2 +- .../Overview/Dialogs/NewInsulinDialog.java | 2 +- .../Overview/Dialogs/NewTreatmentDialog.java | 4 +-- .../Overview/Dialogs/WizardDialog.java | 4 +-- .../interfaces/ConstraintsCheckerTest.java | 15 ++++------ 13 files changed, 43 insertions(+), 34 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 0a5146f1b5..d984e3277f 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -13,9 +13,9 @@ import info.nightscout.androidaps.interfaces.PluginBase; */ public class ConstraintChecker implements ConstraintsInterface { - + private MainApp mainApp; - + public ConstraintChecker(MainApp mainApp) { this.mainApp = mainApp; } @@ -29,22 +29,38 @@ public class ConstraintChecker implements ConstraintsInterface { return isClosedLoopAllowed(new Constraint<>(true)); } - public Constraint isAutosensModeEnabled() { + public Constraint isAutosensModeEnabled() { return isAutosensModeEnabled(new Constraint<>(true)); } - public Constraint isAMAModeEnabled() { + public Constraint isAMAModeEnabled() { return isAMAModeEnabled(new Constraint<>(true)); } - public Constraint isSMBModeEnabled() { + public Constraint isSMBModeEnabled() { return isSMBModeEnabled(new Constraint<>(true)); } - public Constraint getMaxBasalAllowed(Profile profile) { + public Constraint getMaxBasalAllowed(Profile profile) { return applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile); } + public Constraint getMaxBasalPercentAllowed(Profile profile) { + return applyBasalPercentConstraints(new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE), profile); + } + + public Constraint getMaxBolusAllowed() { + return applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)); + } + + public Constraint getMaxCarbsAllowed() { + return applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)); + } + + public Constraint getMaxIOBAllowed() { + return applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)); + } + @Override public Constraint isLoopInvokationAllowed(Constraint value) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java index 97bef4c88e..c0e668439c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -21,7 +21,6 @@ import org.slf4j.LoggerFactory; import java.text.DecimalFormat; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; @@ -61,7 +60,7 @@ public class FillDialog extends DialogFragment implements OnClickListener { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); + Double maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); double bolusstep = ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep; editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_insulinamount); editInsulin.setParams(0d, 0d, maxInsulin, bolusstep, new DecimalFormat("0.00"), false); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java index db5575fc47..04dd783b04 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/NewExtendedBolusDialog.java @@ -17,7 +17,6 @@ import org.slf4j.LoggerFactory; import java.text.DecimalFormat; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.Constraint; @@ -44,7 +43,7 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli View view = inflater.inflate(R.layout.overview_newextendedbolus_dialog, container, false); - Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); + Double maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); editInsulin = (NumberPicker) view.findViewById(R.id.overview_newextendedbolus_insulin); editInsulin.setParams(0d, 0d, maxInsulin, 0.1d, new DecimalFormat("0.00"), false); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index e6d1e3d580..3c77532e11 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -272,11 +272,11 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }); - Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)).value(); + Integer maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value(); editCarbs = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbsinput); editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false); - Double maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); + Double maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); editInsulin = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_insulininput); editInsulin.setParams(0d, 0d, maxInsulin, 0.05d, new DecimalFormat("0.00"), false); @@ -305,7 +305,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick Integer maxPercent = 200; if (profile != null) - maxPercent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE), profile).value(); + maxPercent = MainApp.getConstraintChecker().getMaxBasalPercentAllowed(profile).value(); editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput); editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true, percentTextWatcher); @@ -327,7 +327,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick } }; - Double maxAbsolute = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile).value(); + Double maxAbsolute = MainApp.getConstraintChecker().getMaxBasalAllowed(profile).value(); editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput); editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true, absoluteTextWatcher); 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 0e4b7476cf..6b9e796468 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 @@ -201,7 +201,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { SafetyPlugin.getPlugin().isClosedLoopAllowed(closedLoopEnabled); return new RequirementResult(closedLoopEnabled.value(), MainApp.gs(R.string.closedmodeenabled) + ": " + yesOrNo(closedLoopEnabled.value())); case 4: - double maxIOB = MainApp.getConstraintChecker().applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)).value(); + double maxIOB = MainApp.getConstraintChecker().getMaxIOBAllowed().value(); boolean maxIobSet = maxIOB > 0; return new RequirementResult(maxIobSet, MainApp.gs(R.string.maxiobset) + ": " + yesOrNo(maxIobSet)); default: 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 25f92c17de..47b0964142 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 @@ -192,7 +192,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); Profiler.log(log, "getMealData()", startPart); - double maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)).value(); + double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value(); minBg = HardLimits.verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); maxBg = HardLimits.verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]); 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 4d794c4931..1c9edbc278 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 @@ -194,7 +194,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); - double maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)).value(); + double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value(); Profiler.log(log, "MA data gathering", start); minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); 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 a029297cdf..af0810cfa6 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 @@ -197,7 +197,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { MealData mealData = MainApp.getConfigBuilder().getMealData(); Profiler.log(log, "getMealData()", startPart); - double maxIob = MainApp.getConstraintChecker().applyMaxIOBConstraints(new Constraint<>(Constants.REALLYHIGHIOB)).value(); + double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value(); minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); maxBg = verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index 2eeb89a597..a17470eb2c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -121,7 +121,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)).value(); + maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value(); editCarbs = view.findViewById(R.id.newcarb_carbsamount); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java index 07eb3289a7..11eabfb3c6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewInsulinDialog.java @@ -120,7 +120,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener, getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); + maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newinsulin_amount); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index 510085d1aa..72776b58e9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -96,8 +96,8 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)).value(); - maxInsulin = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); + maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value(); + maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); editCarbs = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_carbsamount); editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_insulinamount); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index d226f5ac48..34980b054a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -237,8 +237,8 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com superbolusCheckbox.setVisibility(SP.getBoolean(R.string.key_usesuperbolus, false) ? View.VISIBLE : View.GONE); - Integer maxCarbs = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)).value(); - Double maxCorrection = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(Constants.REALLYHIGHBOLUS)).value(); + Integer maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value(); + Double maxCorrection = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); editBg.setParams(0d, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false, textWatcher); editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false, textWatcher); 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 5c65bfb2fc..fc721d7173 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -209,8 +209,7 @@ public class ConstraintsCheckerTest { Assert.assertEquals("SafetyPlugin: Limiting basal rate to 0.00 U/h because of it must be positive value", d.getReasons()); // Apply all limits - d = new Constraint<>(Constants.REALLYHIGHBASALRATE); - constraintChecker.applyBasalConstraints(d, profile); + d = constraintChecker.getMaxBasalAllowed(profile); Assert.assertEquals(0.8d, d.value()); Assert.assertEquals("SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + @@ -255,8 +254,7 @@ public class ConstraintsCheckerTest { "InsightPumpPlugin: Limiting percent rate to 0% because of it must be positive value", i.getReasons()); // Apply all limits - i = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); - constraintChecker.applyBasalPercentConstraints(i, profile); + i = constraintChecker.getMaxBasalPercentAllowed(profile); Assert.assertEquals((Integer)100, i.value()); Assert.assertEquals("SafetyPlugin: Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h\n" + "SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + @@ -296,8 +294,7 @@ public class ConstraintsCheckerTest { Assert.assertEquals("SafetyPlugin: Limiting bolus to 0.0 U because of it must be positive value", d.getReasons()); // Apply all limits - d = new Constraint<>(Constants.REALLYHIGHBOLUS); - constraintChecker.applyBolusConstraints(d); + d = constraintChecker.getMaxBolusAllowed(); Assert.assertEquals(3d, d.value()); Assert.assertEquals("SafetyPlugin: Limiting bolus to 3.0 U because of max value in preferences\n" + "SafetyPlugin: Limiting bolus to 5.0 U because of hard limit\n" + @@ -320,8 +317,7 @@ public class ConstraintsCheckerTest { Assert.assertEquals("SafetyPlugin: Limiting carbs to 0 g because of it must be positive value", i.getReasons()); // Apply all limits - i = new Constraint<>(Constants.REALLYHIGHCARBS); - constraintChecker.applyCarbsConstraints(i); + i = constraintChecker.getMaxCarbsAllowed(); Assert.assertEquals((Integer) 48, i.value()); Assert.assertEquals("SafetyPlugin: Limiting carbs to 48 g because of max value in preferences", i.getReasons()); } @@ -349,8 +345,7 @@ public class ConstraintsCheckerTest { OpenAPSSMBPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); // Apply all limits - Constraint d = new Constraint<>(Constants.REALLYHIGHIOB); - constraintChecker.applyMaxIOBConstraints(d); + Constraint d = constraintChecker.getMaxIOBAllowed(); Assert.assertEquals(1.5d, d.value()); Assert.assertEquals("SafetyPlugin: Limiting IOB to 1.5 U because of max value in preferences\n" + "SafetyPlugin: Limiting IOB to 7.0 U because of hard limit\n" + From 3f5808eca36449b34f19e4fc694a4145c5428e52 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 23 Mar 2018 09:59:07 +0100 Subject: [PATCH 24/28] show max basal limit on smb fragment --- .../androidaps/interfaces/Constraint.java | 2 +- .../androidaps/plugins/Loop/APSResult.java | 2 + .../OpenAPSSMB/OpenAPSSMBFragment.java | 48 +++++++++---------- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 8 +++- .../res/layout/configbuilder_fragment.xml | 2 +- .../main/res/layout/openapsama_fragment.xml | 38 ++++++++++++++- app/src/main/res/values-bg/strings.xml | 2 +- app/src/main/res/values-cs/strings.xml | 2 +- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-el/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values-it/strings.xml | 2 +- app/src/main/res/values-ko/strings.xml | 2 +- app/src/main/res/values-nl/strings.xml | 2 +- app/src/main/res/values-ro/strings.xml | 2 +- app/src/main/res/values-ru/strings.xml | 2 +- app/src/main/res/values-sv/strings.xml | 2 +- app/src/main/res/values/strings.xml | 6 +-- 19 files changed, 87 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java index de3d266d7b..7b6609b79c 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java @@ -64,7 +64,7 @@ public class Constraint { } public Constraint reason(String reason, Object from) { - reasons.add(from.getClass().getSimpleName() + ": " + reason); + reasons.add(from.getClass().getSimpleName().replace("Plugin", "") + ": " + reason); return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java index 958743234d..9b851c64c1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java @@ -40,6 +40,8 @@ public class APSResult { public double smb = 0d; // super micro bolus in units public long deliverAt = 0; + public Constraint inputConstraints; + public Constraint rateConstraint; public Constraint smbConstraint; 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 6c720adc48..dcba600fb4 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 @@ -16,6 +16,9 @@ import org.json.JSONException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; @@ -24,19 +27,32 @@ import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateRes import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.JSONFormatter; -public class OpenAPSSMBFragment extends SubscriberFragment implements View.OnClickListener { +public class OpenAPSSMBFragment extends SubscriberFragment { private static Logger log = LoggerFactory.getLogger(OpenAPSSMBFragment.class); + @BindView(R.id.openapsma_run) Button run; + @BindView(R.id.openapsma_lastrun) TextView lastRunView; + @BindView(R.id.openapsma_constraints) + TextView constraintsView; + @BindView(R.id.openapsma_glucosestatus) TextView glucoseStatusView; + @BindView(R.id.openapsma_currenttemp) TextView currentTempView; + @BindView(R.id.openapsma_iobdata) TextView iobDataView; + @BindView(R.id.openapsma_profile) TextView profileView; + @BindView(R.id.openapsma_mealdata) TextView mealDataView; + @BindView(R.id.openapsma_autosensdata) TextView autosensDataView; + @BindView(R.id.openapsma_result) TextView resultView; + @BindView(R.id.openapsma_scriptdebugdata) TextView scriptdebugView; + @BindView(R.id.openapsma_request) TextView requestView; @Override @@ -44,32 +60,14 @@ public class OpenAPSSMBFragment extends SubscriberFragment implements View.OnCli Bundle savedInstanceState) { View view = inflater.inflate(R.layout.openapsama_fragment, container, false); - run = (Button) view.findViewById(R.id.openapsma_run); - run.setOnClickListener(this); - lastRunView = (TextView) view.findViewById(R.id.openapsma_lastrun); - glucoseStatusView = (TextView) view.findViewById(R.id.openapsma_glucosestatus); - currentTempView = (TextView) view.findViewById(R.id.openapsma_currenttemp); - iobDataView = (TextView) view.findViewById(R.id.openapsma_iobdata); - profileView = (TextView) view.findViewById(R.id.openapsma_profile); - mealDataView = (TextView) view.findViewById(R.id.openapsma_mealdata); - autosensDataView = (TextView) view.findViewById(R.id.openapsma_autosensdata); - scriptdebugView = (TextView) view.findViewById(R.id.openapsma_scriptdebugdata); - resultView = (TextView) view.findViewById(R.id.openapsma_result); - requestView = (TextView) view.findViewById(R.id.openapsma_request); - - updateGUI(); + unbinder = ButterKnife.bind(this, view); return view; } - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.openapsma_run: - OpenAPSSMBPlugin.getPlugin().invoke("OpenAPSSMB button"); - FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_SMB_Run")); - break; - } - + @OnClick(R.id.openapsma_run) + public void onRunClick() { + OpenAPSSMBPlugin.getPlugin().invoke("OpenAPSSMB button"); + FabricPrivacy.getInstance().logCustom(new CustomEvent("OpenAPS_SMB_Run")); } @Subscribe @@ -109,6 +107,8 @@ public class OpenAPSSMBFragment extends SubscriberFragment implements View.OnCli profileView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getProfileParam())); mealDataView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getMealDataParam())); scriptdebugView.setText(determineBasalAdapterSMBJS.getScriptDebug()); + if (lastAPSResult != null && lastAPSResult.inputConstraints != null) + constraintsView.setText(lastAPSResult.inputConstraints.getReasons()); } if (plugin.lastAPSRun != null) { lastRunView.setText(plugin.lastAPSRun.toLocaleString()); 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 af0810cfa6..7ea785988d 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 @@ -180,7 +180,11 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { String units = profile.getUnits(); - double maxBasal = MainApp.getConstraintChecker().getMaxBasalAllowed(profile).value(); + Constraint inputConstraints = new Constraint<>(0d); // fake. only for collecting all results + + Constraint maxBasalConstraint = MainApp.getConstraintChecker().getMaxBasalAllowed(profile); + inputConstraints.copyReasons(maxBasalConstraint); + double maxBasal = maxBasalConstraint.value(); double minBg = Profile.toMgdl(profile.getTargetLow(), units); double maxBg = Profile.toMgdl(profile.getTargetHigh(), units); double targetBg = Profile.toMgdl(profile.getTarget(), units); @@ -269,6 +273,8 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { log.error("Unhandled exception", e); } + determineBasalResultSMB.inputConstraints = inputConstraints; + lastDetermineBasalAdapterSMBJS = determineBasalAdapterSMBJS; lastAPSResult = determineBasalResultSMB; lastAPSRun = new Date(now); diff --git a/app/src/main/res/layout/configbuilder_fragment.xml b/app/src/main/res/layout/configbuilder_fragment.xml index 2b2a7ef185..90e0026418 100644 --- a/app/src/main/res/layout/configbuilder_fragment.xml +++ b/app/src/main/res/layout/configbuilder_fragment.xml @@ -184,7 +184,7 @@ android:layout_marginBottom="5dp" android:background="@color/mdtp_circle_color" android:paddingLeft="5dp" - android:text="@string/configbuilder_constraints" + android:text="@string/constraints" android:textAllCaps="true" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@android:color/black" diff --git a/app/src/main/res/layout/openapsama_fragment.xml b/app/src/main/res/layout/openapsama_fragment.xml index a471429567..bfe3b73d69 100644 --- a/app/src/main/res/layout/openapsama_fragment.xml +++ b/app/src/main/res/layout/openapsama_fragment.xml @@ -89,9 +89,45 @@ + + + + + + + + + + Общи
дни Минимална продължителност - Ограничения + Ограничения Loop Loop APS diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index a62329ae4b..30cd42c89e 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -47,7 +47,7 @@ Konfigurace APS Zdroj glykémie - Omezení + Omezení Obecné Smyčka Profil diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index bbe2ed5497..0bad8b97e0 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -141,7 +141,7 @@ Eingegeben durch Anderes Split - Beschränkungen + Beschränkungen Generell Behandlungen Beschränkungen angewendet! diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 1b7fdf8022..d071fa7f8e 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -86,7 +86,7 @@ Γενικά ημέρες Ελάχ.Διάρκεια - Περιορισμοί + Περιορισμοί Κύκλωμα Κύκλωμα APS diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index af936ed618..ee63a899c3 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -78,7 +78,7 @@ General días Duración mínima - Restricciones + Restricciones Lazo Lazo APS diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 67ecda2b0f..2d0d2ff32d 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -81,7 +81,7 @@ Général Jours Durée minimale - Restrictions + Restrictions Loop Loop APS diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 352f802ee6..3c738259b3 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -71,7 +71,7 @@ Configurazione Strutturale APS Origine glicemia - Costrizione + Costrizione Generale Insulina Loop diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 46f1cb3525..89d0dca30d 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -84,7 +84,7 @@ 일반 최소기간 - 제한 + 제한 Loop Loop APS diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index ca73dcbffa..86cab639c4 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -129,7 +129,7 @@ Configurator APS BG bron - Beperkingen + Beperkingen Algemeen Insuline curve Ledig wachtrij diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index aca835b34d..e6ade785bb 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -83,7 +83,7 @@ General zile Durată minimă - Constrângeri + Constrângeri Loop Loop diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index ddc446b62c..f1e03c7251 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -84,7 +84,7 @@ конфигуратор APS источник СК - ограничения + ограничения общее инсулин замкнутый цикл diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index 669f96e4e9..47bd827e0a 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -84,7 +84,7 @@ Konfigurationsverktyg APS BG Källa - Begränsningar + Begränsningar Generell Insulin Loop diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9e6bbe311b..4ff9bce87d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -89,7 +89,7 @@ General days Minimal duration - Constraints + Constraints Loop Loop @@ -967,7 +967,7 @@ Closed loop mode disabled in preferences Autosens disabled in preferences SMB disabled in preferences - Limiting basal rate to %.2f U/h because of %s + Limiting max basal rate to %.2f U/h because of %s pump limit openapsma_max_basal openapsama_current_basal_safety_multiplier @@ -978,7 +978,7 @@ openapsma_max_iob A bolus was delivered within the last 3 minutes, skipping SMB Basal set correctly - Limiting percent rate to %d%% because of %s + Limiting max percent rate to %d%% because of %s treatmentssafety_maxbolus Limiting bolus to %.1f U because of %s Limiting max IOB to %.1f U because of %s From 15d8719508a158089f12fa496ecf6dee54df98c3 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 23 Mar 2018 17:36:49 +0100 Subject: [PATCH 25/28] collect most limited reason along with all reasons --- .../androidaps/interfaces/Constraint.java | 34 ++- .../ConstraintsSafety/SafetyPlugin.java | 2 +- app/src/main/res/values-cs/strings.xml | 2 +- app/src/test/java/info/AAPSMocker.java | 94 +++++++ .../androidaps/PumpDanaR/DanaRPluginTest.java | 76 ++++++ .../androidaps/interfaces/ConstraintTest.java | 7 + .../interfaces/ConstraintsCheckerTest.java | 250 ++++-------------- .../ObjectivesPluginTest.java | 90 +++++++ .../ConstraintsSafety/SafetyPluginTest.java | 214 +++++++++++++++ .../plugins/PumpCombo/ComboPluginTest.java | 54 ++-- .../plugins/PumpDanaRS/DanaRSPluginTest.java | 75 ++++++ .../plugins/PumpInsight/PumpInsightTest.java | 56 ++++ 12 files changed, 724 insertions(+), 230 deletions(-) create mode 100644 app/src/test/java/info/AAPSMocker.java create mode 100644 app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPluginTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPluginTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/PumpInsightTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java index 7b6609b79c..58e6045bd1 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java @@ -17,6 +17,7 @@ public class Constraint { T originalValue; List reasons = new ArrayList<>(); + List mostLimiting = new ArrayList<>(); public Constraint(T value) { this.value = value; @@ -39,16 +40,19 @@ public class Constraint { public Constraint set(T value, String reason, Object from) { this.value = value; - reason(reason, from); + addReason(reason, from); + addMostLimingReason(reason, from); return this; } public Constraint setIfSmaller(T value, String reason, Object from) { if (value.compareTo(this.value) < 0) { this.value = value; + mostLimiting.clear(); + addMostLimingReason(reason, from); } if (value.compareTo(this.originalValue) < 0) { - reason(reason, from); + addReason(reason, from); } return this; } @@ -56,18 +60,25 @@ public class Constraint { public Constraint setIfGreater(T value, String reason, Object from) { if (value.compareTo(this.value) > 0) { this.value = value; + mostLimiting.clear(); + addMostLimingReason(reason, from); } if (value.compareTo(this.originalValue) > 0) { - reason(reason, from); + addReason(reason, from); } return this; } - public Constraint reason(String reason, Object from) { + public Constraint addReason(String reason, Object from) { reasons.add(from.getClass().getSimpleName().replace("Plugin", "") + ": " + reason); return this; } + public Constraint addMostLimingReason(String reason, Object from) { + mostLimiting.add(from.getClass().getSimpleName().replace("Plugin", "") + ": " + reason); + return this; + } + public String getReasons() { StringBuilder sb = new StringBuilder(); int count = 0; @@ -83,6 +94,21 @@ public class Constraint { return reasons; } + public String getMostLimitedReasons() { + StringBuilder sb = new StringBuilder(); + int count = 0; + for (String r : mostLimiting) { + if (count++ != 0) sb.append("\n"); + sb.append(r); + } + log.debug("Limiting origial value: " + originalValue + " to " + value + ". Reason: " + sb.toString()); + return sb.toString(); + } + + public List getMostLimitedReasonList() { + return mostLimiting; + } + public void copyReasons(Constraint another) { for (String s: another.getReasonList()) { reasons.add(s); 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 048e48f8b3..7194380360 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 @@ -156,7 +156,7 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { Double currentBasal = profile.getBasal(); Double absoluteRate = currentBasal * ((double) percentRate.originalValue() / 100); - percentRate.reason("Percent rate " + percentRate.originalValue() + "% recalculated to " + DecimalFormatter.to2Decimal(absoluteRate) + " U/h with current basal " + DecimalFormatter.to2Decimal(currentBasal) + " U/h", this); + percentRate.addReason("Percent rate " + percentRate.originalValue() + "% recalculated to " + DecimalFormatter.to2Decimal(absoluteRate) + " U/h with current basal " + DecimalFormatter.to2Decimal(currentBasal) + " U/h", this); Constraint absoluteConstraint = new Constraint<>(absoluteRate); applyBasalConstraints(absoluteConstraint, profile); diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 30cd42c89e..4ce126527c 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -734,7 +734,7 @@ Použít super mikro bolusy místo dočasných bazálů pro zrychlení účinku Detekce neoznámených jídel Insight - Pumpa Insight + Insight Stav Změněno PUMPA ZASTAVENA diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java new file mode 100644 index 0000000000..adf5136dd0 --- /dev/null +++ b/app/src/test/java/info/AAPSMocker.java @@ -0,0 +1,94 @@ +package info; + +import android.content.Context; + +import com.squareup.otto.Bus; + +import org.json.JSONException; +import org.json.JSONObject; +import org.powermock.api.mockito.PowerMockito; + +import java.util.Locale; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.utils.SP; + +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Created by mike on 23.03.2018. + */ + +public class AAPSMocker { + static String validProfile = "{\"dia\":\"3\",\"carbratio\":[{\"time\":\"00:00\",\"value\":\"30\"}],\"carbs_hr\":\"20\",\"delay\":\"20\",\"sens\":[{\"time\":\"00:00\",\"value\":\"100\"},{\"time\":\"2:00\",\"value\":\"110\"}],\"timezone\":\"UTC\",\"basal\":[{\"time\":\"00:00\",\"value\":\"1\"}],\"target_low\":[{\"time\":\"00:00\",\"value\":\"4\"}],\"target_high\":[{\"time\":\"00:00\",\"value\":\"5\"}],\"startDate\":\"1970-01-01T00:00:00.000Z\",\"units\":\"mmol\"}"; + static Profile profile; + + public static void mockStrings() { + Locale.setDefault(new Locale("en", "US")); + + when(MainApp.gs(R.string.closed_loop_disabled_on_dev_branch)).thenReturn("Running dev version. Closed loop is disabled."); + 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.autosensdisabledinpreferences)).thenReturn("Autosens disabled in preferences"); + when(MainApp.gs(R.string.smbdisabledinpreferences)).thenReturn("SMB disabled in preferences"); + when(MainApp.gs(R.string.limitingbasalratio)).thenReturn("Limiting basal rate to %.2f U/h because of %s"); + when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); + when(MainApp.gs(R.string.itmustbepositivevalue)).thenReturn("it must be positive value"); + when(MainApp.gs(R.string.maxvalueinpreferences)).thenReturn("max value in preferences"); + when(MainApp.gs(R.string.maxbasalmultiplier)).thenReturn("max basal multiplier"); + when(MainApp.gs(R.string.maxdailybasalmultiplier)).thenReturn("max daily basal multiplier"); + when(MainApp.gs(R.string.limitingpercentrate)).thenReturn("Limiting percent rate to %d%% because of %s"); + when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); + when(MainApp.gs(R.string.limitingbolus)).thenReturn("Limiting bolus to %.1f U because of %s"); + when(MainApp.gs(R.string.hardlimit)).thenReturn("hard limit"); + when(MainApp.gs(R.string.key_child)).thenReturn("child"); + when(MainApp.gs(R.string.limitingcarbs)).thenReturn("Limiting carbs to %d g because of %s"); + when(MainApp.gs(R.string.limitingiob)).thenReturn("Limiting IOB to %.1f U because of %s"); + when(MainApp.gs(R.string.pumpisnottempbasalcapable)).thenReturn("Pump is not temp basal capable"); + } + + public static MainApp mockMainApp() { + PowerMockito.mockStatic(MainApp.class); + MainApp mainApp = mock(MainApp.class); + when(MainApp.instance()).thenReturn(mainApp); + return mainApp; + } + + public static void mockConfigBuilder() { + PowerMockito.mockStatic(ConfigBuilderPlugin.class); + ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); + when(MainApp.getConfigBuilder()).thenReturn(configBuilderPlugin); + } + + public static void mockBus() { + Bus bus = PowerMockito.mock(Bus.class); + when(MainApp.bus()).thenReturn(bus); + } + + public static void mockSP() { + PowerMockito.mockStatic(SP.class); + when(SP.getLong(anyInt(), anyLong())).thenReturn(0L); + when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false); + when(SP.getInt(anyInt(), anyInt())).thenReturn(0); + } + + public static void mockApplicationContext() { + Context context = mock(Context.class); + when(MainApp.instance().getApplicationContext()).thenReturn(context); + } + + public static Profile getValidProfile() throws JSONException { + if (profile == null) + profile = new Profile(new JSONObject(validProfile), Constants.MGDL); + return profile; + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java b/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java new file mode 100644 index 0000000000..5f288dc9c7 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java @@ -0,0 +1,76 @@ +package info.nightscout.androidaps.PumpDanaR; + +import android.content.Context; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.AAPSMocker; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.Constraint; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; +import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; +import info.nightscout.utils.SP; +import info.nightscout.utils.ToastUtils; + +import static org.mockito.Mockito.when; + +/** + * Created by mike on 23.03.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class, SP.class}) +public class DanaRPluginTest { + + DanaRPlugin danaRPlugin; + + @Test + public void basalRateShouldBeLimited() throws Exception { + danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + DanaRPump.getInstance().maxBasal = 0.8d; + + Constraint c = new Constraint<>(Constants.REALLYHIGHBASALRATE); + danaRPlugin.applyBasalConstraints(c, AAPSMocker.getValidProfile()); + Assert.assertEquals(0.8d, c.value()); + Assert.assertEquals("DanaR: Limiting basal rate to 0.80 U/h because of pump limit", c.getReasons()); + Assert.assertEquals("DanaR: Limiting basal rate to 0.80 U/h because of pump limit", c.getMostLimitedReasons()); + } + + @Test + public void percentBasalRateShouldBeLimited() throws Exception { + danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + DanaRPump.getInstance().maxBasal = 0.8d; + + Constraint c = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); + danaRPlugin.applyBasalPercentConstraints(c, AAPSMocker.getValidProfile()); + Assert.assertEquals((Integer) 200, c.value()); + Assert.assertEquals("DanaR: Limiting percent rate to 200% because of pump limit", c.getReasons()); + Assert.assertEquals("DanaR: Limiting percent rate to 200% because of pump limit", c.getMostLimitedReasons()); + } + + @Before + public void prepareMocks() throws Exception { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockBus(); + AAPSMocker.mockStrings(); + AAPSMocker.mockApplicationContext(); + AAPSMocker.mockSP(); + + when(SP.getString(R.string.key_danars_address, "")).thenReturn(""); + + danaRPlugin = DanaRPlugin.getPlugin(); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java index 01c3a927f7..23352a57f9 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintTest.java @@ -18,26 +18,33 @@ public class ConstraintTest { Constraint b = new Constraint<>(true); Assert.assertEquals(Boolean.TRUE, b.value()); Assert.assertEquals("", b.getReasons()); + Assert.assertEquals("", b.getMostLimitedReasons()); b.set(false); Assert.assertEquals(Boolean.FALSE, b.value()); Assert.assertEquals("", b.getReasons()); + Assert.assertEquals("", b.getMostLimitedReasons()); b.set(true, "Set true", this); Assert.assertEquals(Boolean.TRUE, b.value()); Assert.assertEquals("ConstraintTest: Set true", b.getReasons()); + Assert.assertEquals("ConstraintTest: Set true", b.getMostLimitedReasons()); b.set(false, "Set false", this); Assert.assertEquals(Boolean.FALSE, b.value()); Assert.assertEquals("ConstraintTest: Set true\nConstraintTest: Set false", b.getReasons()); + Assert.assertEquals("ConstraintTest: Set true\nConstraintTest: Set false", b.getMostLimitedReasons()); Constraint d = new Constraint<>(10d); d.set(5d, "Set 5d", this); Assert.assertEquals(5d, d.value()); Assert.assertEquals("ConstraintTest: Set 5d", d.getReasons()); + Assert.assertEquals("ConstraintTest: Set 5d", d.getMostLimitedReasons()); d.setIfSmaller(6d, "Set 6d", this); Assert.assertEquals(5d, d.value()); Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d", d.getReasons()); + Assert.assertEquals("ConstraintTest: Set 5d", d.getMostLimitedReasons()); d.setIfSmaller(4d, "Set 4d", this); Assert.assertEquals(4d, d.value()); Assert.assertEquals("ConstraintTest: Set 5d\nConstraintTest: Set 6d\nConstraintTest: Set 4d", d.getReasons()); + Assert.assertEquals("ConstraintTest: Set 4d", d.getMostLimitedReasons()); Assert.assertEquals(10d, d.originalValue()); } } 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 fc721d7173..65e949c83c 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -7,7 +7,6 @@ import com.squareup.otto.Bus; import junit.framework.Assert; import org.json.JSONException; -import org.json.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,13 +16,11 @@ import org.powermock.modules.junit4.PowerMockRunner; import java.util.ArrayList; import java.util.Date; -import java.util.Locale; -import info.nightscout.androidaps.Constants; +import info.AAPSMocker; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.ConstraintChecker; -import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; @@ -40,7 +37,6 @@ import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.SP; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** @@ -50,16 +46,9 @@ import static org.mockito.Mockito.when; @PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, FabricPrivacy.class, SP.class, Context.class, OpenAPSMAPlugin.class, OpenAPSAMAPlugin.class, OpenAPSSMBPlugin.class}) public class ConstraintsCheckerTest { - PumpInterface pump = new VirtualPumpPlugin(); + VirtualPumpPlugin pump = new VirtualPumpPlugin(); ConstraintChecker constraintChecker; - ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); - MainApp mainApp = mock(MainApp.class); - MockedBus bus = new MockedBus(); - - String validProfile = "{\"dia\":\"3\",\"carbratio\":[{\"time\":\"00:00\",\"value\":\"30\"}],\"carbs_hr\":\"20\",\"delay\":\"20\",\"sens\":[{\"time\":\"00:00\",\"value\":\"100\"},{\"time\":\"2:00\",\"value\":\"110\"}],\"timezone\":\"UTC\",\"basal\":[{\"time\":\"00:00\",\"value\":\"1\"}],\"target_low\":[{\"time\":\"00:00\",\"value\":\"4\"}],\"target_high\":[{\"time\":\"00:00\",\"value\":\"5\"}],\"startDate\":\"1970-01-01T00:00:00.000Z\",\"units\":\"mmol\"}"; - Profile profile = new Profile(new JSONObject(validProfile), Constants.MGDL); - SafetyPlugin safetyPlugin; ObjectivesPlugin objectivesPlugin; ComboPlugin comboPlugin; @@ -72,112 +61,63 @@ public class ConstraintsCheckerTest { public ConstraintsCheckerTest() throws JSONException { } - // isLoopInvokationAllowed tests @Test - public void pumpDescriptionShouldLimitLoopInvokation() throws Exception { - pump.getPumpDescription().isTempBasalCapable = false; - - Constraint c = constraintChecker.isLoopInvokationAllowed(); - Assert.assertEquals(true, c.getReasons().contains("Pump is not temp basal capable")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - @Test - public void notStartedObjectivesShouldLimitLoopInvokation() throws Exception { - objectivesPlugin.objectives.get(0).setStarted(new Date(0)); - - Constraint c = constraintChecker.isLoopInvokationAllowed(); - Assert.assertEquals(true, c.getReasons().contains("Objective 1 not started")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - @Test - public void invalidBasalRateOnComboPumpShouldLimitLoopInvokation() throws Exception { + public void isLoopInvokationAllowedTest() throws Exception { comboPlugin.setFragmentEnabled(PluginBase.PUMP, true); comboPlugin.setValidBasalRateProfileSelectedOnPump(false); Constraint c = constraintChecker.isLoopInvokationAllowed(); - Assert.assertEquals(true, c.getReasons().contains("No valid basal rate read from pump")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - // isClosedLoopAllowed tests - @Test - public void disabledEngineeringModeShouldLimitClosedLoop() throws Exception { - when(SP.getString("aps_mode", "open")).thenReturn("closed"); - when(MainApp.isEngineeringModeOrRelease()).thenReturn(false); - - Constraint c = constraintChecker.isClosedLoopAllowed(); - Assert.assertEquals(true, c.getReasons().contains("Running dev version. Closed loop is disabled.")); + Assert.assertEquals(true, c.getReasonList().size() == 2); // Combo & Objectives + Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 2); // Combo & Objectives Assert.assertEquals(Boolean.FALSE, c.value()); } @Test - public void setOpenLoopInPreferencesShouldLimitClosedLoop() throws Exception { - when(SP.getString("aps_mode", "open")).thenReturn("open"); - - Constraint c = constraintChecker.isClosedLoopAllowed(); - Assert.assertEquals(true, c.getReasons().contains("Closed loop mode disabled in preferences")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - @Test - public void notStartedObjective4ShouldLimitClosedLoop() throws Exception { + public void isClosedLoopAllowedTest() throws Exception { when(SP.getString("aps_mode", "open")).thenReturn("closed"); objectivesPlugin.objectives.get(3).setStarted(new Date(0)); Constraint c = constraintChecker.isClosedLoopAllowed(); - Assert.assertEquals(true, c.getReasons().contains("Objective 4 not started")); + Assert.assertEquals(true, c.getReasonList().size() == 2); // Safety & Objectives + Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 2); // Safety & Objectives + Assert.assertEquals(Boolean.FALSE, c.value()); + + when(SP.getString("aps_mode", "open")).thenReturn("open"); + c = constraintChecker.isClosedLoopAllowed(); + Assert.assertEquals(true, c.getReasonList().size() == 3); // 2x Safety & Objectives + Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 3); // 2x Safety & Objectives Assert.assertEquals(Boolean.FALSE, c.value()); } - // isAutosensModeEnabled tests @Test - public void notStartedObjective6ShouldLimitAutosensMode() throws Exception { - objectivesPlugin.objectives.get(5).setStarted(new Date(0)); - - Constraint c = constraintChecker.isAutosensModeEnabled(); - Assert.assertEquals(true, c.getReasons().contains("Objective 6 not started")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - // isAutosensModeEnabled tests - @Test - public void notEnabledAutosensInPreferencesDisablesAutosens() throws Exception { + public void isAutosensModeEnabledTest() throws Exception { objectivesPlugin.objectives.get(5).setStarted(new Date(0)); when(SP.getBoolean(R.string.key_openapsama_useautosens, false)).thenReturn(false); Constraint c = constraintChecker.isAutosensModeEnabled(); - Assert.assertEquals(true, c.getReasons().contains("Autosens disabled in preferences")); - Assert.assertEquals(true, c.getReasons().contains("Objective 6 not started")); + Assert.assertEquals(true, c.getReasonList().size() == 2); // Safety & Objectives + Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 2); // Safety & Objectives Assert.assertEquals(Boolean.FALSE, c.value()); } @Test - public void notStartedObjective7ShouldLimitAMAMode() throws Exception { + public void isAMAModeEnabledTest() 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(true, c.getReasonList().size() == 1); // Objectives + Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 1); // Objectives Assert.assertEquals(Boolean.FALSE, c.value()); } - // isSMBModeEnabled tests @Test - public void notEnabledSMBInPreferencesDisablesSMB() throws Exception { + public void isSMBModeEnabledTest() throws Exception { + objectivesPlugin.objectives.get(7).setStarted(new Date(0)); when(SP.getBoolean(R.string.key_use_smb, false)).thenReturn(false); Constraint c = constraintChecker.isSMBModeEnabled(); - Assert.assertEquals(true, c.getReasons().contains("SMB disabled in preferences")); - Assert.assertEquals(Boolean.FALSE, c.value()); - } - - @Test - public void notStartedObjective8ShouldLimitSMBMode() throws Exception { - objectivesPlugin.objectives.get(7).setStarted(new Date(0)); - - Constraint c = constraintChecker.isSMBModeEnabled(); - Assert.assertEquals(true, c.getReasons().contains("Objective 8 not started")); + Assert.assertEquals(true, c.getReasonList().size() == 2); // Safety & Objectives + Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 2); // Safety & Objectives Assert.assertEquals(Boolean.FALSE, c.value()); } @@ -195,33 +135,20 @@ public class ConstraintsCheckerTest { result.maximumBasalAmount = 1.1d; insightPlugin.setStatusResult(result); - // No limit by default when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d); when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d); when(SP.getString(R.string.key_age, "")).thenReturn("child"); - // Negative basal not allowed - Constraint d = new Constraint<>(-0.5d); - constraintChecker.applyBasalConstraints(d, profile); - Assert.assertEquals(0d, d.value()); - Assert.assertEquals("SafetyPlugin: Limiting basal rate to 0.00 U/h because of it must be positive value", d.getReasons()); - // Apply all limits - d = constraintChecker.getMaxBasalAllowed(profile); + Constraint d = constraintChecker.getMaxBasalAllowed(AAPSMocker.getValidProfile()); Assert.assertEquals(0.8d, d.value()); - Assert.assertEquals("SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + - "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + - "SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + - "SafetyPlugin: Limiting basal rate to 2.00 U/h because of hard limit\n" + - "DanaRPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" + - "DanaRSPlugin: Limiting basal rate to 0.80 U/h because of pump limit\n" + - "InsightPumpPlugin: Limiting basal rate to 1.10 U/h because of pump limit", d.getReasons()); + Assert.assertEquals(true, d.getReasonList().size() == 7); // 4x Safety & RS & R & Insight + Assert.assertEquals("DanaR: Limiting basal rate to 0.80 U/h because of pump limit", d.getMostLimitedReasons()); } - // applyBasalConstraints tests @Test public void percentBasalRateShouldBeLimited() throws Exception { // DanaR, RS @@ -235,36 +162,17 @@ public class ConstraintsCheckerTest { result.maximumBasalAmount = 1.1d; insightPlugin.setStatusResult(result); - // No limit by default when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d); when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d); when(SP.getString(R.string.key_age, "")).thenReturn("child"); - // Negative basal not allowed - Constraint i = new Constraint<>(-22); - constraintChecker.applyBasalPercentConstraints(i, profile); - Assert.assertEquals((Integer)0, i.value()); - Assert.assertEquals("SafetyPlugin: Percent rate -22% recalculated to -0.22 U/h with current basal 1.00 U/h\n" + - "SafetyPlugin: Limiting basal rate to 0.00 U/h because of it must be positive value\n" + - "SafetyPlugin: Limiting percent rate to 0% because of pump limit\n" + - "DanaRPlugin: Limiting percent rate to 0% because of it must be positive value\n" + - "DanaRSPlugin: Limiting percent rate to 0% because of it must be positive value\n" + - "InsightPumpPlugin: Limiting percent rate to 0% because of it must be positive value", i.getReasons()); - // Apply all limits - i = constraintChecker.getMaxBasalPercentAllowed(profile); - Assert.assertEquals((Integer)100, i.value()); - Assert.assertEquals("SafetyPlugin: Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h\n" + - "SafetyPlugin: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + - "SafetyPlugin: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + - "SafetyPlugin: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + - "SafetyPlugin: Limiting basal rate to 2.00 U/h because of hard limit\n" + - "SafetyPlugin: Limiting percent rate to 100% because of pump limit\n" + - "DanaRPlugin: Limiting percent rate to 200% because of pump limit\n" + - "DanaRSPlugin: Limiting percent rate to 200% because of pump limit\n" + - "InsightPumpPlugin: Limiting percent rate to 250% because of pump limit", i.getReasons()); + Constraint i = constraintChecker.getMaxBasalPercentAllowed(AAPSMocker.getValidProfile()); + Assert.assertEquals((Integer) 100, i.value()); + Assert.assertEquals(true, i.getReasonList().size() == 9); // 6x Safety & RS & R & Insight + Assert.assertEquals("Safety: Limiting percent rate to 100% because of pump limit", i.getMostLimitedReasons()); } @@ -282,25 +190,15 @@ public class ConstraintsCheckerTest { result.maximumBolusAmount = 7d; insightPlugin.setStatusResult(result); - // No limit by default when(SP.getDouble(R.string.key_treatmentssafety_maxbolus, 3d)).thenReturn(3d); when(SP.getString(R.string.key_age, "")).thenReturn("child"); - // Negative bolus not allowed - Constraint d = new Constraint<>(-22d); - constraintChecker.applyBolusConstraints(d); - Assert.assertEquals(0d, d.value()); - Assert.assertEquals("SafetyPlugin: Limiting bolus to 0.0 U because of it must be positive value", d.getReasons()); - // Apply all limits - d = constraintChecker.getMaxBolusAllowed(); + Constraint d = constraintChecker.getMaxBolusAllowed(); Assert.assertEquals(3d, d.value()); - Assert.assertEquals("SafetyPlugin: Limiting bolus to 3.0 U because of max value in preferences\n" + - "SafetyPlugin: Limiting bolus to 5.0 U because of hard limit\n" + - "DanaRPlugin: Limiting bolus to 6.0 U because of pump limit\n" + - "DanaRSPlugin: Limiting bolus to 6.0 U because of pump limit\n" + - "InsightPumpPlugin: Limiting bolus to 7.0 U because of pump limit", d.getReasons()); + Assert.assertEquals(true, d.getReasonList().size() == 5); // 2x Safety & RS & R & Insight + Assert.assertEquals("Safety: Limiting bolus to 3.0 U because of max value in preferences", d.getMostLimitedReasons()); } @@ -310,33 +208,16 @@ public class ConstraintsCheckerTest { // No limit by default when(SP.getInt(R.string.key_treatmentssafety_maxcarbs, 48)).thenReturn(48); - // Negative carbs not allowed - Constraint i = new Constraint<>(-22); - constraintChecker.applyCarbsConstraints(i); - Assert.assertEquals((Integer) 0, i.value()); - Assert.assertEquals("SafetyPlugin: Limiting carbs to 0 g because of it must be positive value", i.getReasons()); - // Apply all limits - i = constraintChecker.getMaxCarbsAllowed(); + Constraint i = constraintChecker.getMaxCarbsAllowed(); Assert.assertEquals((Integer) 48, i.value()); - Assert.assertEquals("SafetyPlugin: Limiting carbs to 48 g because of max value in preferences", i.getReasons()); + Assert.assertEquals(true, i.getReasonList().size() == 1); + Assert.assertEquals("Safety: Limiting carbs to 48 g because of max value in preferences", i.getMostLimitedReasons()); } // applyMaxIOBConstraints tests @Test public void iobShouldBeLimited() throws Exception { - // DanaR, RS - danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); - danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); - DanaRPump.getInstance().maxBolus = 6d; - - // Insight - insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); - StatusTaskRunner.Result result = new StatusTaskRunner.Result(); - result.maximumBolusAmount = 7d; - insightPlugin.setStatusResult(result); - - // No limit by default when(SP.getDouble(R.string.key_openapsma_max_iob, 1.5d)).thenReturn(1.5d); when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); @@ -347,62 +228,31 @@ public class ConstraintsCheckerTest { // Apply all limits Constraint d = constraintChecker.getMaxIOBAllowed(); Assert.assertEquals(1.5d, d.value()); - Assert.assertEquals("SafetyPlugin: Limiting IOB to 1.5 U because of max value in preferences\n" + - "SafetyPlugin: Limiting IOB to 7.0 U because of hard limit\n" + - "SafetyPlugin: Limiting IOB to 7.0 U because of hard limit\n" + - "SafetyPlugin: Limiting IOB to 12.0 U because of hard limit", d.getReasons()); + Assert.assertEquals(true, d.getReasonList().size() == 4); + Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences", d.getMostLimitedReasons()); } @Before public void prepareMock() throws Exception { - Locale.setDefault(new Locale("en", "US")); - PowerMockito.mockStatic(ConfigBuilderPlugin.class); - - PowerMockito.mockStatic(MainApp.class); - when(MainApp.instance()).thenReturn(mainApp); - when(MainApp.getConfigBuilder()).thenReturn(configBuilderPlugin); - when(MainApp.getConfigBuilder().getActivePump()).thenReturn(pump); - - constraintChecker = new ConstraintChecker(mainApp); PowerMockito.mockStatic(FabricPrivacy.class); - Context context = mock(Context.class); - when(MainApp.instance().getApplicationContext()).thenReturn(context); + MainApp mainApp = AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockApplicationContext(); + AAPSMocker.mockBus(); + AAPSMocker.mockStrings(); + AAPSMocker.mockSP(); - when(MainApp.bus()).thenReturn(bus); - - when(MainApp.gs(R.string.pumpisnottempbasalcapable)).thenReturn("Pump is not temp basal capable"); - when(MainApp.gs(R.string.closed_loop_disabled_on_dev_branch)).thenReturn("Running dev version. Closed loop is disabled."); - 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.autosensdisabledinpreferences)).thenReturn("Autosens disabled in preferences"); - when(MainApp.gs(R.string.smbdisabledinpreferences)).thenReturn("SMB disabled in preferences"); - when(MainApp.gs(R.string.limitingbasalratio)).thenReturn("Limiting basal rate to %.2f U/h because of %s"); - when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); - when(MainApp.gs(R.string.itmustbepositivevalue)).thenReturn("it must be positive value"); - when(MainApp.gs(R.string.maxvalueinpreferences)).thenReturn("max value in preferences"); - when(MainApp.gs(R.string.maxbasalmultiplier)).thenReturn("max basal multiplier"); - when(MainApp.gs(R.string.maxdailybasalmultiplier)).thenReturn("max daily basal multiplier"); - when(MainApp.gs(R.string.limitingpercentrate)).thenReturn("Limiting percent rate to %d%% because of %s"); - when(MainApp.gs(R.string.pumplimit)).thenReturn("pump limit"); - when(MainApp.gs(R.string.limitingbolus)).thenReturn("Limiting bolus to %.1f U because of %s"); - when(MainApp.gs(R.string.hardlimit)).thenReturn("hard limit"); - when(MainApp.gs(R.string.key_child)).thenReturn("child"); - when(MainApp.gs(R.string.limitingcarbs)).thenReturn("Limiting carbs to %d g because of %s"); - when(MainApp.gs(R.string.limitingiob)).thenReturn("Limiting IOB to %.1f U because of %s"); - - PowerMockito.mockStatic(SP.class); - //PowerMockito.mock(OpenAPSMAPlugin.class); - //PowerMockito.mock(OpenAPSAMAPlugin.class); - //PowerMockito.mock(OpenAPSSMBPlugin.class); - - PowerMockito.mockStatic(SP.class); // RS constructor when(SP.getString(R.string.key_danars_address, "")).thenReturn(""); + //SafetyPlugin + when(MainApp.getConfigBuilder().getActivePump()).thenReturn(pump); + + constraintChecker = new ConstraintChecker(mainApp); + safetyPlugin = SafetyPlugin.getPlugin(); objectivesPlugin = ObjectivesPlugin.getPlugin(); comboPlugin = ComboPlugin.getPlugin(); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPluginTest.java new file mode 100644 index 0000000000..116fd3fee6 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsObjectives/ObjectivesPluginTest.java @@ -0,0 +1,90 @@ +package info.nightscout.androidaps.plugins.ConstraintsObjectives; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.Date; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.Constraint; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.utils.SP; + +/** + * Created by mike on 23.03.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, SP.class}) +public class ObjectivesPluginTest { + + ObjectivesPlugin objectivesPlugin; + + @Test + public void notStartedObjectivesShouldLimitLoopInvokation() throws Exception { + objectivesPlugin.objectives.get(0).setStarted(new Date(0)); + + Constraint c = new Constraint<>(true); + c = objectivesPlugin.isLoopInvokationAllowed(c); + Assert.assertEquals("Objectives: Objective 1 not started", c.getReasons()); + Assert.assertEquals(Boolean.FALSE, c.value()); + objectivesPlugin.objectives.get(0).setStarted(new Date()); + } + + @Test + public void notStartedObjective4ShouldLimitClosedLoop() throws Exception { + objectivesPlugin.objectives.get(3).setStarted(new Date(0)); + + Constraint c = new Constraint<>(true); + c = objectivesPlugin.isClosedLoopAllowed(c); + Assert.assertEquals(true, c.getReasons().contains("Objective 4 not started")); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + @Test + public void notStartedObjective6ShouldLimitAutosensMode() throws Exception { + objectivesPlugin.objectives.get(5).setStarted(new Date(0)); + + Constraint c = new Constraint<>(true); + c = objectivesPlugin.isAutosensModeEnabled(c); + Assert.assertEquals(true, c.getReasons().contains("Objective 6 not started")); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + @Test + public void notStartedObjective7ShouldLimitAMAMode() throws Exception { + objectivesPlugin.objectives.get(6).setStarted(new Date(0)); + + Constraint c = new Constraint<>(true); + c = objectivesPlugin.isAMAModeEnabled(c); + Assert.assertEquals(true, c.getReasons().contains("Objective 7 not started")); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + @Test + public void notStartedObjective8ShouldLimitSMBMode() throws Exception { + objectivesPlugin.objectives.get(7).setStarted(new Date(0)); + + Constraint c = new Constraint<>(true); + c = objectivesPlugin.isSMBModeEnabled(c); + Assert.assertEquals(true, c.getReasons().contains("Objective 8 not started")); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + @Before + public void prepareMock() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockBus(); + AAPSMocker.mockSP(); + AAPSMocker.mockStrings(); + + objectivesPlugin = ObjectivesPlugin.getPlugin(); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java new file mode 100644 index 0000000000..c88f60fd92 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java @@ -0,0 +1,214 @@ +package info.nightscout.androidaps.plugins.ConstraintsSafety; + +import android.content.Context; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.AAPSMocker; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.Constraint; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; +import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAPlugin; +import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin; +import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; +import info.nightscout.utils.SP; + +import static org.mockito.Mockito.when; + +/** + * Created by mike on 23.03.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, SP.class, Context.class}) +public class SafetyPluginTest { + + VirtualPumpPlugin pump = new VirtualPumpPlugin(); + SafetyPlugin safetyPlugin; + + @Test + public void pumpDescriptionShouldLimitLoopInvokation() throws Exception { + pump.getPumpDescription().isTempBasalCapable = false; + + Constraint c = new Constraint<>(true); + c = safetyPlugin.isLoopInvokationAllowed(c); + Assert.assertEquals("Safety: Pump is not temp basal capable", c.getReasons()); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + @Test + public void disabledEngineeringModeShouldLimitClosedLoop() throws Exception { + when(SP.getString("aps_mode", "open")).thenReturn("closed"); + when(MainApp.isEngineeringModeOrRelease()).thenReturn(false); + + Constraint c = new Constraint<>(true); + c = safetyPlugin.isClosedLoopAllowed(c); + Assert.assertEquals(true, c.getReasons().contains("Running dev version. Closed loop is disabled.")); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + @Test + public void setOpenLoopInPreferencesShouldLimitClosedLoop() throws Exception { + when(SP.getString("aps_mode", "open")).thenReturn("open"); + + Constraint c = new Constraint<>(true); + c = safetyPlugin.isClosedLoopAllowed(c); + Assert.assertEquals(true, c.getReasons().contains("Closed loop mode disabled in preferences")); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + @Test + public void notEnabledSMBInPreferencesDisablesSMB() throws Exception { + when(SP.getBoolean(R.string.key_use_smb, false)).thenReturn(false); + + Constraint c = new Constraint<>(true); + c = safetyPlugin.isSMBModeEnabled(c); + Assert.assertEquals(true, c.getReasons().contains("SMB disabled in preferences")); + Assert.assertEquals(Boolean.FALSE, c.value()); + } + + @Test + public void basalRateShouldBeLimited() throws Exception { + when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); + when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d); + when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d); + when(SP.getString(R.string.key_age, "")).thenReturn("child"); + + Constraint c = new Constraint<>(Constants.REALLYHIGHBASALRATE); + safetyPlugin.applyBasalConstraints(c, AAPSMocker.getValidProfile()); + Assert.assertEquals(1d, c.value()); + Assert.assertEquals("Safety: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + + "Safety: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + + "Safety: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + + "Safety: Limiting basal rate to 2.00 U/h because of hard limit", c.getReasons()); + Assert.assertEquals("Safety: Limiting basal rate to 1.00 U/h because of max value in preferences", c.getMostLimitedReasons()); + + } + + @Test + public void doNotAllowNegativeBasalRate() throws Exception { + when(SP.getString(R.string.key_age, "")).thenReturn("child"); + + Constraint d = new Constraint<>(-0.5d); + safetyPlugin.applyBasalConstraints(d, AAPSMocker.getValidProfile()); + Assert.assertEquals(0d, d.value()); + Assert.assertEquals("Safety: Limiting basal rate to 0.00 U/h because of it must be positive value", d.getReasons()); + } + + @Test + public void percentBasalRateShouldBeLimited() throws Exception { + // No limit by default + when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); + when(SP.getDouble(R.string.key_openapsama_current_basal_safety_multiplier, 4d)).thenReturn(4d); + when(SP.getDouble(R.string.key_openapsama_max_daily_safety_multiplier, 3d)).thenReturn(3d); + when(SP.getString(R.string.key_age, "")).thenReturn("child"); + + + Constraint i = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); + safetyPlugin.applyBasalPercentConstraints(i, AAPSMocker.getValidProfile()); + Assert.assertEquals((Integer) 100, i.value()); + Assert.assertEquals("Safety: Percent rate 1111111% recalculated to 11111.11 U/h with current basal 1.00 U/h\n" + + "Safety: Limiting basal rate to 1.00 U/h because of max value in preferences\n" + + "Safety: Limiting basal rate to 4.00 U/h because of max basal multiplier\n" + + "Safety: Limiting basal rate to 3.00 U/h because of max daily basal multiplier\n" + + "Safety: Limiting basal rate to 2.00 U/h because of hard limit\n" + + "Safety: Limiting percent rate to 100% because of pump limit", i.getReasons()); + Assert.assertEquals("Safety: Limiting percent rate to 100% because of pump limit", i.getMostLimitedReasons()); + } + + @Test + public void doNotAllowNegativePercentBasalRate() throws Exception { + when(SP.getString(R.string.key_age, "")).thenReturn("child"); + + Constraint i = new Constraint<>(-22); + safetyPlugin.applyBasalPercentConstraints(i, AAPSMocker.getValidProfile()); + Assert.assertEquals((Integer) 0, i.value()); + Assert.assertEquals("Safety: Percent rate -22% recalculated to -0.22 U/h with current basal 1.00 U/h\n" + + "Safety: Limiting basal rate to 0.00 U/h because of it must be positive value\n" + + "Safety: Limiting percent rate to 0% because of pump limit", i.getReasons()); + Assert.assertEquals("Safety: Limiting percent rate to 0% because of pump limit", i.getMostLimitedReasons()); + } + + @Test + public void bolusAmountShouldBeLimited() throws Exception { + when(SP.getDouble(R.string.key_treatmentssafety_maxbolus, 3d)).thenReturn(3d); + when(SP.getString(R.string.key_age, "")).thenReturn("child"); + + Constraint d = new Constraint<>(Constants.REALLYHIGHBOLUS); + d = safetyPlugin.applyBolusConstraints(d); + Assert.assertEquals(3d, d.value()); + Assert.assertEquals("Safety: Limiting bolus to 3.0 U because of max value in preferences\n" + + "Safety: Limiting bolus to 5.0 U because of hard limit", d.getReasons()); + Assert.assertEquals("Safety: Limiting bolus to 3.0 U because of max value in preferences", d.getMostLimitedReasons()); + } + + @Test + public void doNotAllowNegativeBolusAmount() throws Exception { + when(SP.getDouble(R.string.key_treatmentssafety_maxbolus, 3d)).thenReturn(3d); + when(SP.getString(R.string.key_age, "")).thenReturn("child"); + + Constraint d = new Constraint<>(-22d); + d = safetyPlugin.applyBolusConstraints(d); + Assert.assertEquals(0d, d.value()); + Assert.assertEquals("Safety: Limiting bolus to 0.0 U because of it must be positive value", d.getReasons()); + Assert.assertEquals("Safety: Limiting bolus to 0.0 U because of it must be positive value", d.getMostLimitedReasons()); + } + + @Test + public void carbsAmountShouldBeLimited() throws Exception { + // No limit by default + when(SP.getInt(R.string.key_treatmentssafety_maxcarbs, 48)).thenReturn(48); + + // Negative carbs not allowed + Constraint i = new Constraint<>(-22); + safetyPlugin.applyCarbsConstraints(i); + Assert.assertEquals((Integer) 0, i.value()); + Assert.assertEquals("Safety: Limiting carbs to 0 g because of it must be positive value", i.getReasons()); + + // Apply all limits + i = safetyPlugin.applyCarbsConstraints(new Constraint<>(Constants.REALLYHIGHCARBS)); + Assert.assertEquals((Integer) 48, i.value()); + Assert.assertEquals("Safety: Limiting carbs to 48 g because of max value in preferences", i.getReasons()); + } + + @Test + public void iobShouldBeLimited() throws Exception { + when(SP.getDouble(R.string.key_openapsma_max_iob, 1.5d)).thenReturn(1.5d); + when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); + OpenAPSMAPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); + OpenAPSAMAPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); + OpenAPSSMBPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); + + // Apply all limits + Constraint d = new Constraint<>(Constants.REALLYHIGHIOB); + d = safetyPlugin.applyMaxIOBConstraints(d); + Assert.assertEquals(1.5d, d.value()); + Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences\n" + + "Safety: Limiting IOB to 7.0 U because of hard limit\n" + + "Safety: Limiting IOB to 7.0 U because of hard limit\n" + + "Safety: Limiting IOB to 12.0 U because of hard limit", d.getReasons()); + Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences", d.getMostLimitedReasons()); + } + + @Before + public void prepareMock() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockSP(); + AAPSMocker.mockStrings(); + + when(MainApp.getConfigBuilder().getActivePump()).thenReturn(pump); + + safetyPlugin = SafetyPlugin.getPlugin(); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPluginTest.java index a87cbbfbd4..05e3e58866 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPluginTest.java @@ -2,62 +2,68 @@ package info.nightscout.androidaps.plugins.PumpCombo; import android.content.Context; -import com.squareup.otto.Bus; -import com.squareup.otto.ThreadEnforcer; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import info.AAPSMocker; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.Constraint; +import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.PumpCombo.ruffyscripter.history.Bolus; import info.nightscout.utils.ToastUtils; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class}) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class}) public class ComboPluginTest { - @Before - public void prepareMocks() throws Exception { - ConfigBuilderPlugin configBuilderPlugin = mock(ConfigBuilderPlugin.class); - PowerMockito.mockStatic(ConfigBuilderPlugin.class); + ComboPlugin comboPlugin; - PowerMockito.mockStatic(MainApp.class); - MainApp mainApp = mock(MainApp.class); - when(MainApp.getConfigBuilder()).thenReturn(configBuilderPlugin); - when(MainApp.instance()).thenReturn(mainApp); - Bus bus = new Bus(ThreadEnforcer.ANY); - when(MainApp.bus()).thenReturn(bus); - when(MainApp.gs(0)).thenReturn(""); + @Test + public void invalidBasalRateOnComboPumpShouldLimitLoopInvokation() throws Exception { + comboPlugin.setFragmentEnabled(PluginBase.PUMP, true); + comboPlugin.setValidBasalRateProfileSelectedOnPump(false); + + Constraint c = new Constraint<>(true); + c = comboPlugin.isLoopInvokationAllowed(c); + Assert.assertEquals("Combo: No valid basal rate read from pump", c.getReasons()); + Assert.assertEquals(Boolean.FALSE, c.value()); + comboPlugin.setFragmentEnabled(PluginBase.PUMP, false); } @Test public void calculateFakePumpTimestamp() throws Exception { - ComboPlugin plugin = ComboPlugin.getPlugin(); long now = System.currentTimeMillis(); long pumpTimestamp = now - now % 1000; // same timestamp, different bolus leads to different fake timestamp Assert.assertNotEquals( - plugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.1, true)), - plugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.3, true)) + comboPlugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.1, true)), + comboPlugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.3, true)) ); // different timestamp, same bolus leads to different fake timestamp Assert.assertNotEquals( - plugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.3, true)), - plugin.calculateFakeBolusDate(new Bolus(pumpTimestamp + 60 * 1000, 0.3, true)) + comboPlugin.calculateFakeBolusDate(new Bolus(pumpTimestamp, 0.3, true)), + comboPlugin.calculateFakeBolusDate(new Bolus(pumpTimestamp + 60 * 1000, 0.3, true)) ); // generated timestamp has second-precision Bolus bolus = new Bolus(pumpTimestamp, 0.2, true); - long calculatedTimestamp = plugin.calculateFakeBolusDate(bolus); + long calculatedTimestamp = comboPlugin.calculateFakeBolusDate(bolus); assertEquals(calculatedTimestamp, calculatedTimestamp - calculatedTimestamp % 1000); } + + @Before + public void prepareMocks() throws Exception { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockBus(); + AAPSMocker.mockStrings(); + + comboPlugin = ComboPlugin.getPlugin(); + } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPluginTest.java new file mode 100644 index 0000000000..837298fe1d --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPluginTest.java @@ -0,0 +1,75 @@ +package info.nightscout.androidaps.plugins.PumpDanaRS; + +import android.content.Context; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.AAPSMocker; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.Constraint; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; +import info.nightscout.utils.SP; +import info.nightscout.utils.ToastUtils; + +import static org.mockito.Mockito.when; + +/** + * Created by mike on 23.03.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class, SP.class}) +public class DanaRSPluginTest { + + DanaRSPlugin danaRSPlugin; + + @Test + public void basalRateShouldBeLimited() throws Exception { + danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + DanaRPump.getInstance().maxBasal = 0.8d; + + Constraint c = new Constraint<>(Constants.REALLYHIGHBASALRATE); + danaRSPlugin.applyBasalConstraints(c, AAPSMocker.getValidProfile()); + Assert.assertEquals(0.8d, c.value()); + Assert.assertEquals("DanaRS: Limiting basal rate to 0.80 U/h because of pump limit", c.getReasons()); + Assert.assertEquals("DanaRS: Limiting basal rate to 0.80 U/h because of pump limit", c.getMostLimitedReasons()); + } + + @Test + public void percentBasalRateShouldBeLimited() throws Exception { + danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + DanaRPump.getInstance().maxBasal = 0.8d; + + Constraint c = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); + danaRSPlugin.applyBasalPercentConstraints(c, AAPSMocker.getValidProfile()); + Assert.assertEquals((Integer) 200, c.value()); + Assert.assertEquals("DanaRS: Limiting percent rate to 200% because of pump limit", c.getReasons()); + Assert.assertEquals("DanaRS: Limiting percent rate to 200% because of pump limit", c.getMostLimitedReasons()); + } + + @Before + public void prepareMocks() throws Exception { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockBus(); + AAPSMocker.mockStrings(); + AAPSMocker.mockApplicationContext(); + AAPSMocker.mockSP(); + + when(SP.getString(R.string.key_danars_address, "")).thenReturn(""); + + danaRSPlugin = DanaRSPlugin.getPlugin(); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/PumpInsightTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/PumpInsightTest.java new file mode 100644 index 0000000000..e8481f64ef --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/PumpInsightTest.java @@ -0,0 +1,56 @@ +package info.nightscout.androidaps.plugins.PumpInsight; + +import android.content.Context; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.AAPSMocker; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.interfaces.Constraint; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.PumpInsight.connector.StatusTaskRunner; +import info.nightscout.utils.ToastUtils; + +/** + * Created by mike on 23.03.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class}) +public class PumpInsightTest { + + InsightPumpPlugin insightPlugin; + + @Test + public void basalRateShouldBeLimited() throws Exception { + insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + StatusTaskRunner.Result result = new StatusTaskRunner.Result(); + result.maximumBasalAmount = 1.1d; + insightPlugin.setStatusResult(result); + + Constraint c = new Constraint<>(Constants.REALLYHIGHBASALRATE); + insightPlugin.applyBasalConstraints(c, AAPSMocker.getValidProfile()); + Assert.assertEquals(1.1d, c.value()); + Assert.assertEquals("InsightPump: Limiting basal rate to 1.10 U/h because of pump limit", c.getReasons()); + Assert.assertEquals("InsightPump: Limiting basal rate to 1.10 U/h because of pump limit", c.getMostLimitedReasons()); + } + + @Before + public void prepareMocks() throws Exception { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockBus(); + AAPSMocker.mockStrings(); + + insightPlugin = InsightPumpPlugin.getPlugin(); + } + +} From 661595792f403af1ec882f30b218d57b02d23dd7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 23 Mar 2018 17:51:05 +0100 Subject: [PATCH 26/28] rename InsightPump -> Insight --- .../info/nightscout/androidaps/MainApp.java | 4 +-- ...cAdapter.java => InsightAsyncAdapter.java} | 10 +++--- ...PumpFragment.java => InsightFragment.java} | 12 +++---- ...ightPumpPlugin.java => InsightPlugin.java} | 36 +++++++++---------- .../plugins/PumpInsight/Mstatus.java | 4 +-- .../PumpInsight/connector/Connector.java | 4 +-- ...allback.java => EventInsightCallback.java} | 4 +-- ...ateGui.java => EventInsightUpdateGui.java} | 2 +- .../main/res/layout/insightpump_fragment.xml | 2 +- .../interfaces/ConstraintsCheckerTest.java | 6 ++-- ...nsightTest.java => InsightPluginTest.java} | 10 +++--- 11 files changed, 47 insertions(+), 47 deletions(-) rename app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/{InsightPumpAsyncAdapter.java => InsightAsyncAdapter.java} (91%) rename app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/{InsightPumpFragment.java => InsightFragment.java} (89%) rename app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/{InsightPumpPlugin.java => InsightPlugin.java} (97%) rename app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/{EventInsightPumpCallback.java => EventInsightCallback.java} (86%) rename app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/{EventInsightPumpUpdateGui.java => EventInsightUpdateGui.java} (72%) rename app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/{PumpInsightTest.java => InsightPluginTest.java} (81%) diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 28c3add9ba..d369b13fa7 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -60,7 +60,7 @@ import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin; import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin; -import info.nightscout.androidaps.plugins.PumpInsight.InsightPumpPlugin; +import info.nightscout.androidaps.plugins.PumpInsight.InsightPlugin; import info.nightscout.androidaps.plugins.PumpMDI.MDIPlugin; import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; import info.nightscout.androidaps.plugins.SensitivityAAPS.SensitivityAAPSPlugin; @@ -156,7 +156,7 @@ public class MainApp extends Application { if (Config.HWPUMPS) pluginsList.add(DanaRv2Plugin.getPlugin()); if (Config.HWPUMPS) pluginsList.add(DanaRSPlugin.getPlugin()); pluginsList.add(CareportalPlugin.getPlugin()); - if (Config.HWPUMPS && engineeringMode) pluginsList.add(InsightPumpPlugin.getPlugin()); // <-- Enable Insight plugin here + if (Config.HWPUMPS && engineeringMode) pluginsList.add(InsightPlugin.getPlugin()); // <-- Enable Insight plugin here if (Config.HWPUMPS && engineeringMode) pluginsList.add(ComboPlugin.getPlugin()); // <-- Enable Combo plugin here if (Config.MDI) pluginsList.add(MDIPlugin.getPlugin()); if (Config.VIRTUALPUMP) pluginsList.add(VirtualPumpPlugin.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightAsyncAdapter.java similarity index 91% rename from app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java rename to app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightAsyncAdapter.java index 6f48934059..3759a3f721 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpAsyncAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightAsyncAdapter.java @@ -8,7 +8,7 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback; +import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightCallback; import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.getWakeLock; import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.msSince; @@ -22,11 +22,11 @@ import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.tsl; * */ -public class InsightPumpAsyncAdapter { +public class InsightAsyncAdapter { - private final ConcurrentHashMap commandResults = new ConcurrentHashMap<>(); + private final ConcurrentHashMap commandResults = new ConcurrentHashMap<>(); - InsightPumpAsyncAdapter() { + InsightAsyncAdapter() { MainApp.bus().register(this); } @@ -36,7 +36,7 @@ public class InsightPumpAsyncAdapter { } @Subscribe - public void onStatusEvent(final EventInsightPumpCallback ev) { + public void onStatusEvent(final EventInsightCallback ev) { log("Received callback event: " + ev.toString()); commandResults.put(ev.request_uuid, ev); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightFragment.java similarity index 89% rename from app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpFragment.java rename to app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightFragment.java index 3dfcb9b0a3..156c8271e3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightFragment.java @@ -19,14 +19,14 @@ import java.util.List; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; -import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui; +import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightUpdateGui; import info.nightscout.androidaps.plugins.PumpInsight.utils.StatusItem; import info.nightscout.androidaps.plugins.PumpInsight.utils.ui.StatusItemViewAdapter; import info.nightscout.utils.FabricPrivacy; -public class InsightPumpFragment extends SubscriberFragment { - private static final Logger log = LoggerFactory.getLogger(InsightPumpFragment.class); +public class InsightFragment extends SubscriberFragment { + private static final Logger log = LoggerFactory.getLogger(InsightFragment.class); private static final Handler sLoopHandler = new Handler(); private static volatile boolean refresh = false; private static volatile boolean pending = false; @@ -88,7 +88,7 @@ public class InsightPumpFragment extends SubscriberFragment { @Subscribe - public void onStatusEvent(final EventInsightPumpUpdateGui ev) { + public void onStatusEvent(final EventInsightUpdateGui ev) { updateGUI(); } @@ -99,8 +99,8 @@ public class InsightPumpFragment extends SubscriberFragment { activity.runOnUiThread(new Runnable() { @Override public void run() { - final InsightPumpPlugin insightPumpPlugin = InsightPumpPlugin.getPlugin(); - final List l = insightPumpPlugin.getStatusItems(refresh); + final InsightPlugin insightPlugin = InsightPlugin.getPlugin(); + final List l = insightPlugin.getStatusItems(refresh); holder.removeAllViews(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java similarity index 97% rename from app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java rename to app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java index 9b84f56da6..ba58093d84 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java @@ -36,8 +36,8 @@ import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector; import info.nightscout.androidaps.plugins.PumpInsight.connector.SetTBRTaskRunner; import info.nightscout.androidaps.plugins.PumpInsight.connector.StatusTaskRunner; import info.nightscout.androidaps.plugins.PumpInsight.connector.WriteBasalProfileTaskRunner; -import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback; -import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui; +import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightCallback; +import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightUpdateGui; import info.nightscout.androidaps.plugins.PumpInsight.history.HistoryReceiver; import info.nightscout.androidaps.plugins.PumpInsight.history.LiveHistory; import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers; @@ -74,16 +74,16 @@ import static info.nightscout.androidaps.plugins.PumpInsight.history.PumpIdCache */ @SuppressWarnings("AccessStaticViaInstance") -public class InsightPumpPlugin implements PluginBase, PumpInterface, ConstraintsInterface { +public class InsightPlugin implements PluginBase, PumpInterface, ConstraintsInterface { private static final long BUSY_WAIT_TIME = 20000; static Integer batteryPercent = 0; static Integer reservoirInUnits = 0; static boolean initialized = false; private static volatile boolean update_pending = false; - private static Logger log = LoggerFactory.getLogger(InsightPumpPlugin.class); - private static volatile InsightPumpPlugin plugin; - private final InsightPumpAsyncAdapter async = new InsightPumpAsyncAdapter(); + private static Logger log = LoggerFactory.getLogger(InsightPlugin.class); + private static volatile InsightPlugin plugin; + private final InsightAsyncAdapter async = new InsightAsyncAdapter(); private StatusTaskRunner.Result statusResult; private long statusResultTime = -1; private Date lastDataTime = new Date(0); @@ -96,8 +96,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints private volatile boolean connector_enabled = false; private List profileBlocks; - private InsightPumpPlugin() { - log("InsightPumpPlugin instantiated"); + private InsightPlugin() { + log("InsightPlugin instantiated"); pumpDescription.isBolusCapable = true; pumpDescription.bolusStep = 0.05d; // specification says 0.05U up to 2U then 0.1U @ 2-5U 0.2U @ 10-20U 0.5U 10-20U (are these just UI restrictions?) @@ -129,7 +129,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints } - public static InsightPumpPlugin getPlugin() { + public static InsightPlugin getPlugin() { if (plugin == null) { createInstance(); } @@ -139,7 +139,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints private static synchronized void createInstance() { if (plugin == null) { log("creating instance"); - plugin = new InsightPumpPlugin(); + plugin = new InsightPlugin(); } } @@ -150,10 +150,10 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints private static void updateGui() { update_pending = false; - MainApp.bus().post(new EventInsightPumpUpdateGui()); + MainApp.bus().post(new EventInsightUpdateGui()); } - private static void pushCallbackEvent(EventInsightPumpCallback e) { + private static void pushCallbackEvent(EventInsightCallback e) { MainApp.bus().post(e); } @@ -184,7 +184,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints @Override public String getFragmentClass() { - return InsightPumpFragment.class.getName(); + return InsightFragment.class.getName(); } @Override @@ -289,7 +289,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints @Override public void connect(String reason) { - log("InsightPumpPlugin::connect()"); + log("InsightPlugin::connect()"); try { if (!connector.isPumpConnected()) { if (Helpers.ratelimit("insight-connect-timer", 40)) { @@ -311,7 +311,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints @Override public void disconnect(String reason) { - log("InsightPumpPlugin::disconnect()"); + log("InsightPlugin::disconnect()"); try { if (!SP.getBoolean("insight_always_connected", false)) { log("Requesting disconnect"); @@ -326,7 +326,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints @Override public void stopConnecting() { - log("InsightPumpPlugin::stopConnecting()"); + log("InsightPlugin::stopConnecting()"); try { if (isConnecting()) { if (!SP.getBoolean("insight_always_connected", false)) { @@ -1011,7 +1011,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints // if (!isConnected()) return false; //if (isBusy()) return false; log("asyncSinglecommand called: " + name); - final EventInsightPumpCallback event = new EventInsightPumpCallback(); + final EventInsightCallback event = new EventInsightCallback(); new Thread() { @Override public void run() { @@ -1050,7 +1050,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints // if (!isConnected()) return false; //if (isBusy()) return false; log("asyncTaskRunner called: " + name); - final EventInsightPumpCallback event = new EventInsightPumpCallback(); + final EventInsightCallback event = new EventInsightCallback(); new Thread() { @Override public void run() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java index 57e6da7f2a..8797325f7c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/Mstatus.java @@ -1,6 +1,6 @@ package info.nightscout.androidaps.plugins.PumpInsight; -import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback; +import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightCallback; /** * Created by jamorham on 01/02/2018. @@ -11,7 +11,7 @@ import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCal class Mstatus { Cstatus cstatus = Cstatus.UNKNOWN; - EventInsightPumpCallback event; + EventInsightCallback event; // comment field preparation for results String getCommandComment() { 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 311e487b9f..a96c98a39a 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 @@ -14,7 +14,7 @@ import java.util.Map; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventFeatureRunning; -import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui; +import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightUpdateGui; import info.nightscout.androidaps.plugins.PumpInsight.history.HistoryReceiver; import info.nightscout.androidaps.plugins.PumpInsight.history.LiveHistory; import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers; @@ -76,7 +76,7 @@ public class Connector { extendKeepAliveIfActive(); } - MainApp.bus().post(new EventInsightPumpUpdateGui()); + MainApp.bus().post(new EventInsightUpdateGui()); } else { log("Same status as before: " + status); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightCallback.java similarity index 86% rename from app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java rename to app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightCallback.java index 964270cb45..16cb5af457 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpCallback.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightCallback.java @@ -7,7 +7,7 @@ import info.nightscout.androidaps.events.Event; /** * Created by jamorham on 23/01/2018. */ -public class EventInsightPumpCallback extends Event { +public class EventInsightCallback extends Event { public UUID request_uuid; public boolean success = false; @@ -15,7 +15,7 @@ public class EventInsightPumpCallback extends Event { public int response_id = -1; public Object response_object = null; - public EventInsightPumpCallback() { + public EventInsightCallback() { request_uuid = UUID.randomUUID(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpUpdateGui.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightUpdateGui.java similarity index 72% rename from app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpUpdateGui.java rename to app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightUpdateGui.java index 79e975ac31..3741c607c4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightPumpUpdateGui.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/events/EventInsightUpdateGui.java @@ -5,5 +5,5 @@ import info.nightscout.androidaps.events.EventUpdateGui; /** * Created by jamorham on 23/01/2018. */ -public class EventInsightPumpUpdateGui extends EventUpdateGui { +public class EventInsightUpdateGui extends EventUpdateGui { } diff --git a/app/src/main/res/layout/insightpump_fragment.xml b/app/src/main/res/layout/insightpump_fragment.xml index 4d3d580336..2ab1a50d2a 100644 --- a/app/src/main/res/layout/insightpump_fragment.xml +++ b/app/src/main/res/layout/insightpump_fragment.xml @@ -2,7 +2,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="info.nightscout.androidaps.plugins.PumpInsight.InsightPumpFragment"> + tools:context="info.nightscout.androidaps.plugins.PumpInsight.InsightFragment"> constraintsPluginsList = new ArrayList<>(); constraintsPluginsList.add(safetyPlugin); constraintsPluginsList.add(objectivesPlugin); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/PumpInsightTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPluginTest.java similarity index 81% rename from app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/PumpInsightTest.java rename to app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPluginTest.java index e8481f64ef..a4432b6ffc 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/PumpInsightTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPluginTest.java @@ -25,9 +25,9 @@ import info.nightscout.utils.ToastUtils; @RunWith(PowerMockRunner.class) @PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, ToastUtils.class, Context.class}) -public class PumpInsightTest { +public class InsightPluginTest { - InsightPumpPlugin insightPlugin; + InsightPlugin insightPlugin; @Test public void basalRateShouldBeLimited() throws Exception { @@ -39,8 +39,8 @@ public class PumpInsightTest { Constraint c = new Constraint<>(Constants.REALLYHIGHBASALRATE); insightPlugin.applyBasalConstraints(c, AAPSMocker.getValidProfile()); Assert.assertEquals(1.1d, c.value()); - Assert.assertEquals("InsightPump: Limiting basal rate to 1.10 U/h because of pump limit", c.getReasons()); - Assert.assertEquals("InsightPump: Limiting basal rate to 1.10 U/h because of pump limit", c.getMostLimitedReasons()); + Assert.assertEquals("Insight: Limiting basal rate to 1.10 U/h because of pump limit", c.getReasons()); + Assert.assertEquals("Insight: Limiting basal rate to 1.10 U/h because of pump limit", c.getMostLimitedReasons()); } @Before @@ -50,7 +50,7 @@ public class PumpInsightTest { AAPSMocker.mockBus(); AAPSMocker.mockStrings(); - insightPlugin = InsightPumpPlugin.getPlugin(); + insightPlugin = InsightPlugin.getPlugin(); } } From 30f09dc416f76cc019af2927b0b70f502189c106 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 23 Mar 2018 18:23:19 +0100 Subject: [PATCH 27/28] show constraints on LoopFragment --- .../androidaps/plugins/Loop/LoopFragment.java | 31 +++++++++---- app/src/main/res/layout/loop_fragment.xml | 45 +++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java index c9b60ab3d6..19ed6496e7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java @@ -20,6 +20,7 @@ import butterknife.ButterKnife; import butterknife.OnClick; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui; import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui; @@ -40,6 +41,8 @@ public class LoopFragment extends SubscriberFragment { TextView requestView; @BindView(R.id.loop_constraintsprocessed) TextView constraintsProcessedView; + @BindView(R.id.loop_constraints) + TextView constraintsView; @BindView(R.id.loop_tbrsetbypump) TextView tbrSetByPumpView; @BindView(R.id.loop_smbsetbypump) @@ -85,14 +88,26 @@ public class LoopFragment extends SubscriberFragment { Activity activity = getActivity(); if (activity != null) activity.runOnUiThread(() -> { - if (LoopPlugin.lastRun != null) { - requestView.setText(LoopPlugin.lastRun.request != null ? LoopPlugin.lastRun.request.toSpanned() : ""); - constraintsProcessedView.setText(LoopPlugin.lastRun.constraintsProcessed != null ? LoopPlugin.lastRun.constraintsProcessed.toSpanned() : ""); - sourceView.setText(LoopPlugin.lastRun.source != null ? LoopPlugin.lastRun.source : ""); - lastRunView.setText(LoopPlugin.lastRun.lastAPSRun != null && LoopPlugin.lastRun.lastAPSRun.getTime() != 0 ? LoopPlugin.lastRun.lastAPSRun.toLocaleString() : ""); - lastEnactView.setText(LoopPlugin.lastRun.lastEnact != null && LoopPlugin.lastRun.lastEnact.getTime() != 0 ? LoopPlugin.lastRun.lastEnact.toLocaleString() : ""); - tbrSetByPumpView.setText(LoopPlugin.lastRun.tbrSetByPump != null ? LoopPlugin.lastRun.tbrSetByPump.toSpanned() : ""); - smbSetByPumpView.setText(LoopPlugin.lastRun.smbSetByPump != null ? LoopPlugin.lastRun.smbSetByPump.toSpanned() : ""); + LoopPlugin.LastRun lastRun = LoopPlugin.lastRun; + if (lastRun != null) { + requestView.setText(lastRun.request != null ? lastRun.request.toSpanned() : ""); + constraintsProcessedView.setText(lastRun.constraintsProcessed != null ? lastRun.constraintsProcessed.toSpanned() : ""); + sourceView.setText(lastRun.source != null ? lastRun.source : ""); + lastRunView.setText(lastRun.lastAPSRun != null && lastRun.lastAPSRun.getTime() != 0 ? lastRun.lastAPSRun.toLocaleString() : ""); + lastEnactView.setText(lastRun.lastEnact != null && lastRun.lastEnact.getTime() != 0 ? lastRun.lastEnact.toLocaleString() : ""); + tbrSetByPumpView.setText(lastRun.tbrSetByPump != null ? lastRun.tbrSetByPump.toSpanned() : ""); + smbSetByPumpView.setText(lastRun.smbSetByPump != null ? lastRun.smbSetByPump.toSpanned() : ""); + + String constraints = ""; + if (lastRun.constraintsProcessed != null) { + Constraint allConstraints = new Constraint<>(0d); + if (lastRun.constraintsProcessed.rateConstraint != null) + allConstraints.copyReasons(lastRun.constraintsProcessed.rateConstraint); + if (lastRun.constraintsProcessed.smbConstraint != null) + allConstraints.copyReasons(lastRun.constraintsProcessed.smbConstraint); + constraints = allConstraints.getMostLimitedReasons(); + } + constraintsView.setText(constraints); } }); } diff --git a/app/src/main/res/layout/loop_fragment.xml b/app/src/main/res/layout/loop_fragment.xml index 36f310cc81..32668d3394 100644 --- a/app/src/main/res/layout/loop_fragment.xml +++ b/app/src/main/res/layout/loop_fragment.xml @@ -236,6 +236,51 @@ + + + + + + + + + + + + Date: Fri, 23 Mar 2018 20:10:45 +0100 Subject: [PATCH 28/28] LoopPluginTest --- .../androidaps/interfaces/PluginBase.java | 2 +- .../plugins/Actions/ActionsPlugin.java | 2 +- .../plugins/Careportal/CareportalPlugin.java | 2 +- .../ConfigBuilder/ConfigBuilderFragment.java | 16 +-- .../ConfigBuilder/ConfigBuilderPlugin.java | 6 +- .../ObjectivesPlugin.java | 2 +- .../ConstraintsSafety/SafetyPlugin.java | 2 +- .../androidaps/plugins/Food/FoodPlugin.java | 2 +- .../Insulin/InsulinFastactingPlugin.java | 2 +- .../InsulinFastactingProlongedPlugin.java | 2 +- .../Insulin/InsulinOrefFreePeakPlugin.java | 2 +- .../Insulin/InsulinOrefRapidActingPlugin.java | 2 +- .../InsulinOrefUltraRapidActingPlugin.java | 2 +- .../IobCobCalculatorPlugin.java | 2 +- .../androidaps/plugins/Loop/LoopPlugin.java | 14 +-- .../NSClientInternalPlugin.java | 2 +- .../plugins/OpenAPSAMA/OpenAPSAMAPlugin.java | 2 +- .../plugins/OpenAPSMA/OpenAPSMAPlugin.java | 2 +- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 2 +- .../plugins/Overview/OverviewFragment.java | 4 +- .../plugins/Overview/OverviewPlugin.java | 2 +- .../PersistentNotificationPlugin.java | 2 +- .../ProfileLocal/LocalProfilePlugin.java | 2 +- .../plugins/ProfileNS/NSProfilePlugin.java | 2 +- .../ProfileSimple/SimpleProfilePlugin.java | 2 +- .../plugins/PumpCombo/ComboPlugin.java | 2 +- .../PumpDanaR/AbstractDanaRPlugin.java | 7 +- .../PumpDanaR/comm/MsgInitConnStatusTime.java | 8 +- .../comm/MsgInitConnStatusTime_k.java | 8 +- .../plugins/PumpDanaRS/DanaRSPlugin.java | 7 +- .../PumpDanaRv2/comm/MsgCheckValue_v2.java | 18 ++- .../plugins/PumpInsight/InsightPlugin.java | 2 +- .../androidaps/plugins/PumpMDI/MDIPlugin.java | 2 +- .../PumpVirtual/VirtualPumpPlugin.java | 2 +- .../SensitivityAAPSPlugin.java | 2 +- .../SensitivityOref0Plugin.java | 2 +- .../SensitivityWeightedAveragePlugin.java | 2 +- .../SmsCommunicatorPlugin.java | 6 +- .../SourceDexcomG5/SourceDexcomG5Plugin.java | 2 +- .../SourceGlimp/SourceGlimpPlugin.java | 2 +- .../SourceMM640g/SourceMM640gPlugin.java | 2 +- .../SourceNSClient/SourceNSClientPlugin.java | 2 +- .../SourceXdrip/SourceXdripPlugin.java | 2 +- .../plugins/Treatments/TreatmentsPlugin.java | 2 +- .../androidaps/plugins/Wear/WearPlugin.java | 2 +- .../XDripStatusline/StatuslinePlugin.java | 2 +- app/src/test/java/info/AAPSMocker.java | 2 + .../androidaps/PumpDanaR/DanaRPluginTest.java | 8 +- .../interfaces/ConstraintsCheckerTest.java | 26 ++--- .../ConstraintsSafety/SafetyPluginTest.java | 6 +- .../Insulin/InsulinOrefBasePluginTest.java | 2 +- .../plugins/Loop/LoopPluginTest.java | 110 ++++++++++++++++++ .../plugins/PumpCombo/ComboPluginTest.java | 4 +- .../plugins/PumpDanaRS/DanaRSPluginTest.java | 8 +- .../PumpInsight/InsightPluginTest.java | 2 +- 55 files changed, 221 insertions(+), 113 deletions(-) create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/Loop/LoopPluginTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java index 08db6210d4..75101b2413 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java @@ -28,7 +28,7 @@ public interface PluginBase { boolean canBeHidden(int type); boolean hasFragment(); boolean showInList(int type); - void setFragmentEnabled(int type, boolean fragmentEnabled); + void setPluginEnabled(int type, boolean fragmentEnabled); void setFragmentVisible(int type, boolean fragmentVisible); int getPreferencesId(); } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java index 3c71626e64..3de3acbf18 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsPlugin.java @@ -65,7 +65,7 @@ public class ActionsPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == GENERAL) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalPlugin.java index a72fc200ea..1cfd5822ab 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/CareportalPlugin.java @@ -71,7 +71,7 @@ public class CareportalPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == GENERAL) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java index 8d4c74d607..fd224531b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java @@ -235,7 +235,7 @@ public class ConfigBuilderFragment extends Fragment { public void onClick(View v) { CheckBox cb = (CheckBox) v; PluginBase plugin = (PluginBase) cb.getTag(); - plugin.setFragmentEnabled(type, cb.isChecked()); + plugin.setPluginEnabled(type, cb.isChecked()); plugin.setFragmentVisible(type, cb.isChecked()); onEnabledCategoryChanged(plugin, type); configBuilderPlugin.storeSettings(); @@ -316,7 +316,7 @@ public class ConfigBuilderFragment extends Fragment { if (type == PluginBase.INSULIN || type == PluginBase.PUMP || type == PluginBase.TREATMENT || type == PluginBase.PROFILE || type == PluginBase.SENSITIVITY) if (pluginList.size() < 2) { holder.checkboxEnabled.setEnabled(false); - plugin.setFragmentEnabled(type, true); + plugin.setPluginEnabled(type, true); getPlugin().storeSettings(); } @@ -390,21 +390,21 @@ public class ConfigBuilderFragment extends Fragment { if (p.getName().equals(changedPlugin.getName())) { // this is new selected } else { - p.setFragmentEnabled(type, false); + p.setPluginEnabled(type, false); p.setFragmentVisible(type, false); } } } else { // enable first plugin in list if (type == PluginBase.PUMP) - MainApp.getSpecificPlugin(VirtualPumpPlugin.class).setFragmentEnabled(type, true); + MainApp.getSpecificPlugin(VirtualPumpPlugin.class).setPluginEnabled(type, true); else if (type == PluginBase.INSULIN) - MainApp.getSpecificPlugin(InsulinFastactingPlugin.class).setFragmentEnabled(type, true); + MainApp.getSpecificPlugin(InsulinFastactingPlugin.class).setPluginEnabled(type, true); else if (type == PluginBase.SENSITIVITY) - MainApp.getSpecificPlugin(SensitivityOref0Plugin.class).setFragmentEnabled(type, true); + MainApp.getSpecificPlugin(SensitivityOref0Plugin.class).setPluginEnabled(type, true); else if (type == PluginBase.PROFILE) - MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentEnabled(type, true); + MainApp.getSpecificPlugin(NSProfilePlugin.class).setPluginEnabled(type, true); else - pluginsInCategory.get(0).setFragmentEnabled(type, true); + pluginsInCategory.get(0).setPluginEnabled(type, true); } setViews(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index f93e67fd50..8818c11921 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -128,7 +128,7 @@ public class ConfigBuilderPlugin implements PluginBase, TreatmentsInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { // Always enabled } @@ -178,7 +178,7 @@ public class ConfigBuilderPlugin implements PluginBase, TreatmentsInterface { String settingEnabled = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Enabled"; String settingVisible = "ConfigBuilder_" + type + "_" + p.getClass().getSimpleName() + "_Visible"; if (SP.contains(settingEnabled)) - p.setFragmentEnabled(type, SP.getBoolean(settingEnabled, true)); + p.setPluginEnabled(type, SP.getBoolean(settingEnabled, true)); if (SP.contains(settingVisible)) p.setFragmentVisible(type, SP.getBoolean(settingVisible, true) && SP.getBoolean(settingEnabled, true)); } catch (Exception e) { @@ -338,7 +338,7 @@ public class ConfigBuilderPlugin implements PluginBase, TreatmentsInterface { found = p; } else if (p.isEnabled(type)) { // set others disabled - p.setFragmentEnabled(type, false); + p.setPluginEnabled(type, false); } } // If none enabled, enable first one 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 6b9e796468..ba67fbe8d3 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 @@ -104,7 +104,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { } @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 7194380360..5c792c9405 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 @@ -79,7 +79,7 @@ public class SafetyPlugin implements PluginBase, ConstraintsInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodPlugin.java index 64517a19a5..2f7337566e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodPlugin.java @@ -73,7 +73,7 @@ public class FoodPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == GENERAL) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinFastactingPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinFastactingPlugin.java index 6218a2ecfe..bb9c035115 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinFastactingPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinFastactingPlugin.java @@ -71,7 +71,7 @@ public class InsulinFastactingPlugin implements PluginBase, InsulinInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == INSULIN) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinFastactingProlongedPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinFastactingProlongedPlugin.java index d336786f49..6afb867ec9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinFastactingProlongedPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinFastactingProlongedPlugin.java @@ -72,7 +72,7 @@ public class InsulinFastactingProlongedPlugin implements PluginBase, InsulinInte } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == INSULIN) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefFreePeakPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefFreePeakPlugin.java index 3cc2e6b9ca..76667d3ad7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefFreePeakPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefFreePeakPlugin.java @@ -59,7 +59,7 @@ public class InsulinOrefFreePeakPlugin extends InsulinOrefBasePlugin { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == INSULIN) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefRapidActingPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefRapidActingPlugin.java index cae9ac540d..86af7f8130 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefRapidActingPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefRapidActingPlugin.java @@ -58,7 +58,7 @@ public class InsulinOrefRapidActingPlugin extends InsulinOrefBasePlugin { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == INSULIN) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefUltraRapidActingPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefUltraRapidActingPlugin.java index 0d390a3ba2..0c24bb1034 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefUltraRapidActingPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefUltraRapidActingPlugin.java @@ -58,7 +58,7 @@ public class InsulinOrefUltraRapidActingPlugin extends InsulinOrefBasePlugin { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == INSULIN) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index c60991253c..0009287354 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -117,7 +117,7 @@ public class IobCobCalculatorPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { } 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 477c2a11c3..c2f765e47e 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 @@ -46,7 +46,7 @@ import info.nightscout.utils.SP; public class LoopPlugin implements PluginBase { private static Logger log = LoggerFactory.getLogger(LoopPlugin.class); - private static LoopPlugin loopPlugin; + protected static LoopPlugin loopPlugin; public static LoopPlugin getPlugin() { if (loopPlugin == null) { @@ -55,7 +55,7 @@ public class LoopPlugin implements PluginBase { return loopPlugin; } - private boolean fragmentEnabled = false; + private boolean pluginEnabled = false; private boolean fragmentVisible = false; private long loopSuspendedTill = 0L; // end of manual loop suspend @@ -94,12 +94,12 @@ public class LoopPlugin implements PluginBase { @Override public String getName() { - return MainApp.instance().getString(R.string.loop); + return MainApp.instance().gs(R.string.loop); } @Override public String getNameShort() { - String name = MainApp.sResources.getString(R.string.loop_shortname); + String name = MainApp.gs(R.string.loop_shortname); if (!name.trim().isEmpty()) { //only if translation exists return name; @@ -111,7 +111,7 @@ public class LoopPlugin implements PluginBase { @Override public boolean isEnabled(int type) { boolean pumpCapable = ConfigBuilderPlugin.getActivePump() == null || ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable; - return type == LOOP && fragmentEnabled && pumpCapable; + return type == LOOP && pluginEnabled && pumpCapable; } @Override @@ -136,8 +136,8 @@ public class LoopPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { - if (type == LOOP) this.fragmentEnabled = fragmentEnabled; + public void setPluginEnabled(int type, boolean pluginEnabled) { + if (type == LOOP) this.pluginEnabled = pluginEnabled; } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java index a51ea18cc7..162c33381f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSClientInternalPlugin.java @@ -126,7 +126,7 @@ public class NSClientInternalPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == GENERAL) this.fragmentEnabled = fragmentEnabled; } 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 47b0964142..4c69a347b4 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 @@ -112,7 +112,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == APS) this.fragmentEnabled = fragmentEnabled; } 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 1c9edbc278..7fb7a3e207 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 @@ -112,7 +112,7 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == APS) this.fragmentEnabled = fragmentEnabled; } 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 7ea785988d..c0b57a80d4 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 @@ -117,7 +117,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == APS) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 5ecaff8887..dc0cb27ed7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -479,7 +479,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, return true; final LoopPlugin activeloop = ConfigBuilderPlugin.getActiveLoop(); if (item.getTitle().equals(MainApp.sResources.getString(R.string.disableloop))) { - activeloop.setFragmentEnabled(PluginBase.LOOP, false); + activeloop.setPluginEnabled(PluginBase.LOOP, false); activeloop.setFragmentVisible(PluginBase.LOOP, false); MainApp.getConfigBuilder().storeSettings(); updateGUI("suspendmenu"); @@ -494,7 +494,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, NSUpload.uploadOpenAPSOffline(24 * 60); // upload 24h, we don't know real duration return true; } else if (item.getTitle().equals(MainApp.sResources.getString(R.string.enableloop))) { - activeloop.setFragmentEnabled(PluginBase.LOOP, true); + activeloop.setPluginEnabled(PluginBase.LOOP, true); activeloop.setFragmentVisible(PluginBase.LOOP, true); MainApp.getConfigBuilder().storeSettings(); updateGUI("suspendmenu"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java index 8b276f937d..376ee9ca87 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewPlugin.java @@ -96,7 +96,7 @@ public class OverviewPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { // Always enabled } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java index 26d5851865..282b3e16a7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Persistentnotification/PersistentNotificationPlugin.java @@ -94,7 +94,7 @@ public class PersistentNotificationPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (getType() == type) { this.fragmentEnabled = fragmentEnabled; enableDisableNotification(fragmentEnabled); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java index 69272cc2ae..c59537ad6b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java @@ -107,7 +107,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PROFILE) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileNS/NSProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileNS/NSProfilePlugin.java index c2d7cfabfc..a50e47b998 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileNS/NSProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileNS/NSProfilePlugin.java @@ -96,7 +96,7 @@ public class NSProfilePlugin implements PluginBase, ProfileInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PROFILE) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfilePlugin.java index b538a28512..92f810c3a0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileSimple/SimpleProfilePlugin.java @@ -102,7 +102,7 @@ public class SimpleProfilePlugin implements PluginBase, ProfileInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PROFILE) this.fragmentEnabled = fragmentEnabled; } 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 f9858d4cce..c8e5fcdbe4 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 @@ -221,7 +221,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PUMP) this.fragmentEnabled = fragmentEnabled; } 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 008cecc590..910a7a8b28 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 @@ -10,7 +10,6 @@ import java.util.Date; import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; @@ -106,16 +105,16 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface, } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PluginBase.PROFILE) mPluginProfileEnabled = fragmentEnabled; else if (type == PluginBase.PUMP) mPluginPumpEnabled = fragmentEnabled; // if pump profile was enabled need to switch to another too if (type == PluginBase.PUMP && !fragmentEnabled && mPluginProfileEnabled) { - setFragmentEnabled(PluginBase.PROFILE, false); + setPluginEnabled(PluginBase.PROFILE, false); setFragmentVisible(PluginBase.PROFILE, false); - NSProfilePlugin.getPlugin().setFragmentEnabled(PluginBase.PROFILE, true); + NSProfilePlugin.getPlugin().setPluginEnabled(PluginBase.PROFILE, true); NSProfilePlugin.getPlugin().setFragmentVisible(PluginBase.PROFILE, true); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgInitConnStatusTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgInitConnStatusTime.java index ee0a9c8aaf..93f66aa200 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgInitConnStatusTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/comm/MsgInitConnStatusTime.java @@ -31,17 +31,17 @@ public class MsgInitConnStatusTime extends MessageBase { MainApp.bus().post(new EventNewNotification(notification)); MainApp.getSpecificPlugin(DanaRPlugin.class).disconnect("Wrong Model"); log.debug("Wrong model selected. Switching to Korean DanaR"); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentEnabled(PluginBase.PUMP, true); + MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setPluginEnabled(PluginBase.PUMP, true); MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentVisible(PluginBase.PUMP, true); - MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentEnabled(PluginBase.PUMP, false); + MainApp.getSpecificPlugin(DanaRPlugin.class).setPluginEnabled(PluginBase.PUMP, false); MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentVisible(PluginBase.PUMP, false); DanaRPump.reset(); // mark not initialized //If profile coming from pump, switch it as well if(MainApp.getSpecificPlugin(DanaRPlugin.class).isEnabled(PluginBase.PROFILE)){ - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentEnabled(PluginBase.PROFILE, false); - (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setFragmentEnabled(PluginBase.PROFILE, true); + (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginBase.PROFILE, false); + (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setPluginEnabled(PluginBase.PROFILE, true); } MainApp.getConfigBuilder().storeSettings(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/comm/MsgInitConnStatusTime_k.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/comm/MsgInitConnStatusTime_k.java index 89ccb7ae60..6e2b66d2d0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/comm/MsgInitConnStatusTime_k.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/comm/MsgInitConnStatusTime_k.java @@ -33,17 +33,17 @@ public class MsgInitConnStatusTime_k extends MessageBase { MainApp.bus().post(new EventNewNotification(notification)); DanaRKoreanPlugin.getPlugin().disconnect("Wrong Model"); log.debug("Wrong model selected. Switching to export DanaR"); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentEnabled(PluginBase.PUMP, false); + MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setPluginEnabled(PluginBase.PUMP, false); MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentVisible(PluginBase.PUMP, false); - MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentEnabled(PluginBase.PUMP, true); + MainApp.getSpecificPlugin(DanaRPlugin.class).setPluginEnabled(PluginBase.PUMP, true); MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentVisible(PluginBase.PUMP, true); DanaRPump.reset(); // mark not initialized //If profile coming from pump, switch it as well if (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).isEnabled(PluginBase.PROFILE)) { - (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setFragmentEnabled(PluginBase.PROFILE, false); - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentEnabled(PluginBase.PROFILE, true); + (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setPluginEnabled(PluginBase.PROFILE, false); + (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginBase.PROFILE, true); } MainApp.getConfigBuilder().storeSettings(); 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 4473f110d4..126c8a63a0 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 @@ -18,7 +18,6 @@ import java.util.Date; import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; @@ -114,16 +113,16 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface, } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PluginBase.PROFILE) this.fragmentProfileEnabled = fragmentEnabled; else if (type == PluginBase.PUMP) this.fragmentPumpEnabled = fragmentEnabled; // if pump profile was enabled need to switch to another too if (type == PluginBase.PUMP && !fragmentEnabled && this.fragmentProfileEnabled) { - setFragmentEnabled(PluginBase.PROFILE, false); + setPluginEnabled(PluginBase.PROFILE, false); setFragmentVisible(PluginBase.PROFILE, false); - MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentEnabled(PluginBase.PROFILE, true); + MainApp.getSpecificPlugin(NSProfilePlugin.class).setPluginEnabled(PluginBase.PROFILE, true); MainApp.getSpecificPlugin(NSProfilePlugin.class).setFragmentVisible(PluginBase.PROFILE, true); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgCheckValue_v2.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgCheckValue_v2.java index 336230faf8..fe84ea8114 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgCheckValue_v2.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgCheckValue_v2.java @@ -3,8 +3,6 @@ package info.nightscout.androidaps.plugins.PumpDanaRv2.comm; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; - import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -45,17 +43,17 @@ public class MsgCheckValue_v2 extends MessageBase { MainApp.bus().post(new EventNewNotification(notification)); MainApp.getSpecificPlugin(DanaRPlugin.class).disconnect("Wrong Model"); log.debug("Wrong model selected. Switching to Korean DanaR"); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentEnabled(PluginBase.PUMP, true); + MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setPluginEnabled(PluginBase.PUMP, true); MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentVisible(PluginBase.PUMP, true); - MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentEnabled(PluginBase.PUMP, false); + MainApp.getSpecificPlugin(DanaRPlugin.class).setPluginEnabled(PluginBase.PUMP, false); MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentVisible(PluginBase.PUMP, false); DanaRPump.reset(); // mark not initialized //If profile coming from pump, switch it as well if(MainApp.getSpecificPlugin(DanaRPlugin.class).isEnabled(PluginBase.PROFILE)){ - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentEnabled(PluginBase.PROFILE, false); - (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setFragmentEnabled(PluginBase.PROFILE, true); + (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginBase.PROFILE, false); + (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setPluginEnabled(PluginBase.PROFILE, true); } MainApp.getConfigBuilder().storeSettings(); @@ -70,15 +68,15 @@ public class MsgCheckValue_v2 extends MessageBase { MainApp.bus().post(new EventNewNotification(notification)); DanaRKoreanPlugin.getPlugin().disconnect("Wrong Model"); log.debug("Wrong model selected. Switching to non APS DanaR"); - (MainApp.getSpecificPlugin(DanaRv2Plugin.class)).setFragmentEnabled(PluginBase.PUMP, false); + (MainApp.getSpecificPlugin(DanaRv2Plugin.class)).setPluginEnabled(PluginBase.PUMP, false); (MainApp.getSpecificPlugin(DanaRv2Plugin.class)).setFragmentVisible(PluginBase.PUMP, false); - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentEnabled(PluginBase.PUMP, true); + (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginBase.PUMP, true); (MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentVisible(PluginBase.PUMP, true); //If profile coming from pump, switch it as well if(MainApp.getSpecificPlugin(DanaRv2Plugin.class).isEnabled(PluginBase.PROFILE)){ - (MainApp.getSpecificPlugin(DanaRv2Plugin.class)).setFragmentEnabled(PluginBase.PROFILE, false); - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentEnabled(PluginBase.PROFILE, true); + (MainApp.getSpecificPlugin(DanaRv2Plugin.class)).setPluginEnabled(PluginBase.PROFILE, false); + (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginBase.PROFILE, true); } MainApp.getConfigBuilder().storeSettings(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java index ba58093d84..2506e5321d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java @@ -231,7 +231,7 @@ public class InsightPlugin implements PluginBase, PumpInterface, ConstraintsInte } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PUMP) { if (fragmentEnabled) { enableConnector(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java index 629ffe0d49..407edc298a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java @@ -90,7 +90,7 @@ public class MDIPlugin implements PluginBase, PumpInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PUMP) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java index debda40e0d..00ff2fc7a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java @@ -151,7 +151,7 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == PUMP) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityAAPS/SensitivityAAPSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityAAPS/SensitivityAAPSPlugin.java index 067ecdef8f..e321e98f1e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityAAPS/SensitivityAAPSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityAAPS/SensitivityAAPSPlugin.java @@ -88,7 +88,7 @@ public class SensitivityAAPSPlugin implements PluginBase, SensitivityInterface{ } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == SENSITIVITY) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java index 5c6188788c..bcac02f51c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java @@ -87,7 +87,7 @@ public class SensitivityOref0Plugin implements PluginBase, SensitivityInterface } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == SENSITIVITY) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityWeightedAverage/SensitivityWeightedAveragePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityWeightedAverage/SensitivityWeightedAveragePlugin.java index d80510d8a8..7b2592c7e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityWeightedAverage/SensitivityWeightedAveragePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityWeightedAverage/SensitivityWeightedAveragePlugin.java @@ -84,7 +84,7 @@ public class SensitivityWeightedAveragePlugin implements PluginBase, Sensitivity } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == SENSITIVITY) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 09dfeac901..a67c8d4212 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -177,7 +177,7 @@ public class SmsCommunicatorPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == GENERAL) this.fragmentEnabled = fragmentEnabled; } @@ -286,7 +286,7 @@ public class SmsCommunicatorPlugin implements PluginBase { case "STOP": LoopPlugin loopPlugin = MainApp.getSpecificPlugin(LoopPlugin.class); if (loopPlugin != null && loopPlugin.isEnabled(PluginBase.LOOP)) { - loopPlugin.setFragmentEnabled(PluginBase.LOOP, false); + loopPlugin.setPluginEnabled(PluginBase.LOOP, false); ConfigBuilderPlugin.getCommandQueue().cancelTempBasal(true, new Callback() { @Override public void run() { @@ -304,7 +304,7 @@ public class SmsCommunicatorPlugin implements PluginBase { case "START": loopPlugin = MainApp.getSpecificPlugin(LoopPlugin.class); if (loopPlugin != null && !loopPlugin.isEnabled(PluginBase.LOOP)) { - loopPlugin.setFragmentEnabled(PluginBase.LOOP, true); + loopPlugin.setPluginEnabled(PluginBase.LOOP, true); reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeenenabled); sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_START")); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceDexcomG5/SourceDexcomG5Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceDexcomG5/SourceDexcomG5Plugin.java index 8b9734cb79..719b2eb1f4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceDexcomG5/SourceDexcomG5Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceDexcomG5/SourceDexcomG5Plugin.java @@ -68,7 +68,7 @@ public class SourceDexcomG5Plugin implements PluginBase, BgSourceInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == BGSOURCE) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpPlugin.java index eb9a6b1c6b..719a4b3059 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceGlimp/SourceGlimpPlugin.java @@ -68,7 +68,7 @@ public class SourceGlimpPlugin implements PluginBase, BgSourceInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == BGSOURCE) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gPlugin.java index 1111c8c9eb..38b787d750 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceMM640g/SourceMM640gPlugin.java @@ -68,7 +68,7 @@ public class SourceMM640gPlugin implements PluginBase, BgSourceInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == BGSOURCE) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientPlugin.java index f43c44047e..bbc1759735 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceNSClient/SourceNSClientPlugin.java @@ -70,7 +70,7 @@ public class SourceNSClientPlugin implements PluginBase, BgSourceInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == BGSOURCE) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripPlugin.java index 532492d40e..66e8d470fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SourceXdrip/SourceXdripPlugin.java @@ -69,7 +69,7 @@ public class SourceXdripPlugin implements PluginBase, BgSourceInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == BGSOURCE) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 4bf96f470b..69b410e757 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -113,7 +113,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == TREATMENT) this.fragmentEnabled = fragmentEnabled; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/WearPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/WearPlugin.java index 5f11fc5fda..f37effebe3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/WearPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/WearPlugin.java @@ -107,7 +107,7 @@ public class WearPlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == GENERAL) { this.fragmentEnabled = fragmentEnabled; if (watchUS != null) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java index 76e8ac9e4d..d2df702cd4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/XDripStatusline/StatuslinePlugin.java @@ -117,7 +117,7 @@ public class StatuslinePlugin implements PluginBase { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { if (type == GENERAL) { this.fragmentEnabled = fragmentEnabled; diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index adf5136dd0..a39fd4595e 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -54,6 +54,8 @@ public class AAPSMocker { when(MainApp.gs(R.string.limitingcarbs)).thenReturn("Limiting carbs to %d g because of %s"); when(MainApp.gs(R.string.limitingiob)).thenReturn("Limiting IOB to %.1f U because of %s"); when(MainApp.gs(R.string.pumpisnottempbasalcapable)).thenReturn("Pump is not temp basal capable"); + when(MainApp.gs(R.string.loop)).thenReturn("Loop"); + when(MainApp.gs(R.string.loop_shortname)).thenReturn("LOOP"); } public static MainApp mockMainApp() { diff --git a/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java b/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java index 5f288dc9c7..33b5dad925 100644 --- a/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/PumpDanaR/DanaRPluginTest.java @@ -36,8 +36,8 @@ public class DanaRPluginTest { @Test public void basalRateShouldBeLimited() throws Exception { - danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); - danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRPlugin.setPluginEnabled(PluginBase.PUMP, true); + danaRPlugin.setPluginEnabled(PluginBase.PUMP, true); DanaRPump.getInstance().maxBasal = 0.8d; Constraint c = new Constraint<>(Constants.REALLYHIGHBASALRATE); @@ -49,8 +49,8 @@ public class DanaRPluginTest { @Test public void percentBasalRateShouldBeLimited() throws Exception { - danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); - danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRPlugin.setPluginEnabled(PluginBase.PUMP, true); + danaRPlugin.setPluginEnabled(PluginBase.PUMP, true); DanaRPump.getInstance().maxBasal = 0.8d; Constraint c = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); 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 3ea63fda32..46b8bb7a53 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -63,7 +63,7 @@ public class ConstraintsCheckerTest { @Test public void isLoopInvokationAllowedTest() throws Exception { - comboPlugin.setFragmentEnabled(PluginBase.PUMP, true); + comboPlugin.setPluginEnabled(PluginBase.PUMP, true); comboPlugin.setValidBasalRateProfileSelectedOnPump(false); Constraint c = constraintChecker.isLoopInvokationAllowed(); @@ -125,12 +125,12 @@ public class ConstraintsCheckerTest { @Test public void basalRateShouldBeLimited() throws Exception { // DanaR, RS - danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); - danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRPlugin.setPluginEnabled(PluginBase.PUMP, true); + danaRSPlugin.setPluginEnabled(PluginBase.PUMP, true); DanaRPump.getInstance().maxBasal = 0.8d; // Insight - insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + insightPlugin.setPluginEnabled(PluginBase.PUMP, true); StatusTaskRunner.Result result = new StatusTaskRunner.Result(); result.maximumBasalAmount = 1.1d; insightPlugin.setStatusResult(result); @@ -152,12 +152,12 @@ public class ConstraintsCheckerTest { @Test public void percentBasalRateShouldBeLimited() throws Exception { // DanaR, RS - danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); - danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRPlugin.setPluginEnabled(PluginBase.PUMP, true); + danaRSPlugin.setPluginEnabled(PluginBase.PUMP, true); DanaRPump.getInstance().maxBasal = 0.8d; // Insight - insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + insightPlugin.setPluginEnabled(PluginBase.PUMP, true); StatusTaskRunner.Result result = new StatusTaskRunner.Result(); result.maximumBasalAmount = 1.1d; insightPlugin.setStatusResult(result); @@ -180,12 +180,12 @@ public class ConstraintsCheckerTest { @Test public void bolusAmountShouldBeLimited() throws Exception { // DanaR, RS - danaRPlugin.setFragmentEnabled(PluginBase.PUMP, true); - danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRPlugin.setPluginEnabled(PluginBase.PUMP, true); + danaRSPlugin.setPluginEnabled(PluginBase.PUMP, true); DanaRPump.getInstance().maxBolus = 6d; // Insight - insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + insightPlugin.setPluginEnabled(PluginBase.PUMP, true); StatusTaskRunner.Result result = new StatusTaskRunner.Result(); result.maximumBolusAmount = 7d; insightPlugin.setStatusResult(result); @@ -221,9 +221,9 @@ public class ConstraintsCheckerTest { // No limit by default when(SP.getDouble(R.string.key_openapsma_max_iob, 1.5d)).thenReturn(1.5d); when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); - OpenAPSMAPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); - OpenAPSAMAPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); - OpenAPSSMBPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); + OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginBase.APS, true); + OpenAPSAMAPlugin.getPlugin().setPluginEnabled(PluginBase.APS, true); + OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginBase.APS, true); // Apply all limits Constraint d = constraintChecker.getMaxIOBAllowed(); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java index c88f60fd92..4b45734b2a 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java @@ -185,9 +185,9 @@ public class SafetyPluginTest { public void iobShouldBeLimited() throws Exception { when(SP.getDouble(R.string.key_openapsma_max_iob, 1.5d)).thenReturn(1.5d); when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); - OpenAPSMAPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); - OpenAPSAMAPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); - OpenAPSSMBPlugin.getPlugin().setFragmentEnabled(PluginBase.APS, true); + OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginBase.APS, true); + OpenAPSAMAPlugin.getPlugin().setPluginEnabled(PluginBase.APS, true); + OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginBase.APS, true); // Apply all limits Constraint d = new Constraint<>(Constants.REALLYHIGHIOB); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefBasePluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefBasePluginTest.java index 83e9442364..7dd40883fa 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefBasePluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Insulin/InsulinOrefBasePluginTest.java @@ -137,7 +137,7 @@ public class InsulinOrefBasePluginTest extends InsulinOrefBasePlugin { } @Override - public void setFragmentEnabled(int type, boolean fragmentEnabled) { + public void setPluginEnabled(int type, boolean fragmentEnabled) { } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Loop/LoopPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Loop/LoopPluginTest.java new file mode 100644 index 0000000000..d533782f13 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Loop/LoopPluginTest.java @@ -0,0 +1,110 @@ +package info.nightscout.androidaps.plugins.Loop; + +import android.content.Context; + +import com.squareup.otto.Bus; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.events.EventTreatmentChange; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.PumpVirtual.VirtualPumpPlugin; +import info.nightscout.utils.SP; + +import static org.powermock.api.mockito.PowerMockito.when; + +/** + * Created by mike on 23.03.2018. + */ + +@RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, ConfigBuilderPlugin.class, SP.class, Context.class}) +public class LoopPluginTest { + + VirtualPumpPlugin virtualPumpPlugin; + LoopPlugin loopPlugin; + MockedBus bus; + + @Test + public void testPluginInterface() { + Assert.assertEquals(LoopFragment.class.getName(), loopPlugin.getFragmentClass()); + Assert.assertEquals(PluginBase.LOOP, loopPlugin.getType()); + Assert.assertEquals("Loop", loopPlugin.getName()); + Assert.assertEquals("LOOP", loopPlugin.getNameShort()); + Assert.assertEquals(true, loopPlugin.canBeHidden(PluginBase.LOOP)); + Assert.assertEquals(true, loopPlugin.hasFragment()); + Assert.assertEquals(true, loopPlugin.showInList(PluginBase.LOOP)); + Assert.assertEquals(R.xml.pref_closedmode, loopPlugin.getPreferencesId()); + + // Plugin is disabled by default + Assert.assertEquals(false, loopPlugin.isEnabled(PluginBase.LOOP)); + loopPlugin.setPluginEnabled(PluginBase.LOOP, true); + Assert.assertEquals(true, loopPlugin.isEnabled(PluginBase.LOOP)); + + // No temp basal capable pump should disable plugin + virtualPumpPlugin.getPumpDescription().isTempBasalCapable = false; + Assert.assertEquals(false, loopPlugin.isEnabled(PluginBase.LOOP)); + virtualPumpPlugin.getPumpDescription().isTempBasalCapable = true; + + + // Fragment is hidden by default + Assert.assertEquals(false, loopPlugin.isVisibleInTabs(PluginBase.LOOP)); + loopPlugin.setFragmentVisible(PluginBase.LOOP, true); + Assert.assertEquals(true, loopPlugin.isVisibleInTabs(PluginBase.LOOP)); + + } + +/* *********** not working + @Test + public void eventTreatmentChangeShouldTriggerInvoke() { + + // Unregister tested plugin to prevent calling real invoke + MainApp.bus().unregister(loopPlugin); + + class MockedLoopPlugin extends LoopPlugin { + boolean invokeCalled = false; + + @Override + public void invoke(String initiator, boolean allowNotification) { + invokeCalled = true; + } + + } + + MockedLoopPlugin mockedLoopPlugin = new MockedLoopPlugin(); + Treatment t = new Treatment(); + bus.post(new EventTreatmentChange(t)); + Assert.assertEquals(true, mockedLoopPlugin.invokeCalled); + } +*/ + @Before + public void prepareMock() { + AAPSMocker.mockMainApp(); + AAPSMocker.mockConfigBuilder(); + AAPSMocker.mockSP(); + AAPSMocker.mockStrings(); + + bus = new MockedBus(); + when(MainApp.bus()).thenReturn(bus); + + loopPlugin = LoopPlugin.getPlugin(); + virtualPumpPlugin = VirtualPumpPlugin.getPlugin(); + + when(ConfigBuilderPlugin.getActivePump()).thenReturn(virtualPumpPlugin); + } + + class MockedBus extends Bus { + } + + +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPluginTest.java index 05e3e58866..2e856029f9 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPluginTest.java @@ -27,14 +27,14 @@ public class ComboPluginTest { @Test public void invalidBasalRateOnComboPumpShouldLimitLoopInvokation() throws Exception { - comboPlugin.setFragmentEnabled(PluginBase.PUMP, true); + comboPlugin.setPluginEnabled(PluginBase.PUMP, true); comboPlugin.setValidBasalRateProfileSelectedOnPump(false); Constraint c = new Constraint<>(true); c = comboPlugin.isLoopInvokationAllowed(c); Assert.assertEquals("Combo: No valid basal rate read from pump", c.getReasons()); Assert.assertEquals(Boolean.FALSE, c.value()); - comboPlugin.setFragmentEnabled(PluginBase.PUMP, false); + comboPlugin.setPluginEnabled(PluginBase.PUMP, false); } @Test diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPluginTest.java index 837298fe1d..1b88e7d025 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPluginTest.java @@ -35,8 +35,8 @@ public class DanaRSPluginTest { @Test public void basalRateShouldBeLimited() throws Exception { - danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); - danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRSPlugin.setPluginEnabled(PluginBase.PUMP, true); + danaRSPlugin.setPluginEnabled(PluginBase.PUMP, true); DanaRPump.getInstance().maxBasal = 0.8d; Constraint c = new Constraint<>(Constants.REALLYHIGHBASALRATE); @@ -48,8 +48,8 @@ public class DanaRSPluginTest { @Test public void percentBasalRateShouldBeLimited() throws Exception { - danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); - danaRSPlugin.setFragmentEnabled(PluginBase.PUMP, true); + danaRSPlugin.setPluginEnabled(PluginBase.PUMP, true); + danaRSPlugin.setPluginEnabled(PluginBase.PUMP, true); DanaRPump.getInstance().maxBasal = 0.8d; Constraint c = new Constraint<>(Constants.REALLYHIGHPERCENTBASALRATE); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPluginTest.java index a4432b6ffc..a93757af87 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPluginTest.java @@ -31,7 +31,7 @@ public class InsightPluginTest { @Test public void basalRateShouldBeLimited() throws Exception { - insightPlugin.setFragmentEnabled(PluginBase.PUMP, true); + insightPlugin.setPluginEnabled(PluginBase.PUMP, true); StatusTaskRunner.Result result = new StatusTaskRunner.Result(); result.maximumBasalAmount = 1.1d; insightPlugin.setStatusResult(result);