stop bolus

This commit is contained in:
Andrei Vereha 2021-06-06 13:43:44 +02:00
parent 4f459b9fc1
commit a25fffd075

View file

@ -36,11 +36,11 @@ import info.nightscout.androidaps.queue.commands.CustomCommand
import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.TimeChangeType import info.nightscout.androidaps.utils.TimeChangeType
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.sharedPreferences.SP 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.subscribeBy
import org.json.JSONObject import org.json.JSONObject
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -57,6 +57,8 @@ class OmnipodDashPumpPlugin @Inject constructor(
private val history: DashHistory, private val history: DashHistory,
private val pumpSync: PumpSync, private val pumpSync: PumpSync,
private val rxBus: RxBusWrapper, private val rxBus: RxBusWrapper,
private val aapsSchedulers: AapsSchedulers,
// private val disposable: CompositeDisposable = CompositeDisposable(), // private val disposable: CompositeDisposable = CompositeDisposable(),
// private val aapsSchedulers: AapsSchedulers, // private val aapsSchedulers: AapsSchedulers,
@ -186,12 +188,12 @@ class OmnipodDashPumpPlugin @Inject constructor(
) )
} }
/* override fun onStop() { /* override fun onStop() {
super.onStop() super.onStop()
disposable.clear() disposable.clear()
} }
*/ */
private fun observeDeliverySuspended(): Completable = Completable.defer { private fun observeDeliverySuspended(): Completable = Completable.defer {
if (podStateManager.deliveryStatus == DeliveryStatus.SUSPENDED) if (podStateManager.deliveryStatus == DeliveryStatus.SUSPENDED)
@ -251,14 +253,14 @@ class OmnipodDashPumpPlugin @Inject constructor(
.enacted(false) .enacted(false)
.bolusDelivered(0.0) .bolusDelivered(0.0)
.carbsDelivered(0.0) .carbsDelivered(0.0)
.comment("Invalid input"); .comment("Invalid input")
} }
val requestedBolusAmount = detailedBolusInfo.insulin val requestedBolusAmount = detailedBolusInfo.insulin
var delieveredBolusAmount = 0.0 var delieveredBolusAmount = 0.0
aapsLogger.info( aapsLogger.info(
LTag.PUMP, LTag.PUMP,
"deliverTreatment: units: ${requestedBolusAmount}" "deliverTreatment: units: $requestedBolusAmount"
) )
return executeProgrammingCommand( return executeProgrammingCommand(
pre = observeNoActiveTempBasal(true), pre = observeNoActiveTempBasal(true),
@ -292,22 +294,24 @@ class OmnipodDashPumpPlugin @Inject constructor(
val estimatedDeliveryTimeSeconds = ceil(requestedBolusAmount / 0.05).toLong() * 2 val estimatedDeliveryTimeSeconds = ceil(requestedBolusAmount / 0.05).toLong() * 2
aapsLogger.info(LTag.PUMP, "estimatedDeliveryTimeSeconds: $estimatedDeliveryTimeSeconds") aapsLogger.info(LTag.PUMP, "estimatedDeliveryTimeSeconds: $estimatedDeliveryTimeSeconds")
return Completable.concat(listOf( return Completable.concat(
Observable.interval(1, TimeUnit.SECONDS) listOf(
.take(estimatedDeliveryTimeSeconds) Observable.interval(1, TimeUnit.SECONDS)
.doOnNext { .take(estimatedDeliveryTimeSeconds)
if (bolusType == DetailedBolusInfo.BolusType.SMB) { .doOnNext {
return@doOnNext if (bolusType == DetailedBolusInfo.BolusType.SMB) {
} return@doOnNext
val progressUpdateEvent = EventOverviewBolusProgress }
val percent = (100 * it) / estimatedDeliveryTimeSeconds val progressUpdateEvent = EventOverviewBolusProgress
progressUpdateEvent.status = resourceHelper.gs(R.string.bolusdelivering, requestedBolusAmount) val percent = (100 * it) / estimatedDeliveryTimeSeconds
progressUpdateEvent.percent = percent.toInt() progressUpdateEvent.status = resourceHelper.gs(R.string.bolusdelivering, requestedBolusAmount)
rxBus.send(progressUpdateEvent) progressUpdateEvent.percent = percent.toInt()
}.ignoreElements(), rxBus.send(progressUpdateEvent)
Observable.interval(5, TimeUnit.SECONDS).ignoreElements() }.ignoreElements(),
// TODO check delivery status. for now, we are just sleeping for 5 sec Observable.interval(5, TimeUnit.SECONDS).take(1).ignoreElements()
)) // TODO check delivery status. for now, we are just sleeping for 5 sec
)
)
} }
private fun pumpSyncBolusStart( private fun pumpSyncBolusStart(
@ -342,7 +346,8 @@ class OmnipodDashPumpPlugin @Inject constructor(
val ret = executeProgrammingCommand( val ret = executeProgrammingCommand(
historyEntry = history.createRecord(OmnipodCommandType.CANCEL_BOLUS), historyEntry = history.createRecord(OmnipodCommandType.CANCEL_BOLUS),
command = omnipodManager.stopBolus().ignoreElements() command = omnipodManager.stopBolus().ignoreElements()
).toPumpEnactResult() ).subscribeOn(aapsSchedulers.io) // stopBolusDelivering is executed on the main thread
.toPumpEnactResult()
aapsLogger.info(LTag.PUMP, "stopBolusDelivering finished with result: $ret") aapsLogger.info(LTag.PUMP, "stopBolusDelivering finished with result: $ret")
} }