APSResult { public boolean isChangeRequested() { Constraint<Boolean> closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed(); if (closedLoopEnabled.value()) { if (L.isEnabled(L.APS)) log.debug("DEFAULT: Closed mode"); return tempBasalRequested || bolusRequested; } if (!tempBasalRequested && !bolusRequested) { if (L.isEnabled(L.APS)) log.debug("FALSE: No request"); return false; } long now = System.currentTimeMillis(); TemporaryBasal activeTemp = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(now); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); Profile profile = ProfileFunctions.getInstance().getProfile(); if (profile == null) { log.error("FALSE: No Profile"); return false; } if (usePercent) { if (activeTemp == null && percent == 100) { if (L.isEnabled(L.APS)) log.debug("FALSE: No temp running, asking cancel temp"); return false; } if (activeTemp != null && Math.abs(percent - activeTemp.tempBasalConvertedToPercent(now, profile)) < pump.getPumpDescription().basalStep) { if (L.isEnabled(L.APS)) log.debug("FALSE: Temp equal"); return false; } if (percent == 0) { if (L.isEnabled(L.APS)) log.debug("TRUE: Zero temp"); return true; } if (pump != null && pump.getPumpDescription().tempBasalStyle == PumpDescription.PERCENT) { double pumpLimit = pump.getPumpDescription().pumpType.getTbrSettings().getMaxDose(); if (percent == pumpLimit) { if (L.isEnabled(L.APS)) log.debug("TRUE: Pump limit"); return true; } } double percentMinChangeChange = SP.getDouble(R.string.key_loop_openmode_min_change, 30d); percentMinChangeChange /= 100d; double lowThreshold = 1 - percentMinChangeChange; double highThreshold = 1 + percentMinChangeChange; double change = percent / 100d; if (activeTemp != null) change = percent / (double) activeTemp.tempBasalConvertedToPercent(now, profile); if (change < lowThreshold || change > highThreshold) { if (L.isEnabled(L.APS)) log.debug("TRUE: Outside allowed range " + (change * 100d) + "%"); return true; } else { if (L.isEnabled(L.APS)) log.debug("TRUE: Inside allowed range " + (change * 100d) + "%"); return false; } } else { if (activeTemp == null && rate == pump.getBaseBasalRate()) { if (L.isEnabled(L.APS)) log.debug("FALSE: No temp running, asking cancel temp"); return false; } if (activeTemp != null && Math.abs(rate - activeTemp.tempBasalConvertedToAbsolute(now, profile)) < pump.getPumpDescription().basalStep) { if (L.isEnabled(L.APS)) log.debug("FALSE: Temp equal"); return false; } if (rate == 0) { if (L.isEnabled(L.APS)) log.debug("TRUE: Zero temp"); return true; } if (pump != null && pump.getPumpDescription().tempBasalStyle == PumpDescription.ABSOLUTE) { double pumpLimit = pump.getPumpDescription().pumpType.getTbrSettings().getMaxDose(); if (rate == pumpLimit) { if (L.isEnabled(L.APS)) log.debug("TRUE: Pump limit"); return true; } } double percentMinChangeChange = SP.getDouble(R.string.key_loop_openmode_min_change, 30d); percentMinChangeChange /= 100d; double lowThreshold = 1 - percentMinChangeChange; double highThreshold = 1 + percentMinChangeChange; double change = rate / profile.getBasal(); if (activeTemp != null) change = rate / activeTemp.tempBasalConvertedToAbsolute(now, profile); if (change < lowThreshold || change > highThreshold) { if (L.isEnabled(L.APS)) log.debug("TRUE: Outside allowed range " + (change * 100d) + "%"); return true; } else { if (L.isEnabled(L.APS)) log.debug("TRUE: Inside allowed range " + (change * 100d) + "%"); return false; } } } APSResult(); APSResult rate(double rate); APSResult duration(int duration); APSResult percent(int percent); APSResult tempBasalRequested(boolean tempBasalRequested); APSResult usePercent(boolean usePercent); APSResult json(JSONObject json); @Override String toString(); Spanned toSpanned(); APSResult clone(); JSONObject json(); List<BgReading> getPredictions(); long getLatestPredictionsTime(); boolean isChangeRequested(); public long date; public String reason; public double rate; public int percent; public boolean usePercent; public int duration; public boolean tempBasalRequested; public boolean bolusRequested; public IobTotal iob; public JSONObject json; public boolean hasPredictions; public double smb; public long deliverAt; public Constraint<Double> inputConstraints; public Constraint<Double> rateConstraint; public Constraint<Integer> percentConstraint; public Constraint<Double> smbConstraint; }
@Test public void isChangeRequestedTest() { APSResult apsResult = new APSResult(); virtualPumpPlugin.getPumpDescription().setPumpDescription(PumpType.Cellnovo1); apsResult.usePercent(true); closedLoopEnabled.set(true); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(null); apsResult.tempBasalRequested(false); Assert.assertEquals(false, apsResult.isChangeRequested()); apsResult.tempBasalRequested(true).percent(200).duration(30); Assert.assertEquals(true, apsResult.isChangeRequested()); closedLoopEnabled.set(false); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(null); apsResult.tempBasalRequested(false); Assert.assertEquals(false, apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(null); apsResult.tempBasalRequested(true).percent(100).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().percent(70).duration(30)); apsResult.tempBasalRequested(true).percent(70).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().percent(10).duration(30)); apsResult.tempBasalRequested(true).percent(0).duration(30); Assert.assertEquals(true , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().percent(190).duration(30)); apsResult.tempBasalRequested(true).percent(200).duration(30); Assert.assertEquals(true , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().percent(70).duration(30)); apsResult.tempBasalRequested(true).percent(80).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().percent(70).duration(30)); apsResult.tempBasalRequested(true).percent(120).duration(30); Assert.assertEquals(true , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().absolute(1).duration(30)); apsResult.tempBasalRequested(true).percent(100).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().absolute(2).duration(30)); apsResult.tempBasalRequested(true).percent(50).duration(30); Assert.assertEquals(true , apsResult.isChangeRequested()); virtualPumpPlugin.getPumpDescription().setPumpDescription(PumpType.Medtronic_515_715); apsResult.usePercent(false); closedLoopEnabled.set(false); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(null); apsResult.tempBasalRequested(true).rate(1).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().absolute(2).duration(30)); apsResult.tempBasalRequested(true).rate(2).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().percent(200).duration(30)); apsResult.tempBasalRequested(true).rate(2).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().absolute(0.1d).duration(30)); apsResult.tempBasalRequested(true).rate(0).duration(30); Assert.assertEquals(true , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().absolute(34.9).duration(30)); apsResult.tempBasalRequested(true).rate(35).duration(30); Assert.assertEquals(true , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().absolute(1.1d).duration(30)); apsResult.tempBasalRequested(true).rate(1.2d).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().absolute(1.1d).duration(30)); apsResult.tempBasalRequested(true).rate(1.5d).duration(30); Assert.assertEquals(true , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().percent(110).duration(30)); apsResult.tempBasalRequested(true).rate(1.1d).duration(30); Assert.assertEquals(false , apsResult.isChangeRequested()); when(treatmentsPlugin.getTempBasalFromHistory(anyLong())).thenReturn(new TemporaryBasal().percent(200).duration(30)); apsResult.tempBasalRequested(true).rate(0.5d).duration(30); Assert.assertEquals(true , apsResult.isChangeRequested()); }