create fake TBR on suspend. Fix createHistory timing bug

This commit is contained in:
Andrei Vereha 2021-06-04 22:56:58 +02:00
parent 5d3b2faeb9
commit 33243a7ed4
5 changed files with 58 additions and 47 deletions

View file

@ -158,11 +158,10 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun setNewBasalProfile(profile: Profile): PumpEnactResult { override fun setNewBasalProfile(profile: Profile): PumpEnactResult {
return executeSimpleProgrammingCommand( return executeSimpleProgrammingCommand(
historyEntry = history.createRecord(
commandType = OmnipodCommandType.SET_BASAL_PROFILE
),
command = omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile)).ignoreElements(),
pre = suspendDeliveryIfActive(), pre = suspendDeliveryIfActive(),
historyEntry = history.createRecord(commandType = OmnipodCommandType.SET_BASAL_PROFILE),
command = omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile))
.ignoreElements(),
).toPumpEnactResult() ).toPumpEnactResult()
} }
@ -172,7 +171,17 @@ class OmnipodDashPumpPlugin @Inject constructor(
else else
executeSimpleProgrammingCommand( executeSimpleProgrammingCommand(
history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY), history.createRecord(OmnipodCommandType.SUSPEND_DELIVERY),
omnipodManager.suspendDelivery().ignoreElements() omnipodManager.suspendDelivery()
.filter { podEvent -> podEvent is PodEvent.CommandSent }
.map {
pumpSyncTempBasal(
it,
0.0,
PodConstants.MAX_POD_LIFETIME.standardMinutes,
PumpSync.TemporaryBasalType.PUMP_SUSPEND
)
}
.ignoreElements(),
) )
} }
@ -528,10 +537,12 @@ class OmnipodDashPumpPlugin @Inject constructor(
command = omnipodManager.suspendDelivery() command = omnipodManager.suspendDelivery()
.filter { podEvent -> podEvent is PodEvent.CommandSent } .filter { podEvent -> podEvent is PodEvent.CommandSent }
.map { .map {
pumpSyncTempBasal(it, pumpSyncTempBasal(
0.0, it,
PodConstants.MAX_POD_LIFETIME.standardMinutes, 0.0,
PumpSync.TemporaryBasalType.PUMP_SUSPEND) PodConstants.MAX_POD_LIFETIME.standardMinutes,
PumpSync.TemporaryBasalType.PUMP_SUSPEND
)
} }
.ignoreElements(), .ignoreElements(),
pre = observeDeliveryActive(), pre = observeDeliveryActive(),
@ -626,7 +637,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun handleCommandConfirmation(confirmation: CommandConfirmed) { private fun handleCommandConfirmation(confirmation: CommandConfirmed) {
val historyEntry = history.getById(confirmation.historyId) val historyEntry = history.getById(confirmation.historyId)
aapsLogger.debug(LTag.PUMPCOMM, "handling confirmation command: $confirmation") aapsLogger.debug(LTag.PUMPCOMM, "handling command confirmation: $confirmation")
when (historyEntry.commandType) { when (historyEntry.commandType) {
OmnipodCommandType.CANCEL_TEMPORARY_BASAL, OmnipodCommandType.CANCEL_TEMPORARY_BASAL,
OmnipodCommandType.SET_BASAL_PROFILE, OmnipodCommandType.SET_BASAL_PROFILE,

View file

@ -73,7 +73,7 @@ interface OmnipodDashPodStateManager {
fun updateFromPairing(uniqueId: Id, pairResult: PairResult) fun updateFromPairing(uniqueId: Id, pairResult: PairResult)
fun reset() fun reset()
fun createActiveCommand(historyId: String, basalProgram: BasalProgram?=null): Single<ActiveCommand> fun createActiveCommand(historyId: String, basalProgram: BasalProgram? = null): Single<ActiveCommand>
fun updateActiveCommand(): Maybe<CommandConfirmed> fun updateActiveCommand(): Maybe<CommandConfirmed>
fun observeNoActiveCommand(): Observable<PodEvent> fun observeNoActiveCommand(): Observable<PodEvent>
fun getCommandConfirmationFromState(): CommandConfirmationFromState fun getCommandConfirmationFromState(): CommandConfirmationFromState

View file

@ -190,27 +190,27 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
@Synchronized @Synchronized
override fun createActiveCommand(historyId: String, basalProgram: BasalProgram?): override fun createActiveCommand(historyId: String, basalProgram: BasalProgram?):
Single<OmnipodDashPodStateManager.ActiveCommand> { Single<OmnipodDashPodStateManager.ActiveCommand> {
return Single.create { source -> return Single.create { source ->
if (activeCommand == null) { if (activeCommand == null) {
val command = OmnipodDashPodStateManager.ActiveCommand( val command = OmnipodDashPodStateManager.ActiveCommand(
podState.messageSequenceNumber, podState.messageSequenceNumber,
createdRealtime = SystemClock.elapsedRealtime(), createdRealtime = SystemClock.elapsedRealtime(),
historyId = historyId, historyId = historyId,
sendError = null, sendError = null,
basalProgram = basalProgram, basalProgram = basalProgram,
)
podState.activeCommand = command
source.onSuccess(command)
} else {
source.onError(
java.lang.IllegalStateException(
"Trying to send a command " +
"and the last command was not confirmed"
) )
) podState.activeCommand = command
source.onSuccess(command)
} else {
source.onError(
java.lang.IllegalStateException(
"Trying to send a command " +
"and the last command was not confirmed"
)
)
}
} }
} }
}
@Synchronized @Synchronized
override fun observeNoActiveCommand(): Observable<PodEvent> { override fun observeNoActiveCommand(): Observable<PodEvent> {

View file

@ -55,29 +55,29 @@ class DashHistory @Inject constructor(
bolusRecord: BolusRecord? = null, bolusRecord: BolusRecord? = null,
resolveResult: ResolvedResult? = null, resolveResult: ResolvedResult? = null,
resolvedAt: Long? = null resolvedAt: Long? = null
): Single<String> { ): Single<String> = Single.defer {
val id = ULID.random() val id = ULID.random()
when { when {
commandType == SET_BOLUS && bolusRecord == null -> commandType == SET_BOLUS && bolusRecord == null ->
return Single.error(IllegalArgumentException("bolusRecord missing on SET_BOLUS")) Single.error(IllegalArgumentException("bolusRecord missing on SET_BOLUS"))
commandType == SET_TEMPORARY_BASAL && tempBasalRecord == null -> commandType == SET_TEMPORARY_BASAL && tempBasalRecord == null ->
return Single.error(IllegalArgumentException("tempBasalRecord missing on SET_TEMPORARY_BASAL")) Single.error(IllegalArgumentException("tempBasalRecord missing on SET_TEMPORARY_BASAL"))
else ->
dao.save(
HistoryRecordEntity(
id = id,
date = date,
createdAt = currentTimeMillis(),
commandType = commandType,
tempBasalRecord = tempBasalRecord,
bolusRecord = bolusRecord,
initialResult = initialResult,
resolvedResult = resolveResult,
resolvedAt = resolvedAt
)
).toSingle { id }
} }
return dao.save(
HistoryRecordEntity(
id = id,
date = date,
createdAt = currentTimeMillis(),
commandType = commandType,
tempBasalRecord = tempBasalRecord,
bolusRecord = bolusRecord,
initialResult = initialResult,
resolvedResult = resolveResult,
resolvedAt = resolvedAt
)
).toSingle { id }
} }
fun getRecords(): Single<List<HistoryRecord>> = fun getRecords(): Single<List<HistoryRecord>> =