This commit is contained in:
Andrei Vereha 2021-05-31 22:09:49 +02:00
parent 9d61c8ee8a
commit a20cc1cca3
2 changed files with 38 additions and 40 deletions

View file

@ -36,8 +36,6 @@ import io.reactivex.Single
import io.reactivex.rxkotlin.blockingSubscribeBy import io.reactivex.rxkotlin.blockingSubscribeBy
import io.reactivex.rxkotlin.subscribeBy import io.reactivex.rxkotlin.subscribeBy
import org.json.JSONObject import org.json.JSONObject
import java.lang.IllegalArgumentException
import java.lang.IllegalStateException
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -161,7 +159,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
history.createRecord( history.createRecord(
commandType = OmnipodCommandType.SET_BASAL_PROFILE commandType = OmnipodCommandType.SET_BASAL_PROFILE
), ),
omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile)) omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile)).ignoreElements()
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -248,7 +246,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
// TODO update Treatments (?) // TODO update Treatments (?)
executeSimpleProgrammingCommand( executeSimpleProgrammingCommand(
history.createRecord(OmnipodCommandType.CANCEL_BOLUS), history.createRecord(OmnipodCommandType.CANCEL_BOLUS),
omnipodManager.stopBolus(), omnipodManager.stopBolus().ignoreElements()
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -261,34 +259,35 @@ class OmnipodDashPumpPlugin @Inject constructor(
): PumpEnactResult { ): PumpEnactResult {
val tempBasalBeeps = sp.getBoolean(R.string.key_omnipod_common_tbr_beeps_enabled, false) val tempBasalBeeps = sp.getBoolean(R.string.key_omnipod_common_tbr_beeps_enabled, false)
return Completable.concat( return executeSimpleProgrammingCommand(
listOf( historyEntry = history.createRecord(
observeNoActiveTempBasal(enforceNew), commandType = OmnipodCommandType.SET_TEMPORARY_BASAL,
podStateManager.observeNoActiveCommand().ignoreElements(), tempBasalRecord = TempBasalRecord(duration = durationInMinutes, rate = absoluteRate)
history.createRecord( ),
commandType = OmnipodCommandType.SET_TEMPORARY_BASAL, command = omnipodManager.setTempBasal(
tempBasalRecord = TempBasalRecord(duration = durationInMinutes, rate = absoluteRate) absoluteRate,
).flatMap { podStateManager.createActiveCommand(it) } durationInMinutes.toShort(),
.map { pumpSyncTempBasal(it, tbrType) } tempBasalBeeps
.ignoreElement(),
omnipodManager.setTempBasal(
absoluteRate,
durationInMinutes.toShort(),
tempBasalBeeps,
).ignoreElements(),
history.updateFromState(podStateManager),
podStateManager.updateActiveCommand()
.map { handleCommandConfirmation(it) }
.ignoreElement()
) )
.filter { podEvent -> podEvent is PodEvent.CommandSent }
.map { pumpSyncTempBasal(it, tbrType) }
.ignoreElements(),
pre = observeNoActiveTempBasal(enforceNew)
).toPumpEnactResult() ).toPumpEnactResult()
// TODO: on error, invalidateTemporaryBasal or sync it only after sending the command
} }
private fun pumpSyncTempBasal( private fun pumpSyncTempBasal(
activeCommand: OmnipodDashPodStateManager.ActiveCommand, podEvent: PodEvent,
tbrType: PumpSync.TemporaryBasalType tbrType: PumpSync.TemporaryBasalType
): Boolean { ): Boolean {
val activeCommand = podStateManager.activeCommand
if (activeCommand == null || podEvent !is PodEvent.CommandSent) {
throw IllegalArgumentException(
"No active command or illegal podEvent: " +
"activeCommand=$activeCommand" +
"podEvent=$podEvent"
)
}
val historyEntry = history.getById(activeCommand.historyId) val historyEntry = history.getById(activeCommand.historyId)
val record = historyEntry.record val record = historyEntry.record
if (record == null || !(record is TempBasalRecord)) { if (record == null || !(record is TempBasalRecord)) {
@ -324,7 +323,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
// TODO: chain with the completable? // TODO: chain with the completable?
executeSimpleProgrammingCommand( executeSimpleProgrammingCommand(
history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL), history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL),
omnipodManager.stopTempBasal() omnipodManager.stopTempBasal().ignoreElements()
) )
} }
} }
@ -363,8 +362,8 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun cancelTempBasal(enforceNew: Boolean): PumpEnactResult { override fun cancelTempBasal(enforceNew: Boolean): PumpEnactResult {
return executeSimpleProgrammingCommand( return executeSimpleProgrammingCommand(
observeCreateHistoryEntry = history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL), historyEntry = history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL),
command = omnipodManager.stopTempBasal(), command = omnipodManager.stopTempBasal().ignoreElements(),
pre = observeActiveTempBasal(), pre = observeActiveTempBasal(),
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -521,7 +520,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun suspendDelivery(): PumpEnactResult { private fun suspendDelivery(): PumpEnactResult {
return executeSimpleProgrammingCommand( return executeSimpleProgrammingCommand(
history.createRecord(OmnipodCommandType.RESUME_DELIVERY), history.createRecord(OmnipodCommandType.RESUME_DELIVERY),
omnipodManager.suspendDelivery() omnipodManager.suspendDelivery().ignoreElements()
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -529,7 +528,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
return profileFunction.getProfile()?.let { return profileFunction.getProfile()?.let {
executeSimpleProgrammingCommand( executeSimpleProgrammingCommand(
history.createRecord(OmnipodCommandType.RESUME_DELIVERY), history.createRecord(OmnipodCommandType.RESUME_DELIVERY),
omnipodManager.setBasalProgram(mapProfileToBasalProgram(it)) omnipodManager.setBasalProgram(mapProfileToBasalProgram(it)).ignoreElements()
).toPumpEnactResult() ).toPumpEnactResult()
} ?: PumpEnactResult(injector).success(false).enacted(false).comment("No profile active") // TODO i18n } ?: PumpEnactResult(injector).success(false).enacted(false).comment("No profile active") // TODO i18n
} }
@ -537,7 +536,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun deactivatePod(): PumpEnactResult { private fun deactivatePod(): PumpEnactResult {
return executeSimpleProgrammingCommand( return executeSimpleProgrammingCommand(
history.createRecord(OmnipodCommandType.DEACTIVATE_POD), history.createRecord(OmnipodCommandType.DEACTIVATE_POD),
omnipodManager.deactivatePod() omnipodManager.deactivatePod().ignoreElements()
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -554,7 +553,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun playTestBeep(): PumpEnactResult { private fun playTestBeep(): PumpEnactResult {
return executeSimpleProgrammingCommand( return executeSimpleProgrammingCommand(
history.createRecord(OmnipodCommandType.PLAY_TEST_BEEP), history.createRecord(OmnipodCommandType.PLAY_TEST_BEEP),
omnipodManager.playBeep(BeepType.LONG_SINGLE_BEEP) omnipodManager.playBeep(BeepType.LONG_SINGLE_BEEP).ignoreElements()
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -580,26 +579,25 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
private fun executeSimpleProgrammingCommand( private fun executeSimpleProgrammingCommand(
observeCreateHistoryEntry: Single<String>, historyEntry: Single<String>,
command: Observable<PodEvent>, command: Completable,
pre: Completable = Completable.complete(), pre: Completable = Completable.complete(),
): Completable { ): Completable {
return Completable.concat( return Completable.concat(
listOf( listOf(
pre, pre,
podStateManager.observeNoActiveCommand().ignoreElements(), podStateManager.observeNoActiveCommand().ignoreElements(),
observeCreateHistoryEntry historyEntry
.flatMap { podStateManager.createActiveCommand(it) } .flatMap { podStateManager.createActiveCommand(it) }
.ignoreElement(), .ignoreElement(),
command.ignoreElements(), command.doOnError {
aapsLogger.error(LTag.PUMP, "Error executing command", it)
}.onErrorComplete(),
history.updateFromState(podStateManager), history.updateFromState(podStateManager),
podStateManager.updateActiveCommand() podStateManager.updateActiveCommand()
.map { handleCommandConfirmation(it) } .map { handleCommandConfirmation(it) }
.ignoreElement() .ignoreElement()
) )
).doOnError { error -> )
aapsLogger.error(LTag.PUMP, "Error executing command", error)
podStateManager.maybeMarkActiveCommandFailed()
}
} }
} }