Better EffectiveProfileSwitch handling

This commit is contained in:
Milos Kozak 2021-10-10 23:26:50 +02:00
parent a8895668bd
commit e8d2d03c16
12 changed files with 103 additions and 50 deletions

View file

@ -31,6 +31,7 @@ import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin
import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged
import info.nightscout.androidaps.events.EventTreatmentUpdateGui
import info.nightscout.androidaps.activities.fragments.TreatmentsProfileSwitchFragment.RecyclerProfileViewAdapter.ProfileSwitchViewHolder
import info.nightscout.androidaps.events.EventEffectiveProfileSwitchChanged
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.T
@ -92,6 +93,7 @@ class TreatmentsProfileSwitchFragment : DaggerFragment() {
onError = { aapsLogger.error("Error removing entries", it) },
onComplete = {
rxBus.send(EventProfileSwitchChanged())
rxBus.send(EventEffectiveProfileSwitchChanged(0L))
rxBus.send(EventNewHistoryData(0, false))
}
)

View file

@ -78,9 +78,9 @@ class CompatDBHelper @Inject constructor(
aapsLogger.debug(LTag.DATABASE, "Firing EventProfileSwitchChanged")
rxBus.send(EventProfileSwitchChanged())
}
it.filterIsInstance<EffectiveProfileSwitch>().firstOrNull()?.let {
it.filterIsInstance<EffectiveProfileSwitch>().firstOrNull()?.let { eps ->
aapsLogger.debug(LTag.DATABASE, "Firing EventProfileSwitchChanged")
rxBus.send(EventProfileSwitchChanged())
rxBus.send(EventEffectiveProfileSwitchChanged(eps))
}
it.filterIsInstance<OfflineEvent>().firstOrNull()?.let {
aapsLogger.debug(LTag.DATABASE, "Firing EventOfflineChange")

View file

@ -23,6 +23,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP
import info.nightscout.androidaps.plugins.pump.PumpSyncImplementation
import info.nightscout.androidaps.queue.CommandQueue
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.HardLimits
import info.nightscout.androidaps.utils.androidNotification.NotificationHolderImpl
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
@ -74,11 +75,12 @@ open class AppModule {
fun provideProfileFunction(
aapsLogger: AAPSLogger, sp: SP, rxBus: RxBusWrapper, resourceHelper:
ResourceHelper, activePlugin:
ActivePlugin, repository: AppRepository, dateUtil: DateUtil, config: Config, hardLimits: HardLimits
ActivePlugin, repository: AppRepository, dateUtil: DateUtil, config: Config, hardLimits: HardLimits,
aapsSchedulers: AapsSchedulers, fabricPrivacy: FabricPrivacy
): ProfileFunction =
ProfileFunctionImplementation(
aapsLogger, sp, rxBus, resourceHelper, activePlugin, repository, dateUtil,
config, hardLimits
config, hardLimits, aapsSchedulers, fabricPrivacy
)
@Module

View file

@ -1,3 +0,0 @@
package info.nightscout.androidaps.events
class EventNewBasalProfile : Event()

View file

@ -8,15 +8,18 @@ import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.database.ValueWrapper
import info.nightscout.androidaps.database.entities.ProfileSwitch
import info.nightscout.androidaps.database.transactions.InsertOrUpdateProfileSwitch
import info.nightscout.androidaps.events.EventEffectiveProfileSwitchChanged
import info.nightscout.androidaps.extensions.fromConstant
import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.HardLimits
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
@ -34,13 +37,33 @@ class ProfileFunctionImplementation @Inject constructor(
private val repository: AppRepository,
private val dateUtil: DateUtil,
private val config: Config,
private val hardLimits: HardLimits
private val hardLimits: HardLimits,
private val aapsSchedulers: AapsSchedulers,
private val fabricPrivacy: FabricPrivacy
) : ProfileFunction {
val cache = LongSparseArray<Profile>()
private val disposable = CompositeDisposable()
init {
disposable += rxBus
.toObservable(EventEffectiveProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe(
{
for (index in cache.size() - 1 downTo 0) {
if (cache.keyAt(index) > it.startDate) {
aapsLogger.debug(LTag.AUTOSENS, "Removing from profileCache: " + dateUtil.dateAndTimeAndSecondsString(cache.keyAt(index)))
cache.removeAt(index)
} else {
break
}
}
}, fabricPrivacy::logException
)
}
override fun getProfileName(): String =
getProfileName(System.currentTimeMillis(), customized = true, showRemainingTime = false)
@ -92,7 +115,7 @@ class ProfileFunctionImplementation @Inject constructor(
if (sp.getString(R.string.key_units, Constants.MGDL) == Constants.MGDL) GlucoseUnit.MGDL
else GlucoseUnit.MMOL
override fun buildProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long) : ProfileSwitch {
override fun buildProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long): ProfileSwitch {
val pureProfile = profileStore.getSpecificProfile(profileName)
?: throw InvalidParameterSpecException(profileName)
return ProfileSwitch(

View file

@ -5,9 +5,10 @@ import android.content.Intent
import android.content.pm.ResolveInfo
import android.os.Bundle
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.interfaces.Config
import info.nightscout.androidaps.R
import info.nightscout.androidaps.events.*
import info.nightscout.androidaps.extensions.durationInMinutes
import info.nightscout.androidaps.extensions.toStringFull
import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
@ -22,8 +23,6 @@ import info.nightscout.androidaps.services.Intents
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.extensions.durationInMinutes
import info.nightscout.androidaps.extensions.toStringFull
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.disposables.CompositeDisposable
@ -79,7 +78,7 @@ class DataBroadcastPlugin @Inject constructor(
.observeOn(aapsSchedulers.io)
.subscribe({ sendData(it) }, fabricPrivacy::logException))
disposable.add(rxBus
.toObservable(EventNewBasalProfile::class.java)
.toObservable(EventEffectiveProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ sendData(it) }, fabricPrivacy::logException))
disposable.add(rxBus

View file

@ -6,6 +6,7 @@ import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.R
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.database.ValueWrapper
import info.nightscout.androidaps.database.entities.EffectiveProfileSwitch
import info.nightscout.androidaps.events.*
import info.nightscout.androidaps.extensions.*
import info.nightscout.androidaps.interfaces.*
@ -136,9 +137,12 @@ class OverviewPlugin @Inject constructor(
.observeOn(aapsSchedulers.io)
.subscribe({ overviewData.preparePredictions("EventLoopInvoked") }, fabricPrivacy::logException)
disposable += rxBus
.toObservable(EventNewBasalProfile::class.java)
.toObservable(EventEffectiveProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ loadProfile("EventNewBasalProfile") }, fabricPrivacy::logException)
.subscribe({
loadProfile("EventEffectiveProfileSwitchChanged")
overviewData.prepareBasalData("EventEffectiveProfileSwitchChanged")
}, fabricPrivacy::logException)
disposable += rxBus
.toObservable(EventAutosensCalculationFinished::class.java)
.observeOn(aapsSchedulers.io)

View file

@ -10,18 +10,17 @@ import androidx.core.app.RemoteInput
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.Profile
import info.nightscout.androidaps.events.*
import info.nightscout.androidaps.extensions.toStringShort
import info.nightscout.androidaps.extensions.valueToUnitsString
import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.extensions.toStringShort
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.extensions.valueToUnitsString
import io.reactivex.disposables.CompositeDisposable
import javax.inject.Inject
import javax.inject.Singleton
@ -89,7 +88,7 @@ class PersistentNotificationPlugin @Inject constructor(
.observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException))
disposable.add(rxBus
.toObservable(EventNewBasalProfile::class.java)
.toObservable(EventEffectiveProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ triggerNotificationUpdate() }, fabricPrivacy::logException))
disposable.add(rxBus

View file

@ -68,7 +68,7 @@ class WearPlugin @Inject constructor(
.observeOn(aapsSchedulers.io)
.subscribe({ sendDataToWatch(status = true, basals = true, bgValue = false) }, fabricPrivacy::logException))
disposable.add(rxBus
.toObservable(EventNewBasalProfile::class.java)
.toObservable(EventEffectiveProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ sendDataToWatch(status = false, basals = true, bgValue = false) }, fabricPrivacy::logException))
disposable.add(rxBus

View file

@ -60,12 +60,13 @@ class IobCobCalculatorPlugin @Inject constructor(
private val fabricPrivacy: FabricPrivacy,
private val dateUtil: DateUtil,
private val repository: AppRepository
) : PluginBase(PluginDescription()
.mainType(PluginType.GENERAL)
.pluginName(R.string.iobcobcalculator)
.showInList(false)
.neverVisible(true)
.alwaysEnabled(true),
) : PluginBase(
PluginDescription()
.mainType(PluginType.GENERAL)
.pluginName(R.string.iobcobcalculator)
.showInList(false)
.neverVisible(true)
.alwaysEnabled(true),
aapsLogger, resourceHelper, injector
), IobCobCalculator {
@ -86,38 +87,51 @@ class IobCobCalculatorPlugin @Inject constructor(
disposable += rxBus
.toObservable(EventConfigBuilderChange::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ event -> resetDataAndRunCalculation("onEventConfigBuilderChange", event) }, fabricPrivacy::logException)
// EventNewBasalProfile
.subscribe({ event ->
resetDataAndRunCalculation("onEventConfigBuilderChange", event)
}, fabricPrivacy::logException)
// EventEffectiveProfileSwitchChanged
disposable += rxBus
.toObservable(EventNewBasalProfile::class.java)
.toObservable(EventEffectiveProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ event -> resetDataAndRunCalculation("onNewProfile", event) }, fabricPrivacy::logException)
.subscribe({ event ->
newHistoryData(event.startDate, false, event)
}, fabricPrivacy::logException)
// EventPreferenceChange
disposable += rxBus
.toObservable(EventPreferenceChange::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ event ->
if (event.isChanged(resourceHelper, R.string.key_openapsama_autosens_period) ||
event.isChanged(resourceHelper, R.string.key_age) ||
event.isChanged(resourceHelper, R.string.key_absorption_maxtime) ||
event.isChanged(resourceHelper, R.string.key_openapsama_min_5m_carbimpact) ||
event.isChanged(resourceHelper, R.string.key_absorption_cutoff) ||
event.isChanged(resourceHelper, R.string.key_openapsama_autosens_max) ||
event.isChanged(resourceHelper, R.string.key_openapsama_autosens_min) ||
event.isChanged(resourceHelper, R.string.key_insulin_oref_peak)) {
resetDataAndRunCalculation("onEventPreferenceChange", event)
}
}, fabricPrivacy::logException)
if (event.isChanged(resourceHelper, R.string.key_openapsama_autosens_period) ||
event.isChanged(resourceHelper, R.string.key_age) ||
event.isChanged(resourceHelper, R.string.key_absorption_maxtime) ||
event.isChanged(resourceHelper, R.string.key_openapsama_min_5m_carbimpact) ||
event.isChanged(resourceHelper, R.string.key_absorption_cutoff) ||
event.isChanged(resourceHelper, R.string.key_openapsama_autosens_max) ||
event.isChanged(resourceHelper, R.string.key_openapsama_autosens_min) ||
event.isChanged(resourceHelper, R.string.key_insulin_oref_peak)
) {
resetDataAndRunCalculation("onEventPreferenceChange", event)
}
}, fabricPrivacy::logException)
// EventAppInitialized
disposable += rxBus
.toObservable(EventAppInitialized::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ event -> runCalculation("onEventAppInitialized", System.currentTimeMillis(), bgDataReload = true, limitDataToOldestAvailable = true, cause = event) }, fabricPrivacy::logException)
.subscribe(
{ event -> runCalculation("onEventAppInitialized", System.currentTimeMillis(), bgDataReload = true, limitDataToOldestAvailable = true, cause = event) },
fabricPrivacy::logException
)
// EventNewHistoryData
disposable += rxBus
.toObservable(EventNewHistoryData::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ event -> newHistoryData(event.oldDataTimestamp, event.reloadBgData, if (event.newestGlucoseValue != null) EventNewBG(event.newestGlucoseValue) else event) }, fabricPrivacy::logException)
.subscribe(
{ event ->
newHistoryData(event.oldDataTimestamp, event.reloadBgData, if (event.newestGlucoseValue != null) EventNewBG(event.newestGlucoseValue) else event)
},
fabricPrivacy::logException
)
}
override fun onStop() {
@ -187,7 +201,8 @@ class IobCobCalculatorPlugin @Inject constructor(
duration = 240,
rate = 0.0,
isAbsolute = true,
type = TemporaryBasal.Type.NORMAL)
type = TemporaryBasal.Type.NORMAL
)
if (t.timestamp < time) {
val calc = t.iobCalc(time, profile, activePlugin.activeInsulin)
basalIobWithZeroTemp.plus(calc)
@ -214,7 +229,8 @@ class IobCobCalculatorPlugin @Inject constructor(
duration = 240,
rate = 0.0,
isAbsolute = true,
type = TemporaryBasal.Type.NORMAL)
type = TemporaryBasal.Type.NORMAL
)
if (t.timestamp < time) {
val profile = profileFunction.getProfile(t.timestamp)
if (profile != null) {
@ -400,7 +416,7 @@ class IobCobCalculatorPlugin @Inject constructor(
}
ads.newHistoryData(time, aapsLogger, dateUtil)
}
runCalculation("onEventNewHistoryData", System.currentTimeMillis(), bgDataReload, true, event)
runCalculation(event.javaClass.simpleName, System.currentTimeMillis(), bgDataReload, true, event)
//log.debug("Releasing onNewHistoryData");
}

