From f48dcc77e23d499ab0c1af8499ce9ce8ed574ba1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 16 Nov 2021 17:09:47 +0100 Subject: [PATCH] fix double profile --- .../TreatmentsProfileSwitchFragment.kt | 7 +- .../androidaps/db/CompatDBHelper.kt | 36 ++++---- .../queue/CommandQueueImplementation.kt | 82 ++++++++++--------- .../androidaps/data/ProfileSealed.kt | 4 +- 4 files changed, 69 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsProfileSwitchFragment.kt b/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsProfileSwitchFragment.kt index 5fce0fc4bf..266cf3d12c 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsProfileSwitchFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsProfileSwitchFragment.kt @@ -149,11 +149,14 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() { override fun onResume() { super.onResume() swapAdapter() - disposable.add(rxBus + disposable += rxBus .toObservable(EventProfileSwitchChanged::class.java) .observeOn(aapsSchedulers.main) .subscribe({ swapAdapter() }, fabricPrivacy::logException) - ) + disposable += rxBus + .toObservable(EventEffectiveProfileSwitchChanged::class.java) + .observeOn(aapsSchedulers.main) + .subscribe({ swapAdapter() }, fabricPrivacy::logException) } @Synchronized diff --git a/app/src/main/java/info/nightscout/androidaps/db/CompatDBHelper.kt b/app/src/main/java/info/nightscout/androidaps/db/CompatDBHelper.kt index 0ffd88d2d1..ca170443a3 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/CompatDBHelper.kt +++ b/app/src/main/java/info/nightscout/androidaps/db/CompatDBHelper.kt @@ -2,8 +2,6 @@ package info.nightscout.androidaps.db import info.nightscout.androidaps.database.AppRepository import info.nightscout.androidaps.database.entities.* -import info.nightscout.androidaps.database.entities.ExtendedBolus -import info.nightscout.androidaps.database.entities.TemporaryBasal import info.nightscout.androidaps.events.* import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag @@ -34,56 +32,56 @@ class CompatDBHelper @Inject constructor( */ var newestGlucoseValue: GlucoseValue? = null it.filterIsInstance().lastOrNull()?.let { gv -> - aapsLogger.debug(LTag.DATABASE, "Firing EventNewBg") + aapsLogger.debug(LTag.DATABASE, "Firing EventNewBg $gv") rxBus.send(EventNewBG(gv)) newestGlucoseValue = gv } it.filterIsInstance().map { gv -> gv.timestamp }.minOrNull()?.let { timestamp -> - aapsLogger.debug(LTag.DATABASE, "Firing EventNewHistoryData") + aapsLogger.debug(LTag.DATABASE, "Firing EventNewHistoryData $newestGlucoseValue") rxBus.send(EventNewHistoryData(timestamp, true, newestGlucoseValue)) } it.filterIsInstance().map { t -> t.timestamp }.minOrNull()?.let { timestamp -> - aapsLogger.debug(LTag.DATABASE, "Firing EventTreatmentChange") + aapsLogger.debug(LTag.DATABASE, "Firing EventTreatmentChange $timestamp") rxBus.send(EventTreatmentChange()) rxBus.send(EventNewHistoryData(timestamp, false)) } it.filterIsInstance().map { t -> t.timestamp }.minOrNull()?.let { timestamp -> - aapsLogger.debug(LTag.DATABASE, "Firing EventTreatmentChange") + aapsLogger.debug(LTag.DATABASE, "Firing EventTreatmentChange $timestamp") rxBus.send(EventTreatmentChange()) rxBus.send(EventNewHistoryData(timestamp, false)) } it.filterIsInstance().map { t -> t.timestamp }.minOrNull()?.let { timestamp -> - aapsLogger.debug(LTag.DATABASE, "Firing EventTempBasalChange") + aapsLogger.debug(LTag.DATABASE, "Firing EventTempBasalChange $timestamp") rxBus.send(EventTempBasalChange()) rxBus.send(EventNewHistoryData(timestamp, false)) } it.filterIsInstance().map { t -> t.timestamp }.minOrNull()?.let { timestamp -> - aapsLogger.debug(LTag.DATABASE, "Firing EventExtendedBolusChange") + aapsLogger.debug(LTag.DATABASE, "Firing EventExtendedBolusChange $timestamp") rxBus.send(EventExtendedBolusChange()) rxBus.send(EventNewHistoryData(timestamp, false)) } - it.filterIsInstance().firstOrNull()?.let { - aapsLogger.debug(LTag.DATABASE, "Firing EventTempTargetChange") + it.filterIsInstance().firstOrNull()?.let { tt -> + aapsLogger.debug(LTag.DATABASE, "Firing EventTempTargetChange $tt") rxBus.send(EventTempTargetChange()) } - it.filterIsInstance().firstOrNull()?.let { - aapsLogger.debug(LTag.DATABASE, "Firing EventTherapyEventChange") + it.filterIsInstance().firstOrNull()?.let { te -> + aapsLogger.debug(LTag.DATABASE, "Firing EventTherapyEventChange $te") rxBus.send(EventTherapyEventChange()) } - it.filterIsInstance().firstOrNull()?.let { - aapsLogger.debug(LTag.DATABASE, "Firing EventFoodDatabaseChanged") + it.filterIsInstance().firstOrNull()?.let { food -> + aapsLogger.debug(LTag.DATABASE, "Firing EventFoodDatabaseChanged $food") rxBus.send(EventFoodDatabaseChanged()) } - it.filterIsInstance().firstOrNull()?.let { - aapsLogger.debug(LTag.DATABASE, "Firing EventProfileSwitchChanged") + it.filterIsInstance().firstOrNull()?.let { ps -> + aapsLogger.debug(LTag.DATABASE, "Firing EventProfileSwitchChanged $ps") rxBus.send(EventProfileSwitchChanged()) } it.filterIsInstance().firstOrNull()?.let { eps -> - aapsLogger.debug(LTag.DATABASE, "Firing EventProfileSwitchChanged") + aapsLogger.debug(LTag.DATABASE, "Firing EventEffectiveProfileSwitchChanged $eps") rxBus.send(EventEffectiveProfileSwitchChanged(eps)) } - it.filterIsInstance().firstOrNull()?.let { - aapsLogger.debug(LTag.DATABASE, "Firing EventOfflineChange") + it.filterIsInstance().firstOrNull()?.let { oe -> + aapsLogger.debug(LTag.DATABASE, "Firing EventOfflineChange $oe") rxBus.send(EventOfflineChange()) } } diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt index 2f3fa6d9ce..6841ac4c25 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt @@ -79,39 +79,40 @@ class CommandQueueImplementation @Inject constructor( .toObservable(EventProfileSwitchChanged::class.java) .observeOn(aapsSchedulers.io) .subscribe({ - if (config.NSCLIENT) { // Effective profileswitch should be synced over NS, do not create EffectiveProfileSwitch here - return@subscribe - } - aapsLogger.debug(LTag.PROFILE, "onProfileSwitch") - profileFunction.getRequestedProfile()?.let { - val nonCustomized = ProfileSealed.PS(it).convertToNonCustomizedProfile(dateUtil) - setProfile(ProfileSealed.Pure(nonCustomized), it.interfaceIDs.nightscoutId != null, object : Callback() { - override fun run() { - if (!result.success) { - ErrorHelperActivity.runAlarm(context, result.comment, rh.gs(R.string.failedupdatebasalprofile), R.raw.boluserror) - } else { - repository.createEffectiveProfileSwitch( - EffectiveProfileSwitch( - timestamp = dateUtil.now(), - basalBlocks = nonCustomized.basalBlocks, - isfBlocks = nonCustomized.isfBlocks, - icBlocks = nonCustomized.icBlocks, - targetBlocks = nonCustomized.targetBlocks, - glucoseUnit = if (it.glucoseUnit == ProfileSwitch.GlucoseUnit.MGDL) EffectiveProfileSwitch.GlucoseUnit.MGDL else EffectiveProfileSwitch.GlucoseUnit.MMOL, - originalProfileName = it.profileName, - originalCustomizedName = it.getCustomizedName(), - originalTimeshift = it.timeshift, - originalPercentage = it.percentage, - originalDuration = it.duration, - originalEnd = it.end, - insulinConfiguration = it.insulinConfiguration - ) - ) - } - } - }) - } - }, fabricPrivacy::logException) + if (config.NSCLIENT) { // Effective profileswitch should be synced over NS, do not create EffectiveProfileSwitch here + return@subscribe + } + aapsLogger.debug(LTag.PROFILE, "onEventProfileSwitchChanged") + profileFunction.getRequestedProfile()?.let { + setProfile(ProfileSealed.PS(it), it.interfaceIDs.nightscoutId != null, object : Callback() { + override fun run() { + if (!result.success) { + ErrorHelperActivity.runAlarm(context, result.comment, rh.gs(R.string.failedupdatebasalprofile), R.raw.boluserror) + } else if (result.enacted) { + val nonCustomized = ProfileSealed.PS(it).convertToNonCustomizedProfile(dateUtil) + EffectiveProfileSwitch( + timestamp = dateUtil.now(), + basalBlocks = nonCustomized.basalBlocks, + isfBlocks = nonCustomized.isfBlocks, + icBlocks = nonCustomized.icBlocks, + targetBlocks = nonCustomized.targetBlocks, + glucoseUnit = if (it.glucoseUnit == ProfileSwitch.GlucoseUnit.MGDL) EffectiveProfileSwitch.GlucoseUnit.MGDL else EffectiveProfileSwitch.GlucoseUnit.MMOL, + originalProfileName = it.profileName, + originalCustomizedName = it.getCustomizedName(), + originalTimeshift = it.timeshift, + originalPercentage = it.percentage, + originalDuration = it.duration, + originalEnd = it.end, + insulinConfiguration = it.insulinConfiguration + ).also { eps -> + repository.createEffectiveProfileSwitch(eps) + aapsLogger.debug(LTag.DATABASE, "Inserted EffectiveProfileSwitch $eps") + } + } + } + }) + } + }, fabricPrivacy::logException) } private fun executingNowError(): PumpEnactResult = @@ -195,9 +196,11 @@ class CommandQueueImplementation @Inject constructor( override fun independentConnect(reason: String, callback: Callback?) { aapsLogger.debug(LTag.PUMPQUEUE, "Starting new queue") - val tempCommandQueue = CommandQueueImplementation(injector, aapsLogger, rxBus, aapsSchedulers, rh, - constraintChecker, profileFunction, activePlugin, context, sp, - buildHelper, dateUtil, repository, fabricPrivacy, config) + val tempCommandQueue = CommandQueueImplementation( + injector, aapsLogger, rxBus, aapsSchedulers, rh, + constraintChecker, profileFunction, activePlugin, context, sp, + buildHelper, dateUtil, repository, fabricPrivacy, config + ) tempCommandQueue.readStatus(reason, callback) tempCommandQueue.disposable.clear() } @@ -222,7 +225,7 @@ class CommandQueueImplementation @Inject constructor( // If not, it's not necessary add command to the queue and initiate connection // Assuming carbs in the future and carbs with duration are NOT stores anyway - var carbsRunnable = Runnable { } + var carbsRunnable = Runnable { } val originalCarbs = detailedBolusInfo.carbs if ((detailedBolusInfo.carbs > 0) && (!activePlugin.activePump.pumpDescription.storesCarbInfo || @@ -398,6 +401,11 @@ class CommandQueueImplementation @Inject constructor( // returns true if command is queued override fun setProfile(profile: Profile, hasNsId: Boolean, callback: Callback?): Boolean { + if (isRunning(CommandType.BASAL_PROFILE)) { + aapsLogger.debug(LTag.PUMPQUEUE, "Command is already executed") + callback?.result(PumpEnactResult(injector).success(true).enacted(false))?.run() + return false + } if (isThisProfileSet(profile) && repository.getEffectiveProfileSwitchActiveAt(dateUtil.now()).blockingGet() is ValueWrapper.Existing) { aapsLogger.debug(LTag.PUMPQUEUE, "Correct profile already set") callback?.result(PumpEnactResult(injector).success(true).enacted(false))?.run() diff --git a/core/src/main/java/info/nightscout/androidaps/data/ProfileSealed.kt b/core/src/main/java/info/nightscout/androidaps/data/ProfileSealed.kt index 462259b76b..57f95bfc74 100644 --- a/core/src/main/java/info/nightscout/androidaps/data/ProfileSealed.kt +++ b/core/src/main/java/info/nightscout/androidaps/data/ProfileSealed.kt @@ -211,9 +211,9 @@ sealed class ProfileSealed( if (getIcTimeFromMidnight(seconds) != profile.getIcTimeFromMidnight(seconds)) return false if (getTargetLowMgdlTimeFromMidnight(seconds) != profile.getTargetLowMgdlTimeFromMidnight(seconds)) return false if (getTargetHighMgdlTimeFromMidnight(seconds) != profile.getTargetHighMgdlTimeFromMidnight(seconds)) return false - if (dia != profile.dia) return false - if ((profile is EPS) && profileName != profile.value.originalProfileName) return false // handle profile name change too } + if (dia != profile.dia) return false + if ((profile is EPS) && profileName != profile.value.originalProfileName) return false // handle profile name change too return true }