From fc919f24cdb8d16cfc930bd0e622d86e466fad70 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sat, 5 Jun 2021 18:14:13 +0200 Subject: [PATCH 1/8] save temp basal to pod state --- .../omnipod/dash/OmnipodDashPumpPlugin.kt | 43 ++++++++++++------- .../pod/state/OmnipodDashPodStateManager.kt | 8 ++-- .../state/OmnipodDashPodStateManagerImpl.kt | 21 ++++++--- .../dash/ui/OmnipodDashOverviewFragment.kt | 8 +++- 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index d9d4bf7845..587355a3ad 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -4,6 +4,7 @@ import dagger.android.HasAndroidInjector import info.nightscout.androidaps.data.DetailedBolusInfo import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.events.EventProfileSwitchChanged +import info.nightscout.androidaps.events.EventTempBasalChange import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag @@ -39,7 +40,6 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP import io.reactivex.Completable import io.reactivex.Observable import io.reactivex.Single -import io.reactivex.rxkotlin.blockingSubscribeBy import io.reactivex.rxkotlin.subscribeBy import org.json.JSONObject import java.util.* @@ -140,7 +140,7 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun setNewBasalProfile(profile: Profile): PumpEnactResult { val basalProgram = mapProfileToBasalProgram(profile) - return executeSimpleProgrammingCommand( + return executeProgrammingCommand( pre = suspendDeliveryIfActive(), historyEntry = history.createRecord(commandType = OmnipodCommandType.SET_BASAL_PROFILE), command = omnipodManager.setBasalProgram(basalProgram).ignoreElements(), @@ -150,6 +150,7 @@ class OmnipodDashPumpPlugin @Inject constructor( } private fun failWhenUnconfirmed(): Completable = Completable.defer{ + rxBus.send(EventTempBasalChange()) if (podStateManager.activeCommand != null) { Completable.error(java.lang.IllegalStateException("Command not confirmed")) }else { @@ -161,7 +162,7 @@ class OmnipodDashPumpPlugin @Inject constructor( if (podStateManager.deliveryStatus == DeliveryStatus.SUSPENDED) Completable.complete() else - executeSimpleProgrammingCommand( + executeProgrammingCommand( history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY), omnipodManager.suspendDelivery() .filter { podEvent -> podEvent is PodEvent.CommandSent } @@ -172,6 +173,7 @@ class OmnipodDashPumpPlugin @Inject constructor( PodConstants.MAX_POD_LIFETIME.standardMinutes, PumpSync.TemporaryBasalType.PUMP_SUSPEND ) + rxBus.send(EventTempBasalChange()) } .ignoreElements(), ) @@ -274,7 +276,7 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun stopBolusDelivering() { // TODO update Treatments (?) - executeSimpleProgrammingCommand( + executeProgrammingCommand( history.createRecord(OmnipodCommandType.CANCEL_BOLUS), omnipodManager.stopBolus().ignoreElements() ).toPumpEnactResult() @@ -288,8 +290,9 @@ class OmnipodDashPumpPlugin @Inject constructor( tbrType: PumpSync.TemporaryBasalType ): PumpEnactResult { val tempBasalBeeps = sp.getBoolean(R.string.key_omnipod_common_tbr_beeps_enabled, false) - - return executeSimpleProgrammingCommand( + aapsLogger.info(LTag.PUMP, "setTempBasalAbsolute: $durationInMinutes min :: $absoluteRate U/h :: " + + "enforce: $enforceNew :: tbrType: $tbrType") + return executeProgrammingCommand( historyEntry = history.createRecord( commandType = OmnipodCommandType.SET_TEMPORARY_BASAL, tempBasalRecord = TempBasalRecord(duration = durationInMinutes, rate = absoluteRate) @@ -302,7 +305,12 @@ class OmnipodDashPumpPlugin @Inject constructor( .filter { podEvent -> podEvent is PodEvent.CommandSent } .map { pumpSyncTempBasal(it, absoluteRate, durationInMinutes.toLong(), tbrType) } .ignoreElements(), - pre = observeNoActiveTempBasal(enforceNew) + pre = observeNoActiveTempBasal(enforceNew), + tempBasal = OmnipodDashPodStateManager.TempBasal( + startTime = System.currentTimeMillis(), + rate=absoluteRate, + durationInMinutes = durationInMinutes.toShort(), + ) ).toPumpEnactResult() } @@ -356,7 +364,7 @@ class OmnipodDashPumpPlugin @Inject constructor( else -> { // enforceNew == true aapsLogger.info(LTag.PUMP, "Canceling existing temp basal") - executeSimpleProgrammingCommand( + executeProgrammingCommand( history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL), omnipodManager.stopTempBasal().ignoreElements() ) @@ -398,7 +406,7 @@ class OmnipodDashPumpPlugin @Inject constructor( } override fun cancelTempBasal(enforceNew: Boolean): PumpEnactResult { - return executeSimpleProgrammingCommand( + return executeProgrammingCommand( historyEntry = history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL), command = omnipodManager.stopTempBasal().ignoreElements(), pre = observeActiveTempBasal(), @@ -526,7 +534,7 @@ class OmnipodDashPumpPlugin @Inject constructor( } private fun suspendDelivery(): PumpEnactResult { - return executeSimpleProgrammingCommand( + return executeProgrammingCommand( historyEntry = history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY), command = omnipodManager.suspendDelivery() .filter { podEvent -> podEvent is PodEvent.CommandSent } @@ -552,7 +560,7 @@ class OmnipodDashPumpPlugin @Inject constructor( private fun resumeDelivery(): PumpEnactResult { return profileFunction.getProfile()?.let { - executeSimpleProgrammingCommand( + executeProgrammingCommand( history.createRecord(OmnipodCommandType.RESUME_DELIVERY), omnipodManager.setBasalProgram(mapProfileToBasalProgram(it)).ignoreElements(), pre = observeDeliverySuspended(), @@ -561,7 +569,7 @@ class OmnipodDashPumpPlugin @Inject constructor( } private fun deactivatePod(): PumpEnactResult { - return executeSimpleProgrammingCommand( + return executeProgrammingCommand( history.createRecord(OmnipodCommandType.DEACTIVATE_POD), omnipodManager.deactivatePod().ignoreElements() ).toPumpEnactResult() @@ -578,7 +586,7 @@ class OmnipodDashPumpPlugin @Inject constructor( } private fun playTestBeep(): PumpEnactResult { - return executeSimpleProgrammingCommand( + return executeProgrammingCommand( history.createRecord(OmnipodCommandType.PLAY_TEST_BEEP), omnipodManager.playBeep(BeepType.LONG_SINGLE_BEEP).ignoreElements() ).toPumpEnactResult() @@ -605,19 +613,20 @@ class OmnipodDashPumpPlugin @Inject constructor( commandQueue.customCommand(CommandHandleTimeChange(false), null) } - private fun executeSimpleProgrammingCommand( + private fun executeProgrammingCommand( historyEntry: Single, command: Completable, pre: Completable = Completable.complete(), - basalProgram: BasalProgram? = null, post: Completable = Completable.complete(), + basalProgram: BasalProgram? = null, + tempBasal: OmnipodDashPodStateManager.TempBasal? = null, ): Completable { return Completable.concat( listOf( pre, podStateManager.observeNoActiveCommand().ignoreElements(), historyEntry - .flatMap { podStateManager.createActiveCommand(it, basalProgram) } + .flatMap { podStateManager.createActiveCommand(it, basalProgram, tempBasal) } .ignoreElement(), command.doOnError { podStateManager.activeCommand?.sendError = it @@ -674,6 +683,8 @@ class OmnipodDashPumpPlugin @Inject constructor( aapsLogger.info(LTag.PUMPCOMM, "temporary basal denied. PumpId: ${historyEntry.pumpId()}") if (!confirmation.success) { pumpSync.invalidateTemporaryBasal(historyEntry.pumpId()) + } else { + podStateManager.tempBasal = command.tempBasal } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index 2b9dc3dc7e..d7f0ff4a6d 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -58,7 +58,7 @@ interface OmnipodDashPodStateManager { val minutesSinceActivation: Short? val activeAlerts: EnumSet? - val tempBasal: TempBasal? + var tempBasal: TempBasal? val tempBasalActive: Boolean var basalProgram: BasalProgram? val activeCommand: ActiveCommand? @@ -73,7 +73,8 @@ interface OmnipodDashPodStateManager { fun updateFromPairing(uniqueId: Id, pairResult: PairResult) fun reset() - fun createActiveCommand(historyId: String, basalProgram: BasalProgram? = null): Single + fun createActiveCommand(historyId: String, basalProgram: BasalProgram? = null, tempBasal: TempBasal?=null): + Single fun updateActiveCommand(): Maybe fun observeNoActiveCommand(): Observable fun getCommandConfirmationFromState(): CommandConfirmationFromState @@ -84,7 +85,8 @@ interface OmnipodDashPodStateManager { var sentRealtime: Long = 0, val historyId: String, var sendError: Throwable?, - var basalProgram: BasalProgram? + var basalProgram: BasalProgram?, + val tempBasal: TempBasal? ) // TODO: set created to "now" on boot data class TempBasal(val startTime: Long, val rate: Double, val durationInMinutes: Short) : Serializable diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index 104f4f1cfc..d1c32e9dc9 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -137,15 +137,17 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( override val activeAlerts: EnumSet? get() = podState.activeAlerts - override val tempBasal: OmnipodDashPodStateManager.TempBasal? + override var tempBasal: OmnipodDashPodStateManager.TempBasal? get() = podState.tempBasal + set(tempBasal) { + podState.tempBasal = tempBasal + store() + } override val tempBasalActive: Boolean - get() = podState.deliveryStatus in - arrayOf( - DeliveryStatus.TEMP_BASAL_ACTIVE, - DeliveryStatus.BOLUS_AND_TEMP_BASAL_ACTIVE - ) + get() = tempBasal?.let { + it.startTime + it.durationInMinutes *60 * 1000 > System.currentTimeMillis() + } ?: false override var basalProgram: BasalProgram? get() = podState.basalProgram @@ -188,7 +190,11 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( get() = podState.activeCommand @Synchronized - override fun createActiveCommand(historyId: String, basalProgram: BasalProgram?): + override fun createActiveCommand( + historyId: String, + basalProgram: BasalProgram?, + tempBasal: OmnipodDashPodStateManager.TempBasal? + ): Single { return Single.create { source -> if (activeCommand == null) { @@ -198,6 +204,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( historyId = historyId, sendError = null, basalProgram = basalProgram, + tempBasal = tempBasal, ) podState.activeCommand = command source.onSuccess(command) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 16963b3da8..671cf34d01 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -42,6 +42,7 @@ import info.nightscout.androidaps.utils.ui.UIRunnable import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.plusAssign import org.apache.commons.lang3.StringUtils +import org.joda.time.DateTime import org.joda.time.Duration import java.util.* import javax.inject.Inject @@ -343,7 +344,9 @@ class OmnipodDashOverviewFragment : DaggerFragment() { ) } - podInfoBinding.podActiveAlerts.text = PLACEHOLDER + podInfoBinding.podActiveAlerts.text = podStateManager.activeAlerts?.let { + it.map { it.toString() }.joinToString(",") + } ?: PLACEHOLDER } if (errors.size == 0) { @@ -503,7 +506,8 @@ class OmnipodDashOverviewFragment : DaggerFragment() { } private fun updateSilenceAlertsButton() { - if (isAutomaticallySilenceAlertsEnabled() && podStateManager.isPodRunning && + if (!isAutomaticallySilenceAlertsEnabled() && + podStateManager.isPodRunning && ( podStateManager.activeAlerts!!.size > 0 || commandQueue.isCustomCommandInQueue(CommandSilenceAlerts::class.java) From d0c947c988fbc9204dba86598873357a00dd9acb Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sat, 5 Jun 2021 20:32:35 +0200 Subject: [PATCH 2/8] updates for display of temp basal --- .../omnipod/dash/OmnipodDashPumpPlugin.kt | 34 ++++++++++++++++--- .../state/OmnipodDashPodStateManagerImpl.kt | 4 ++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 587355a3ad..68405114d7 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash import dagger.android.HasAndroidInjector import info.nightscout.androidaps.data.DetailedBolusInfo import info.nightscout.androidaps.data.PumpEnactResult +import info.nightscout.androidaps.events.EventPreferenceChange import info.nightscout.androidaps.events.EventProfileSwitchChanged import info.nightscout.androidaps.events.EventTempBasalChange import info.nightscout.androidaps.interfaces.* @@ -36,10 +37,14 @@ import info.nightscout.androidaps.queue.commands.CustomCommand import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.TimeChangeType import info.nightscout.androidaps.utils.resources.ResourceHelper +import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.sharedPreferences.SP import io.reactivex.Completable import io.reactivex.Observable import io.reactivex.Single +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.functions.Consumer +import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.subscribeBy import org.json.JSONObject import java.util.* @@ -55,6 +60,8 @@ class OmnipodDashPumpPlugin @Inject constructor( private val history: DashHistory, private val pumpSync: PumpSync, private val rxBus: RxBusWrapper, +// private val disposable: CompositeDisposable = CompositeDisposable(), + // private val aapsSchedulers: AapsSchedulers, injector: HasAndroidInjector, aapsLogger: AAPSLogger, @@ -175,10 +182,17 @@ class OmnipodDashPumpPlugin @Inject constructor( ) rxBus.send(EventTempBasalChange()) } - .ignoreElements(), + .ignoreElements() ) } + /* override fun onStop() { + super.onStop() + disposable.clear() + } + + */ + private fun observeDeliverySuspended(): Completable = Completable.defer { if (podStateManager.deliveryStatus == DeliveryStatus.SUSPENDED) Completable.complete() @@ -204,7 +218,12 @@ class OmnipodDashPumpPlugin @Inject constructor( } override val baseBasalRate: Double - get() = podStateManager.basalProgram?.rateAt(Date()) ?: 0.0 + get() { + val date = Date() + val ret = podStateManager.basalProgram?.rateAt(date) ?: 0.0 + aapsLogger.info(LTag.PUMP, "baseBasalRate: %ret at $date}") + return ret + } override val reservoirLevel: Double get() { @@ -290,8 +309,10 @@ class OmnipodDashPumpPlugin @Inject constructor( tbrType: PumpSync.TemporaryBasalType ): PumpEnactResult { val tempBasalBeeps = sp.getBoolean(R.string.key_omnipod_common_tbr_beeps_enabled, false) - aapsLogger.info(LTag.PUMP, "setTempBasalAbsolute: $durationInMinutes min :: $absoluteRate U/h :: " + - "enforce: $enforceNew :: tbrType: $tbrType") + aapsLogger.info( + LTag.PUMP, "setTempBasalAbsolute: $durationInMinutes min :: $absoluteRate U/h :: " + + "enforce: $enforceNew :: tbrType: $tbrType" + ) return executeProgrammingCommand( historyEntry = history.createRecord( commandType = OmnipodCommandType.SET_TEMPORARY_BASAL, @@ -308,7 +329,7 @@ class OmnipodDashPumpPlugin @Inject constructor( pre = observeNoActiveTempBasal(enforceNew), tempBasal = OmnipodDashPodStateManager.TempBasal( startTime = System.currentTimeMillis(), - rate=absoluteRate, + rate = absoluteRate, durationInMinutes = durationInMinutes.toShort(), ) ).toPumpEnactResult() @@ -657,6 +678,7 @@ class OmnipodDashPumpPlugin @Inject constructor( PumpType.OMNIPOD_DASH, serialNumber() ) + podStateManager.tempBasal = null } OmnipodCommandType.SET_BASAL_PROFILE -> { @@ -691,6 +713,8 @@ class OmnipodDashPumpPlugin @Inject constructor( OmnipodCommandType.SUSPEND_DELIVERY -> { if (!confirmation.success) { pumpSync.invalidateTemporaryBasal(historyEntry.pumpId()) + } else { + podStateManager.tempBasal = null } } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index d1c32e9dc9..fc79643af7 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -141,11 +141,12 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( get() = podState.tempBasal set(tempBasal) { podState.tempBasal = tempBasal + rxBus.send(EventOmnipodDashPumpValuesChanged()) store() } override val tempBasalActive: Boolean - get() = tempBasal?.let { + get() = !isSuspended && tempBasal?.let { it.startTime + it.durationInMinutes *60 * 1000 > System.currentTimeMillis() } ?: false @@ -153,6 +154,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( get() = podState.basalProgram set(basalProgram) { podState.basalProgram = basalProgram + rxBus.send(EventOmnipodDashPumpValuesChanged()) store() } From c72f2f352f69704affaec141120906712076ecdf Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sat, 5 Jun 2021 20:37:17 +0200 Subject: [PATCH 3/8] always enforceNew --- .../plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 68405114d7..3979406821 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -326,7 +326,7 @@ class OmnipodDashPumpPlugin @Inject constructor( .filter { podEvent -> podEvent is PodEvent.CommandSent } .map { pumpSyncTempBasal(it, absoluteRate, durationInMinutes.toLong(), tbrType) } .ignoreElements(), - pre = observeNoActiveTempBasal(enforceNew), + pre = observeNoActiveTempBasal(true), tempBasal = OmnipodDashPodStateManager.TempBasal( startTime = System.currentTimeMillis(), rate = absoluteRate, From 79b4a5d656c061f68e1b695a859107701d1b46d9 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 6 Jun 2021 10:47:29 +0200 Subject: [PATCH 4/8] simplify executeProgrammingCommand. Start implementing alerts --- .../omnipod/dash/OmnipodDashPumpPlugin.kt | 94 ++++++++++--------- .../dash/driver/comm/session/Session.kt | 9 +- .../driver/pod/definition/BasalProgram.kt | 2 +- .../pod/state/OmnipodDashPodStateManager.kt | 2 +- .../state/OmnipodDashPodStateManagerImpl.kt | 7 +- .../dash/ui/OmnipodDashOverviewFragment.kt | 3 +- 6 files changed, 60 insertions(+), 57 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 3979406821..846679b503 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash import dagger.android.HasAndroidInjector import info.nightscout.androidaps.data.DetailedBolusInfo import info.nightscout.androidaps.data.PumpEnactResult -import info.nightscout.androidaps.events.EventPreferenceChange import info.nightscout.androidaps.events.EventProfileSwitchChanged import info.nightscout.androidaps.events.EventTempBasalChange import info.nightscout.androidaps.interfaces.* @@ -19,7 +18,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.common.queue.command.* import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.OmnipodDashManager 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.BasalProgram 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.PodConstants @@ -37,13 +35,10 @@ import info.nightscout.androidaps.queue.commands.CustomCommand import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.TimeChangeType import info.nightscout.androidaps.utils.resources.ResourceHelper -import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.sharedPreferences.SP import io.reactivex.Completable import io.reactivex.Observable import io.reactivex.Single -import io.reactivex.disposables.CompositeDisposable -import io.reactivex.functions.Consumer import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.subscribeBy import org.json.JSONObject @@ -61,7 +56,7 @@ class OmnipodDashPumpPlugin @Inject constructor( private val pumpSync: PumpSync, private val rxBus: RxBusWrapper, // private val disposable: CompositeDisposable = CompositeDisposable(), - // private val aapsSchedulers: AapsSchedulers, + // private val aapsSchedulers: AapsSchedulers, injector: HasAndroidInjector, aapsLogger: AAPSLogger, @@ -128,20 +123,21 @@ class OmnipodDashPumpPlugin @Inject constructor( } override fun getPumpStatus(reason: String) { - val throwable = Completable.concat(listOf( - omnipodManager - .getStatus(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE) - .ignoreElements(), - history.updateFromState(podStateManager), - podStateManager.updateActiveCommand() - .map { handleCommandConfirmation(it) } - .ignoreElement(), - )).blockingGet() - if (throwable != null){ + val throwable = Completable.concat( + listOf( + omnipodManager + .getStatus(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE) + .ignoreElements(), + history.updateFromState(podStateManager), + podStateManager.updateActiveCommand() + .map { handleCommandConfirmation(it) } + .ignoreElement(), + ) + ).blockingGet() + if (throwable != null) { aapsLogger.error(LTag.PUMP, "Error in getPumpStatus", throwable) } else { aapsLogger.info(LTag.PUMP, "getPumpStatus executed with success") - } } @@ -150,17 +146,19 @@ class OmnipodDashPumpPlugin @Inject constructor( return executeProgrammingCommand( pre = suspendDeliveryIfActive(), historyEntry = history.createRecord(commandType = OmnipodCommandType.SET_BASAL_PROFILE), + activeCommandEntry = { historyId -> + podStateManager.createActiveCommand(historyId, basalProgram = basalProgram) + }, command = omnipodManager.setBasalProgram(basalProgram).ignoreElements(), - basalProgram = basalProgram, post = failWhenUnconfirmed(), ).toPumpEnactResult() } - private fun failWhenUnconfirmed(): Completable = Completable.defer{ + private fun failWhenUnconfirmed(): Completable = Completable.defer { rxBus.send(EventTempBasalChange()) if (podStateManager.activeCommand != null) { Completable.error(java.lang.IllegalStateException("Command not confirmed")) - }else { + } else { Completable.complete() } } @@ -170,8 +168,8 @@ class OmnipodDashPumpPlugin @Inject constructor( Completable.complete() else executeProgrammingCommand( - history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY), - omnipodManager.suspendDelivery() + historyEntry = history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY), + command = omnipodManager.suspendDelivery() .filter { podEvent -> podEvent is PodEvent.CommandSent } .map { pumpSyncTempBasal( @@ -190,7 +188,7 @@ class OmnipodDashPumpPlugin @Inject constructor( super.onStop() disposable.clear() } - + */ private fun observeDeliverySuspended(): Completable = Completable.defer { @@ -296,8 +294,8 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun stopBolusDelivering() { // TODO update Treatments (?) executeProgrammingCommand( - history.createRecord(OmnipodCommandType.CANCEL_BOLUS), - omnipodManager.stopBolus().ignoreElements() + historyEntry = history.createRecord(OmnipodCommandType.CANCEL_BOLUS), + command = omnipodManager.stopBolus().ignoreElements() ).toPumpEnactResult() } @@ -310,14 +308,25 @@ class OmnipodDashPumpPlugin @Inject constructor( ): PumpEnactResult { val tempBasalBeeps = sp.getBoolean(R.string.key_omnipod_common_tbr_beeps_enabled, false) aapsLogger.info( - LTag.PUMP, "setTempBasalAbsolute: $durationInMinutes min :: $absoluteRate U/h :: " + - "enforce: $enforceNew :: tbrType: $tbrType" + LTag.PUMP, + "setTempBasalAbsolute: $durationInMinutes min :: $absoluteRate U/h :: " + + "enforce: $enforceNew :: tbrType: $tbrType" ) return executeProgrammingCommand( historyEntry = history.createRecord( commandType = OmnipodCommandType.SET_TEMPORARY_BASAL, tempBasalRecord = TempBasalRecord(duration = durationInMinutes, rate = absoluteRate) ), + activeCommandEntry = { historyId -> + podStateManager.createActiveCommand( + historyId, + tempBasal = OmnipodDashPodStateManager.TempBasal( + startTime = System.currentTimeMillis(), + rate = absoluteRate, + durationInMinutes = durationInMinutes.toShort(), + ) + ) + }, command = omnipodManager.setTempBasal( absoluteRate, durationInMinutes.toShort(), @@ -327,11 +336,6 @@ class OmnipodDashPumpPlugin @Inject constructor( .map { pumpSyncTempBasal(it, absoluteRate, durationInMinutes.toLong(), tbrType) } .ignoreElements(), pre = observeNoActiveTempBasal(true), - tempBasal = OmnipodDashPodStateManager.TempBasal( - startTime = System.currentTimeMillis(), - rate = absoluteRate, - durationInMinutes = durationInMinutes.toShort(), - ) ).toPumpEnactResult() } @@ -386,8 +390,8 @@ class OmnipodDashPumpPlugin @Inject constructor( // enforceNew == true aapsLogger.info(LTag.PUMP, "Canceling existing temp basal") executeProgrammingCommand( - history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL), - omnipodManager.stopTempBasal().ignoreElements() + historyEntry = history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL), + command = omnipodManager.stopTempBasal().ignoreElements() ) } } @@ -582,17 +586,17 @@ class OmnipodDashPumpPlugin @Inject constructor( private fun resumeDelivery(): PumpEnactResult { return profileFunction.getProfile()?.let { executeProgrammingCommand( - history.createRecord(OmnipodCommandType.RESUME_DELIVERY), - omnipodManager.setBasalProgram(mapProfileToBasalProgram(it)).ignoreElements(), pre = observeDeliverySuspended(), + historyEntry = history.createRecord(OmnipodCommandType.RESUME_DELIVERY), + command = omnipodManager.setBasalProgram(mapProfileToBasalProgram(it)).ignoreElements() ).toPumpEnactResult() } ?: PumpEnactResult(injector).success(false).enacted(false).comment("No profile active") // TODO i18n } private fun deactivatePod(): PumpEnactResult { return executeProgrammingCommand( - history.createRecord(OmnipodCommandType.DEACTIVATE_POD), - omnipodManager.deactivatePod().ignoreElements() + historyEntry = history.createRecord(OmnipodCommandType.DEACTIVATE_POD), + command = omnipodManager.deactivatePod().ignoreElements() ).toPumpEnactResult() } @@ -608,8 +612,8 @@ class OmnipodDashPumpPlugin @Inject constructor( private fun playTestBeep(): PumpEnactResult { return executeProgrammingCommand( - history.createRecord(OmnipodCommandType.PLAY_TEST_BEEP), - omnipodManager.playBeep(BeepType.LONG_SINGLE_BEEP).ignoreElements() + historyEntry = history.createRecord(OmnipodCommandType.PLAY_TEST_BEEP), + command = omnipodManager.playBeep(BeepType.LONG_SINGLE_BEEP).ignoreElements() ).toPumpEnactResult() } @@ -635,19 +639,19 @@ class OmnipodDashPumpPlugin @Inject constructor( } private fun executeProgrammingCommand( - historyEntry: Single, - command: Completable, pre: Completable = Completable.complete(), + historyEntry: Single, + activeCommandEntry: (historyId: String) -> Single = + { historyId -> podStateManager.createActiveCommand(historyId) }, + command: Completable, post: Completable = Completable.complete(), - basalProgram: BasalProgram? = null, - tempBasal: OmnipodDashPodStateManager.TempBasal? = null, ): Completable { return Completable.concat( listOf( pre, podStateManager.observeNoActiveCommand().ignoreElements(), historyEntry - .flatMap { podStateManager.createActiveCommand(it, basalProgram, tempBasal) } + .flatMap { activeCommandEntry(it) } .ignoreElement(), command.doOnError { podStateManager.activeCommand?.sendError = it diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt index 128768965e..3819ee8d05 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Session.kt @@ -6,15 +6,10 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Ids import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.endecrypt.EnDecrypt import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.CouldNotParseResponseException -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.IllegalResponseException -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.NakResponseException -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions.PodAlarmException import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.* import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageType import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.StringLengthPrefixEncoding.Companion.parseKeys import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.base.Command -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse -import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.NakResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.Response import kotlin.reflect.KClass @@ -88,7 +83,7 @@ class Session( val response = parseResponse(decrypted) - if (!responseType.isInstance(response)) { + /*if (!responseType.isInstance(response)) { if (response is AlarmStatusResponse) { throw PodAlarmException(response) } @@ -98,6 +93,8 @@ class Session( throw IllegalResponseException(responseType, response) } + */ + sessionKeys.msgSequenceNumber++ val ack = getAck(responseMsgPacket) aapsLogger.debug(LTag.PUMPBTCOMM, "Sending ACK: ${ack.payload.toHex()} in packet $ack") diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/BasalProgram.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/BasalProgram.kt index 73b70126d6..553051444b 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/BasalProgram.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/definition/BasalProgram.kt @@ -23,7 +23,7 @@ class BasalProgram( val hourOfDay = instance[Calendar.HOUR_OF_DAY] val minuteOfHour = instance[Calendar.MINUTE] val slotIndex = hourOfDay * 2 + minuteOfHour.div(30) - val slot = segments.find { it.startSlotIndex <= slotIndex && slotIndex< it.endSlotIndex } + val slot = segments.find { it.startSlotIndex <= slotIndex && slotIndex < it.endSlotIndex } return (slot?.basalRateInHundredthUnitsPerHour ?: 0).toDouble() / 100 } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index d7f0ff4a6d..7be3770745 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -73,7 +73,7 @@ interface OmnipodDashPodStateManager { fun updateFromPairing(uniqueId: Id, pairResult: PairResult) fun reset() - fun createActiveCommand(historyId: String, basalProgram: BasalProgram? = null, tempBasal: TempBasal?=null): + fun createActiveCommand(historyId: String, basalProgram: BasalProgram? = null, tempBasal: TempBasal? = null): Single fun updateActiveCommand(): Maybe fun observeNoActiveCommand(): Observable diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index fc79643af7..c1303e66cb 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -147,7 +147,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( override val tempBasalActive: Boolean get() = !isSuspended && tempBasal?.let { - it.startTime + it.durationInMinutes *60 * 1000 > System.currentTimeMillis() + it.startTime + it.durationInMinutes * 60 * 1000 > System.currentTimeMillis() } ?: false override var basalProgram: BasalProgram? @@ -388,7 +388,10 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( LTag.PUMP, "Not implemented: OmnipodDashPodStateManagerImpl.updateFromAlarmStatusResponse(AlarmStatusResponse)" ) - + logger.info( + LTag.PUMP, + "Received AlarmStatusReponse: $response" + ) store() rxBus.send(EventOmnipodDashPumpValuesChanged()) } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 671cf34d01..6962b5f4f4 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -42,7 +42,6 @@ import info.nightscout.androidaps.utils.ui.UIRunnable import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.plusAssign import org.apache.commons.lang3.StringUtils -import org.joda.time.DateTime import org.joda.time.Duration import java.util.* import javax.inject.Inject @@ -346,7 +345,7 @@ class OmnipodDashOverviewFragment : DaggerFragment() { podInfoBinding.podActiveAlerts.text = podStateManager.activeAlerts?.let { it.map { it.toString() }.joinToString(",") - } ?: PLACEHOLDER + } ?: PLACEHOLDER } if (errors.size == 0) { From d82be687967ffeaffbc9893dd1aefdf6708aacf2 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 6 Jun 2021 13:09:27 +0200 Subject: [PATCH 5/8] fix alerts(UNKNOWN will always match) --- .../omnipod/dash/OmnipodDashPumpPlugin.kt | 38 +++++-------------- .../omnipod/dash/driver/pod/util/AlertUtil.kt | 7 ++-- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 846679b503..fb0c430054 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -293,10 +293,12 @@ class OmnipodDashPumpPlugin @Inject constructor( override fun stopBolusDelivering() { // TODO update Treatments (?) - executeProgrammingCommand( + aapsLogger.info(LTag.PUMP, "stopBolusDelivering called") + val ret = executeProgrammingCommand( historyEntry = history.createRecord(OmnipodCommandType.CANCEL_BOLUS), command = omnipodManager.stopBolus().ignoreElements() ).toPumpEnactResult() + aapsLogger.info(LTag.PUMP, "stopBolusDelivering finished with result: $ret") } override fun setTempBasalAbsolute( @@ -440,6 +442,9 @@ class OmnipodDashPumpPlugin @Inject constructor( fun Completable.toPumpEnactResult(): PumpEnactResult { return this.toSingleDefault(PumpEnactResult(injector).success(true).enacted(true)) + .doOnError { throwable -> + aapsLogger.error(LTag.PUMP, "toPumpEnactResult, error executing command: $throwable") + } .onErrorReturnItem(PumpEnactResult(injector).success(false).enacted(false)) .blockingGet() } @@ -528,33 +533,10 @@ class OmnipodDashPumpPlugin @Inject constructor( private fun silenceAlerts(): PumpEnactResult { // TODO filter alert types return podStateManager.activeAlerts?.let { - Single.create { source -> - Observable.concat( - // TODO: is this a programming command? if yes, save to history - omnipodManager.silenceAlerts(it), - history.updateFromState(podStateManager).toObservable(), - podStateManager.updateActiveCommand().toObservable(), - ).subscribeBy( - onNext = { podEvent -> - aapsLogger.debug( - LTag.PUMP, - "Received PodEvent in silenceAlerts: $podEvent" - ) - }, - onError = { throwable -> - aapsLogger.error(LTag.PUMP, "Error in silenceAlerts", throwable) - source.onSuccess( - PumpEnactResult(injector).success(false).comment( - throwable.toString() - ) - ) - }, - onComplete = { - aapsLogger.debug("silenceAlerts completed") - source.onSuccess(PumpEnactResult(injector).success(true)) - } - ) - }.blockingGet() + executeProgrammingCommand( + historyEntry = history.createRecord(commandType = OmnipodCommandType.ACKNOWLEDGE_ALERTS), + command = omnipodManager.silenceAlerts(it).ignoreElements(), + ).toPumpEnactResult() } ?: PumpEnactResult(injector).success(false).enacted(false).comment("No active alerts") // TODO i18n } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/util/AlertUtil.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/util/AlertUtil.kt index 9d5db05579..5a12dabb23 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/util/AlertUtil.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/util/AlertUtil.kt @@ -8,9 +8,10 @@ object AlertUtil { fun decodeAlertSet(encoded: Byte): EnumSet { val encodedInt = encoded.toInt() and 0xff - val alertList = AlertType.values().filter { - (it.value.toInt() and 0xff) and encodedInt != 0 - }.toList() + val alertList = AlertType.values() + .filter { it != AlertType.UNKNOWN } // 0xff && will always be true + .filter { (it.value.toInt() and 0xff) and encodedInt != 0 } + .toList() return if (alertList.isEmpty()) { EnumSet.noneOf(AlertType::class.java) From 909eae3f3fdcc9ad530de4d44d6316837618ce73 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 6 Jun 2021 17:47:36 +0200 Subject: [PATCH 6/8] log --- .../dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index c1303e66cb..9d24462b68 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -317,6 +317,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( } override fun updateFromDefaultStatusResponse(response: DefaultStatusResponse) { + logger.debug(LTag.PUMPBTCOMM, "Default status reponse :$response") podState.deliveryStatus = response.deliveryStatus podState.podStatus = response.podStatus podState.pulsesDelivered = response.totalPulsesDelivered From aa719e0b9f5bb2a6289f18bcfbf7558856e92054 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 6 Jun 2021 18:35:19 +0200 Subject: [PATCH 7/8] handle Pod Alarm --- .../omnipod/dash/OmnipodDashPumpPlugin.kt | 26 ++++++++++++++++++- .../pod/state/OmnipodDashPodStateManager.kt | 2 ++ .../state/OmnipodDashPodStateManagerImpl.kt | 26 +++++++++++++++---- .../dash/ui/OmnipodDashOverviewFragment.kt | 8 +++++- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index fb0c430054..2ce0d5852e 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -45,6 +45,7 @@ import org.json.JSONObject import java.util.* import javax.inject.Inject import javax.inject.Singleton +import kotlin.random.Random @Singleton class OmnipodDashPumpPlugin @Inject constructor( @@ -131,6 +132,7 @@ class OmnipodDashPumpPlugin @Inject constructor( history.updateFromState(podStateManager), podStateManager.updateActiveCommand() .map { handleCommandConfirmation(it) } + .map { checkPodKaput() } .ignoreElement(), ) ).blockingGet() @@ -141,6 +143,25 @@ class OmnipodDashPumpPlugin @Inject constructor( } } + private fun checkPodKaput() { + if (podStateManager.isPodKaput) { + val tbr = pumpSync.expectedPumpState().temporaryBasal + if (tbr?.rate == 0.0) { + return + } + pumpSync.syncTemporaryBasalWithPumpId( + timestamp = System.currentTimeMillis(), + rate = 0.0, + duration = T.mins(PodConstants.MAX_POD_LIFETIME.standardMinutes).msecs(), + isAbsolute = true, + type = PumpSync.TemporaryBasalType.PUMP_SUSPEND, + pumpId = Random.Default.nextLong(), // we don't use this, just make sure it's unique + pumpType = PumpType.OMNIPOD_DASH, + pumpSerial = serialNumber() + ) + } + } + override fun setNewBasalProfile(profile: Profile): PumpEnactResult { val basalProgram = mapProfileToBasalProgram(profile) return executeProgrammingCommand( @@ -220,7 +241,10 @@ class OmnipodDashPumpPlugin @Inject constructor( val date = Date() val ret = podStateManager.basalProgram?.rateAt(date) ?: 0.0 aapsLogger.info(LTag.PUMP, "baseBasalRate: %ret at $date}") - return ret + return if (podStateManager.alarmType != null) { + 0.0 + } else + ret } override val reservoirLevel: Double diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index 7be3770745..46e9652aaf 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -28,6 +28,7 @@ interface OmnipodDashPodStateManager { val isActivationCompleted: Boolean val isSuspended: Boolean val isPodRunning: Boolean + val isPodKaput: Boolean var bluetoothConnectionState: BluetoothConnectionState val lastUpdatedSystem: Long // System.currentTimeMillis() @@ -57,6 +58,7 @@ interface OmnipodDashPodStateManager { val deliveryStatus: DeliveryStatus? val minutesSinceActivation: Short? val activeAlerts: EnumSet? + val alarmType: AlarmType? var tempBasal: TempBasal? val tempBasalActive: Boolean diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index 9d24462b68..423cd8f836 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -55,6 +55,9 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( get() = podState.deliveryStatus?.equals(DeliveryStatus.SUSPENDED) ?: false + override val isPodKaput: Boolean + get() = podState.podStatus in arrayOf(PodStatus.ALARM, PodStatus.DEACTIVATED) + override val isPodRunning: Boolean get() = podState.podStatus?.isRunning() ?: false @@ -137,6 +140,9 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( override val activeAlerts: EnumSet? get() = podState.activeAlerts + override val alarmType: AlarmType? + get() = podState.alarmType + override var tempBasal: OmnipodDashPodStateManager.TempBasal? get() = podState.tempBasal set(tempBasal) { @@ -384,15 +390,24 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( } override fun updateFromAlarmStatusResponse(response: AlarmStatusResponse) { - // TODO - logger.error( - LTag.PUMP, - "Not implemented: OmnipodDashPodStateManagerImpl.updateFromAlarmStatusResponse(AlarmStatusResponse)" - ) logger.info( LTag.PUMP, "Received AlarmStatusReponse: $response" ) + podState.deliveryStatus = response.deliveryStatus + podState.podStatus = response.podStatus + podState.pulsesDelivered = response.totalPulsesDelivered + if (response.reservoirPulsesRemaining < 1023) { + podState.pulsesRemaining = response.reservoirPulsesRemaining + } + podState.sequenceNumberOfLastProgrammingCommand = response.sequenceNumberOfLastProgrammingCommand + podState.minutesSinceActivation = response.minutesSinceActivation + podState.activeAlerts = response.activeAlerts + podState.alarmType = response.alarmType + + podState.lastUpdatedSystem = System.currentTimeMillis() + podState.lastStatusResponseReceived = SystemClock.elapsedRealtime() + store() rxBus.send(EventOmnipodDashPumpValuesChanged()) } @@ -464,6 +479,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( var deliveryStatus: DeliveryStatus? = null var minutesSinceActivation: Short? = null var activeAlerts: EnumSet? = null + var alarmType: AlarmType? = null var basalProgram: BasalProgram? = null var tempBasal: OmnipodDashPodStateManager.TempBasal? = null diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 6962b5f4f4..8ec1db4fa4 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -212,7 +212,6 @@ class OmnipodDashOverviewFragment : DaggerFragment() { } private fun updateUi() { - // TODO update bluetooth status updateBluetoothStatus() updateOmnipodStatus() updatePodActionButtons() @@ -296,6 +295,13 @@ class OmnipodDashOverviewFragment : DaggerFragment() { errors.add(resourceHelper.gs(R.string.omnipod_common_pod_status_pod_fault_description, faultEventCode.value, faultEventCode.name)) } */ + podStateManager.alarmType?.let { + errors.add(resourceHelper.gs( + R.string.omnipod_common_pod_status_pod_fault_description, + it.value, + it.toString()) + ) + } // base basal rate podInfoBinding.baseBasalRate.text = if (podStateManager.basalProgram != null && !podStateManager.isSuspended) { From b55dacd12cd784292b5c42553381defc2d33c241 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 6 Jun 2021 18:41:17 +0200 Subject: [PATCH 8/8] add checkPodKaput --- .../pump/omnipod/dash/OmnipodDashPumpPlugin.kt | 14 +++++++------- .../pod/state/OmnipodDashPodStateManagerImpl.kt | 2 +- .../omnipod/dash/ui/OmnipodDashOverviewFragment.kt | 10 ++++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt index 2ce0d5852e..9dc3f521b6 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/OmnipodDashPumpPlugin.kt @@ -132,8 +132,8 @@ class OmnipodDashPumpPlugin @Inject constructor( history.updateFromState(podStateManager), podStateManager.updateActiveCommand() .map { handleCommandConfirmation(it) } - .map { checkPodKaput() } .ignoreElement(), + checkPodKaput() ) ).blockingGet() if (throwable != null) { @@ -143,12 +143,11 @@ class OmnipodDashPumpPlugin @Inject constructor( } } - private fun checkPodKaput() { - if (podStateManager.isPodKaput) { - val tbr = pumpSync.expectedPumpState().temporaryBasal - if (tbr?.rate == 0.0) { - return - } + private fun checkPodKaput(): Completable = Completable.defer { + val tbr = pumpSync.expectedPumpState().temporaryBasal + if (podStateManager.isPodKaput && + (tbr == null || tbr.rate != 0.0) + ) { pumpSync.syncTemporaryBasalWithPumpId( timestamp = System.currentTimeMillis(), rate = 0.0, @@ -160,6 +159,7 @@ class OmnipodDashPumpPlugin @Inject constructor( pumpSerial = serialNumber() ) } + Completable.complete() } override fun setNewBasalProfile(profile: Profile): PumpEnactResult { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index 423cd8f836..7d2df0242b 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -140,7 +140,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( override val activeAlerts: EnumSet? get() = podState.activeAlerts - override val alarmType: AlarmType? + override val alarmType: AlarmType? get() = podState.alarmType override var tempBasal: OmnipodDashPodStateManager.TempBasal? diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 8ec1db4fa4..3d0099c1af 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -296,10 +296,12 @@ class OmnipodDashOverviewFragment : DaggerFragment() { } */ podStateManager.alarmType?.let { - errors.add(resourceHelper.gs( - R.string.omnipod_common_pod_status_pod_fault_description, - it.value, - it.toString()) + errors.add( + resourceHelper.gs( + R.string.omnipod_common_pod_status_pod_fault_description, + it.value, + it.toString() + ) ) }