diff --git a/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsBolusCarbsFragment.kt b/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsBolusCarbsFragment.kt index 20028cea25..f4614d5a41 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsBolusCarbsFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsBolusCarbsFragment.kt @@ -17,6 +17,7 @@ import info.nightscout.androidaps.database.entities.Carbs import info.nightscout.androidaps.database.entities.UserEntry.Action import info.nightscout.androidaps.database.entities.UserEntry.Sources import info.nightscout.androidaps.database.entities.ValueWithUnit +import info.nightscout.androidaps.database.transactions.CutCarbsTransaction import info.nightscout.androidaps.database.transactions.InvalidateBolusCalculatorResultTransaction import info.nightscout.androidaps.database.transactions.InvalidateBolusTransaction import info.nightscout.androidaps.database.transactions.InvalidateCarbsTransaction @@ -30,8 +31,6 @@ import info.nightscout.androidaps.extensions.iobCalc import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.ProfileFunction -import info.nightscout.shared.logging.AAPSLogger -import info.nightscout.shared.logging.LTag import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart @@ -43,6 +42,8 @@ import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.buildHelper.BuildHelper import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.logging.LTag import info.nightscout.shared.sharedPreferences.SP import io.reactivex.Completable import io.reactivex.disposables.CompositeDisposable @@ -131,15 +132,26 @@ class TreatmentsBolusCarbsFragment : DaggerFragment() { } } repository - .getCarbsDataFromTime(dateUtil.now(), false) + .getCarbsDataFromTimeNotExpanded(dateUtil.now(), false) .observeOn(aapsSchedulers.main) .subscribe { list -> list.forEach { carb -> - disposable += repository.runTransactionForResult(InvalidateCarbsTransaction(carb.id)) - .subscribe( - { result -> result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it") } }, - { aapsLogger.error(LTag.DATABASE, "Error while invalidating carbs", it) } - ) + if (carb.duration == 0L) + disposable += repository.runTransactionForResult(InvalidateCarbsTransaction(carb.id)) + .subscribe( + { result -> result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it") } }, + { aapsLogger.error(LTag.DATABASE, "Error while invalidating carbs", it) } + ) + else { + disposable += repository.runTransactionForResult(CutCarbsTransaction(carb.id, dateUtil.now())) + .subscribe( + { result -> + result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it") } + result.updated.forEach { aapsLogger.debug(LTag.DATABASE, "Updated (cut end) carbs $it") } + }, + { aapsLogger.error(LTag.DATABASE, "Error while invalidating carbs", it) } + ) + } } } repository diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt index b24c8fc041..259c62d9f5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt @@ -131,6 +131,13 @@ class NSClientAddUpdateWorker( ) aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it") } + result.updated.forEach { + uel.log(Action.CARBS, Sources.NSClient, + ValueWithUnit.Timestamp(it.timestamp), + ValueWithUnit.Gram(it.amount.toInt()) + ) + aapsLogger.debug(LTag.DATABASE, "Updated carbs $it") + } result.updatedNsId.forEach { aapsLogger.debug(LTag.DATABASE, "Updated nsId carbs $it") } diff --git a/build.gradle b/build.gradle index 5869d5181c..a69746ea68 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ buildscript { ext { - kotlin_version = '1.6.0' + kotlin_version = '1.6.10' coreVersion = '1.6.0' rxjava_version = '2.2.21' rxandroid_version = '2.1.1' diff --git a/danars/src/main/java/info/nightscout/androidaps/danars/DanaRSPlugin.kt b/danars/src/main/java/info/nightscout/androidaps/danars/DanaRSPlugin.kt index b212b3807c..6d1e60ce63 100644 --- a/danars/src/main/java/info/nightscout/androidaps/danars/DanaRSPlugin.kt +++ b/danars/src/main/java/info/nightscout/androidaps/danars/DanaRSPlugin.kt @@ -621,6 +621,7 @@ class DanaRSPlugin @Inject constructor( override fun loadTDDs(): PumpEnactResult = loadHistory(RecordTypes.RECORD_TYPE_DAILY) override fun canHandleDST(): Boolean = false override fun clearPairing() { + aapsLogger.debug(LTag.PUMPCOMM, "Pairing keys cleared") sp.remove(rh.gs(R.string.key_danars_pairingkey) + mDeviceName) sp.remove(rh.gs(R.string.key_danars_v3_randompairingkey) + mDeviceName) sp.remove(rh.gs(R.string.key_danars_v3_pairingkey) + mDeviceName) diff --git a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt index caff9784bb..4d787f596d 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt @@ -543,6 +543,11 @@ import kotlin.math.roundToInt .map { if (!ascending) it.reversed() else it } .subscribeOn(Schedulers.io()) + fun getCarbsDataFromTimeNotExpanded(timestamp: Long, ascending: Boolean): Single> = + database.carbsDao.getCarbsFromTimeExpandable(timestamp) + .map { if (!ascending) it.reversed() else it } + .subscribeOn(Schedulers.io()) + fun getCarbsDataFromTimeToTime(from: Long, to: Long, ascending: Boolean): Single> = database.carbsDao.getCarbsFromTimeToTime(from, to) .map { if (!ascending) it.reversed() else it } diff --git a/database/src/main/java/info/nightscout/androidaps/database/transactions/CutCarbsTransaction.kt b/database/src/main/java/info/nightscout/androidaps/database/transactions/CutCarbsTransaction.kt new file mode 100644 index 0000000000..90637de496 --- /dev/null +++ b/database/src/main/java/info/nightscout/androidaps/database/transactions/CutCarbsTransaction.kt @@ -0,0 +1,32 @@ +package info.nightscout.androidaps.database.transactions + +import info.nightscout.androidaps.database.entities.Carbs +import info.nightscout.androidaps.database.interfaces.end +import kotlin.math.roundToInt + +class CutCarbsTransaction(val id: Long, val end: Long) : Transaction() { + + override fun run(): TransactionResult { + val result = TransactionResult() + val carbs = database.carbsDao.findById(id) + ?: throw IllegalArgumentException("There is no such Carbs with the specified ID.") + if (carbs.timestamp == end) { + carbs.isValid = false + database.carbsDao.updateExistingEntry(carbs) + result.invalidated.add(carbs) + } else if (end in carbs.timestamp..carbs.end) { + val pctRun = (end - carbs.timestamp) / carbs.duration.toDouble() + carbs.amount = (carbs.amount * pctRun).roundToInt().toDouble() + carbs.end = end + database.carbsDao.updateExistingEntry(carbs) + result.updated.add(carbs) + } + return result + } + + class TransactionResult { + + val invalidated = mutableListOf() + val updated = mutableListOf() + } +} \ No newline at end of file diff --git a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncNsCarbsTransaction.kt b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncNsCarbsTransaction.kt index 9d4378c01b..ce916a321e 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncNsCarbsTransaction.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/transactions/SyncNsCarbsTransaction.kt @@ -22,6 +22,13 @@ class SyncNsCarbsTransaction(private val carbs: Carbs) : Transaction() val updatedNsId = mutableListOf() val inserted = mutableListOf() val invalidated = mutableListOf()