suspend/resume
This commit is contained in:
parent
d392caa6e9
commit
2afe220d28
5 changed files with 70 additions and 28 deletions
omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash
|
@ -16,6 +16,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.OmnipodDashMa
|
||||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEvent
|
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEvent
|
||||||
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.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
|
||||||
|
@ -36,7 +37,6 @@ import io.reactivex.Single
|
||||||
import io.reactivex.rxkotlin.blockingSubscribeBy
|
import io.reactivex.rxkotlin.blockingSubscribeBy
|
||||||
import io.reactivex.rxkotlin.subscribeBy
|
import io.reactivex.rxkotlin.subscribeBy
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.util.*
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@ -156,16 +156,41 @@ class OmnipodDashPumpPlugin @Inject constructor(
|
||||||
|
|
||||||
override fun setNewBasalProfile(profile: Profile): PumpEnactResult {
|
override fun setNewBasalProfile(profile: Profile): PumpEnactResult {
|
||||||
return executeSimpleProgrammingCommand(
|
return executeSimpleProgrammingCommand(
|
||||||
history.createRecord(
|
historyEntry = history.createRecord(
|
||||||
commandType = OmnipodCommandType.SET_BASAL_PROFILE
|
commandType = OmnipodCommandType.SET_BASAL_PROFILE
|
||||||
),
|
),
|
||||||
omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile)).ignoreElements()
|
command = omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile)).ignoreElements(),
|
||||||
|
pre = suspendDeliveryIfActive(),
|
||||||
).toPumpEnactResult()
|
).toPumpEnactResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun isThisProfileSet(profile: Profile): Boolean = podStateManager.basalProgram?.let {
|
private fun suspendDeliveryIfActive(): Completable = Completable.defer {
|
||||||
it == mapProfileToBasalProgram(profile)
|
if (podStateManager.deliveryStatus == DeliveryStatus.SUSPENDED)
|
||||||
} ?: true
|
Completable.complete()
|
||||||
|
else
|
||||||
|
executeSimpleProgrammingCommand(
|
||||||
|
history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY),
|
||||||
|
omnipodManager.suspendDelivery().ignoreElements()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun observeDeliverySuspended(): Completable = Completable.defer {
|
||||||
|
if (podStateManager.deliveryStatus == DeliveryStatus.SUSPENDED)
|
||||||
|
Completable.complete()
|
||||||
|
else {
|
||||||
|
Completable.error(java.lang.IllegalStateException("Expected suspended delivery"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isThisProfileSet(profile: Profile): Boolean {
|
||||||
|
if (!podStateManager.isActivationCompleted) {
|
||||||
|
// prevent setBasal requests
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// TODO: what do we have to answer here if delivery is suspended?
|
||||||
|
val running = pumpSync.expectedPumpState().profile
|
||||||
|
return running?.isEqual(profile) ?: false
|
||||||
|
}
|
||||||
|
|
||||||
override fun lastDataTime(): Long {
|
override fun lastDataTime(): Long {
|
||||||
return podStateManager.lastUpdatedSystem
|
return podStateManager.lastUpdatedSystem
|
||||||
|
@ -173,6 +198,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
|
||||||
|
|
||||||
override val baseBasalRate: Double
|
override val baseBasalRate: Double
|
||||||
get() = podStateManager.basalProgram?.rateAt(Date()) ?: 0.0
|
get() = podStateManager.basalProgram?.rateAt(Date()) ?: 0.0
|
||||||
|
//pumpSync.expectedPumpState().profile?.getBasal() ?: 0.0
|
||||||
|
|
||||||
override val reservoirLevel: Double
|
override val reservoirLevel: Double
|
||||||
get() {
|
get() {
|
||||||
|
@ -309,9 +335,10 @@ class OmnipodDashPumpPlugin @Inject constructor(
|
||||||
|
|
||||||
private fun observeNoActiveTempBasal(enforeNew: Boolean): Completable {
|
private fun observeNoActiveTempBasal(enforeNew: Boolean): Completable {
|
||||||
return Completable.defer {
|
return Completable.defer {
|
||||||
val expectedState = pumpSync.expectedPumpState()
|
|
||||||
when {
|
when {
|
||||||
expectedState.temporaryBasal == null -> {
|
podStateManager.deliveryStatus !in
|
||||||
|
arrayOf(DeliveryStatus.TEMP_BASAL_ACTIVE, DeliveryStatus.BOLUS_AND_TEMP_BASAL_ACTIVE) -> {
|
||||||
|
// TODO: what happens if we try to cancel inexistent temp basal?
|
||||||
aapsLogger.info(LTag.PUMP, "No temporary basal to cancel")
|
aapsLogger.info(LTag.PUMP, "No temporary basal to cancel")
|
||||||
Completable.complete()
|
Completable.complete()
|
||||||
}
|
}
|
||||||
|
@ -496,16 +523,25 @@ class OmnipodDashPumpPlugin @Inject constructor(
|
||||||
|
|
||||||
private fun suspendDelivery(): PumpEnactResult {
|
private fun suspendDelivery(): PumpEnactResult {
|
||||||
return executeSimpleProgrammingCommand(
|
return executeSimpleProgrammingCommand(
|
||||||
history.createRecord(OmnipodCommandType.RESUME_DELIVERY),
|
history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY),
|
||||||
omnipodManager.suspendDelivery().ignoreElements()
|
omnipodManager.suspendDelivery().ignoreElements(),
|
||||||
|
pre = observeDeliveryActive(),
|
||||||
).toPumpEnactResult()
|
).toPumpEnactResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun observeDeliveryActive(): Completable = Completable.defer {
|
||||||
|
if (podStateManager.deliveryStatus != DeliveryStatus.SUSPENDED)
|
||||||
|
Completable.complete()
|
||||||
|
else
|
||||||
|
Completable.error(java.lang.IllegalStateException("Expected active delivery"))
|
||||||
|
}
|
||||||
|
|
||||||
private fun resumeDelivery(): PumpEnactResult {
|
private fun resumeDelivery(): PumpEnactResult {
|
||||||
return profileFunction.getProfile()?.let {
|
return profileFunction.getProfile()?.let {
|
||||||
executeSimpleProgrammingCommand(
|
executeSimpleProgrammingCommand(
|
||||||
history.createRecord(OmnipodCommandType.RESUME_DELIVERY),
|
history.createRecord(OmnipodCommandType.RESUME_DELIVERY),
|
||||||
omnipodManager.setBasalProgram(mapProfileToBasalProgram(it)).ignoreElements()
|
omnipodManager.setBasalProgram(mapProfileToBasalProgram(it)).ignoreElements(),
|
||||||
|
pre = observeDeliverySuspended(),
|
||||||
).toPumpEnactResult()
|
).toPumpEnactResult()
|
||||||
} ?: PumpEnactResult(injector).success(false).enacted(false).comment("No profile active") // TODO i18n
|
} ?: PumpEnactResult(injector).success(false).enacted(false).comment("No profile active") // TODO i18n
|
||||||
}
|
}
|
||||||
|
@ -581,6 +617,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
|
||||||
|
|
||||||
private fun handleCommandConfirmation(confirmation: CommandConfirmed) {
|
private fun handleCommandConfirmation(confirmation: CommandConfirmed) {
|
||||||
val historyEntry = history.getById(confirmation.historyId)
|
val historyEntry = history.getById(confirmation.historyId)
|
||||||
|
aapsLogger.debug(LTag.PUMPCOMM, "handling confirmation command: $confirmation")
|
||||||
when (historyEntry.commandType) {
|
when (historyEntry.commandType) {
|
||||||
OmnipodCommandType.CANCEL_TEMPORARY_BASAL ->
|
OmnipodCommandType.CANCEL_TEMPORARY_BASAL ->
|
||||||
// We can't invalidate this command,
|
// We can't invalidate this command,
|
||||||
|
@ -593,11 +630,14 @@ class OmnipodDashPumpPlugin @Inject constructor(
|
||||||
serialNumber()
|
serialNumber()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
OmnipodCommandType.SET_TEMPORARY_BASAL ->
|
|
||||||
|
OmnipodCommandType.SET_TEMPORARY_BASAL -> {
|
||||||
// This treatment was synced before sending the command
|
// This treatment was synced before sending the command
|
||||||
|
aapsLogger.info(LTag.PUMPCOMM, "temporary basal denied. PumpId: ${historyEntry.pumpId()}")
|
||||||
if (!confirmation.success) {
|
if (!confirmation.success) {
|
||||||
pumpSync.invalidateTemporaryBasal(historyEntry.pumpId())
|
pumpSync.invalidateTemporaryBasal(historyEntry.pumpId())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
else ->
|
else ->
|
||||||
aapsLogger.warn(
|
aapsLogger.warn(
|
||||||
|
|
|
@ -172,6 +172,7 @@ class OmnipodDashManagerImpl @Inject constructor(
|
||||||
DefaultStatusResponse::class
|
DefaultStatusResponse::class
|
||||||
)
|
)
|
||||||
}.doOnComplete {
|
}.doOnComplete {
|
||||||
|
// TODO: remove podStateManager.basalProgram?
|
||||||
podStateManager.basalProgram = basalProgram
|
podStateManager.basalProgram = basalProgram
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state
|
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state
|
||||||
|
|
||||||
class CommandConfirmed(val historyId: String, val success: Boolean)
|
data class CommandConfirmed(val historyId: String, val success: Boolean)
|
||||||
|
|
|
@ -53,7 +53,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
|
||||||
|
|
||||||
override val isSuspended: Boolean
|
override val isSuspended: Boolean
|
||||||
get() = podState.deliveryStatus?.equals(DeliveryStatus.SUSPENDED)
|
get() = podState.deliveryStatus?.equals(DeliveryStatus.SUSPENDED)
|
||||||
?: true
|
?: false
|
||||||
|
|
||||||
override val isPodRunning: Boolean
|
override val isPodRunning: Boolean
|
||||||
get() = podState.podStatus?.isRunning() ?: false
|
get() = podState.podStatus?.isRunning() ?: false
|
||||||
|
@ -141,7 +141,11 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
|
||||||
get() = podState.tempBasal
|
get() = podState.tempBasal
|
||||||
|
|
||||||
override val tempBasalActive: Boolean
|
override val tempBasalActive: Boolean
|
||||||
get() = tempBasal != null && tempBasal!!.startTime + tempBasal!!.durationInMinutes * 60 * 1000 > System.currentTimeMillis()
|
get() = podState.deliveryStatus in
|
||||||
|
arrayOf(
|
||||||
|
DeliveryStatus.TEMP_BASAL_ACTIVE,
|
||||||
|
DeliveryStatus.BOLUS_AND_TEMP_BASAL_ACTIVE
|
||||||
|
)
|
||||||
|
|
||||||
override var basalProgram: BasalProgram?
|
override var basalProgram: BasalProgram?
|
||||||
get() = podState.basalProgram
|
get() = podState.basalProgram
|
||||||
|
@ -230,8 +234,9 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
|
||||||
source.onComplete()
|
source.onComplete()
|
||||||
return@create
|
return@create
|
||||||
}
|
}
|
||||||
|
val cmdConfirmation = getCommandConfirmationFromState()
|
||||||
when (getCommandConfirmationFromState()) {
|
logger.info(LTag.PUMPCOMM, "Update active command with confirmation: $cmdConfirmation")
|
||||||
|
when (cmdConfirmation) {
|
||||||
CommandSendingFailure -> {
|
CommandSendingFailure -> {
|
||||||
podState.activeCommand = null
|
podState.activeCommand = null
|
||||||
source.onError(
|
source.onError(
|
||||||
|
@ -255,7 +260,8 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
|
||||||
|
|
||||||
CommandConfirmationSuccess -> {
|
CommandConfirmationSuccess -> {
|
||||||
podState.activeCommand = null
|
podState.activeCommand = null
|
||||||
source.onSuccess(CommandConfirmed(activeCommand.historyId, false))
|
|
||||||
|
source.onSuccess(CommandConfirmed(activeCommand.historyId, false)) // TODO: remove test
|
||||||
}
|
}
|
||||||
|
|
||||||
NoActiveCommand -> {
|
NoActiveCommand -> {
|
||||||
|
|
|
@ -298,15 +298,8 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// base basal rate
|
// base basal rate
|
||||||
podInfoBinding.baseBasalRate.text = if (podStateManager.basalProgram != null) {
|
// TODO: check if delivery is suspended
|
||||||
resourceHelper.gs(
|
podInfoBinding.baseBasalRate.text = PLACEHOLDER
|
||||||
R.string.pump_basebasalrate,
|
|
||||||
omnipodDashPumpPlugin.model()
|
|
||||||
.determineCorrectBasalSize(podStateManager.basalProgram!!.rateAt(Date()))
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
PLACEHOLDER
|
|
||||||
}
|
|
||||||
|
|
||||||
// total delivered
|
// total delivered
|
||||||
podInfoBinding.totalDelivered.text =
|
podInfoBinding.totalDelivered.text =
|
||||||
|
@ -518,7 +511,9 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
|
||||||
|
|
||||||
private fun updateSuspendDeliveryButton() {
|
private fun updateSuspendDeliveryButton() {
|
||||||
// If the Pod is currently suspended, we show the Resume delivery button instead.
|
// If the Pod is currently suspended, we show the Resume delivery button instead.
|
||||||
if (isSuspendDeliveryButtonEnabled() &&
|
// TODO: isSuspendDeliveryButtonEnabled doesn't work
|
||||||
|
val isSuspendDeliveryButtonEnabled = true
|
||||||
|
if (isSuspendDeliveryButtonEnabled &&
|
||||||
podStateManager.isPodRunning &&
|
podStateManager.isPodRunning &&
|
||||||
(!podStateManager.isSuspended || commandQueue.isCustomCommandInQueue(CommandSuspendDelivery::class.java))
|
(!podStateManager.isSuspended || commandQueue.isCustomCommandInQueue(CommandSuspendDelivery::class.java))
|
||||||
) {
|
) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue