move the common code to getCommandConfirmationFromState

This commit is contained in:
Andrei Vereha 2021-06-01 21:41:13 +02:00
parent c8fee31ae8
commit b91e8506a0
5 changed files with 96 additions and 44 deletions

View file

@ -4,6 +4,7 @@ import android.content.Context
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.Reusable 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.DashHistory
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.DashHistoryDatabase import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.DashHistoryDatabase
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.HistoryRecordDao import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.HistoryRecordDao
@ -28,6 +29,6 @@ class OmnipodDashHistoryModule {
@Provides @Provides
@Singleton @Singleton
internal fun provideDashHistory(dao: HistoryRecordDao, historyMapper: HistoryMapper) = internal fun provideDashHistory(dao: HistoryRecordDao, historyMapper: HistoryMapper, logger: AAPSLogger) =
DashHistory(dao, historyMapper) DashHistory(dao, historyMapper, logger)
} }

View file

@ -0,0 +1,3 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
class CouldNotReadResponse

View file

@ -14,6 +14,13 @@ import io.reactivex.Single
import java.io.Serializable import java.io.Serializable
import java.util.* import java.util.*
sealed class CommandConfirmationFromState
object CommandSendingFailure : CommandConfirmationFromState()
object CommandSendingNotConfirmed : CommandConfirmationFromState()
object CommandConfirmationDenied : CommandConfirmationFromState()
object CommandConfirmationSuccess : CommandConfirmationFromState()
object NoActiveCommand : CommandConfirmationFromState()
interface OmnipodDashPodStateManager { interface OmnipodDashPodStateManager {
var activationProgress: ActivationProgress var activationProgress: ActivationProgress
@ -70,6 +77,7 @@ interface OmnipodDashPodStateManager {
fun createActiveCommand(historyId: String): Single<ActiveCommand> fun createActiveCommand(historyId: String): Single<ActiveCommand>
fun updateActiveCommand(): Maybe<CommandConfirmed> fun updateActiveCommand(): Maybe<CommandConfirmed>
fun observeNoActiveCommand(): Observable<PodEvent> fun observeNoActiveCommand(): Observable<PodEvent>
fun getCommandConfirmationFromState(): CommandConfirmationFromState
data class ActiveCommand( data class ActiveCommand(
val sequence: Short, val sequence: Short,

View file

@ -231,30 +231,70 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
@Synchronized @Synchronized
override fun updateActiveCommand() = Maybe.create<CommandConfirmed> { source -> 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( logger.debug(
"Trying to confirm active command with parameters: $activeCommand " + "Getting command state with parameters: $activeCommand " +
"lastResponse=$lastStatusResponseReceived " + "lastResponse=$lastStatusResponseReceived " +
"$sequenceNumberOfLastProgrammingCommand $historyId" "$sequenceNumberOfLastProgrammingCommand $historyId"
) )
when {
if (sentRealtime < createdRealtime) { // command was not sent, clear it up createdRealtime <= podState.lastStatusResponseReceived &&
podState.activeCommand = null sequence == podState.sequenceNumberOfLastProgrammingCommand ->
source.onError(this.sendError CommandConfirmationSuccess
?: java.lang.IllegalStateException("Could not send command and sendError is " + createdRealtime <= podState.lastStatusResponseReceived &&
"missing") ) sequence != podState.sequenceNumberOfLastProgrammingCommand ->
} else if (createdRealtime >= lastStatusResponseReceived) CommandConfirmationDenied
// we did not receive a valid response yet // no response received after this point
source.onComplete() createdRealtime <= sentRealtime ->
else { CommandSendingNotConfirmed
podState.activeCommand = null createdRealtime > sentRealtime ->
if (sequenceNumberOfLastProgrammingCommand == sequence) CommandSendingFailure
source.onSuccess(CommandConfirmed(historyId, true)) else -> // this can't happen, see the previous two conditions
else NoActiveCommand
source.onSuccess(CommandConfirmed(historyId, false))
} }
} ?: source.onComplete() } ?: NoActiveCommand
// no active programming command
} }
override fun increaseEapAkaSequenceNumber(): ByteArray { override fun increaseEapAkaSequenceNumber(): ByteArray {

View file

@ -1,10 +1,12 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.history package info.nightscout.androidaps.plugins.pump.omnipod.dash.history
import com.github.guepardoapps.kulid.ULID 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
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_BOLUS 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.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.BolusRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.HistoryRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.HistoryRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult
@ -20,7 +22,8 @@ import javax.inject.Inject
class DashHistory @Inject constructor( class DashHistory @Inject constructor(
private val dao: HistoryRecordDao, private val dao: HistoryRecordDao,
private val historyMapper: HistoryMapper private val historyMapper: HistoryMapper,
private val logger: AAPSLogger
) { ) {
private fun markSuccess(id: String): Completable = dao.markResolved( 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 getRecordsAfter(time: Long): Single<List<HistoryRecordEntity>> = dao.allSince(time)
fun updateFromState(podState: OmnipodDashPodStateManager) = Completable.defer { fun updateFromState(podState: OmnipodDashPodStateManager) = Completable.defer {
podState.activeCommand?.run { val historyId = podState.activeCommand?.historyId
when { if (historyId == null) {
createdRealtime <= podState.lastStatusResponseReceived && logger.error(LTag.PUMP, "HistoryId not found to for updating from state")
sequence == podState.sequenceNumberOfLastProgrammingCommand -> 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) dao.setInitialResult(historyId, InitialResult.SENT)
.andThen(markSuccess(historyId)) .andThen(markSuccess(historyId))
NoActiveCommand ->
createdRealtime <= podState.lastStatusResponseReceived && Completable.complete()
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"))
} }
} ?: Completable.complete() // no active programming command
} }
} }