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 {
return Single.create<PumpEnactResult> { source ->
Observable.concat(
podStateManager.observeNoActiveCommand(),
historyId.flatMapObservable { recordId ->
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 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<PodEvent>
fun observeNoActiveCommand(): Observable<PodEvent>
fun maybeMarkActiveCommandFailed()
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 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<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
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