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() podStateManager.updateActiveCommand()
.map { handleCommandConfirmation(it) } .map { handleCommandConfirmation(it) }
.ignoreElement(), .ignoreElement(),
verifyPumpState(),
checkPodKaput(), checkPodKaput(),
) )
) )
@ -572,9 +573,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
.bolusDelivered(0.0) .bolusDelivered(0.0)
.comment(rh.gs(R.string.omnipod_dash_not_enough_insulin)) .comment(rh.gs(R.string.omnipod_dash_not_enough_insulin))
} }
if (podStateManager.deliveryStatus == DeliveryStatus.BOLUS_AND_BASAL_ACTIVE || if (podStateManager.deliveryStatus?.bolusDeliveringActive() == true) {
podStateManager.deliveryStatus == DeliveryStatus.BOLUS_AND_TEMP_BASAL_ACTIVE
) {
return PumpEnactResult(injector) return PumpEnactResult(injector)
.success(false) .success(false)
.enacted(false) .enacted(false)
@ -632,14 +631,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
) )
} else { } else {
if (podStateManager.activeCommand != null) { if (podStateManager.activeCommand != null) {
val sound = val sound = if (hasBolusErrorBeepEnabled()) info.nightscout.core.ui.R.raw.boluserror else 0
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
showErrorDialog(rh.gs(R.string.bolus_delivery_status_uncertain), sound) showErrorDialog(rh.gs(R.string.bolus_delivery_status_uncertain), sound)
} }
} }
@ -714,7 +706,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
val percent = (waited.toFloat() / estimatedDeliveryTimeSeconds) * 100 val percent = (waited.toFloat() / estimatedDeliveryTimeSeconds) * 100
updateBolusProgressDialog( 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() percent.toInt()
) )
} }
@ -897,9 +889,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun observeNoActiveTempBasal(): Completable { private fun observeNoActiveTempBasal(): Completable {
return Completable.defer { return Completable.defer {
if (podStateManager.deliveryStatus !in if (podStateManager.deliveryStatus?.tempBasalActive() == false) {
arrayOf(DeliveryStatus.TEMP_BASAL_ACTIVE, DeliveryStatus.BOLUS_AND_TEMP_BASAL_ACTIVE)
) {
// TODO: what happens if we try to cancel nonexistent temp basal? // TODO: what happens if we try to cancel nonexistent temp basal?
aapsLogger.info(LTag.PUMP, "No temporary basal to cancel") aapsLogger.info(LTag.PUMP, "No temporary basal to cancel")
Completable.complete() 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) 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 { override fun cancelTempBasal(enforceNew: Boolean): PumpEnactResult {
if (!podStateManager.tempBasalActive && if (!podStateManager.tempBasalActive &&
pumpSync.expectedPumpState().temporaryBasal == null pumpSync.expectedPumpState().temporaryBasal == null
@ -1323,6 +1320,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
podStateManager.updateActiveCommand() podStateManager.updateActiveCommand()
.map { handleCommandConfirmation(it) } .map { handleCommandConfirmation(it) }
.ignoreElement(), .ignoreElement(),
verifyPumpState(),
checkPodKaput(), checkPodKaput(),
refreshOverview(), refreshOverview(),
post, 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) { private fun showErrorDialog(message: String, sound: Int) {
uiInteraction.runAlarm(message, rh.gs(info.nightscout.core.ui.R.string.error), sound) 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) 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 { fun tempBasalActive(): Boolean {
return value in arrayOf(BOLUS_AND_TEMP_BASAL_ACTIVE.value, TEMP_BASAL_ACTIVE.value) 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
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_BOLUS 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.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.CommandConfirmationDenied
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandConfirmationSuccess import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandConfirmationSuccess
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandSendingFailure import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandSendingFailure
@ -57,6 +58,7 @@ class DashHistory @Inject constructor(
tempBasalRecord: TempBasalRecord? = null, tempBasalRecord: TempBasalRecord? = null,
bolusRecord: BolusRecord? = null, bolusRecord: BolusRecord? = null,
basalProfileRecord: BasalValuesRecord? = null, basalProfileRecord: BasalValuesRecord? = null,
totalAmountDeliveredRecord: Double? = null,
resolveResult: ResolvedResult? = null, resolveResult: ResolvedResult? = null,
resolvedAt: Long? = null resolvedAt: Long? = null
): Single<Long> = Single.defer { ): Single<Long> = Single.defer {
@ -79,6 +81,7 @@ class DashHistory @Inject constructor(
tempBasalRecord = tempBasalRecord, tempBasalRecord = tempBasalRecord,
bolusRecord = bolusRecord, bolusRecord = bolusRecord,
basalProfileRecord = basalProfileRecord, basalProfileRecord = basalProfileRecord,
totalAmountDelivered = totalAmountDeliveredRecord,
initialResult = initialResult, initialResult = initialResult,
resolvedResult = resolveResult, resolvedResult = resolveResult,
resolvedAt = resolvedAt resolvedAt = resolvedAt
@ -99,18 +102,23 @@ class DashHistory @Inject constructor(
logger.error(LTag.PUMP, "HistoryId not found to for updating from state") logger.error(LTag.PUMP, "HistoryId not found to for updating from state")
return@defer Completable.complete() 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) dao.setInitialResult(historyId, InitialResult.FAILURE_SENDING)
CommandSendingNotConfirmed -> CommandSendingNotConfirmed ->
dao.setInitialResult(historyId, InitialResult.SENT) dao.setInitialResult(historyId, InitialResult.SENT)
CommandConfirmationDenied -> CommandConfirmationDenied ->
markFailure(historyId) markFailure(historyId)
CommandConfirmationSuccess -> CommandConfirmationSuccess ->
dao.setInitialResult(historyId, InitialResult.SENT) dao.setInitialResult(historyId, InitialResult.SENT)
.andThen(markSuccess(historyId)) .andThen(markSuccess(historyId))
NoActiveCommand -> NoActiveCommand ->
Completable.complete() Completable.complete()
} }
Completable.concat(listOf(setTotalAmountDelivered, commandConfirmation))
} }
} }

View file

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

View file

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

View file

@ -36,4 +36,7 @@ abstract class HistoryRecordDao {
@Query("UPDATE historyrecords SET initialResult = :initialResult WHERE id = :id ") @Query("UPDATE historyrecords SET initialResult = :initialResult WHERE id = :id ")
abstract fun setInitialResult(id: Long, initialResult: InitialResult): Completable 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 = "tempBasalRecord_") val tempBasalRecord: TempBasalRecord?,
@Embedded(prefix = "bolusRecord_") val bolusRecord: BolusRecord?, @Embedded(prefix = "bolusRecord_") val bolusRecord: BolusRecord?,
@Embedded(prefix = "basalprofile_") val basalProfileRecord: BasalValuesRecord?, @Embedded(prefix = "basalprofile_") val basalProfileRecord: BasalValuesRecord?,
val totalAmountDelivered: Double?,
val resolvedResult: ResolvedResult?, val resolvedResult: ResolvedResult?,
val resolvedAt: Long? val resolvedAt: Long?
) )

View file

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

View file

@ -213,6 +213,7 @@ class DashPodHistoryActivity : TranslatedDaggerAppCompatActivity() {
holder.timeView.text = DateTimeUtil.toStringFromTimeInMillis(it.displayTimestamp()) holder.timeView.text = DateTimeUtil.toStringFromTimeInMillis(it.displayTimestamp())
setValue(it, holder.valueView) setValue(it, holder.valueView)
setType(it, holder.typeView) setType(it, holder.typeView)
setAmount(it, holder.amountView)
} }
} }
@ -297,6 +298,12 @@ class DashPodHistoryActivity : TranslatedDaggerAppCompatActivity() {
setTextViewColor(check_result = false, valueView, historyEntry) 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 { override fun getItemCount(): Int {
return historyList.size return historyList.size
} }
@ -306,6 +313,7 @@ class DashPodHistoryActivity : TranslatedDaggerAppCompatActivity() {
val timeView: TextView = itemView.findViewById(R.id.omnipod_history_time) val timeView: TextView = itemView.findViewById(R.id.omnipod_history_time)
val typeView: TextView = itemView.findViewById(R.id.omnipod_history_source) val typeView: TextView = itemView.findViewById(R.id.omnipod_history_source)
val valueView: TextView = itemView.findViewById(R.id.omnipod_history_description) 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:text="@string/omnipod_dash_history_item_date"
android:textSize="12sp" /> android:textSize="12sp" />
<TextView <RelativeLayout
android:id="@+id/omnipod_history_source" android:layout_width="0dp"
android:layout_width="132dp" android:layout_height="wrap_content"
android:layout_height="match_parent" android:layout_weight="1">
android:text="@string/omnipod_dash_history_item_source"
android:textSize="12sp" />
<TextView <TextView
android:id="@+id/omnipod_history_description" android:id="@+id/omnipod_history_source"
android:layout_width="fill_parent" android:layout_width="132dp"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:text="@string/omnipod_dash_history_item_description" android:text="@string/omnipod_dash_history_item_source"
android:textSize="12sp" /> android:layout_alignParentTop="true"
android:textSize="12sp" />
<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> </LinearLayout>

View file

@ -11,12 +11,12 @@
<!-- Omnipod Dash - History --> <!-- Omnipod Dash - History -->
<string name="omnipod_dash_history_title">Pod History</string> <string name="omnipod_dash_history_title">Pod History</string>
<string name="omnipod_dash_history_item_description">Description</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_source">Source</string>
<string name="omnipod_dash_history_item_date">Date</string> <string name="omnipod_dash_history_item_date">Date</string>
<string name="omnipod_dash_history_type">Type:</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">%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_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 --> <!-- Omnipod Dash - Overview -->
<string name="omnipod_dash_overview_bluetooth_status">Bluetooth Status</string> <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_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_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_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_history_bolus_value">%1$.2f U</string>
<string name="omnipod_common_alert_delivery_suspended">Insulin delivery is suspended</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_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_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> <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="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="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="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="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="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> <string name="cancel_temp_basal_result_is_uncertain">Cancel temp basal result is uncertain</string>