SafetyPlugin extends PluginBase implements ConstraintsInterface { @Override public Constraint<Integer> applyBasalPercentConstraints(Constraint<Integer> percentRate, Profile profile) { Double currentBasal = profile.getBasal(); Double absoluteRate = currentBasal * ((double) percentRate.originalValue() / 100); percentRate.addReason("Percent rate " + percentRate.originalValue() + "% recalculated to " + DecimalFormatter.to2Decimal(absoluteRate) + " U/h with current basal " + DecimalFormatter.to2Decimal(currentBasal) + " U/h", this); Constraint<Double> absoluteConstraint = new Constraint<>(absoluteRate); applyBasalConstraints(absoluteConstraint, profile); percentRate.copyReasons(absoluteConstraint); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); Integer percentRateAfterConst = Double.valueOf(absoluteConstraint.value() / currentBasal * 100).intValue(); if (pump != null) { if (percentRateAfterConst < 100) percentRateAfterConst = Round.ceilTo((double) percentRateAfterConst, (double) pump.getPumpDescription().tempPercentStep).intValue(); else percentRateAfterConst = Round.floorTo((double) percentRateAfterConst, (double) pump.getPumpDescription().tempPercentStep).intValue(); } percentRate.set(percentRateAfterConst, String.format(MainApp.gs(R.string.limitingpercentrate), percentRateAfterConst, MainApp.gs(R.string.pumplimit)), this); if (pump != null && pump.getPumpDescription().tempBasalStyle == PumpDescription.PERCENT) { double pumpLimit = pump.getPumpDescription().pumpType.getTbrSettings().getMaxDose(); percentRate.setIfSmaller((int) pumpLimit, String.format(MainApp.gs(R.string.limitingbasalratio), pumpLimit, MainApp.gs(R.string.pumplimit)), this); } return percentRate; } 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 percentBasalRateShouldBeLimited() { 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<Integer> 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() { when(SP.getString(R.string.key_age, "")).thenReturn("child"); Constraint<Integer> 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: Increasing max basal value because setting is lower than your max basal in profile\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()); }