diff --git a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/ComboV2Plugin.kt b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/ComboV2Plugin.kt index cf7def4f64..9f04934b82 100644 --- a/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/ComboV2Plugin.kt +++ b/pump/combov2/src/main/kotlin/info/nightscout/pump/combov2/ComboV2Plugin.kt @@ -1012,6 +1012,10 @@ class ComboV2Plugin @Inject constructor( } override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } + val oldInsulinAmount = detailedBolusInfo.insulin detailedBolusInfo.insulin = constraintChecker .applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)) @@ -1021,14 +1025,6 @@ class ComboV2Plugin @Inject constructor( "Applied bolus constraints: old insulin amount: $oldInsulinAmount new: ${detailedBolusInfo.insulin}" ) - // Carbs are not allowed because the Combo does not record carbs. - // This is defined in the ACCU_CHEK_COMBO PumpType enum's - // pumpCapability field, so AndroidAPS is informed about this - // lack of carb storage capability. We therefore do not expect - // nonzero carbs here. - // (Also, a zero insulin value makes no sense when bolusing.) - require((detailedBolusInfo.insulin > 0) && (detailedBolusInfo.carbs <= 0.0)) { detailedBolusInfo.toString() } - val acquiredPump = getAcquiredPump() val requestedBolusAmount = detailedBolusInfo.insulin.iuToCctlBolus() diff --git a/pump/danar/src/main/java/info/nightscout/androidaps/danaRKorean/DanaRKoreanPlugin.kt b/pump/danar/src/main/java/info/nightscout/androidaps/danaRKorean/DanaRKoreanPlugin.kt index 3676a77d3e..04920c92a4 100644 --- a/pump/danar/src/main/java/info/nightscout/androidaps/danaRKorean/DanaRKoreanPlugin.kt +++ b/pump/danar/src/main/java/info/nightscout/androidaps/danaRKorean/DanaRKoreanPlugin.kt @@ -147,45 +147,41 @@ class DanaRKoreanPlugin @Inject constructor( } override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } + detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value() - require(detailedBolusInfo.carbs > 0) - return if (detailedBolusInfo.insulin > 0) { - val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) - var connectionOK = false - if (detailedBolusInfo.insulin > 0) - connectionOK = sExecutionService.bolus( - detailedBolusInfo.insulin, detailedBolusInfo.carbs.toInt(), detailedBolusInfo.carbsTimestamp - ?: detailedBolusInfo.timestamp, t - ) - val result = PumpEnactResult(injector) - result.success(connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep) - .bolusDelivered(t.insulin) - if (!result.success) result.comment( - rh.gs( - info.nightscout.pump.dana.R.string.boluserrorcode, - detailedBolusInfo.insulin, - t.insulin, - danaPump.bolusStartErrorCode - ) - ) else result.comment(app.aaps.core.ui.R.string.ok) - aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered) - detailedBolusInfo.insulin = t.insulin - detailedBolusInfo.timestamp = dateUtil.now() - if (detailedBolusInfo.insulin > 0) pumpSync.syncBolusWithPumpId( - detailedBolusInfo.timestamp, - detailedBolusInfo.insulin, - detailedBolusInfo.bolusType, - dateUtil.now(), - PumpType.DANA_R_KOREAN, - serialNumber() + val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) + var connectionOK = false + if (detailedBolusInfo.insulin > 0) + connectionOK = sExecutionService.bolus( + detailedBolusInfo.insulin, detailedBolusInfo.carbs.toInt(), detailedBolusInfo.carbsTimestamp + ?: detailedBolusInfo.timestamp, t ) - result - } else { - val result = PumpEnactResult(injector) - result.success(false).bolusDelivered(0.0).comment(app.aaps.core.ui.R.string.invalid_input) - aapsLogger.error("deliverTreatment: Invalid input") - result - } + val result = PumpEnactResult(injector) + result.success(connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep) + .bolusDelivered(t.insulin) + if (!result.success) result.comment( + rh.gs( + info.nightscout.pump.dana.R.string.boluserrorcode, + detailedBolusInfo.insulin, + t.insulin, + danaPump.bolusStartErrorCode + ) + ) else result.comment(app.aaps.core.ui.R.string.ok) + aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered) + detailedBolusInfo.insulin = t.insulin + detailedBolusInfo.timestamp = dateUtil.now() + if (detailedBolusInfo.insulin > 0) pumpSync.syncBolusWithPumpId( + detailedBolusInfo.timestamp, + detailedBolusInfo.insulin, + detailedBolusInfo.bolusType, + dateUtil.now(), + PumpType.DANA_R_KOREAN, + serialNumber() + ) + return result } // This is called from APS diff --git a/pump/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java b/pump/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java index 8c6649f95d..e3474f695c 100644 --- a/pump/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java +++ b/pump/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java @@ -159,54 +159,50 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { // Pump interface @NonNull @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(new ConstraintObject<>(detailedBolusInfo.insulin, getAapsLogger())).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 - int preferencesSpeed = sp.getInt(info.nightscout.pump.dana.R.string.key_danars_bolusspeed, 0); - int speed = 12; - switch (preferencesSpeed) { - case 0: - speed = 12; - break; - case 1: - speed = 30; - break; - case 2: - speed = 60; - break; - } - detailedBolusInfo.timestamp = dateUtil.now() + (long) (speed * detailedBolusInfo.insulin * 1000); - // clean carbs to prevent counting them as twice because they will picked up as another record - // I don't think it's necessary to copy DetailedBolusInfo right now for carbs records - double carbs = detailedBolusInfo.carbs; - detailedBolusInfo.carbs = 0; - long carbTimeStamp = detailedBolusInfo.getCarbsTimestamp() != null ? detailedBolusInfo.getCarbsTimestamp() : detailedBolusInfo.timestamp; - if (carbTimeStamp == detailedBolusInfo.timestamp) carbTimeStamp -= T.Companion.mins(1).msecs(); // better set 1 min back to prevents clash with insulin - - detailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history - - EventOverviewBolusProgress.Treatment t = new EventOverviewBolusProgress.Treatment(0, 0, detailedBolusInfo.getBolusType() == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.getId()); - boolean connectionOK = false; - if (detailedBolusInfo.insulin > 0 || carbs > 0) - connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) carbs, carbTimeStamp, t); - PumpEnactResult result = new PumpEnactResult(getInjector()); - result.success(connectionOK && Math.abs(detailedBolusInfo.insulin - t.getInsulin()) < pumpDescription.getBolusStep()) - .bolusDelivered(t.getInsulin()); - if (!result.getSuccess()) - result.comment(rh.gs(info.nightscout.pump.dana.R.string.boluserrorcode, detailedBolusInfo.insulin, t.getInsulin(), - danaPump.getBolusStartErrorCode())); - else - result.comment(app.aaps.core.ui.R.string.ok); - aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.getBolusDelivered()); - // remove carbs because it's get from history separately - return result; - } else { - PumpEnactResult result = new PumpEnactResult(getInjector()); - result.success(false).bolusDelivered(0d).comment(app.aaps.core.ui.R.string.invalid_input); - aapsLogger.error("deliverTreatment: Invalid input"); - return result; + if (detailedBolusInfo.insulin == 0 || detailedBolusInfo.carbs > 0) { + throw new IllegalArgumentException(detailedBolusInfo.toString(), new Exception()); } + detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(new ConstraintObject<>(detailedBolusInfo.insulin, getAapsLogger())).value(); + // v2 stores end time for bolus, we need to adjust time + // default delivery speed is 12 sec/U + int preferencesSpeed = sp.getInt(info.nightscout.pump.dana.R.string.key_danars_bolusspeed, 0); + int speed = 12; + switch (preferencesSpeed) { + case 0: + speed = 12; + break; + case 1: + speed = 30; + break; + case 2: + speed = 60; + break; + } + detailedBolusInfo.timestamp = dateUtil.now() + (long) (speed * detailedBolusInfo.insulin * 1000); + // clean carbs to prevent counting them as twice because they will picked up as another record + // I don't think it's necessary to copy DetailedBolusInfo right now for carbs records + double carbs = detailedBolusInfo.carbs; + detailedBolusInfo.carbs = 0; + long carbTimeStamp = detailedBolusInfo.getCarbsTimestamp() != null ? detailedBolusInfo.getCarbsTimestamp() : detailedBolusInfo.timestamp; + if (carbTimeStamp == detailedBolusInfo.timestamp) carbTimeStamp -= T.Companion.mins(1).msecs(); // better set 1 min back to prevents clash with insulin + + detailedBolusInfoStorage.add(detailedBolusInfo); // will be picked up on reading history + + EventOverviewBolusProgress.Treatment t = new EventOverviewBolusProgress.Treatment(0, 0, detailedBolusInfo.getBolusType() == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.getId()); + boolean connectionOK = false; + if (detailedBolusInfo.insulin > 0 || carbs > 0) + connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) carbs, carbTimeStamp, t); + PumpEnactResult result = new PumpEnactResult(getInjector()); + result.success(connectionOK && Math.abs(detailedBolusInfo.insulin - t.getInsulin()) < pumpDescription.getBolusStep()) + .bolusDelivered(t.getInsulin()); + if (!result.getSuccess()) + result.comment(rh.gs(info.nightscout.pump.dana.R.string.boluserrorcode, detailedBolusInfo.insulin, t.getInsulin(), + danaPump.getBolusStartErrorCode())); + else + result.comment(app.aaps.core.ui.R.string.ok); + aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.getBolusDelivered()); + // remove carbs because it's get from history separately + return result; } @Override diff --git a/pump/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java b/pump/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java index 9463c6c121..59434f381e 100644 --- a/pump/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java +++ b/pump/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java @@ -160,44 +160,40 @@ public class DanaRPlugin extends AbstractDanaRPlugin { @NonNull @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(new ConstraintObject<>(detailedBolusInfo.insulin, getAapsLogger())).value(); - if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { - EventOverviewBolusProgress.Treatment t = new EventOverviewBolusProgress.Treatment(0, 0, detailedBolusInfo.getBolusType() == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.getId()); - boolean connectionOK = false; - if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) - connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, detailedBolusInfo.getCarbsTimestamp() != null ? detailedBolusInfo.getCarbsTimestamp() : detailedBolusInfo.timestamp, t); - PumpEnactResult result = new PumpEnactResult(getInjector()); - result.success(connectionOK && Math.abs(detailedBolusInfo.insulin - t.getInsulin()) < pumpDescription.getBolusStep()) - .bolusDelivered(t.getInsulin()); - if (!result.getSuccess()) - result.comment(rh.gs(info.nightscout.pump.dana.R.string.boluserrorcode, detailedBolusInfo.insulin, t.getInsulin(), danaPump.getBolusStartErrorCode())); - else - result.comment(app.aaps.core.ui.R.string.ok); - aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.getBolusDelivered()); - detailedBolusInfo.insulin = t.getInsulin(); - detailedBolusInfo.timestamp = System.currentTimeMillis(); - if (detailedBolusInfo.insulin > 0) - pumpSync.syncBolusWithPumpId( - detailedBolusInfo.timestamp, - detailedBolusInfo.insulin, - detailedBolusInfo.getBolusType(), - dateUtil.now(), - PumpType.DANA_R, - serialNumber()); - if (detailedBolusInfo.carbs > 0) - pumpSync.syncCarbsWithTimestamp( - detailedBolusInfo.getCarbsTimestamp() != null ? detailedBolusInfo.getCarbsTimestamp() : detailedBolusInfo.timestamp, - detailedBolusInfo.carbs, - null, - PumpType.DANA_R, - serialNumber()); - return result; - } else { - PumpEnactResult result = new PumpEnactResult(getInjector()); - result.success(false).bolusDelivered(0d).comment(app.aaps.core.ui.R.string.invalid_input); - aapsLogger.error("deliverTreatment: Invalid input"); - return result; + if (detailedBolusInfo.insulin == 0 || detailedBolusInfo.carbs > 0) { + throw new IllegalArgumentException(detailedBolusInfo.toString(), new Exception()); } + detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(new ConstraintObject<>(detailedBolusInfo.insulin, getAapsLogger())).value(); + EventOverviewBolusProgress.Treatment t = new EventOverviewBolusProgress.Treatment(0, 0, detailedBolusInfo.getBolusType() == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.getId()); + boolean connectionOK = false; + if (detailedBolusInfo.insulin > 0) + connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, detailedBolusInfo.getCarbsTimestamp() != null ? detailedBolusInfo.getCarbsTimestamp() : detailedBolusInfo.timestamp, t); + PumpEnactResult result = new PumpEnactResult(getInjector()); + result.success(connectionOK && Math.abs(detailedBolusInfo.insulin - t.getInsulin()) < pumpDescription.getBolusStep()) + .bolusDelivered(t.getInsulin()); + if (!result.getSuccess()) + result.comment(rh.gs(info.nightscout.pump.dana.R.string.boluserrorcode, detailedBolusInfo.insulin, t.getInsulin(), danaPump.getBolusStartErrorCode())); + else + result.comment(app.aaps.core.ui.R.string.ok); + aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.getBolusDelivered()); + detailedBolusInfo.insulin = t.getInsulin(); + detailedBolusInfo.timestamp = System.currentTimeMillis(); + if (detailedBolusInfo.insulin > 0) + pumpSync.syncBolusWithPumpId( + detailedBolusInfo.timestamp, + detailedBolusInfo.insulin, + detailedBolusInfo.getBolusType(), + dateUtil.now(), + PumpType.DANA_R, + serialNumber()); + if (detailedBolusInfo.carbs > 0) + pumpSync.syncCarbsWithTimestamp( + detailedBolusInfo.getCarbsTimestamp() != null ? detailedBolusInfo.getCarbsTimestamp() : detailedBolusInfo.timestamp, + detailedBolusInfo.carbs, + null, + PumpType.DANA_R, + serialNumber()); + return result; } // This is called from APS diff --git a/pump/danars/src/main/java/info/nightscout/pump/danars/DanaRSPlugin.kt b/pump/danars/src/main/java/info/nightscout/pump/danars/DanaRSPlugin.kt index acd242c277..7f5f481452 100644 --- a/pump/danars/src/main/java/info/nightscout/pump/danars/DanaRSPlugin.kt +++ b/pump/danars/src/main/java/info/nightscout/pump/danars/DanaRSPlugin.kt @@ -288,52 +288,47 @@ class DanaRSPlugin @Inject constructor( @Synchronized override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } + detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value() - return if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { - val preferencesSpeed = sp.getInt(info.nightscout.pump.dana.R.string.key_danars_bolusspeed, 0) - var speed = 12 - when (preferencesSpeed) { - 0 -> speed = 12 - 1 -> speed = 30 - 2 -> speed = 60 - } - // RS stores end time for bolus, we need to adjust time - // default delivery speed is 12 sec/U - detailedBolusInfo.timestamp = dateUtil.now() + (speed * detailedBolusInfo.insulin * 1000).toLong() - // clean carbs to prevent counting them as twice because they will picked up as another record - // I don't think it's necessary to copy DetailedBolusInfo right now for carbs records - val carbs = detailedBolusInfo.carbs - detailedBolusInfo.carbs = 0.0 - var carbTimeStamp = detailedBolusInfo.carbsTimestamp ?: detailedBolusInfo.timestamp - if (carbTimeStamp == detailedBolusInfo.timestamp) carbTimeStamp -= T.mins(1).msecs() // better set 1 min back to prevents clash with insulin - detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history - val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) - var connectionOK = false - if (detailedBolusInfo.insulin > 0 || carbs > 0) connectionOK = danaRSService?.bolus(detailedBolusInfo.insulin, carbs.toInt(), carbTimeStamp, t) - ?: false - val result = PumpEnactResult(injector) - result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep - result.bolusDelivered = t.insulin - if (!result.success) { - var error = "" + danaPump.bolusStartErrorCode - when (danaPump.bolusStartErrorCode) { - 0x10 -> error = rh.gs(info.nightscout.pump.dana.R.string.maxbolusviolation) - 0x20 -> error = rh.gs(info.nightscout.pump.dana.R.string.commanderror) - 0x40 -> error = rh.gs(info.nightscout.pump.dana.R.string.speederror) - 0x80 -> error = rh.gs(info.nightscout.pump.dana.R.string.insulinlimitviolation) - } - result.comment = rh.gs(info.nightscout.pump.dana.R.string.boluserrorcode, detailedBolusInfo.insulin, t.insulin, error) - } else result.comment = rh.gs(app.aaps.core.ui.R.string.ok) - aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered) - result - } else { - val result = PumpEnactResult(injector) - result.success = false - result.bolusDelivered = 0.0 - result.comment = rh.gs(app.aaps.core.ui.R.string.invalid_input) - aapsLogger.error("deliverTreatment: Invalid input") - result + val preferencesSpeed = sp.getInt(info.nightscout.pump.dana.R.string.key_danars_bolusspeed, 0) + var speed = 12 + when (preferencesSpeed) { + 0 -> speed = 12 + 1 -> speed = 30 + 2 -> speed = 60 } + // RS stores end time for bolus, we need to adjust time + // default delivery speed is 12 sec/U + detailedBolusInfo.timestamp = dateUtil.now() + (speed * detailedBolusInfo.insulin * 1000).toLong() + // clean carbs to prevent counting them as twice because they will picked up as another record + // I don't think it's necessary to copy DetailedBolusInfo right now for carbs records + val carbs = detailedBolusInfo.carbs + detailedBolusInfo.carbs = 0.0 + var carbTimeStamp = detailedBolusInfo.carbsTimestamp ?: detailedBolusInfo.timestamp + if (carbTimeStamp == detailedBolusInfo.timestamp) carbTimeStamp -= T.mins(1).msecs() // better set 1 min back to prevents clash with insulin + detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history + val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) + var connectionOK = false + if (detailedBolusInfo.insulin > 0 || carbs > 0) connectionOK = danaRSService?.bolus(detailedBolusInfo.insulin, carbs.toInt(), carbTimeStamp, t) + ?: false + val result = PumpEnactResult(injector) + result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep + result.bolusDelivered = t.insulin + if (!result.success) { + var error = "" + danaPump.bolusStartErrorCode + when (danaPump.bolusStartErrorCode) { + 0x10 -> error = rh.gs(info.nightscout.pump.dana.R.string.maxbolusviolation) + 0x20 -> error = rh.gs(info.nightscout.pump.dana.R.string.commanderror) + 0x40 -> error = rh.gs(info.nightscout.pump.dana.R.string.speederror) + 0x80 -> error = rh.gs(info.nightscout.pump.dana.R.string.insulinlimitviolation) + } + result.comment = rh.gs(info.nightscout.pump.dana.R.string.boluserrorcode, detailedBolusInfo.insulin, t.insulin, error) + } else result.comment = rh.gs(app.aaps.core.ui.R.string.ok) + aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered) + return result } override fun stopBolusDelivering() { diff --git a/pump/diaconn/src/main/java/info/nightscout/pump/diaconn/DiaconnG8Plugin.kt b/pump/diaconn/src/main/java/info/nightscout/pump/diaconn/DiaconnG8Plugin.kt index 96ebe852ac..1ebe750078 100644 --- a/pump/diaconn/src/main/java/info/nightscout/pump/diaconn/DiaconnG8Plugin.kt +++ b/pump/diaconn/src/main/java/info/nightscout/pump/diaconn/DiaconnG8Plugin.kt @@ -271,35 +271,30 @@ class DiaconnG8Plugin @Inject constructor( @Synchronized override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { - detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value() - return if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { - val carbs = detailedBolusInfo.carbs - detailedBolusInfo.carbs = 0.0 - var carbTimeStamp = detailedBolusInfo.carbsTimestamp ?: detailedBolusInfo.timestamp - if (carbTimeStamp == detailedBolusInfo.timestamp) carbTimeStamp -= T.mins(1).msecs() // better set 1 min back to prevents clash with insulin - detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history - val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) - var connectionOK = false - if (detailedBolusInfo.insulin > 0 || carbs > 0) connectionOK = diaconnG8Service?.bolus(detailedBolusInfo.insulin, carbs.toInt(), carbTimeStamp, t) - ?: false - val result = PumpEnactResult(injector) - result.success = connectionOK - result.bolusDelivered = t.insulin + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } - if (result.success) result.enacted = true - if (!result.success) { - setErrorMsg(diaconnG8Pump.resultErrorCode, result) - } else result.comment = rh.gs(app.aaps.core.ui.R.string.ok) - aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered) - result - } else { - val result = PumpEnactResult(injector) - result.success = false - result.bolusDelivered = 0.0 - result.comment = rh.gs(app.aaps.core.ui.R.string.invalid_input) - aapsLogger.error("deliverTreatment: Invalid input") - result - } + detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value() + val carbs = detailedBolusInfo.carbs + detailedBolusInfo.carbs = 0.0 + var carbTimeStamp = detailedBolusInfo.carbsTimestamp ?: detailedBolusInfo.timestamp + if (carbTimeStamp == detailedBolusInfo.timestamp) carbTimeStamp -= T.mins(1).msecs() // better set 1 min back to prevents clash with insulin + detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history + val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) + var connectionOK = false + if (detailedBolusInfo.insulin > 0 || carbs > 0) connectionOK = diaconnG8Service?.bolus(detailedBolusInfo.insulin, carbs.toInt(), carbTimeStamp, t) + ?: false + val result = PumpEnactResult(injector) + result.success = connectionOK + result.bolusDelivered = t.insulin + + if (result.success) result.enacted = true + if (!result.success) { + setErrorMsg(diaconnG8Pump.resultErrorCode, result) + } else result.comment = rh.gs(app.aaps.core.ui.R.string.ok) + aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered) + return result } override fun stopBolusDelivering() { diff --git a/pump/eopatch/src/main/java/info/nightscout/androidaps/plugins/pump/eopatch/EopatchPumpPlugin.kt b/pump/eopatch/src/main/java/info/nightscout/androidaps/plugins/pump/eopatch/EopatchPumpPlugin.kt index 89d28165fd..a183f3b064 100644 --- a/pump/eopatch/src/main/java/info/nightscout/androidaps/plugins/pump/eopatch/EopatchPumpPlugin.kt +++ b/pump/eopatch/src/main/java/info/nightscout/androidaps/plugins/pump/eopatch/EopatchPumpPlugin.kt @@ -270,61 +270,58 @@ class EopatchPumpPlugin @Inject constructor( } override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } val askedInsulin = detailedBolusInfo.insulin - if (detailedBolusInfo.insulin > 0.0) { - var isSuccess = true - val result = BehaviorSubject.createDefault(true) - val disposable = result.hide() - .subscribe { - isSuccess = it - } - - mDisposables.add(patchManager.startCalculatorBolus(detailedBolusInfo) - .doOnSuccess { - mLastDataTime = System.currentTimeMillis() - }.subscribe({ - result.onNext(it.isSuccess) - }, { - result.onNext(false) - }) - ) - - val tr = detailedBolusInfo.let { - EventOverviewBolusProgress.Treatment(it.insulin, it.carbs.toInt(), it.bolusType === DetailedBolusInfo.BolusType.SMB, it.id) + var isSuccess = true + val result = BehaviorSubject.createDefault(true) + val disposable = result.hide() + .subscribe { + isSuccess = it } - do { - SystemClock.sleep(100) - if (patchManager.patchConnectionState.isConnected) { - val delivering = patchManager.bolusCurrent.nowBolus.injected - rxBus.send(EventOverviewBolusProgress.apply { - status = rh.gs(app.aaps.core.ui.R.string.bolus_delivering, delivering) - percent = min((delivering / detailedBolusInfo.insulin * 100).toInt(), 100) - t = tr - }) - } - } while (!patchManager.bolusCurrent.nowBolus.endTimeSynced && isSuccess) + mDisposables.add(patchManager.startCalculatorBolus(detailedBolusInfo) + .doOnSuccess { + mLastDataTime = System.currentTimeMillis() + }.subscribe({ + result.onNext(it.isSuccess) + }, { + result.onNext(false) + }) + ) - rxBus.send(EventOverviewBolusProgress.apply { - status = rh.gs(app.aaps.core.ui.R.string.bolus_delivered_successfully, detailedBolusInfo.insulin) - percent = 100 - }) - - detailedBolusInfo.insulin = patchManager.bolusCurrent.nowBolus.injected.toDouble() - patchManager.addBolusToHistory(detailedBolusInfo) - - disposable.dispose() - - return if (isSuccess && abs(askedInsulin - detailedBolusInfo.insulin) < pumpDescription.bolusStep) - PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(askedInsulin) - else - PumpEnactResult(injector).success(false)/*.enacted(false)*/.bolusDelivered(Round.roundTo(detailedBolusInfo.insulin, 0.01)) - - } else { - // no bolus required - return PumpEnactResult(injector).success(false).enacted(false).bolusDelivered(0.0).comment(rh.gs(app.aaps.core.ui.R.string.error)) + val tr = detailedBolusInfo.let { + EventOverviewBolusProgress.Treatment(it.insulin, it.carbs.toInt(), it.bolusType === DetailedBolusInfo.BolusType.SMB, it.id) } + + do { + SystemClock.sleep(100) + if (patchManager.patchConnectionState.isConnected) { + val delivering = patchManager.bolusCurrent.nowBolus.injected + rxBus.send(EventOverviewBolusProgress.apply { + status = rh.gs(app.aaps.core.ui.R.string.bolus_delivering, delivering) + percent = min((delivering / detailedBolusInfo.insulin * 100).toInt(), 100) + t = tr + }) + } + } while (!patchManager.bolusCurrent.nowBolus.endTimeSynced && isSuccess) + + rxBus.send(EventOverviewBolusProgress.apply { + status = rh.gs(app.aaps.core.ui.R.string.bolus_delivered_successfully, detailedBolusInfo.insulin) + percent = 100 + }) + + detailedBolusInfo.insulin = patchManager.bolusCurrent.nowBolus.injected.toDouble() + patchManager.addBolusToHistory(detailedBolusInfo) + + disposable.dispose() + + return if (isSuccess && abs(askedInsulin - detailedBolusInfo.insulin) < pumpDescription.bolusStep) + PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(askedInsulin) + else + PumpEnactResult(injector).success(false)/*.enacted(false)*/.bolusDelivered(Round.roundTo(detailedBolusInfo.insulin, 0.01)) } override fun stopBolusDelivering() { diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt index c237d9c2fd..bf15b3a240 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt @@ -268,7 +268,7 @@ import kotlin.math.abs val patchExpirationPref = preferenceFragment.findPreference(rh.gs(R.string.key_patch_expiration)) val pumpWarningNotificationPref = preferenceFragment.findPreference(rh.gs(R.string.key_pump_warning_notification)) val pumpWarningExpiryHourPref = preferenceFragment.findPreference(rh.gs(R.string.key_pump_warning_expiry_hour)) - + pumpWarningExpiryHourPref?.isEnabled = patchExpirationPref?.isChecked == true && pumpWarningNotificationPref?.isChecked == true } @@ -377,33 +377,27 @@ import kotlin.math.abs @Synchronized override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } + aapsLogger.debug(LTag.PUMP, "deliverTreatment: " + detailedBolusInfo.insulin + "U") if (!isInitialized()) return PumpEnactResult(injector).success(false).enacted(false) detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value() - return if (detailedBolusInfo.insulin > 0 && detailedBolusInfo.carbs == 0.0) { - aapsLogger.debug(LTag.PUMP, "deliverTreatment: Delivering bolus: " + detailedBolusInfo.insulin + "U") - val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) - val connectionOK = medtrumService?.setBolus(detailedBolusInfo, t) ?: false - val result = PumpEnactResult(injector) - result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep - result.bolusDelivered = t.insulin - if (!result.success) { - // Note: There are no error codes - result.comment = "failed" - } else { - result.comment = "ok" - } - aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Success: ${result.success} Asked: ${detailedBolusInfo.insulin} Delivered: ${result.bolusDelivered}") - result + aapsLogger.debug(LTag.PUMP, "deliverTreatment: Delivering bolus: " + detailedBolusInfo.insulin + "U") + val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) + val connectionOK = medtrumService?.setBolus(detailedBolusInfo, t) ?: false + val result = PumpEnactResult(injector) + result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep + result.bolusDelivered = t.insulin + if (!result.success) { + // Note: There are no error codes + result.comment = "failed" } else { - aapsLogger.debug(LTag.PUMP, "deliverTreatment: Invalid input") - val result = PumpEnactResult(injector) - result.success = false - result.bolusDelivered = 0.0 - result.comment = rh.gs(app.aaps.core.ui.R.string.invalid_input) - aapsLogger.error("deliverTreatment: Invalid input") - result + result.comment = "ok" } + aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Success: ${result.success} Asked: ${detailedBolusInfo.insulin} Delivered: ${result.bolusDelivered}") + return result } override fun stopBolusDelivering() { diff --git a/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 47a3f9c819..39cd00ef9a 100644 --- a/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/pump/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -554,19 +554,13 @@ class OmnipodDashPumpPlugin @Inject constructor( get() = 0 override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } + try { bolusDeliveryInProgress = true aapsLogger.info(LTag.PUMP, "Delivering treatment: $detailedBolusInfo $bolusCanceled") - if (detailedBolusInfo.carbs > 0 || - detailedBolusInfo.insulin == 0.0 - ) { - // Accept only valid insulin requests - return PumpEnactResult(injector) - .success(false) - .enacted(false) - .bolusDelivered(0.0) - .comment("Invalid input") - } val requestedBolusAmount = detailedBolusInfo.insulin if (requestedBolusAmount > reservoirLevel) { return PumpEnactResult(injector) diff --git a/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/OmnipodErosPumpPlugin.java b/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/OmnipodErosPumpPlugin.java index a308bb6b10..902d8829b9 100644 --- a/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/OmnipodErosPumpPlugin.java +++ b/pump/omnipod-eros/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/eros/OmnipodErosPumpPlugin.java @@ -22,7 +22,6 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.List; -import java.util.Locale; import java.util.Objects; import java.util.Optional; import java.util.function.Supplier; @@ -661,29 +660,10 @@ public class OmnipodErosPumpPlugin extends PumpPluginBase implements Pump, Riley @NonNull @Override public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { - if (detailedBolusInfo.insulin == 0 && detailedBolusInfo.carbs == 0) { - // neither carbs nor bolus requested - aapsLogger.error("deliverTreatment: Invalid input: neither carbs nor insulin are set in treatment"); - return new PumpEnactResult(getInjector()).success(false).enacted(false).bolusDelivered(0d) - .comment(app.aaps.core.ui.R.string.invalid_input); - } else if (detailedBolusInfo.insulin > 0) { - // bolus needed, ask pump to deliver it - return deliverBolus(detailedBolusInfo); - } else { - // no bolus required, carb only treatment - boolean result = pumpSync.syncCarbsWithTimestamp( - detailedBolusInfo.timestamp, - detailedBolusInfo.carbs, - null, - model(), - serialNumber()); - - aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "syncCarbsWithTimestamp " + - "[date=%d, carbs=%.2f, pumpSerial=%s] - Result: %b", - detailedBolusInfo.timestamp, detailedBolusInfo.carbs, serialNumber(), result)); - - return new PumpEnactResult(getInjector()).success(true).enacted(true).bolusDelivered(0d); + if (detailedBolusInfo.insulin == 0 || detailedBolusInfo.carbs > 0) { + throw new IllegalArgumentException(detailedBolusInfo.toString(), new Exception()); } + return deliverBolus(detailedBolusInfo); } @Override diff --git a/pump/pump-common/src/main/java/info/nightscout/pump/common/PumpPluginAbstract.kt b/pump/pump-common/src/main/java/info/nightscout/pump/common/PumpPluginAbstract.kt index b204793e3a..eda3246426 100644 --- a/pump/pump-common/src/main/java/info/nightscout/pump/common/PumpPluginAbstract.kt +++ b/pump/pump-common/src/main/java/info/nightscout/pump/common/PumpPluginAbstract.kt @@ -25,7 +25,6 @@ import app.aaps.core.interfaces.rx.AapsSchedulers import app.aaps.core.interfaces.rx.bus.RxBus import app.aaps.core.interfaces.rx.events.EventAppExit import app.aaps.core.interfaces.rx.events.EventCustomActionsChanged -import app.aaps.core.interfaces.rx.events.EventOverviewBolusProgress import app.aaps.core.interfaces.sharedPreferences.SP import app.aaps.core.interfaces.utils.DateUtil import app.aaps.core.interfaces.utils.DecimalFormatter @@ -35,7 +34,6 @@ import com.google.gson.GsonBuilder import dagger.android.HasAndroidInjector import info.nightscout.pump.common.data.PumpStatus import info.nightscout.pump.common.defs.PumpDriverState -import info.nightscout.pump.common.sync.PumpDbEntryCarbs import info.nightscout.pump.common.sync.PumpSyncEntriesCreator import info.nightscout.pump.common.sync.PumpSyncStorage import io.reactivex.rxjava3.disposables.CompositeDisposable @@ -303,27 +301,12 @@ abstract class PumpPluginAbstract protected constructor( @Synchronized override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } + return try { - if (detailedBolusInfo.insulin == 0.0 && detailedBolusInfo.carbs == 0.0) { - // neither carbs nor bolus requested - aapsLogger.error("deliverTreatment: Invalid input") - PumpEnactResult(injector).success(false).enacted(false).bolusDelivered(0.0).comment(app.aaps.core.ui.R.string.invalid_input) - } else if (detailedBolusInfo.insulin > 0) { - // bolus needed, ask pump to deliver it - deliverBolus(detailedBolusInfo) - } else { - detailedBolusInfo.timestamp = System.currentTimeMillis() - - // no bolus required, carb only treatment - pumpSyncStorage.addCarbs(PumpDbEntryCarbs(detailedBolusInfo, this)) - - val bolusingEvent = EventOverviewBolusProgress - bolusingEvent.t = EventOverviewBolusProgress.Treatment(0.0, detailedBolusInfo.carbs.toInt(), detailedBolusInfo.bolusType === DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) - bolusingEvent.percent = 100 - rxBus.send(bolusingEvent) - aapsLogger.debug(LTag.PUMP, "deliverTreatment: Carb only treatment.") - PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(0.0).comment(R.string.common_resultok) - } + deliverBolus(detailedBolusInfo) } finally { triggerUIChange() } diff --git a/pump/virtual/src/main/kotlin/app/aaps/pump/virtual/VirtualPumpPlugin.kt b/pump/virtual/src/main/kotlin/app/aaps/pump/virtual/VirtualPumpPlugin.kt index 48d9eff923..dbb679cc66 100644 --- a/pump/virtual/src/main/kotlin/app/aaps/pump/virtual/VirtualPumpPlugin.kt +++ b/pump/virtual/src/main/kotlin/app/aaps/pump/virtual/VirtualPumpPlugin.kt @@ -181,6 +181,10 @@ open class VirtualPumpPlugin @Inject constructor( get() = batteryPercent override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { + // Insulin value must be greater than 0 + require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() } + require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() } + val result = PumpEnactResult(injector) .success(true) .bolusDelivered(detailedBolusInfo.insulin)