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 {
return executeSimpleProgrammingCommand(
historyEntry = history.createRecord(
commandType = OmnipodCommandType.SET_BASAL_PROFILE
),
command = omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile)).ignoreElements(),
pre = suspendDeliveryIfActive(),
historyEntry = history.createRecord(commandType = OmnipodCommandType.SET_BASAL_PROFILE),
command = omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile))
.ignoreElements(),
).toPumpEnactResult()
}
@ -172,7 +171,17 @@ class OmnipodDashPumpPlugin @Inject constructor(
else
executeSimpleProgrammingCommand(
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()
.filter { podEvent -> podEvent is PodEvent.CommandSent }
.map {
pumpSyncTempBasal(it,
0.0,
PodConstants.MAX_POD_LIFETIME.standardMinutes,
PumpSync.TemporaryBasalType.PUMP_SUSPEND)
pumpSyncTempBasal(
it,
0.0,
PodConstants.MAX_POD_LIFETIME.standardMinutes,
PumpSync.TemporaryBasalType.PUMP_SUSPEND
)
}
.ignoreElements(),
pre = observeDeliveryActive(),
@ -626,7 +637,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun handleCommandConfirmation(confirmation: CommandConfirmed) {
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) {
OmnipodCommandType.CANCEL_TEMPORARY_BASAL,
OmnipodCommandType.SET_BASAL_PROFILE,

View file

@ -6,4 +6,4 @@ class PodConstants {
companion object {
val MAX_POD_LIFETIME = Duration.standardHours(80)
}
}
}

View file

@ -73,7 +73,7 @@ interface OmnipodDashPodStateManager {
fun updateFromPairing(uniqueId: Id, pairResult: PairResult)
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 observeNoActiveCommand(): Observable<PodEvent>
fun getCommandConfirmationFromState(): CommandConfirmationFromState

View file

@ -190,27 +190,27 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
@Synchronized
override fun createActiveCommand(historyId: String, basalProgram: BasalProgram?):
Single<OmnipodDashPodStateManager.ActiveCommand> {
return Single.create { source ->
if (activeCommand == null) {
val command = OmnipodDashPodStateManager.ActiveCommand(
podState.messageSequenceNumber,
createdRealtime = SystemClock.elapsedRealtime(),
historyId = historyId,
sendError = null,
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"
return Single.create { source ->
if (activeCommand == null) {
val command = OmnipodDashPodStateManager.ActiveCommand(
podState.messageSequenceNumber,
createdRealtime = SystemClock.elapsedRealtime(),
historyId = historyId,
sendError = null,
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"
)
)
}
}
}
}
@Synchronized
override fun observeNoActiveCommand(): Observable<PodEvent> {

View file

@ -55,29 +55,29 @@ class DashHistory @Inject constructor(
bolusRecord: BolusRecord? = null,
resolveResult: ResolvedResult? = null,
resolvedAt: Long? = null
): Single<String> {
): Single<String> = Single.defer {
val id = ULID.random()
when {
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 ->
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>> =