Unify check in deliverTreatment, fix DanaRKorean

This commit is contained in:
Milos Kozak 2023-11-01 15:41:31 +01:00
parent 32ec95d474
commit 113f511269
12 changed files with 254 additions and 328 deletions

View file

@ -1012,6 +1012,10 @@ class ComboV2Plugin @Inject constructor(
} }
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { 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 val oldInsulinAmount = detailedBolusInfo.insulin
detailedBolusInfo.insulin = constraintChecker detailedBolusInfo.insulin = constraintChecker
.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)) .applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger))
@ -1021,14 +1025,6 @@ class ComboV2Plugin @Inject constructor(
"Applied bolus constraints: old insulin amount: $oldInsulinAmount new: ${detailedBolusInfo.insulin}" "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 acquiredPump = getAcquiredPump()
val requestedBolusAmount = detailedBolusInfo.insulin.iuToCctlBolus() val requestedBolusAmount = detailedBolusInfo.insulin.iuToCctlBolus()

View file

@ -147,45 +147,41 @@ class DanaRKoreanPlugin @Inject constructor(
} }
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { 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() detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value()
require(detailedBolusInfo.carbs > 0) val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id)
return if (detailedBolusInfo.insulin > 0) { var connectionOK = false
val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) if (detailedBolusInfo.insulin > 0)
var connectionOK = false connectionOK = sExecutionService.bolus(
if (detailedBolusInfo.insulin > 0) detailedBolusInfo.insulin, detailedBolusInfo.carbs.toInt(), detailedBolusInfo.carbsTimestamp
connectionOK = sExecutionService.bolus( ?: detailedBolusInfo.timestamp, t
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()
) )
result val result = PumpEnactResult(injector)
} else { result.success(connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep)
val result = PumpEnactResult(injector) .bolusDelivered(t.insulin)
result.success(false).bolusDelivered(0.0).comment(app.aaps.core.ui.R.string.invalid_input) if (!result.success) result.comment(
aapsLogger.error("deliverTreatment: Invalid input") rh.gs(
result 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 // This is called from APS

View file

@ -159,54 +159,50 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
// Pump interface // Pump interface
@NonNull @Override @NonNull @Override
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(new ConstraintObject<>(detailedBolusInfo.insulin, getAapsLogger())).value(); if (detailedBolusInfo.insulin == 0 || detailedBolusInfo.carbs > 0) {
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { throw new IllegalArgumentException(detailedBolusInfo.toString(), new Exception());
// 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;
} }
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 @Override

View file

@ -160,44 +160,40 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
@NonNull @Override @NonNull @Override
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(new ConstraintObject<>(detailedBolusInfo.insulin, getAapsLogger())).value(); if (detailedBolusInfo.insulin == 0 || detailedBolusInfo.carbs > 0) {
if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { throw new IllegalArgumentException(detailedBolusInfo.toString(), new Exception());
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;
} }
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 // This is called from APS

View file

@ -288,52 +288,47 @@ class DanaRSPlugin @Inject constructor(
@Synchronized @Synchronized
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { 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() 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)
val preferencesSpeed = sp.getInt(info.nightscout.pump.dana.R.string.key_danars_bolusspeed, 0) var speed = 12
var speed = 12 when (preferencesSpeed) {
when (preferencesSpeed) { 0 -> speed = 12
0 -> speed = 12 1 -> speed = 30
1 -> speed = 30 2 -> speed = 60
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
} }
// 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() { override fun stopBolusDelivering() {

View file

@ -271,35 +271,30 @@ class DiaconnG8Plugin @Inject constructor(
@Synchronized @Synchronized
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult {
detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value() // Insulin value must be greater than 0
return if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { require(detailedBolusInfo.carbs == 0.0) { detailedBolusInfo.toString() }
val carbs = detailedBolusInfo.carbs require(detailedBolusInfo.insulin > 0) { detailedBolusInfo.toString() }
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 detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value()
if (!result.success) { val carbs = detailedBolusInfo.carbs
setErrorMsg(diaconnG8Pump.resultErrorCode, result) detailedBolusInfo.carbs = 0.0
} else result.comment = rh.gs(app.aaps.core.ui.R.string.ok) var carbTimeStamp = detailedBolusInfo.carbsTimestamp ?: detailedBolusInfo.timestamp
aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered) if (carbTimeStamp == detailedBolusInfo.timestamp) carbTimeStamp -= T.mins(1).msecs() // better set 1 min back to prevents clash with insulin
result detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history
} else { val t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id)
val result = PumpEnactResult(injector) var connectionOK = false
result.success = false if (detailedBolusInfo.insulin > 0 || carbs > 0) connectionOK = diaconnG8Service?.bolus(detailedBolusInfo.insulin, carbs.toInt(), carbTimeStamp, t)
result.bolusDelivered = 0.0 ?: false
result.comment = rh.gs(app.aaps.core.ui.R.string.invalid_input) val result = PumpEnactResult(injector)
aapsLogger.error("deliverTreatment: Invalid input") result.success = connectionOK
result 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() { override fun stopBolusDelivering() {

View file

@ -270,61 +270,58 @@ class EopatchPumpPlugin @Inject constructor(
} }
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { 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 val askedInsulin = detailedBolusInfo.insulin
if (detailedBolusInfo.insulin > 0.0) { var isSuccess = true
var isSuccess = true val result = BehaviorSubject.createDefault(true)
val result = BehaviorSubject.createDefault(true) val disposable = result.hide()
val disposable = result.hide() .subscribe {
.subscribe { isSuccess = it
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)
} }
do { mDisposables.add(patchManager.startCalculatorBolus(detailedBolusInfo)
SystemClock.sleep(100) .doOnSuccess {
if (patchManager.patchConnectionState.isConnected) { mLastDataTime = System.currentTimeMillis()
val delivering = patchManager.bolusCurrent.nowBolus.injected }.subscribe({
rxBus.send(EventOverviewBolusProgress.apply { result.onNext(it.isSuccess)
status = rh.gs(app.aaps.core.ui.R.string.bolus_delivering, delivering) }, {
percent = min((delivering / detailedBolusInfo.insulin * 100).toInt(), 100) result.onNext(false)
t = tr })
}) )
}
} while (!patchManager.bolusCurrent.nowBolus.endTimeSynced && isSuccess)
rxBus.send(EventOverviewBolusProgress.apply { val tr = detailedBolusInfo.let {
status = rh.gs(app.aaps.core.ui.R.string.bolus_delivered_successfully, detailedBolusInfo.insulin) EventOverviewBolusProgress.Treatment(it.insulin, it.carbs.toInt(), it.bolusType === DetailedBolusInfo.BolusType.SMB, it.id)
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))
} }
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() { override fun stopBolusDelivering() {

View file

@ -377,33 +377,27 @@ import kotlin.math.abs
@Synchronized @Synchronized
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { 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") aapsLogger.debug(LTag.PUMP, "deliverTreatment: " + detailedBolusInfo.insulin + "U")
if (!isInitialized()) return PumpEnactResult(injector).success(false).enacted(false) if (!isInitialized()) return PumpEnactResult(injector).success(false).enacted(false)
detailedBolusInfo.insulin = constraintChecker.applyBolusConstraints(ConstraintObject(detailedBolusInfo.insulin, aapsLogger)).value() 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")
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 t = EventOverviewBolusProgress.Treatment(0.0, 0, detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB, detailedBolusInfo.id) val connectionOK = medtrumService?.setBolus(detailedBolusInfo, t) ?: false
val connectionOK = medtrumService?.setBolus(detailedBolusInfo, t) ?: false val result = PumpEnactResult(injector)
val result = PumpEnactResult(injector) result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep
result.success = connectionOK && abs(detailedBolusInfo.insulin - t.insulin) < pumpDescription.bolusStep result.bolusDelivered = t.insulin
result.bolusDelivered = t.insulin if (!result.success) {
if (!result.success) { // Note: There are no error codes
// Note: There are no error codes result.comment = "failed"
result.comment = "failed"
} else {
result.comment = "ok"
}
aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Success: ${result.success} Asked: ${detailedBolusInfo.insulin} Delivered: ${result.bolusDelivered}")
result
} else { } else {
aapsLogger.debug(LTag.PUMP, "deliverTreatment: Invalid input") result.comment = "ok"
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
} }
aapsLogger.debug(LTag.PUMP, "deliverTreatment: OK. Success: ${result.success} Asked: ${detailedBolusInfo.insulin} Delivered: ${result.bolusDelivered}")
return result
} }
override fun stopBolusDelivering() { override fun stopBolusDelivering() {

View file

@ -554,19 +554,13 @@ class OmnipodDashPumpPlugin @Inject constructor(
get() = 0 get() = 0
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { 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 { try {
bolusDeliveryInProgress = true bolusDeliveryInProgress = true
aapsLogger.info(LTag.PUMP, "Delivering treatment: $detailedBolusInfo $bolusCanceled") 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 val requestedBolusAmount = detailedBolusInfo.insulin
if (requestedBolusAmount > reservoirLevel) { if (requestedBolusAmount > reservoirLevel) {
return PumpEnactResult(injector) return PumpEnactResult(injector)

View file

@ -22,7 +22,6 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -661,29 +660,10 @@ public class OmnipodErosPumpPlugin extends PumpPluginBase implements Pump, Riley
@NonNull @Override @NonNull @Override
public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) {
if (detailedBolusInfo.insulin == 0 && detailedBolusInfo.carbs == 0) { if (detailedBolusInfo.insulin == 0 || detailedBolusInfo.carbs > 0) {
// neither carbs nor bolus requested throw new IllegalArgumentException(detailedBolusInfo.toString(), new Exception());
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);
} }
return deliverBolus(detailedBolusInfo);
} }
@Override @Override

View file

@ -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.bus.RxBus
import app.aaps.core.interfaces.rx.events.EventAppExit import app.aaps.core.interfaces.rx.events.EventAppExit
import app.aaps.core.interfaces.rx.events.EventCustomActionsChanged 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.sharedPreferences.SP
import app.aaps.core.interfaces.utils.DateUtil import app.aaps.core.interfaces.utils.DateUtil
import app.aaps.core.interfaces.utils.DecimalFormatter import app.aaps.core.interfaces.utils.DecimalFormatter
@ -35,7 +34,6 @@ import com.google.gson.GsonBuilder
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.pump.common.data.PumpStatus import info.nightscout.pump.common.data.PumpStatus
import info.nightscout.pump.common.defs.PumpDriverState 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.PumpSyncEntriesCreator
import info.nightscout.pump.common.sync.PumpSyncStorage import info.nightscout.pump.common.sync.PumpSyncStorage
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
@ -303,27 +301,12 @@ abstract class PumpPluginAbstract protected constructor(
@Synchronized @Synchronized
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { 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 { return try {
if (detailedBolusInfo.insulin == 0.0 && detailedBolusInfo.carbs == 0.0) { deliverBolus(detailedBolusInfo)
// 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)
}
} finally { } finally {
triggerUIChange() triggerUIChange()
} }

View file

@ -181,6 +181,10 @@ open class VirtualPumpPlugin @Inject constructor(
get() = batteryPercent get() = batteryPercent
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { 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) val result = PumpEnactResult(injector)
.success(true) .success(true)
.bolusDelivered(detailedBolusInfo.insulin) .bolusDelivered(detailedBolusInfo.insulin)