observe there is no activeCommand

This commit is contained in:
Andrei Vereha 2021-05-01 20:56:02 +02:00
parent 24e5223ad1
commit 4bd5e7c625
3 changed files with 26 additions and 6 deletions

View file

@ -473,6 +473,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
): PumpEnactResult { ): PumpEnactResult {
return Single.create<PumpEnactResult> { source -> return Single.create<PumpEnactResult> { source ->
Observable.concat( Observable.concat(
podStateManager.observeNoActiveCommand(),
historyId.flatMapObservable { recordId -> historyId.flatMapObservable { recordId ->
podStateManager.createActiveCommand(recordId).toObservable() podStateManager.createActiveCommand(recordId).toObservable()
}, },

View file

@ -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 info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.VersionResponse
import io.reactivex.Completable import io.reactivex.Completable
import io.reactivex.Maybe import io.reactivex.Maybe
import io.reactivex.Observable
import java.io.Serializable import java.io.Serializable
import java.util.* import java.util.*
@ -67,6 +68,7 @@ interface OmnipodDashPodStateManager {
fun createActiveCommand(historyId: String): Completable fun createActiveCommand(historyId: String): Completable
fun updateActiveCommand(): Maybe<PodEvent> fun updateActiveCommand(): Maybe<PodEvent>
fun observeNoActiveCommand(): Observable<PodEvent>
fun maybeMarkActiveCommandFailed() fun maybeMarkActiveCommandFailed()
data class ActiveCommand( data class ActiveCommand(

View file

@ -19,6 +19,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.
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.Maybe import io.reactivex.Maybe
import io.reactivex.Observable
import java.io.Serializable import java.io.Serializable
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
@ -182,16 +183,16 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
get() = podState.activeCommand get() = podState.activeCommand
@Synchronized @Synchronized
override fun createActiveCommand(historyId: String): Completable { override fun createActiveCommand(historyId: String) = Completable.create { source ->
return if (activeCommand == null) { if (activeCommand == null) {
podState.activeCommand = OmnipodDashPodStateManager.ActiveCommand( podState.activeCommand = OmnipodDashPodStateManager.ActiveCommand(
podState.messageSequenceNumber, podState.messageSequenceNumber,
createdRealtime = SystemClock.elapsedRealtime(), createdRealtime = SystemClock.elapsedRealtime(),
historyId = historyId historyId = historyId
) )
Completable.complete() source.onComplete()
} else { } else {
Completable.error( source.onError(
java.lang.IllegalStateException( java.lang.IllegalStateException(
"Trying to send a command " + "Trying to send a command " +
"and the last command was not confirmed" "and the last command was not confirmed"
@ -200,6 +201,22 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
} }
} }
@Synchronized
override fun observeNoActiveCommand(): Observable<PodEvent> {
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 @Synchronized
override fun maybeMarkActiveCommandFailed() { override fun maybeMarkActiveCommandFailed() {
podState.activeCommand?.run { podState.activeCommand?.run {
@ -207,7 +224,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
// command was not sent // command was not sent
podState.activeCommand = null podState.activeCommand = null
} }
} }
} }
@Synchronized @Synchronized
@ -219,7 +236,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
"$sequenceNumberOfLastProgrammingCommand $historyId" "$sequenceNumberOfLastProgrammingCommand $historyId"
) )
if (createdRealtime >= lastStatusResponseReceived) if (createdRealtime >= lastStatusResponseReceived)
// we did not receive a valid response yet // we did not receive a valid response yet
source.onComplete() source.onComplete()
else { else {
podState.activeCommand = null podState.activeCommand = null