From 1aba9a25647e1cfade6e2e0adfffdd5471b83cd5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 22 Mar 2018 10:31:07 +0100 Subject: [PATCH] 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);