move the common code to getCommandConfirmationFromState
This commit is contained in:
parent
c8fee31ae8
commit
b91e8506a0
5 changed files with 96 additions and 44 deletions
|
@ -4,6 +4,7 @@ import android.content.Context
|
|||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.Reusable
|
||||
import info.nightscout.androidaps.logging.AAPSLogger
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.DashHistory
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.DashHistoryDatabase
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.HistoryRecordDao
|
||||
|
@ -28,6 +29,6 @@ class OmnipodDashHistoryModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
internal fun provideDashHistory(dao: HistoryRecordDao, historyMapper: HistoryMapper) =
|
||||
DashHistory(dao, historyMapper)
|
||||
internal fun provideDashHistory(dao: HistoryRecordDao, historyMapper: HistoryMapper, logger: AAPSLogger) =
|
||||
DashHistory(dao, historyMapper, logger)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
|
||||
|
||||
class CouldNotReadResponse
|
|
@ -14,6 +14,13 @@ import io.reactivex.Single
|
|||
import java.io.Serializable
|
||||
import java.util.*
|
||||
|
||||
sealed class CommandConfirmationFromState
|
||||
object CommandSendingFailure : CommandConfirmationFromState()
|
||||
object CommandSendingNotConfirmed : CommandConfirmationFromState()
|
||||
object CommandConfirmationDenied : CommandConfirmationFromState()
|
||||
object CommandConfirmationSuccess : CommandConfirmationFromState()
|
||||
object NoActiveCommand : CommandConfirmationFromState()
|
||||
|
||||
interface OmnipodDashPodStateManager {
|
||||
|
||||
var activationProgress: ActivationProgress
|
||||
|
@ -70,6 +77,7 @@ interface OmnipodDashPodStateManager {
|
|||
fun createActiveCommand(historyId: String): Single<ActiveCommand>
|
||||
fun updateActiveCommand(): Maybe<CommandConfirmed>
|
||||
fun observeNoActiveCommand(): Observable<PodEvent>
|
||||
fun getCommandConfirmationFromState(): CommandConfirmationFromState
|
||||
|
||||
data class ActiveCommand(
|
||||
val sequence: Short,
|
||||
|
|
|
@ -231,30 +231,70 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
|
|||
|
||||
@Synchronized
|
||||
override fun updateActiveCommand() = Maybe.create<CommandConfirmed> { source ->
|
||||
podState.activeCommand?.run {
|
||||
val activeCommand = podState.activeCommand
|
||||
if (activeCommand == null) {
|
||||
logger.error("No active command to update")
|
||||
source.onComplete()
|
||||
return@create
|
||||
}
|
||||
|
||||
when (getCommandConfirmationFromState()) {
|
||||
CommandSendingFailure -> {
|
||||
podState.activeCommand = null
|
||||
source.onError(
|
||||
activeCommand?.sendError
|
||||
?: java.lang.IllegalStateException(
|
||||
"Could not send command and sendError is " +
|
||||
"missing"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
CommandSendingNotConfirmed -> {
|
||||
// we did not receive a valid response yet
|
||||
source.onComplete()
|
||||
}
|
||||
|
||||
CommandConfirmationDenied -> {
|
||||
podState.activeCommand = null
|
||||
source.onSuccess(CommandConfirmed(activeCommand.historyId, false))
|
||||
}
|
||||
|
||||
CommandConfirmationSuccess -> {
|
||||
podState.activeCommand = null
|
||||
source.onSuccess(CommandConfirmed(activeCommand.historyId, false))
|
||||
}
|
||||
|
||||
NoActiveCommand -> {
|
||||
source.onComplete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun getCommandConfirmationFromState(): CommandConfirmationFromState {
|
||||
return podState.activeCommand?.run {
|
||||
logger.debug(
|
||||
"Trying to confirm active command with parameters: $activeCommand " +
|
||||
"Getting command state with parameters: $activeCommand " +
|
||||
"lastResponse=$lastStatusResponseReceived " +
|
||||
"$sequenceNumberOfLastProgrammingCommand $historyId"
|
||||
)
|
||||
|
||||
if (sentRealtime < createdRealtime) { // command was not sent, clear it up
|
||||
podState.activeCommand = null
|
||||
source.onError(this.sendError
|
||||
?: java.lang.IllegalStateException("Could not send command and sendError is " +
|
||||
"missing") )
|
||||
} else if (createdRealtime >= lastStatusResponseReceived)
|
||||
// we did not receive a valid response yet
|
||||
source.onComplete()
|
||||
else {
|
||||
podState.activeCommand = null
|
||||
if (sequenceNumberOfLastProgrammingCommand == sequence)
|
||||
source.onSuccess(CommandConfirmed(historyId, true))
|
||||
else
|
||||
source.onSuccess(CommandConfirmed(historyId, false))
|
||||
when {
|
||||
createdRealtime <= podState.lastStatusResponseReceived &&
|
||||
sequence == podState.sequenceNumberOfLastProgrammingCommand ->
|
||||
CommandConfirmationSuccess
|
||||
createdRealtime <= podState.lastStatusResponseReceived &&
|
||||
sequence != podState.sequenceNumberOfLastProgrammingCommand ->
|
||||
CommandConfirmationDenied
|
||||
// no response received after this point
|
||||
createdRealtime <= sentRealtime ->
|
||||
CommandSendingNotConfirmed
|
||||
createdRealtime > sentRealtime ->
|
||||
CommandSendingFailure
|
||||
else -> // this can't happen, see the previous two conditions
|
||||
NoActiveCommand
|
||||
}
|
||||
} ?: source.onComplete()
|
||||
// no active programming command
|
||||
} ?: NoActiveCommand
|
||||
}
|
||||
|
||||
override fun increaseEapAkaSequenceNumber(): ByteArray {
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.history
|
||||
|
||||
import com.github.guepardoapps.kulid.ULID
|
||||
import info.nightscout.androidaps.logging.AAPSLogger
|
||||
import info.nightscout.androidaps.logging.LTag
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_BOLUS
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_TEMPORARY_BASAL
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.*
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.HistoryRecord
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult
|
||||
|
@ -20,7 +22,8 @@ import javax.inject.Inject
|
|||
|
||||
class DashHistory @Inject constructor(
|
||||
private val dao: HistoryRecordDao,
|
||||
private val historyMapper: HistoryMapper
|
||||
private val historyMapper: HistoryMapper,
|
||||
private val logger: AAPSLogger
|
||||
) {
|
||||
|
||||
private fun markSuccess(id: String): Completable = dao.markResolved(
|
||||
|
@ -83,26 +86,23 @@ class DashHistory @Inject constructor(
|
|||
fun getRecordsAfter(time: Long): Single<List<HistoryRecordEntity>> = dao.allSince(time)
|
||||
|
||||
fun updateFromState(podState: OmnipodDashPodStateManager) = Completable.defer {
|
||||
podState.activeCommand?.run {
|
||||
when {
|
||||
createdRealtime <= podState.lastStatusResponseReceived &&
|
||||
sequence == podState.sequenceNumberOfLastProgrammingCommand ->
|
||||
val historyId = podState.activeCommand?.historyId
|
||||
if (historyId == null) {
|
||||
logger.error(LTag.PUMP, "HistoryId not found to for updating from state")
|
||||
return@defer Completable.complete()
|
||||
}
|
||||
when (podState.getCommandConfirmationFromState()) {
|
||||
CommandSendingFailure ->
|
||||
dao.setInitialResult(historyId, InitialResult.FAILURE_SENDING)
|
||||
CommandSendingNotConfirmed ->
|
||||
dao.setInitialResult(historyId, InitialResult.SENT)
|
||||
CommandConfirmationDenied ->
|
||||
markFailure(historyId)
|
||||
CommandConfirmationSuccess ->
|
||||
dao.setInitialResult(historyId, InitialResult.SENT)
|
||||
.andThen(markSuccess(historyId))
|
||||
|
||||
createdRealtime <= podState.lastStatusResponseReceived &&
|
||||
sequence != podState.sequenceNumberOfLastProgrammingCommand ->
|
||||
markFailure(historyId)
|
||||
|
||||
// no response received after this point
|
||||
createdRealtime <= sentRealtime ->
|
||||
dao.setInitialResult(historyId, InitialResult.SENT)
|
||||
|
||||
createdRealtime > sentRealtime ->
|
||||
dao.setInitialResult(historyId, InitialResult.FAILURE_SENDING)
|
||||
|
||||
else -> Completable.error(IllegalStateException("This can't happen. Could not update history"))
|
||||
NoActiveCommand ->
|
||||
Completable.complete()
|
||||
}
|
||||
} ?: Completable.complete() // no active programming command
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue