make automation profile switch based on last profile switch instead of profile store

This commit is contained in:
Milos Kozak 2021-06-26 22:57:40 +02:00
parent dc90c6b140
commit cf006790b9
2 changed files with 27 additions and 4 deletions

View file

@ -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)
})
}
}

View file

@ -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<List<ProfileSwitch>> =
database.profileSwitchDao.getAllProfileSwitches()
.subscribeOn(Schedulers.io())