From b91e8506a087633afad6a4e1373f19bd970e9023 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Tue, 1 Jun 2021 21:41:13 +0200 Subject: [PATCH] move the common code to getCommandConfirmationFromState --- .../dash/dagger/OmnipodDashHistoryModule.kt | 5 +- .../comm/exceptions/CouldNotReadResponse.kt | 3 + .../pod/state/OmnipodDashPodStateManager.kt | 8 ++ .../state/OmnipodDashPodStateManagerImpl.kt | 78 ++++++++++++++----- .../pump/omnipod/dash/history/DashHistory.kt | 46 +++++------ 5 files changed, 96 insertions(+), 44 deletions(-) create mode 100644 omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotReadResponse.kt diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/dagger/OmnipodDashHistoryModule.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/dagger/OmnipodDashHistoryModule.kt index 1ac3c8dd3b..92c2368ee9 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/dagger/OmnipodDashHistoryModule.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/dagger/OmnipodDashHistoryModule.kt @@ -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) } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotReadResponse.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotReadResponse.kt new file mode 100644 index 0000000000..98d81b66cf --- /dev/null +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/exceptions/CouldNotReadResponse.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions + +class CouldNotReadResponse 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 bc5fdf060a..b44c82fde3 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 @@ -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 fun updateActiveCommand(): Maybe fun observeNoActiveCommand(): Observable + fun getCommandConfirmationFromState(): CommandConfirmationFromState data class ActiveCommand( val sequence: Short, 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 f100620760..9a3d3c7ceb 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 @@ -231,30 +231,70 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( @Synchronized override fun updateActiveCommand() = Maybe.create { 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 { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistory.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistory.kt index dea243e814..fc623d01f3 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistory.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/history/DashHistory.kt @@ -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> = dao.allSince(time) fun updateFromState(podState: OmnipodDashPodStateManager) = Completable.defer { - podState.activeCommand?.run { - when { - createdRealtime <= podState.lastStatusResponseReceived && - sequence == podState.sequenceNumberOfLastProgrammingCommand -> - 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")) - } - } ?: Completable.complete() // no active programming command + 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)) + NoActiveCommand -> + Completable.complete() + } } }