From 85178ee19ff628dfe4422409973c6f8ae7b511b5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 11 Nov 2021 11:04:24 +0100 Subject: [PATCH] End TBR before connectNewPump --- .../androidaps/interfaces/PumpSync.kt | 32 +++++++++++++++-- .../configBuilder/RunningConfiguration.kt | 2 +- .../plugins/pump/PumpSyncImplementation.kt | 20 ++++++++--- .../plugins/pump/common/defs/PumpType.kt | 35 +++++++++++++++++++ .../database/embedments/InterfaceIDs.kt | 2 -- 5 files changed, 81 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/PumpSync.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/PumpSync.kt index cc14666dac..f7a6acb102 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/PumpSync.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/PumpSync.kt @@ -27,8 +27,13 @@ interface PumpSync { * * Call this function when new pump is paired to accept data from new pump * to prevent overlapping pump histories + * @param endRunning if true end previous running TBR and EB */ - fun connectNewPump() + + // @JvmOverloads and default value impossible on interface + // replace by `fun connectNewPump(endRunning: Boolean = true)` after full conversion to kotlin + fun connectNewPump(endRunning: Boolean) + fun connectNewPump() = connectNewPump(true) /* * GENERAL STATUS @@ -55,8 +60,29 @@ interface PumpSync { */ data class PumpState(val temporaryBasal: TemporaryBasal?, val extendedBolus: ExtendedBolus?, val bolus: Bolus?, val profile: Profile?) { - data class TemporaryBasal(val timestamp: Long, val duration: Long, val rate: Double, val isAbsolute: Boolean, val type: TemporaryBasalType, val id: Long, val pumpId: Long?) - data class ExtendedBolus(val timestamp: Long, val duration: Long, val amount: Double, val rate: Double) + data class TemporaryBasal @JvmOverloads constructor( + val timestamp: Long, + val duration: Long, + val rate: Double, + val isAbsolute: Boolean, + val type: TemporaryBasalType, + val id: Long, + val pumpId: Long?, + // used only to cancel TBR on pump change + val pumpType: PumpType = PumpType.USER, + val pumpSerial: String = "" + ) + + data class ExtendedBolus @JvmOverloads constructor( + val timestamp: Long, + val duration: Long, + val amount: Double, + val rate: Double, + // used only to cancel EB on pump change + val pumpType: PumpType = PumpType.USER, + val pumpSerial: String = "" + ) + data class Bolus(val timestamp: Long, val amount: Double) } diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/configBuilder/RunningConfiguration.kt b/core/src/main/java/info/nightscout/androidaps/plugins/configBuilder/RunningConfiguration.kt index 2e5b3871b5..b255a7e327 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/configBuilder/RunningConfiguration.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/configBuilder/RunningConfiguration.kt @@ -100,7 +100,7 @@ class RunningConfiguration @Inject constructor( if (sp.getString(R.string.key_virtualpump_type, "fake") != pumpType) { sp.putString(R.string.key_virtualpump_type, pumpType) activePlugin.activePump.pumpDescription.fillFor(PumpType.getByDescription(pumpType)) - pumpSync.connectNewPump() + pumpSync.connectNewPump(endRunning = false) // do not end running TBRs, we call this only to accept data properly aapsLogger.debug(LTag.CORE, "Changing pump type to $pumpType") } } diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt b/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt index 7cf4d09b88..66b7112b96 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt @@ -37,7 +37,15 @@ class PumpSyncImplementation @Inject constructor( private val disposable = CompositeDisposable() - override fun connectNewPump() { + override fun connectNewPump(endRunning: Boolean) { + if (endRunning) { + expectedPumpState().temporaryBasal?.let { + syncStopTemporaryBasalWithPumpId(dateUtil.now(), dateUtil.now(), it.pumpType, it.pumpSerial) + } + expectedPumpState().extendedBolus?.let { + syncStopExtendedBolusWithPumpId(dateUtil.now(), dateUtil.now(), it.pumpType, it.pumpSerial) + } + } sp.remove(R.string.key_active_pump_type) sp.remove(R.string.key_active_pump_serial_number) sp.remove(R.string.key_active_pump_change_timestamp) @@ -77,7 +85,7 @@ class PumpSyncImplementation @Inject constructor( } override fun expectedPumpState(): PumpSync.PumpState { - val bolus = repository.getLastBolusRecordWrapped().blockingGet(); + val bolus = repository.getLastBolusRecordWrapped().blockingGet() val temporaryBasal = repository.getTemporaryBasalActiveAt(dateUtil.now()).blockingGet() val extendedBolus = repository.getExtendedBolusActiveAt(dateUtil.now()).blockingGet() @@ -91,7 +99,9 @@ class PumpSyncImplementation @Inject constructor( rate = temporaryBasal.value.rate, isAbsolute = temporaryBasal.value.isAbsolute, type = PumpSync.TemporaryBasalType.fromDbType(temporaryBasal.value.type), - pumpId = temporaryBasal.value.interfaceIDs.pumpId + pumpId = temporaryBasal.value.interfaceIDs.pumpId, + pumpType = temporaryBasal.value.interfaceIDs.pumpType?.let { PumpType.fromDbPumpType(it)} ?: PumpType.USER, + pumpSerial = temporaryBasal.value.interfaceIDs.pumpSerial ?: "", ) else null, extendedBolus = @@ -100,7 +110,9 @@ class PumpSyncImplementation @Inject constructor( timestamp = extendedBolus.value.timestamp, duration = extendedBolus.value.duration, amount = extendedBolus.value.amount, - rate = extendedBolus.value.rate + rate = extendedBolus.value.rate, + pumpType = extendedBolus.value.interfaceIDs.pumpType?.let { PumpType.fromDbPumpType(it)} ?: PumpType.USER, + pumpSerial = extendedBolus.value.interfaceIDs.pumpSerial ?: "" ) else null, bolus = diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpType.kt b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpType.kt index 8783644cde..be39b6dda7 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpType.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/defs/PumpType.kt @@ -359,6 +359,41 @@ enum class PumpType { fun getByDescription(desc: String): PumpType = values().firstOrNull { it.description == desc } ?: GENERIC_AAPS + + fun fromDbPumpType(pt: InterfaceIDs.PumpType): PumpType = + when (pt) { + InterfaceIDs.PumpType.GENERIC_AAPS -> GENERIC_AAPS + InterfaceIDs.PumpType.CELLNOVO -> CELLNOVO + InterfaceIDs.PumpType.ACCU_CHEK_COMBO -> ACCU_CHEK_COMBO + InterfaceIDs.PumpType.ACCU_CHEK_SPIRIT -> ACCU_CHEK_SPIRIT + InterfaceIDs.PumpType.ACCU_CHEK_INSIGHT -> ACCU_CHEK_INSIGHT_VIRTUAL + InterfaceIDs.PumpType.ACCU_CHEK_INSIGHT_BLUETOOTH -> ACCU_CHEK_INSIGHT + InterfaceIDs.PumpType.ACCU_CHEK_SOLO -> ACCU_CHEK_SOLO + InterfaceIDs.PumpType.ANIMAS_VIBE -> ANIMAS_VIBE + InterfaceIDs.PumpType.ANIMAS_PING -> ANIMAS_PING + InterfaceIDs.PumpType.DANA_R -> DANA_R + InterfaceIDs.PumpType.DANA_R_KOREAN -> DANA_R_KOREAN + InterfaceIDs.PumpType.DANA_RS -> DANA_RS + InterfaceIDs.PumpType.DANA_RS_KOREAN -> DANA_RS_KOREAN + InterfaceIDs.PumpType.DANA_RV2 -> DANA_RV2 + InterfaceIDs.PumpType.DANA_I -> DANA_I + InterfaceIDs.PumpType.OMNIPOD_EROS -> OMNIPOD_EROS + InterfaceIDs.PumpType.OMNIPOD_DASH -> OMNIPOD_DASH + InterfaceIDs.PumpType.MEDTRONIC_512_517 -> MEDTRONIC_512_712 + InterfaceIDs.PumpType.MEDTRONIC_515_715 -> MEDTRONIC_515_715 + InterfaceIDs.PumpType.MEDTRONIC_522_722 -> MEDTRONIC_522_722 + InterfaceIDs.PumpType.MEDTRONIC_523_723_REVEL -> MEDTRONIC_523_723_REVEL + InterfaceIDs.PumpType.MEDTRONIC_554_754_VEO -> MEDTRONIC_554_754_VEO + InterfaceIDs.PumpType.MEDTRONIC_640G -> MEDTRONIC_640G + InterfaceIDs.PumpType.TANDEM_T_SLIM -> TANDEM_T_SLIM + InterfaceIDs.PumpType.TANDEM_T_SLIM_G4 -> TANDEM_T_SLIM_G4 + InterfaceIDs.PumpType.TANDEM_T_FLEX -> TANDEM_T_FLEX + InterfaceIDs.PumpType.TANDEM_T_SLIM_X2 -> TANDEM_T_SLIM_X2 + InterfaceIDs.PumpType.YPSOPUMP -> YPSOPUMP + InterfaceIDs.PumpType.MDI -> MDI + InterfaceIDs.PumpType.USER -> USER + InterfaceIDs.PumpType.DIACONN_G8 -> DIACONN_G8 + } } constructor(description: String, model: String, parent: PumpType, pumpCapability: PumpCapability? = null, source: Sources? = null) { diff --git a/database/src/main/java/info/nightscout/androidaps/database/embedments/InterfaceIDs.kt b/database/src/main/java/info/nightscout/androidaps/database/embedments/InterfaceIDs.kt index b06098f4ee..0477dc1c8d 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/embedments/InterfaceIDs.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/embedments/InterfaceIDs.kt @@ -1,7 +1,5 @@ package info.nightscout.androidaps.database.embedments -import info.nightscout.androidaps.database.entities.TherapyEvent - data class InterfaceIDs( var nightscoutSystemId: String? = null, var nightscoutId: String? = null,