From 1684b88270b34d0205f3e10ad3109e2319802532 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 13 Apr 2022 13:40:50 +0200 Subject: [PATCH] fix cutting TBR & EB --- ...PumpCancelExtendedBolusIfAnyTransaction.kt | 2 +- ...umpCancelTemporaryBasalIfAnyTransaction.kt | 14 +++++----- .../SyncPumpExtendedBolusTransaction.kt | 24 ++++++++--------- .../SyncPumpTemporaryBasalTransaction.kt | 26 +++++++++---------- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpCancelExtendedBolusIfAnyTransaction.kt b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpCancelExtendedBolusIfAnyTransaction.kt index c73b7bb201..5553a7b1b1 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpCancelExtendedBolusIfAnyTransaction.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpCancelExtendedBolusIfAnyTransaction.kt @@ -13,7 +13,7 @@ class SyncPumpCancelExtendedBolusIfAnyTransaction( val existing = database.extendedBolusDao.findByPumpEndIds(endPumpId, pumpType, pumpSerial) if (existing != null) // assume EB has been cut already return result - val running = database.extendedBolusDao.getExtendedBolusActiveAt(timestamp, pumpType, pumpSerial).blockingGet() + val running = database.extendedBolusDao.getExtendedBolusActiveAt(timestamp).blockingGet() if (running != null && running.interfaceIDs.endId == null) { // do not allow overwrite if cut by end event val pctRun = (timestamp - running.timestamp) / running.duration.toDouble() running.amount *= pctRun diff --git a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpCancelTemporaryBasalIfAnyTransaction.kt b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpCancelTemporaryBasalIfAnyTransaction.kt index 58510f58c2..d7c3d528a0 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpCancelTemporaryBasalIfAnyTransaction.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpCancelTemporaryBasalIfAnyTransaction.kt @@ -13,13 +13,13 @@ class SyncPumpCancelTemporaryBasalIfAnyTransaction( val existing = database.temporaryBasalDao.findByPumpEndIds(endPumpId, pumpType, pumpSerial) if (existing != null) // assume TBR has been cut already return result - val current = database.temporaryBasalDao.getTemporaryBasalActiveAt(timestamp, pumpType, pumpSerial).blockingGet() - if (current != null && current.interfaceIDs.endId == null) { // do not allow overwrite if cut by end event - if (current.timestamp != timestamp) current.end = timestamp // prevent zero duration - else current.duration = 1 - current.interfaceIDs.endId = endPumpId - database.temporaryBasalDao.updateExistingEntry(current) - result.updated.add(current) + val running = database.temporaryBasalDao.getTemporaryBasalActiveAt(timestamp).blockingGet() + if (running != null && running.interfaceIDs.endId == null) { // do not allow overwrite if cut by end event + if (running.timestamp != timestamp) running.end = timestamp // prevent zero duration + else running.duration = 1 + running.interfaceIDs.endId = endPumpId + database.temporaryBasalDao.updateExistingEntry(running) + result.updated.add(running) } return result } diff --git a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpExtendedBolusTransaction.kt b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpExtendedBolusTransaction.kt index 2cf424fe80..ac73ba74ef 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpExtendedBolusTransaction.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpExtendedBolusTransaction.kt @@ -13,21 +13,21 @@ class SyncPumpExtendedBolusTransaction(private val extendedBolus: ExtendedBolus) ?: extendedBolus.interfaceIDs.pumpSerial ?: throw IllegalStateException("Some pump ID is null") val result = TransactionResult() - val current = database.extendedBolusDao.findByPumpIds(extendedBolus.interfaceIDs.pumpId!!, extendedBolus.interfaceIDs.pumpType!!, extendedBolus.interfaceIDs.pumpSerial!!) - if (current != null) { - if (current.interfaceIDs.endId == null && - (current.timestamp != extendedBolus.timestamp || - current.amount != extendedBolus.amount || - current.duration != extendedBolus.duration) + val existing = database.extendedBolusDao.findByPumpIds(extendedBolus.interfaceIDs.pumpId!!, extendedBolus.interfaceIDs.pumpType!!, extendedBolus.interfaceIDs.pumpSerial!!) + if (existing != null) { + if (existing.interfaceIDs.endId == null && + (existing.timestamp != extendedBolus.timestamp || + existing.amount != extendedBolus.amount || + existing.duration != extendedBolus.duration) ) { - current.timestamp = extendedBolus.timestamp - current.amount = extendedBolus.amount - current.duration = extendedBolus.duration - database.extendedBolusDao.updateExistingEntry(current) - result.updated.add(current) + existing.timestamp = extendedBolus.timestamp + existing.amount = extendedBolus.amount + existing.duration = extendedBolus.duration + database.extendedBolusDao.updateExistingEntry(existing) + result.updated.add(existing) } } else { - val running = database.extendedBolusDao.getExtendedBolusActiveAt(extendedBolus.timestamp, extendedBolus.interfaceIDs.pumpType!!, extendedBolus.interfaceIDs.pumpSerial!!).blockingGet() + val running = database.extendedBolusDao.getExtendedBolusActiveAt(extendedBolus.timestamp).blockingGet() if (running != null) { val pctRun = (extendedBolus.timestamp - running.timestamp) / running.duration.toDouble() running.amount *= pctRun diff --git a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpTemporaryBasalTransaction.kt b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpTemporaryBasalTransaction.kt index e3303c5c90..bcd49fd43a 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpTemporaryBasalTransaction.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncPumpTemporaryBasalTransaction.kt @@ -16,23 +16,23 @@ class SyncPumpTemporaryBasalTransaction( ?: temporaryBasal.interfaceIDs.pumpSerial ?: throw IllegalStateException("Some pump ID is null") val result = TransactionResult() - val current = database.temporaryBasalDao.findByPumpIds(temporaryBasal.interfaceIDs.pumpId!!, temporaryBasal.interfaceIDs.pumpType!!, temporaryBasal.interfaceIDs.pumpSerial!!) - if (current != null) { + val existing = database.temporaryBasalDao.findByPumpIds(temporaryBasal.interfaceIDs.pumpId!!, temporaryBasal.interfaceIDs.pumpType!!, temporaryBasal.interfaceIDs.pumpSerial!!) + if (existing != null) { if ( - current.timestamp != temporaryBasal.timestamp || - current.rate != temporaryBasal.rate || - current.duration != temporaryBasal.duration && current.interfaceIDs.endId == null || - current.type != type ?: current.type + existing.timestamp != temporaryBasal.timestamp || + existing.rate != temporaryBasal.rate || + existing.duration != temporaryBasal.duration && existing.interfaceIDs.endId == null || + existing.type != type ?: existing.type ) { - current.timestamp = temporaryBasal.timestamp - current.rate = temporaryBasal.rate - current.duration = temporaryBasal.duration - current.type = type ?: current.type - database.temporaryBasalDao.updateExistingEntry(current) - result.updated.add(current) + existing.timestamp = temporaryBasal.timestamp + existing.rate = temporaryBasal.rate + existing.duration = temporaryBasal.duration + existing.type = type ?: existing.type + database.temporaryBasalDao.updateExistingEntry(existing) + result.updated.add(existing) } } else { - val running = database.temporaryBasalDao.getTemporaryBasalActiveAt(temporaryBasal.timestamp, temporaryBasal.interfaceIDs.pumpType!!, temporaryBasal.interfaceIDs.pumpSerial!!).blockingGet() + val running = database.temporaryBasalDao.getTemporaryBasalActiveAt(temporaryBasal.timestamp).blockingGet() if (running != null) { running.end = temporaryBasal.timestamp running.interfaceIDs.endId = temporaryBasal.interfaceIDs.pumpId