From cf006790b95f5d498cb8359a96841b0dbfffcc8e Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 26 Jun 2021 22:57:40 +0200 Subject: [PATCH] make automation profile switch based on last profile switch instead of profile store --- .../ProfileFunctionImplementation.kt | 26 ++++++++++++++++--- .../androidaps/database/AppRepository.kt | 5 ++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctionImplementation.kt b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctionImplementation.kt index 4440eda924..3de63dbbd5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctionImplementation.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctionImplementation.kt @@ -117,9 +117,27 @@ class ProfileFunctionImplementation @Inject constructor( } override fun createProfileSwitch(durationInMinutes: Int, percentage: Int, timeShiftInHours: Int) { - val profileStore = activePlugin.activeProfileSource.profile ?: return - val profileName = activePlugin.activeProfileSource.profile?.getDefaultProfileName() - ?: return - createProfileSwitch(profileStore, profileName, durationInMinutes, percentage, timeShiftInHours, dateUtil.now()) + val profile = repository.getPermanentProfileSwitch(dateUtil.now()) + ?: throw InvalidParameterSpecException("No active ProfileSwitch") + val ps = ProfileSwitch( + timestamp = dateUtil.now(), + basalBlocks = profile.basalBlocks, + isfBlocks = profile.isfBlocks, + icBlocks = profile.icBlocks, + targetBlocks = profile.targetBlocks, + glucoseUnit = profile.glucoseUnit, + profileName = profile.profileName, + timeshift = T.hours(timeShiftInHours.toLong()).msecs(), + percentage = percentage, + duration = T.mins(durationInMinutes.toLong()).msecs(), + insulinConfiguration = activePlugin.activeInsulin.insulinConfiguration + ) + disposable += repository.runTransactionForResult(InsertOrUpdateProfileSwitch(ps)) + .subscribe({ result -> + result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted ProfileSwitch $it") } + result.updated.forEach { aapsLogger.debug(LTag.DATABASE, "Updated ProfileSwitch $it") } + }, { + aapsLogger.error(LTag.DATABASE, "Error while saving ProfileSwitch", it) + }) } } \ No newline at end of file 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 da7449f887..11754adf03 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt @@ -213,6 +213,11 @@ open class AppRepository @Inject internal constructor( return null } + fun getPermanentProfileSwitch(timestamp: Long): ProfileSwitch? = + database.profileSwitchDao.getPermanentProfileSwitchActiveAt(timestamp) + .subscribeOn(Schedulers.io()) + .blockingGet() + fun getAllProfileSwitches(): Single> = database.profileSwitchDao.getAllProfileSwitches() .subscribeOn(Schedulers.io())