View file

@ -20,7 +20,6 @@ import info.nightscout.androidaps.database.entities.ProfileSwitch
import info.nightscout.androidaps.database.interfaces.end
import info.nightscout.androidaps.dialogs.BolusProgressDialog
import info.nightscout.androidaps.events.EventBolusRequested
import info.nightscout.androidaps.events.EventNewBasalProfile
import info.nightscout.androidaps.events.EventProfileSwitchChanged
import info.nightscout.androidaps.extensions.getCustomizedName
import info.nightscout.androidaps.interfaces.*
@ -80,8 +79,7 @@ class CommandQueue @Inject constructor(
.toObservable(EventProfileSwitchChanged::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({
if (config.NSCLIENT) { // Effective profileswitch should be synced over NS
rxBus.send(EventNewBasalProfile())
if (config.NSCLIENT) { // Effective profileswitch should be synced over NS, do not create EffectiveProfileSwitch here
return@subscribe
}
aapsLogger.debug(LTag.PROFILE, "onProfileSwitch")
@ -110,7 +108,6 @@ class CommandQueue @Inject constructor(
insulinConfiguration = it.insulinConfiguration
)
)
rxBus.send(EventNewBasalProfile())
}
}
})

View file

@ -0,0 +1,14 @@
package info.nightscout.androidaps.events
import info.nightscout.androidaps.database.entities.EffectiveProfileSwitch
class EventEffectiveProfileSwitchChanged(effectiveProfileSwitch: EffectiveProfileSwitch?) : Event() {
var startDate: Long = 0
init {
effectiveProfileSwitch?.let { startDate = it.timestamp}
}
constructor(startDate: Long) : this(null) {
this.startDate = startDate
}
}