Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app.aaps.pump.omnipod.common.queue.command

import app.aaps.core.interfaces.queue.CustomCommand

class CommandDeliverBasalCorrection : CustomCommand {

override val statusDescription = "BASAL COMPENSATION BOLUS"
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import app.aaps.pump.omnipod.common.definition.OmnipodCommandType
import app.aaps.pump.omnipod.common.keys.OmnipodBooleanPreferenceKey
import app.aaps.pump.omnipod.common.keys.OmnipodIntPreferenceKey
import app.aaps.pump.omnipod.common.queue.command.CommandDeactivatePod
import app.aaps.pump.omnipod.common.queue.command.CommandDeliverBasalCorrection
import app.aaps.pump.omnipod.common.queue.command.CommandDisableSuspendAlerts
import app.aaps.pump.omnipod.common.queue.command.CommandHandleTimeChange
import app.aaps.pump.omnipod.common.queue.command.CommandPlayTestBeep
Expand Down Expand Up @@ -497,13 +498,86 @@ class OmnipodDashPumpPlugin @Inject constructor(
},
fabricPrivacy::logException
)
disposables += rxBus
.toObservable(EventOmnipodDashPumpValuesChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe(
{
if (podStateManager.needsBasalCorrection() &&
!commandQueue.isCustomCommandInQueue(CommandDeliverBasalCorrection::class.java)
) {
commandQueue.customCommand(CommandDeliverBasalCorrection(), null)
}
},
fabricPrivacy::logException
)
}

override fun onStop() {
super.onStop()
disposables.clear()
}

private fun deliverBasalCorrection(): PumpEnactResult {
if (!podStateManager.needsBasalCorrection()) {
aapsLogger.info(LTag.PUMP, "Basal correction no longer appropriate")
return pumpEnactResultProvider.get().success(true).enacted(false).comment("Basal correction no longer appropriate")
}

// Set cooldown to prevent duplicate corrections
podStateManager.lastBasalCorrectionTime = System.currentTimeMillis()

val requestedInsulinAmount = PodConstants.POD_PULSE_BOLUS_UNITS

if (requestedInsulinAmount > reservoirLevel) {
aapsLogger.info(LTag.PUMP, "Basal correction skipped: not enough insulin in reservoir ($requestedInsulinAmount > $reservoirLevel)")
return pumpEnactResultProvider.get().success(false).enacted(false).comment("Not enough insulin in reservoir")
}
if (podStateManager.deliveryStatus?.bolusDeliveringActive() == true) {
aapsLogger.info(LTag.PUMP, "Basal correction skipped: bolus already in progress")
return pumpEnactResultProvider.get().success(false).enacted(false).comment("Bolus already in progress")
}
try {
bolusDeliveryInProgress = true
podStateManager.basalCorrectionInProgress = true
aapsLogger.info(LTag.PUMP, "Delivering basal correction")

val bolusBeeps = preferences.get(OmnipodBooleanPreferenceKey.BolusBeepsEnabled)

return executeProgrammingCommand(
historyEntry = history.createRecord(
commandType = OmnipodCommandType.SET_BOLUS,
bolusRecord = BolusRecord(
requestedInsulinAmount,
BolusType.DEFAULT
)
),
activeCommandEntry = { historyId ->
podStateManager.createActiveCommand(
historyId,
requestedBolus = requestedInsulinAmount
)
},
command = omnipodManager.bolus(
requestedInsulinAmount,
bolusBeeps,
bolusBeeps
).filter { podEvent -> podEvent.isCommandSent() }
.ignoreElements(),
post = waitForBolusDeliveryToComplete(requestedInsulinAmount, BS.Type.NORMAL)
.doOnSuccess { delivered ->
aapsLogger.info(LTag.PUMP, "Basal correction delivered: $delivered U")
}
.ignoreElement()
).doOnError { e ->
aapsLogger.error(LTag.PUMP, "Basal correction delivery failed", e)
}.toPumpEnactResultImpl()
} finally {
bolusDeliveryInProgress = false
podStateManager.basalCorrectionInProgress = false
}
}

private fun observeDeliverySuspended(): Completable = Completable.defer {
if (podStateManager.deliveryStatus == DeliveryStatus.SUSPENDED)
Completable.complete()
Expand Down Expand Up @@ -1008,6 +1082,9 @@ class OmnipodDashPumpPlugin @Inject constructor(
is CommandDisableSuspendAlerts ->
disableSuspendAlerts()

is CommandDeliverBasalCorrection ->
deliverBasalCorrection()

else -> {
aapsLogger.warn(LTag.PUMP, "Unsupported custom command: " + customCommand.javaClass.name)
pumpEnactResultProvider.get().success(false).enacted(false).comment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ interface OmnipodDashPodStateManager {

fun updateLowReservoirAlertSettings(lowReservoirAlertEnabled: Boolean, lowReservoirAlertUnits: Int): Completable

var lastBasalCorrectionTime: Long?
var basalCorrectionInProgress: Boolean
fun needsBasalCorrection(): Boolean

data class ActiveCommand(
val sequence: Short,
val createdRealtime: Long,
Expand Down
Loading
Loading