End TBR before connectNewPump

This commit is contained in:
Milos Kozak 2021-11-11 11:04:24 +01:00
parent 7ad425ae1d
commit 85178ee19f
5 changed files with 81 additions and 10 deletions

View file

@ -27,8 +27,13 @@ interface PumpSync {
* *
* Call this function when new pump is paired to accept data from new pump * Call this function when new pump is paired to accept data from new pump
* to prevent overlapping pump histories * 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 * 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 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 TemporaryBasal @JvmOverloads constructor(
data class ExtendedBolus(val timestamp: Long, val duration: Long, val amount: Double, val rate: Double) 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) data class Bolus(val timestamp: Long, val amount: Double)
} }

View file

@ -100,7 +100,7 @@ class RunningConfiguration @Inject constructor(
if (sp.getString(R.string.key_virtualpump_type, "fake") != pumpType) { if (sp.getString(R.string.key_virtualpump_type, "fake") != pumpType) {
sp.putString(R.string.key_virtualpump_type, pumpType) sp.putString(R.string.key_virtualpump_type, pumpType)
activePlugin.activePump.pumpDescription.fillFor(PumpType.getByDescription(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") aapsLogger.debug(LTag.CORE, "Changing pump type to $pumpType")
} }
} }

View file

@ -37,7 +37,15 @@ class PumpSyncImplementation @Inject constructor(
private val disposable = CompositeDisposable() 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_type)
sp.remove(R.string.key_active_pump_serial_number) sp.remove(R.string.key_active_pump_serial_number)
sp.remove(R.string.key_active_pump_change_timestamp) sp.remove(R.string.key_active_pump_change_timestamp)
@ -77,7 +85,7 @@ class PumpSyncImplementation @Inject constructor(
} }
override fun expectedPumpState(): PumpSync.PumpState { override fun expectedPumpState(): PumpSync.PumpState {
val bolus = repository.getLastBolusRecordWrapped().blockingGet(); val bolus = repository.getLastBolusRecordWrapped().blockingGet()
val temporaryBasal = repository.getTemporaryBasalActiveAt(dateUtil.now()).blockingGet() val temporaryBasal = repository.getTemporaryBasalActiveAt(dateUtil.now()).blockingGet()
val extendedBolus = repository.getExtendedBolusActiveAt(dateUtil.now()).blockingGet() val extendedBolus = repository.getExtendedBolusActiveAt(dateUtil.now()).blockingGet()
@ -91,7 +99,9 @@ class PumpSyncImplementation @Inject constructor(
rate = temporaryBasal.value.rate, rate = temporaryBasal.value.rate,
isAbsolute = temporaryBasal.value.isAbsolute, isAbsolute = temporaryBasal.value.isAbsolute,
type = PumpSync.TemporaryBasalType.fromDbType(temporaryBasal.value.type), 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, else null,
extendedBolus = extendedBolus =
@ -100,7 +110,9 @@ class PumpSyncImplementation @Inject constructor(
timestamp = extendedBolus.value.timestamp, timestamp = extendedBolus.value.timestamp,
duration = extendedBolus.value.duration, duration = extendedBolus.value.duration,
amount = extendedBolus.value.amount, 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, else null,
bolus = bolus =

View file

@ -359,6 +359,41 @@ enum class PumpType {
fun getByDescription(desc: String): PumpType = fun getByDescription(desc: String): PumpType =
values().firstOrNull { it.description == desc } ?: GENERIC_AAPS 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) { constructor(description: String, model: String, parent: PumpType, pumpCapability: PumpCapability? = null, source: Sources? = null) {

View file

@ -1,7 +1,5 @@
package info.nightscout.androidaps.database.embedments package info.nightscout.androidaps.database.embedments
import info.nightscout.androidaps.database.entities.TherapyEvent
data class InterfaceIDs( data class InterfaceIDs(
var nightscoutSystemId: String? = null, var nightscoutSystemId: String? = null,
var nightscoutId: String? = null, var nightscoutId: String? = null,