Merge pull request #2549 from jbr7rr/dash-handle-tbr-out-of-sync

DASH: handle tbr out of sync
This commit is contained in:
Milos Kozak 2023-06-01 14:32:44 +02:00 committed by GitHub
commit cd9f5e4723
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 113 additions and 35 deletions

View file

@ -328,6 +328,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
podStateManager.updateActiveCommand()
.map { handleCommandConfirmation(it) }
.ignoreElement(),
verifyPumpState(),
checkPodKaput(),
)
)
@ -572,9 +573,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
.bolusDelivered(0.0)
.comment(rh.gs(R.string.omnipod_dash_not_enough_insulin))
}
if (podStateManager.deliveryStatus == DeliveryStatus.BOLUS_AND_BASAL_ACTIVE ||
podStateManager.deliveryStatus == DeliveryStatus.BOLUS_AND_TEMP_BASAL_ACTIVE
) {
if (podStateManager.deliveryStatus?.bolusDeliveringActive() == true) {
return PumpEnactResult(injector)
.success(false)
.enacted(false)
@ -632,14 +631,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
)
} else {
if (podStateManager.activeCommand != null) {
val sound =
if (sp.getBoolean(
info.nightscout.androidaps.plugins.pump.omnipod.common.R.string
.key_omnipod_common_notification_uncertain_bolus_sound_enabled, true
)
) info.nightscout.core.ui.R.raw.boluserror
else 0
val sound = if (hasBolusErrorBeepEnabled()) info.nightscout.core.ui.R.raw.boluserror else 0
showErrorDialog(rh.gs(R.string.bolus_delivery_status_uncertain), sound)
}
}
@ -714,7 +706,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
}
val percent = (waited.toFloat() / estimatedDeliveryTimeSeconds) * 100
updateBolusProgressDialog(
rh.gs(info.nightscout.pump.common.R.string.bolus_delivered_so_far, Round.roundTo(percent*requestedBolusAmount/100, PodConstants.POD_PULSE_BOLUS_UNITS), requestedBolusAmount),
rh.gs(info.nightscout.pump.common.R.string.bolus_delivered_so_far, Round.roundTo(percent * requestedBolusAmount / 100, PodConstants.POD_PULSE_BOLUS_UNITS), requestedBolusAmount),
percent.toInt()
)
}
@ -897,9 +889,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun observeNoActiveTempBasal(): Completable {
return Completable.defer {
if (podStateManager.deliveryStatus !in
arrayOf(DeliveryStatus.TEMP_BASAL_ACTIVE, DeliveryStatus.BOLUS_AND_TEMP_BASAL_ACTIVE)
) {
if (podStateManager.deliveryStatus?.tempBasalActive() == false) {
// TODO: what happens if we try to cancel nonexistent temp basal?
aapsLogger.info(LTag.PUMP, "No temporary basal to cancel")
Completable.complete()
@ -946,6 +936,13 @@ class OmnipodDashPumpPlugin @Inject constructor(
return sp.getBoolean(info.nightscout.androidaps.plugins.pump.omnipod.common.R.string.key_omnipod_common_basal_beeps_enabled, false)
}
private fun hasBolusErrorBeepEnabled(): Boolean {
return sp.getBoolean(
info.nightscout.androidaps.plugins.pump.omnipod.common.R.string
.key_omnipod_common_notification_uncertain_bolus_sound_enabled, true
)
}
override fun cancelTempBasal(enforceNew: Boolean): PumpEnactResult {
if (!podStateManager.tempBasalActive &&
pumpSync.expectedPumpState().temporaryBasal == null
@ -1323,6 +1320,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
podStateManager.updateActiveCommand()
.map { handleCommandConfirmation(it) }
.ignoreElement(),
verifyPumpState(),
checkPodKaput(),
refreshOverview(),
post,
@ -1488,6 +1486,40 @@ class OmnipodDashPumpPlugin @Inject constructor(
}
}
private fun verifyPumpState(): Completable = Completable.defer {
aapsLogger.debug(LTag.PUMP, "verifyPumpState, AAPS: ${pumpSync.expectedPumpState().temporaryBasal} Pump: ${podStateManager.deliveryStatus}")
val tbr = pumpSync.expectedPumpState().temporaryBasal
if (tbr != null && podStateManager.deliveryStatus?.basalActive() == true) {
aapsLogger.error(LTag.PUMP, "AAPS expected a TBR running but pump has no TBR running! AAPS: ${pumpSync.expectedPumpState().temporaryBasal} Pump: ${podStateManager.deliveryStatus}")
// Alert user
val sound = if (hasBolusErrorBeepEnabled()) info.nightscout.core.ui.R.raw.boluserror else 0
showErrorDialog(rh.gs(R.string.temp_basal_out_of_sync), sound)
// Sync stopped basal with AAPS
val ret = pumpSync.syncStopTemporaryBasalWithPumpId(
System.currentTimeMillis(), // Note: It would be nice if TBR end could be estimated, but this will add a lot of complexity
tbr.id,
PumpType.OMNIPOD_DASH,
serialNumber()
)
aapsLogger.info(LTag.PUMP, "syncStopTemporaryBasalWithPumpId ret=$ret pumpId=${tbr.id}")
podStateManager.tempBasal = null
} else if (tbr == null && podStateManager.deliveryStatus?.tempBasalActive() == true) {
aapsLogger.error(LTag.PUMP, "AAPS expected no TBR running but pump has a TBR running! AAPS: ${pumpSync.expectedPumpState().temporaryBasal} Pump: ${podStateManager.deliveryStatus}")
// Alert user
val sound = if (hasBolusErrorBeepEnabled()) info.nightscout.core.ui.R.raw.boluserror else 0
showErrorDialog(rh.gs(R.string.temp_basal_out_of_sync), sound)
// If this is reached is reached there is probably a something wrong with the time (maybe it has changed?).
// No way to calculate the TBR end time and update pumpSync properly.
// Cancel TBR running on Pump
return@defer observeNoActiveTempBasal()
.concatWith(podStateManager.updateActiveCommand()
.map { handleCommandConfirmation(it) }
.ignoreElement())
}
return@defer Completable.complete()
}
private fun showErrorDialog(message: String, sound: Int) {
uiInteraction.runAlarm(message, rh.gs(info.nightscout.core.ui.R.string.error), sound)
}

View file

@ -16,6 +16,10 @@ enum class DeliveryStatus(override val value: Byte) : HasValue {
return value in arrayOf(BOLUS_AND_BASAL_ACTIVE.value, BOLUS_AND_TEMP_BASAL_ACTIVE.value)
}
fun basalActive(): Boolean {
return value in arrayOf(BOLUS_AND_BASAL_ACTIVE.value, BASAL_ACTIVE.value)
}
fun tempBasalActive(): Boolean {
return value in arrayOf(BOLUS_AND_TEMP_BASAL_ACTIVE.value, TEMP_BASAL_ACTIVE.value)
}

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.history
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.definition.PodConstants.Companion.POD_PULSE_BOLUS_UNITS
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandConfirmationDenied
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandConfirmationSuccess
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandSendingFailure
@ -57,6 +58,7 @@ class DashHistory @Inject constructor(
tempBasalRecord: TempBasalRecord? = null,
bolusRecord: BolusRecord? = null,
basalProfileRecord: BasalValuesRecord? = null,
totalAmountDeliveredRecord: Double? = null,
resolveResult: ResolvedResult? = null,
resolvedAt: Long? = null
): Single<Long> = Single.defer {
@ -79,6 +81,7 @@ class DashHistory @Inject constructor(
tempBasalRecord = tempBasalRecord,
bolusRecord = bolusRecord,
basalProfileRecord = basalProfileRecord,
totalAmountDelivered = totalAmountDeliveredRecord,
initialResult = initialResult,
resolvedResult = resolveResult,
resolvedAt = resolvedAt
@ -99,18 +102,23 @@ class DashHistory @Inject constructor(
logger.error(LTag.PUMP, "HistoryId not found to for updating from state")
return@defer Completable.complete()
}
when (podState.getCommandConfirmationFromState()) {
CommandSendingFailure ->
val setTotalAmountDelivered = dao.setTotalAmountDelivered(historyId, podState.pulsesDelivered?.times(POD_PULSE_BOLUS_UNITS))
val commandConfirmation = when (podState.getCommandConfirmationFromState()) {
CommandSendingFailure ->
dao.setInitialResult(historyId, InitialResult.FAILURE_SENDING)
CommandSendingNotConfirmed ->
dao.setInitialResult(historyId, InitialResult.SENT)
CommandConfirmationDenied ->
CommandConfirmationDenied ->
markFailure(historyId)
CommandConfirmationSuccess ->
dao.setInitialResult(historyId, InitialResult.SENT)
.andThen(markSuccess(historyId))
NoActiveCommand ->
NoActiveCommand ->
Completable.complete()
}
Completable.concat(listOf(setTotalAmountDelivered, commandConfirmation))
}
}

View file

@ -9,6 +9,7 @@ data class HistoryRecord(
val commandType: OmnipodCommandType,
val initialResult: InitialResult,
val record: Record?,
val totalAmountDelivered: Double?,
val resolvedResult: ResolvedResult?,
val resolvedAt: Long?
) {

View file

@ -18,7 +18,7 @@ abstract class DashHistoryDatabase : RoomDatabase() {
companion object {
const val VERSION = 3
const val VERSION = 4
fun build(context: Context) =
Room.databaseBuilder(

View file

@ -36,4 +36,7 @@ abstract class HistoryRecordDao {
@Query("UPDATE historyrecords SET initialResult = :initialResult WHERE id = :id ")
abstract fun setInitialResult(id: Long, initialResult: InitialResult): Completable
@Query("UPDATE historyrecords SET totalAmountDelivered = :totalAmountDelivered WHERE id = :id ")
abstract fun setTotalAmountDelivered(id: Long, totalAmountDelivered: Double?): Completable
}

View file

@ -27,6 +27,7 @@ data class HistoryRecordEntity(
@Embedded(prefix = "tempBasalRecord_") val tempBasalRecord: TempBasalRecord?,
@Embedded(prefix = "bolusRecord_") val bolusRecord: BolusRecord?,
@Embedded(prefix = "basalprofile_") val basalProfileRecord: BasalValuesRecord?,
val totalAmountDelivered: Double?,
val resolvedResult: ResolvedResult?,
val resolvedAt: Long?
)

View file

@ -13,6 +13,7 @@ class HistoryMapper {
initialResult = entity.initialResult,
commandType = entity.commandType,
record = entity.bolusRecord ?: entity.tempBasalRecord ?: entity.basalProfileRecord,
totalAmountDelivered = entity.totalAmountDelivered,
resolvedResult = entity.resolvedResult,
resolvedAt = entity.resolvedAt
)

View file

@ -213,6 +213,7 @@ class DashPodHistoryActivity : TranslatedDaggerAppCompatActivity() {
holder.timeView.text = DateTimeUtil.toStringFromTimeInMillis(it.displayTimestamp())
setValue(it, holder.valueView)
setType(it, holder.typeView)
setAmount(it, holder.amountView)
}
}
@ -297,6 +298,12 @@ class DashPodHistoryActivity : TranslatedDaggerAppCompatActivity() {
setTextViewColor(check_result = false, valueView, historyEntry)
}
private fun setAmount(historyEntry: HistoryRecord, amountView: TextView) {
amountView.text = historyEntry.totalAmountDelivered?.let { rh.gs(R.string.omnipod_common_history_total_delivered, it) }
// Set some color
setTextViewColor(check_result = false, amountView, historyEntry)
}
override fun getItemCount(): Int {
return historyList.size
}
@ -306,6 +313,7 @@ class DashPodHistoryActivity : TranslatedDaggerAppCompatActivity() {
val timeView: TextView = itemView.findViewById(R.id.omnipod_history_time)
val typeView: TextView = itemView.findViewById(R.id.omnipod_history_source)
val valueView: TextView = itemView.findViewById(R.id.omnipod_history_description)
val amountView: TextView = itemView.findViewById(R.id.omnipod_history_amount)
}
}

View file

@ -14,18 +14,36 @@
android:text="@string/omnipod_dash_history_item_date"
android:textSize="12sp" />
<TextView
android:id="@+id/omnipod_history_source"
android:layout_width="132dp"
android:layout_height="match_parent"
android:text="@string/omnipod_dash_history_item_source"
android:textSize="12sp" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/omnipod_history_description"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="@string/omnipod_dash_history_item_description"
android:textSize="12sp" />
<TextView
android:id="@+id/omnipod_history_source"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:text="@string/omnipod_dash_history_item_source"
android:layout_alignParentTop="true"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:id="@+id/omnipod_history_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/omnipod_dash_history_item_description"
android:layout_toEndOf="@id/omnipod_history_source"
android:textSize="12sp" />
<TextView
android:id="@+id/omnipod_history_amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/omnipod_dash_history_item_amount"
android:layout_below="@+id/omnipod_history_description"
android:layout_toEndOf="@id/omnipod_history_source"
android:textSize="12sp" />
</RelativeLayout>
</LinearLayout>

View file

@ -11,12 +11,12 @@
<!-- Omnipod Dash - History -->
<string name="omnipod_dash_history_title">Pod History</string>
<string name="omnipod_dash_history_item_description">Description</string>
<string name="omnipod_dash_history_item_amount">Amount</string>
<string name="omnipod_dash_history_item_source">Source</string>
<string name="omnipod_dash_history_item_date">Date</string>
<string name="omnipod_dash_history_type">Type:</string>
<string name="omnipod_dash_history_bolus_value">%1$.2f U</string>
<string name="omnipod_dash_history_bolus_value_with_carbs">%1$.2f U, CH=%2$.1f g</string>
<string name="omnipod_dash_history_tbr_value">Rate: %1$.2f U, duration: %2$d minutes</string>
<!-- Omnipod Dash - Overview -->
<string name="omnipod_dash_overview_bluetooth_status">Bluetooth Status</string>
@ -44,9 +44,10 @@
<string name="omnipod_dash_command_not_sent">Command not sent</string>
<string name="omnipod_dash_command_not_received_by_the_pod">Command not received by the pod</string>
<string name="omnipod_dash_unknown">Unknown state for the command</string>
<string name="omnipod_common_history_tbr_value">Rate: %1$.2f U, duration: %2$d minutes</string>
<string name="omnipod_common_history_tbr_value">%1$.2f U/h, %2$d minutes</string>
<string name="omnipod_common_history_bolus_value">%1$.2f U</string>
<string name="omnipod_common_alert_delivery_suspended">Insulin delivery is suspended</string>
<string name="omnipod_common_history_total_delivered">Total delivered: %1$.2f U</string>
<string name="omnipod_dash_connection_lost">Lost connection to pod</string>
<string name="omnipod_dash_bolus_already_in_progress">Another bolus is being delivered</string>
<string name="omnipod_dash_not_enough_insulin">Not enough insulin left in the reservoir</string>
@ -60,6 +61,7 @@
<string name="failed_to_set_the_new_basal_profile">Failed to set the new basal profile. Delivery suspended</string>
<string name="setting_basal_profile_might_have_failed">Setting basal profile might have failed. Delivery might be suspended! Please manually refresh the Pod status from the Omnipod tab and resume delivery if needed.</string>
<string name="bolus_delivery_status_uncertain">Bolus delivery status uncertain. Refresh pod status to confirm or deny.</string>
<string name="temp_basal_out_of_sync">Temp basal status not as expected! If a temp basal was previously running, it has been cancelled. Please check delivered insulin and pod history</string>
<string name="checking_delivery_status">Checking delivery status</string>
<string name="setting_temp_basal_might_have_basal_failed">Setting temp basal might have basal failed. If a temp basal was previously running, it has been cancelled. Please manually refresh the Pod status from the Omnipod tab.</string>
<string name="cancel_temp_basal_result_is_uncertain">Cancel temp basal result is uncertain</string>