bolus: send progress

This commit is contained in:
Andrei Vereha 2021-06-06 12:24:32 +02:00
parent 79b4a5d656
commit b80327c9bf

View file

@ -12,6 +12,7 @@ import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.common.ManufacturerType import info.nightscout.androidaps.plugins.common.ManufacturerType
import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction
import info.nightscout.androidaps.plugins.general.actions.defs.CustomActionType import info.nightscout.androidaps.plugins.general.actions.defs.CustomActionType
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType import info.nightscout.androidaps.plugins.pump.common.defs.PumpType
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType
import info.nightscout.androidaps.plugins.pump.omnipod.common.queue.command.* import info.nightscout.androidaps.plugins.pump.omnipod.common.queue.command.*
@ -39,12 +40,13 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.Completable import io.reactivex.Completable
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single import io.reactivex.Single
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.subscribeBy import io.reactivex.rxkotlin.subscribeBy
import org.json.JSONObject import org.json.JSONObject
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.math.ceil
@Singleton @Singleton
class OmnipodDashPumpPlugin @Inject constructor( class OmnipodDashPumpPlugin @Inject constructor(
@ -240,59 +242,103 @@ class OmnipodDashPumpPlugin @Inject constructor(
get() = 0 get() = 0
override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult {
// TODO update Treatments (?)
// TODO bolus progress
// TODO report actual delivered amount after Pod Alarm and bolus cancellation // TODO report actual delivered amount after Pod Alarm and bolus cancellation
val bolusBeeps = sp.getBoolean(R.string.key_omnipod_common_bolus_beeps_enabled, false)
return Single.create<PumpEnactResult> { source -> if (detailedBolusInfo.carbs > 0 || detailedBolusInfo.insulin == 0.0) {
val bolusBeeps = sp.getBoolean(R.string.key_omnipod_common_bolus_beeps_enabled, false) return PumpEnactResult(injector)
.success(false)
.enacted(false)
.bolusDelivered(0.0)
.carbsDelivered(0.0)
.comment("Invalid input");
}
val requestedBolusAmount = detailedBolusInfo.insulin
var delieveredBolusAmount = 0.0
Observable.concat( aapsLogger.info(
history.createRecord( LTag.PUMP,
commandType = OmnipodCommandType.SET_BOLUS, "deliverTreatment: units: ${requestedBolusAmount}"
bolusRecord = BolusRecord( )
detailedBolusInfo.insulin, return executeProgrammingCommand(
BolusType.fromBolusInfoBolusType(detailedBolusInfo.bolusType), pre = observeNoActiveTempBasal(true),
), historyEntry = history.createRecord(
).flatMapObservable { recordId -> commandType = OmnipodCommandType.SET_BOLUS,
podStateManager.createActiveCommand(recordId).toObservable() bolusRecord = BolusRecord(
}, requestedBolusAmount,
omnipodManager.bolus( BolusType.fromBolusInfoBolusType(detailedBolusInfo.bolusType)
detailedBolusInfo.insulin, )
bolusBeeps, ),
bolusBeeps command = omnipodManager.bolus(
), detailedBolusInfo.insulin,
history.updateFromState(podStateManager).toObservable(), bolusBeeps,
podStateManager.updateActiveCommand().toObservable(), bolusBeeps
).subscribeBy( ).filter { podEvent -> podEvent is PodEvent.CommandSent }
onNext = { podEvent -> .map { pumpSyncBolusStart(it, requestedBolusAmount, detailedBolusInfo.bolusType) }
aapsLogger.debug( .ignoreElements(),
LTag.PUMP, post = waitForBolusDeliveryToComplete(5, requestedBolusAmount, detailedBolusInfo.bolusType)
"Received PodEvent in deliverTreatment: $podEvent" ).toSingleDefault(PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(delieveredBolusAmount))
) .onErrorReturnItem(PumpEnactResult(injector).success(false).enacted(false))
}, .blockingGet()
onError = { throwable -> }
aapsLogger.error(LTag.PUMP, "Error in deliverTreatment", throwable)
source.onSuccess( private fun waitForBolusDeliveryToComplete(
PumpEnactResult(injector).success(false).enacted(false).comment( maxRetriesAtTheEnd: Int,
throwable.toString() requestedBolusAmount: Double,
) bolusType: DetailedBolusInfo.BolusType
) ): Completable {
}, // TODO: wait for bolus delivery to start!
onComplete = { // For now, we assume it started with success
aapsLogger.debug("deliverTreatment completed")
source.onSuccess( val estimatedDeliveryTimeSeconds = ceil(requestedBolusAmount / 0.05).toLong() * 2
PumpEnactResult(injector).success(true).enacted(true) aapsLogger.info(LTag.PUMP, "estimatedDeliveryTimeSeconds: $estimatedDeliveryTimeSeconds")
.bolusDelivered(detailedBolusInfo.insulin) return Completable.concat(listOf(
.carbsDelivered(detailedBolusInfo.carbs) Observable.interval(1, TimeUnit.SECONDS)
) .take(estimatedDeliveryTimeSeconds)
} .doOnNext {
if (bolusType == DetailedBolusInfo.BolusType.SMB) {
return@doOnNext
}
val progressUpdateEvent = EventOverviewBolusProgress
val percent = (100 * it) / estimatedDeliveryTimeSeconds
progressUpdateEvent.status = resourceHelper.gs(R.string.bolusdelivering, requestedBolusAmount)
progressUpdateEvent.percent = percent.toInt()
rxBus.send(progressUpdateEvent)
}.ignoreElements(),
Observable.interval(5, TimeUnit.SECONDS).ignoreElements()
// TODO check delivery status. for now, we are just sleeping for 5 sec
))
}
private fun pumpSyncBolusStart(
podEvent: PodEvent,
requestedBolusAmount: Double,
bolusType: DetailedBolusInfo.BolusType
): Boolean {
val activeCommand = podStateManager.activeCommand
if (activeCommand == null || podEvent !is PodEvent.CommandSent) {
throw IllegalArgumentException(
"No active command or illegal podEvent: " +
"activeCommand=$activeCommand" +
"podEvent=$podEvent"
) )
}.blockingGet() }
val historyEntry = history.getById(activeCommand.historyId)
val ret = pumpSync.syncBolusWithPumpId(
timestamp = historyEntry.createdAt,
amount = requestedBolusAmount,
type = bolusType,
pumpId = historyEntry.pumpId(),
pumpType = PumpType.OMNIPOD_DASH,
pumpSerial = serialNumber()
)
aapsLogger.debug(LTag.PUMP, "pumpSyncBolusStart: $ret")
return ret
} }
override fun stopBolusDelivering() { override fun stopBolusDelivering() {
// TODO update Treatments (?) // TODO update Treatments (?)
aapsLogger.info(LTag.PUMP, "stopBolusDelivering called")
executeProgrammingCommand( executeProgrammingCommand(
historyEntry = history.createRecord(OmnipodCommandType.CANCEL_BOLUS), historyEntry = history.createRecord(OmnipodCommandType.CANCEL_BOLUS),
command = omnipodManager.stopBolus().ignoreElements() command = omnipodManager.stopBolus().ignoreElements()
@ -312,7 +358,9 @@ class OmnipodDashPumpPlugin @Inject constructor(
"setTempBasalAbsolute: $durationInMinutes min :: $absoluteRate U/h :: " + "setTempBasalAbsolute: $durationInMinutes min :: $absoluteRate U/h :: " +
"enforce: $enforceNew :: tbrType: $tbrType" "enforce: $enforceNew :: tbrType: $tbrType"
) )
return executeProgrammingCommand( return executeProgrammingCommand(
pre = observeNoActiveTempBasal(true),
historyEntry = history.createRecord( historyEntry = history.createRecord(
commandType = OmnipodCommandType.SET_TEMPORARY_BASAL, commandType = OmnipodCommandType.SET_TEMPORARY_BASAL,
tempBasalRecord = TempBasalRecord(duration = durationInMinutes, rate = absoluteRate) tempBasalRecord = TempBasalRecord(duration = durationInMinutes, rate = absoluteRate)
@ -335,7 +383,6 @@ class OmnipodDashPumpPlugin @Inject constructor(
.filter { podEvent -> podEvent is PodEvent.CommandSent } .filter { podEvent -> podEvent is PodEvent.CommandSent }
.map { pumpSyncTempBasal(it, absoluteRate, durationInMinutes.toLong(), tbrType) } .map { pumpSyncTempBasal(it, absoluteRate, durationInMinutes.toLong(), tbrType) }
.ignoreElements(), .ignoreElements(),
pre = observeNoActiveTempBasal(true),
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -365,7 +412,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
pumpType = PumpType.OMNIPOD_DASH, pumpType = PumpType.OMNIPOD_DASH,
pumpSerial = serialNumber() pumpSerial = serialNumber()
) )
aapsLogger.debug(LTag.PUMP, "Pump sync temp basal: $ret") aapsLogger.debug(LTag.PUMP, "pumpSyncTempBasal: $ret")
return ret return ret
} }
@ -722,6 +769,19 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
} }
OmnipodCommandType.SET_BOLUS -> {
if (!confirmation.success) {
pumpSync.syncBolusWithPumpId(
timestamp = historyEntry.createdAt,
amount = 0.0,
pumpId = historyEntry.pumpId(),
pumpType = PumpType.OMNIPOD_DASH,
pumpSerial = serialNumber(),
type = null // TODO: set the correct bolus type here!!!
)
}
}
else -> else ->
aapsLogger.warn( aapsLogger.warn(
LTag.PUMP, LTag.PUMP,