diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt index 35ef24854b..d31c51f8e4 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -11,6 +11,7 @@ import info.nightscout.androidaps.database.AppRepository import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin +import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.configBuilder.PluginStore import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctionImplementation @@ -22,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.HardLimits import info.nightscout.androidaps.utils.androidNotification.NotificationHolderImpl import info.nightscout.androidaps.utils.buildHelper.BuildHelper import info.nightscout.androidaps.utils.buildHelper.BuildHelperImpl @@ -69,8 +71,15 @@ open class AppModule { @Provides @Singleton - fun provideProfileFunction(aapsLogger: AAPSLogger, sp: SP, resourceHelper: ResourceHelper, activePlugin: ActivePlugin, repository: AppRepository, dateUtil: DateUtil): ProfileFunction = - ProfileFunctionImplementation(aapsLogger, sp, resourceHelper, activePlugin, repository, dateUtil) + fun provideProfileFunction( + aapsLogger: AAPSLogger, sp: SP, rxBus: RxBusWrapper, resourceHelper: + ResourceHelper, activePlugin: + ActivePlugin, repository: AppRepository, dateUtil: DateUtil, config: Config, hardLimits: HardLimits + ): ProfileFunction = + ProfileFunctionImplementation( + aapsLogger, sp, rxBus, resourceHelper, activePlugin, repository, dateUtil, + config, hardLimits + ) @Module interface AppBindings { 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 5cf7bb4371..83b8185da4 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 @@ -9,14 +9,12 @@ import info.nightscout.androidaps.database.ValueWrapper import info.nightscout.androidaps.database.entities.ProfileSwitch import info.nightscout.androidaps.database.transactions.InsertOrUpdateProfileSwitch import info.nightscout.androidaps.extensions.fromConstant -import info.nightscout.androidaps.interfaces.ActivePlugin -import info.nightscout.androidaps.interfaces.GlucoseUnit -import info.nightscout.androidaps.interfaces.Profile -import info.nightscout.androidaps.interfaces.ProfileFunction -import info.nightscout.androidaps.interfaces.ProfileStore +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.HardLimits import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP @@ -30,10 +28,13 @@ import javax.inject.Singleton class ProfileFunctionImplementation @Inject constructor( private val aapsLogger: AAPSLogger, private val sp: SP, + private val rxBus: RxBusWrapper, private val resourceHelper: ResourceHelper, private val activePlugin: ActivePlugin, private val repository: AppRepository, - private val dateUtil: DateUtil + private val dateUtil: DateUtil, + private val config: Config, + private val hardLimits: HardLimits ) : ProfileFunction { val cache = LongSparseArray() @@ -115,35 +116,39 @@ class ProfileFunctionImplementation @Inject constructor( val ps = buildProfileSwitch(profileStore, profileName, durationInMinutes, percentage, timeShiftInHours, timestamp) 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) - }) + 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) + }) } - override fun createProfileSwitch(durationInMinutes: Int, percentage: Int, timeShiftInHours: Int) { + override fun createProfileSwitch(durationInMinutes: Int, percentage: Int, timeShiftInHours: Int): Boolean { 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 + val profileStore = activePlugin.activeProfileSource.profile ?: return false + val ps = buildProfileSwitch(profileStore, profile.profileName, durationInMinutes, percentage, 0, dateUtil.now()) + val validity = ProfileSealed.PS(ps).isValid( + resourceHelper.gs(info.nightscout.androidaps.automation.R.string.careportal_profileswitch), + activePlugin.activePump, + config, + resourceHelper, + rxBus, + hardLimits ) - 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) - }) + var returnValue = true + if (validity.isValid) { + repository.runTransactionForResult(InsertOrUpdateProfileSwitch(ps)) + .doOnError { + aapsLogger.error(LTag.DATABASE, "Error while saving ProfileSwitch", it) + returnValue = false + } + .blockingGet() + .also { result -> + result.inserted.forEach { aapsLogger.debug(LTag.DATABASE, "Inserted ProfileSwitch $it") } + result.updated.forEach { aapsLogger.debug(LTag.DATABASE, "Updated ProfileSwitch $it") } + } + } else returnValue = false + return returnValue } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewData.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewData.kt index 076c4de02e..2a0b523200 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewData.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewData.kt @@ -72,9 +72,6 @@ class OverviewData @Inject constructor( var endTime: Long = 0 fun reset() { - profile = null - profileName = null - profileNameWithRemainingTime = null calcProgress = "" lastBg = null temporaryBasal = null @@ -125,27 +122,6 @@ class OverviewData @Inject constructor( endTime = toTime } - /* - * PROFILE - */ - var profile: Profile? = null - var profileName: String? = null - var profileNameWithRemainingTime: String? = null - - val profileBackgroundColor: Int - get() = - profile?.let { profile -> - if (profile.percentage != 100 || profile.timeshift != 0) resourceHelper.gc(R.color.ribbonWarning) - else resourceHelper.gc(R.color.ribbonDefault) - } ?: resourceHelper.gc(R.color.ribbonTextDefault) - - val profileTextColor: Int - get() = - profile?.let { profile -> - if (profile.percentage != 100 || profile.timeshift != 0) resourceHelper.gc(R.color.ribbonTextWarning) - else resourceHelper.gc(R.color.ribbonTextDefault) - } ?: resourceHelper.gc(R.color.ribbonTextDefault) - /* * CALC PROGRESS */ @@ -181,14 +157,14 @@ class OverviewData @Inject constructor( val temporaryBasalText: String get() = - profile?.let { profile -> + profileFunction.getProfile()?.let { profile -> if (temporaryBasal?.isInProgress == false) temporaryBasal = null temporaryBasal?.let { "T:" + it.toStringShort() } ?: resourceHelper.gs(R.string.pump_basebasalrate, profile.getBasal()) } ?: resourceHelper.gs(R.string.notavailable) val temporaryBasalDialogText: String - get() = profile?.let { profile -> + get() = profileFunction.getProfile()?.let { profile -> temporaryBasal?.let { temporaryBasal -> "${resourceHelper.gs(R.string.basebasalrate_label)}: ${resourceHelper.gs(R.string.pump_basebasalrate, profile.getBasal())}" + "\n" + resourceHelper.gs(R.string.tempbasal_label) + ": " + temporaryBasal.toStringFull(profile, dateUtil) @@ -198,7 +174,7 @@ class OverviewData @Inject constructor( val temporaryBasalIcon: Int get() = - profile?.let { profile -> + profileFunction.getProfile()?.let { profile -> temporaryBasal?.let { temporaryBasal -> val percentRate = temporaryBasal.convertedToPercent(dateUtil.now(), profile) when { @@ -510,7 +486,7 @@ class OverviewData @Inject constructor( @Synchronized fun prepareTemporaryTargetData(from: String) { // val start = dateUtil.now() - val profile = profile ?: return + val profile = profileFunction.getProfile() ?: return val units = profileFunction.getUnits() var toTime = toTime val targetsSeriesArray: MutableList = java.util.ArrayList() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt index 9da0246b70..e02b359db6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt @@ -26,6 +26,7 @@ import dagger.android.HasAndroidInjector import dagger.android.support.DaggerFragment import info.nightscout.androidaps.Constants import info.nightscout.androidaps.R +import info.nightscout.androidaps.data.ProfileSealed import info.nightscout.androidaps.database.AppRepository import info.nightscout.androidaps.database.entities.UserEntry.Action import info.nightscout.androidaps.database.entities.UserEntry.Sources @@ -598,7 +599,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList fun updateGUI(from: String, what: OverviewData.Property) { // if (what != OverviewData.Property.CALC_PROGRESS) // aapsLogger.debug(LTag.UI, "UpdateGui $from $what") - if (overviewData.profile == null) { + if (profileFunction.getProfile() == null) { binding.loopPumpStatusLayout.pumpStatus.setText(R.string.noprofileset) binding.loopPumpStatusLayout.pumpStatusLayout.visibility = View.VISIBLE binding.loopPumpStatusLayout.loopLayout.visibility = View.GONE @@ -641,10 +642,25 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList } OverviewData.Property.PROFILE -> { - binding.loopPumpStatusLayout.activeProfile.text = overviewData.profileNameWithRemainingTime - ?: "" - binding.loopPumpStatusLayout.activeProfile.setBackgroundColor(overviewData.profileBackgroundColor) - binding.loopPumpStatusLayout.activeProfile.setTextColor(overviewData.profileTextColor) + val profileBackgroundColor = + profileFunction.getProfile()?.let { + val profile = (it as ProfileSealed.EPS).value + if (profile.originalPercentage != 100 || profile.originalTimeshift != 0L || profile.originalDuration != 0L) + resourceHelper.gc(R.color.ribbonWarning) + else resourceHelper.gc(R.color.ribbonDefault) + } ?: resourceHelper.gc(R.color.ribbonTextDefault) + + val profileTextColor = + profileFunction.getProfile()?.let { + val profile = (it as ProfileSealed.EPS).value + if (profile.originalPercentage != 100 || profile.originalTimeshift != 0L || profile.originalDuration != 0L) + resourceHelper.gc(R.color.ribbonTextWarning) + else resourceHelper.gc(R.color.ribbonTextDefault) + } ?: resourceHelper.gc(R.color.ribbonTextDefault) + + binding.loopPumpStatusLayout.activeProfile.text = profileFunction.getProfileNameWithRemainingTime() + binding.loopPumpStatusLayout.activeProfile.setBackgroundColor(profileBackgroundColor) + binding.loopPumpStatusLayout.activeProfile.setTextColor(profileTextColor) } OverviewData.Property.TEMPORARY_BASAL -> { @@ -710,7 +726,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList binding.loopPumpStatusLayout.tempTarget.text = Profile.toTargetRangeString(tempTarget.lowTarget, tempTarget.highTarget, GlucoseUnit.MGDL, units) + " " + dateUtil.untilString(tempTarget.end, resourceHelper) } else { // If the target is not the same as set in the profile then oref has overridden it - overviewData.profile?.let { profile -> + profileFunction.getProfile()?.let { profile -> val targetUsed = loopPlugin.lastRun?.constraintsProcessed?.targetBG ?: 0.0 if (targetUsed != 0.0 && abs(profile.getTargetMgdl() - targetUsed) > 0.01) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt index 9557cf8e4b..c5c0c35757 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt @@ -227,6 +227,7 @@ class OverviewPlugin @Inject constructor( if (runningRefresh) return runningRefresh = true loadIobCobResults(from) + overviewBus.send(EventUpdateOverview(from, OverviewData.Property.PROFILE)) overviewBus.send(EventUpdateOverview(from, OverviewData.Property.BG)) overviewBus.send(EventUpdateOverview(from, OverviewData.Property.TIME)) overviewBus.send(EventUpdateOverview(from, OverviewData.Property.TEMPORARY_BASAL)) @@ -264,9 +265,6 @@ class OverviewPlugin @Inject constructor( } private fun loadProfile(from: String) { - overviewData.profile = profileFunction.getProfile() - overviewData.profileName = profileFunction.getProfileName() - overviewData.profileNameWithRemainingTime = profileFunction.getProfileNameWithRemainingTime() overviewBus.send(EventUpdateOverview(from, OverviewData.Property.PROFILE)) } diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.kt index 546cafa12f..0634021564 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.kt @@ -9,6 +9,7 @@ import info.nightscout.androidaps.database.entities.UserEntry import info.nightscout.androidaps.database.entities.UserEntry.Sources import info.nightscout.androidaps.database.entities.ValueWithUnit import info.nightscout.androidaps.interfaces.ProfileFunction +import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.general.automation.elements.Comparator import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration @@ -43,10 +44,17 @@ class ActionProfileSwitchPercent(injector: HasAndroidInjector) : Action(injector } override fun doAction(callback: Callback) { - uel.log(UserEntry.Action.PROFILE_SWITCH, Sources.Automation, title + ": " + resourceHelper.gs(R.string.startprofile, pct.value.toInt(), duration.value), - ValueWithUnit.Percent(pct.value.toInt()), - ValueWithUnit.Minute(duration.value)) - profileFunction.createProfileSwitch(duration.value, pct.value.toInt(), 0) + if (profileFunction.createProfileSwitch(duration.value, pct.value.toInt(), 0)) { + uel.log( + UserEntry.Action.PROFILE_SWITCH, + Sources.Automation, + title + ": " + resourceHelper.gs(R.string.startprofile, pct.value.toInt(), duration.value), + ValueWithUnit.Percent(pct.value.toInt()), + ValueWithUnit.Minute(duration.value) + ) + } else { + aapsLogger.error(LTag.AUTOMATION, "Final profile not valid") + } callback.result(PumpEnactResult(injector).success(true).comment(R.string.ok))?.run() } diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt index 9f58616ed9..1a4b228398 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt @@ -145,7 +145,7 @@ abstract class Trigger(val injector: HasAndroidInjector) { TriggerWifiSsid::class.java.simpleName -> TriggerWifiSsid(injector).fromJSON( data.toString() ) - else -> throw ClassNotFoundException(type) + else -> TriggerConnector(injector) } } diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/ProfileFunction.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/ProfileFunction.kt index 3cc7939446..03b2f03590 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/ProfileFunction.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/ProfileFunction.kt @@ -76,8 +76,9 @@ interface ProfileFunction { * @param durationInMinutes * @param percentage 100 = no modification * @param timeShiftInHours 0 = no modification + * @return true if profile switch is created */ - fun createProfileSwitch(durationInMinutes: Int, percentage: Int, timeShiftInHours: Int) + fun createProfileSwitch(durationInMinutes: Int, percentage: Int, timeShiftInHours: Int): Boolean /* * Midnight time conversion diff --git a/gradle.properties b/gradle.properties index 43ad963b2f..489d41472f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,8 +16,8 @@ # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects org.gradle.parallel=true - +org.gradle.warning.mode=all org.gradle.jvmargs=-Xmx2g android.enableJetifier=true -android.useAndroidX=true \ No newline at end of file +android.useAndroidX=true