pumpsync on suspend/resume

This commit is contained in:
Andrei Vereha 2021-06-04 19:32:00 +02:00
parent 76a0e93ee2
commit 5d3b2faeb9
4 changed files with 43 additions and 14 deletions

View file

@ -17,6 +17,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEven
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.ActivationProgress import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.ActivationProgress
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BeepType import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BeepType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.DeliveryStatus import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.DeliveryStatus
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.PodConstants
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.ResponseType import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.ResponseType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandConfirmed import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandConfirmed
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager
@ -296,7 +297,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
tempBasalBeeps tempBasalBeeps
) )
.filter { podEvent -> podEvent is PodEvent.CommandSent } .filter { podEvent -> podEvent is PodEvent.CommandSent }
.map { pumpSyncTempBasal(it, tbrType) } .map { pumpSyncTempBasal(it, absoluteRate, durationInMinutes.toLong(), tbrType) }
.ignoreElements(), .ignoreElements(),
pre = observeNoActiveTempBasal(enforceNew) pre = observeNoActiveTempBasal(enforceNew)
).toPumpEnactResult() ).toPumpEnactResult()
@ -304,6 +305,8 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun pumpSyncTempBasal( private fun pumpSyncTempBasal(
podEvent: PodEvent, podEvent: PodEvent,
absoluteRate: Double,
durationInMinutes: Long,
tbrType: PumpSync.TemporaryBasalType tbrType: PumpSync.TemporaryBasalType
): Boolean { ): Boolean {
val activeCommand = podStateManager.activeCommand val activeCommand = podStateManager.activeCommand
@ -315,14 +318,11 @@ class OmnipodDashPumpPlugin @Inject constructor(
) )
} }
val historyEntry = history.getById(activeCommand.historyId) val historyEntry = history.getById(activeCommand.historyId)
val record = historyEntry.record
if (record == null || !(record is TempBasalRecord)) {
throw IllegalArgumentException("Illegal recording in history: $record. Expected a temp basal")
}
val ret = pumpSync.syncTemporaryBasalWithPumpId( val ret = pumpSync.syncTemporaryBasalWithPumpId(
timestamp = historyEntry.createdAt, timestamp = historyEntry.createdAt,
rate = record.rate, rate = absoluteRate,
duration = T.mins(record.duration.toLong()).msecs(), duration = T.mins(durationInMinutes.toLong()).msecs(),
isAbsolute = true, isAbsolute = true,
type = tbrType, type = tbrType,
pumpId = historyEntry.pumpId(), pumpId = historyEntry.pumpId(),
@ -363,8 +363,9 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
private fun observeActiveTempBasal(): Completable { private fun observeActiveTempBasal(): Completable {
return Completable.defer { return Completable.defer {
if (podStateManager.tempBasalActive) if (podStateManager.tempBasalActive || pumpSync.expectedPumpState().temporaryBasal != null)
Completable.complete() Completable.complete()
else else
Completable.error( Completable.error(
@ -523,8 +524,16 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun suspendDelivery(): PumpEnactResult { private fun suspendDelivery(): PumpEnactResult {
return executeSimpleProgrammingCommand( return executeSimpleProgrammingCommand(
history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY), historyEntry = history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY),
omnipodManager.suspendDelivery().ignoreElements(), command = omnipodManager.suspendDelivery()
.filter { podEvent -> podEvent is PodEvent.CommandSent }
.map {
pumpSyncTempBasal(it,
0.0,
PodConstants.MAX_POD_LIFETIME.standardMinutes,
PumpSync.TemporaryBasalType.PUMP_SUSPEND)
}
.ignoreElements(),
pre = observeDeliveryActive(), pre = observeDeliveryActive(),
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -619,7 +628,9 @@ class OmnipodDashPumpPlugin @Inject constructor(
val historyEntry = history.getById(confirmation.historyId) val historyEntry = history.getById(confirmation.historyId)
aapsLogger.debug(LTag.PUMPCOMM, "handling confirmation command: $confirmation") aapsLogger.debug(LTag.PUMPCOMM, "handling confirmation command: $confirmation")
when (historyEntry.commandType) { when (historyEntry.commandType) {
OmnipodCommandType.CANCEL_TEMPORARY_BASAL -> OmnipodCommandType.CANCEL_TEMPORARY_BASAL,
OmnipodCommandType.SET_BASAL_PROFILE,
OmnipodCommandType.RESUME_DELIVERY ->
// We can't invalidate this command, // We can't invalidate this command,
// and this is why it is pumpSync-ed at this point // and this is why it is pumpSync-ed at this point
if (confirmation.success) { if (confirmation.success) {
@ -639,6 +650,12 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
} }
OmnipodCommandType.SUSPEND_DELIVERY -> {
if (!confirmation.success) {
pumpSync.invalidateTemporaryBasal(historyEntry.pumpId())
}
}
else -> else ->
aapsLogger.warn( aapsLogger.warn(
LTag.PUMP, LTag.PUMP,

View file

@ -0,0 +1,9 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition
import org.joda.time.Duration
class PodConstants {
companion object {
val MAX_POD_LIFETIME = Duration.standardHours(80)
}
}

View file

@ -73,7 +73,7 @@ interface OmnipodDashPodStateManager {
fun updateFromPairing(uniqueId: Id, pairResult: PairResult) fun updateFromPairing(uniqueId: Id, pairResult: PairResult)
fun reset() fun reset()
fun createActiveCommand(historyId: String): Single<ActiveCommand> fun createActiveCommand(historyId: String, basalProgram: BasalProgram?=null): Single<ActiveCommand>
fun updateActiveCommand(): Maybe<CommandConfirmed> fun updateActiveCommand(): Maybe<CommandConfirmed>
fun observeNoActiveCommand(): Observable<PodEvent> fun observeNoActiveCommand(): Observable<PodEvent>
fun getCommandConfirmationFromState(): CommandConfirmationFromState fun getCommandConfirmationFromState(): CommandConfirmationFromState
@ -84,6 +84,7 @@ interface OmnipodDashPodStateManager {
var sentRealtime: Long = 0, var sentRealtime: Long = 0,
val historyId: String, val historyId: String,
var sendError: Throwable?, var sendError: Throwable?,
var basalProgram: BasalProgram?
) )
// TODO: set created to "now" on boot // TODO: set created to "now" on boot
data class TempBasal(val startTime: Long, val rate: Double, val durationInMinutes: Short) : Serializable data class TempBasal(val startTime: Long, val rate: Double, val durationInMinutes: Short) : Serializable

View file

@ -188,7 +188,8 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
get() = podState.activeCommand get() = podState.activeCommand
@Synchronized @Synchronized
override fun createActiveCommand(historyId: String): Single<OmnipodDashPodStateManager.ActiveCommand> { override fun createActiveCommand(historyId: String, basalProgram: BasalProgram?):
Single<OmnipodDashPodStateManager.ActiveCommand> {
return Single.create { source -> return Single.create { source ->
if (activeCommand == null) { if (activeCommand == null) {
val command = OmnipodDashPodStateManager.ActiveCommand( val command = OmnipodDashPodStateManager.ActiveCommand(
@ -196,6 +197,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
createdRealtime = SystemClock.elapsedRealtime(), createdRealtime = SystemClock.elapsedRealtime(),
historyId = historyId, historyId = historyId,
sendError = null, sendError = null,
basalProgram = basalProgram,
) )
podState.activeCommand = command podState.activeCommand = command
source.onSuccess(command) source.onSuccess(command)
@ -261,7 +263,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
CommandConfirmationSuccess -> { CommandConfirmationSuccess -> {
podState.activeCommand = null podState.activeCommand = null
source.onSuccess(CommandConfirmed(activeCommand.historyId, true)) source.onSuccess(CommandConfirmed(activeCommand.historyId, true))
} }
NoActiveCommand -> { NoActiveCommand -> {