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 1b2a43af32..31ced6c586 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 @@ -473,6 +473,7 @@ class OmnipodDashPumpPlugin @Inject constructor( ): PumpEnactResult { return Single.create { source -> Observable.concat( + podStateManager.observeNoActiveCommand(), historyId.flatMapObservable { recordId -> podStateManager.createActiveCommand(recordId).toObservable() }, 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 57ea8af437..9fff97203d 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 @@ -10,6 +10,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response. import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.VersionResponse import io.reactivex.Completable import io.reactivex.Maybe +import io.reactivex.Observable import java.io.Serializable import java.util.* @@ -67,6 +68,7 @@ interface OmnipodDashPodStateManager { fun createActiveCommand(historyId: String): Completable fun updateActiveCommand(): Maybe + fun observeNoActiveCommand(): Observable fun maybeMarkActiveCommandFailed() data class ActiveCommand( 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 831060673b..a8e31905bc 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 @@ -19,6 +19,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response. import info.nightscout.androidaps.utils.sharedPreferences.SP import io.reactivex.Completable import io.reactivex.Maybe +import io.reactivex.Observable import java.io.Serializable import java.util.* import javax.inject.Inject @@ -182,16 +183,16 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( get() = podState.activeCommand @Synchronized - override fun createActiveCommand(historyId: String): Completable { - return if (activeCommand == null) { + override fun createActiveCommand(historyId: String) = Completable.create { source -> + if (activeCommand == null) { podState.activeCommand = OmnipodDashPodStateManager.ActiveCommand( podState.messageSequenceNumber, createdRealtime = SystemClock.elapsedRealtime(), historyId = historyId ) - Completable.complete() + source.onComplete() } else { - Completable.error( + source.onError( java.lang.IllegalStateException( "Trying to send a command " + "and the last command was not confirmed" @@ -200,6 +201,22 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( } } + @Synchronized + override fun observeNoActiveCommand(): Observable { + return Observable.defer { + if (activeCommand == null) { + Observable.empty() + } else { + Observable.error( + java.lang.IllegalStateException( + "Trying to send a command " + + "and the last command was not confirmed" + ) + ) + } + } + } + @Synchronized override fun maybeMarkActiveCommandFailed() { podState.activeCommand?.run { @@ -207,7 +224,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( // command was not sent podState.activeCommand = null } - } + } } @Synchronized @@ -219,7 +236,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( "$sequenceNumberOfLastProgrammingCommand $historyId" ) if (createdRealtime >= lastStatusResponseReceived) - // we did not receive a valid response yet + // we did not receive a valid response yet source.onComplete() else { podState.activeCommand = null