SafetyPlugin extends PluginBase implements ConstraintsInterface { @Override public Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile) { absoluteRate.setIfGreater(0d, String.format(MainApp.gs(R.string.limitingbasalratio), 0d, MainApp.gs(R.string.itmustbepositivevalue)), this); if (Config.APS) { double maxBasal = SP.getDouble(R.string.key_openapsma_max_basal, 1d); if (maxBasal < profile.getMaxDailyBasal()) { maxBasal = profile.getMaxDailyBasal(); absoluteRate.addReason(MainApp.gs(R.string.increasingmaxbasal), this); } absoluteRate.setIfSmaller(maxBasal, String.format(MainApp.gs(R.string.limitingbasalratio), maxBasal, MainApp.gs(R.string.maxvalueinpreferences)), this); 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)), 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)), this); } absoluteRate.setIfSmaller(HardLimits.maxBasal(), String.format(MainApp.gs(R.string.limitingbasalratio), HardLimits.maxBasal(), MainApp.gs(R.string.hardlimit)), this); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); if (pump != null && pump.getPumpDescription().tempBasalStyle == PumpDescription.ABSOLUTE) { double pumpLimit = pump.getPumpDescription().pumpType.getTbrSettings().getMaxDose(); absoluteRate.setIfSmaller(pumpLimit, String.format(MainApp.gs(R.string.limitingbasalratio), pumpLimit, MainApp.gs(R.string.pumplimit)), this); } if (pump != null && pump.getPumpDescription().tempBasalStyle == PumpDescription.ABSOLUTE) { absoluteRate.set(Round.roundTo(absoluteRate.value(), pump.getPumpDescription().tempAbsoluteStep)); } return absoluteRate; } SafetyPlugin(); static SafetyPlugin getPlugin(); @Override Constraint<Boolean> isLoopInvocationAllowed(Constraint<Boolean> value); @Override Constraint<Boolean> isClosedLoopAllowed(Constraint<Boolean> value); @Override Constraint<Boolean> isAutosensModeEnabled(Constraint<Boolean> value); @Override Constraint<Boolean> isSMBModeEnabled(Constraint<Boolean> value); @Override Constraint<Boolean> isUAMEnabled(Constraint<Boolean> value); @Override Constraint<Boolean> isAdvancedFilteringEnabled(Constraint<Boolean> value); @Override Constraint<Double> applyBasalConstraints(Constraint<Double> absoluteRate, Profile profile); @Override Constraint<Integer> applyBasalPercentConstraints(Constraint<Integer> percentRate, Profile profile); @Override Constraint<Double> applyBolusConstraints(Constraint<Double> insulin); @Override Constraint<Double> applyExtendedBolusConstraints(Constraint<Double> insulin); @Override Constraint<Integer> applyCarbsConstraints(Constraint<Integer> carbs); @Override Constraint<Double> applyMaxIOBConstraints(Constraint<Double> maxIob);  }
@Test public void basalRateShouldBeLimited() { 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<Double> c = new Constraint<>(Constants.REALLYHIGHBASALRATE); safetyPlugin.applyBasalConstraints(c, AAPSMocker.getValidProfile()); Assert.assertEquals(1d, c.value(), 0.01d); 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() { when(SP.getString(R.string.key_age, "")).thenReturn("child"); Constraint<Double> d = new Constraint<>(-0.5d); safetyPlugin.applyBasalConstraints(d, AAPSMocker.getValidProfile()); Assert.assertEquals(0d, d.value(), 0.01d); Assert.assertEquals("Safety: Limiting basal rate to 0.00 U/h because of it must be positive value\n" + "Safety: Increasing max basal value because setting is lower than your max basal in profile", d.getReasons()); }