From 10d92b89fda5a0b9b40ae61272af0c59d9d71597 Mon Sep 17 00:00:00 2001 From: Philoul Date: Sun, 10 Jul 2022 00:35:54 +0200 Subject: [PATCH] Avoid AutotuneRun from automation if Run detected --- app/build.gradle | 2 +- .../general/autotune/AutotuneFragment.kt | 1 - .../plugins/general/autotune/AutotunePlugin.kt | 8 ++++++-- .../automation/actions/ActionRunAutotune.kt | 18 ++++++++++++------ .../androidaps/interfaces/Autotune.kt | 1 + core/src/main/res/values/strings.xml | 1 + 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ffa92cddd2..da06e17acd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -105,7 +105,7 @@ android { defaultConfig { multiDexEnabled true versionCode 1500 - version "3.0.0.2-dev-n" + version "3.0.0.2-autotune-dev-n0" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"' diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt index 9fcc4b8db2..0f2834c90e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt @@ -100,7 +100,6 @@ class AutotuneFragment : DaggerFragment() { ) binding.autotuneRun.setOnClickListener { val daysBack = SafeParse.stringToInt(binding.tuneDays.text) - autotunePlugin.calculationRunning = true autotunePlugin.lastNbDays = daysBack.toString() log("Run Autotune $profileName, $daysBack days") Thread { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotunePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotunePlugin.kt index e379f22026..6a4f3d6a94 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotunePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotunePlugin.kt @@ -22,6 +22,7 @@ import info.nightscout.androidaps.utils.MidnightTime import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.interfaces.BuildHelper import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.logging.LTag import info.nightscout.shared.sharedPreferences.SP import org.json.JSONException import org.json.JSONObject @@ -65,7 +66,7 @@ class AutotunePlugin @Inject constructor( ), Autotune { @Volatile override var lastRunSuccess: Boolean = false @Volatile var result: String = "" - @Volatile var calculationRunning: Boolean = false + @Volatile override var calculationRunning: Boolean = false @Volatile var lastRun: Long = 0 @Volatile var selectedProfile = "" @Volatile var lastNbDays: String = "" @@ -76,9 +77,12 @@ class AutotunePlugin @Inject constructor( private lateinit var profile: Profile val autotuneStartHour: Int = 4 - @Synchronized override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String) { lastRunSuccess = false + if (calculationRunning) { + aapsLogger.debug(LTag.AUTOMATION, "Autotune run detected, Autotune Run Cancelled") + return + } calculationRunning = true tunedProfile = null updateButtonVisibility = View.GONE diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionRunAutotune.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionRunAutotune.kt index c2687fffed..b1da120478 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionRunAutotune.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionRunAutotune.kt @@ -42,13 +42,19 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { val profileName = if (inputProfileName.value == rh.gs(R.string.active)) "" else inputProfileName.value var message = if (autoSwitch) R.string.autotune_run_with_autoswitch else R.string.autotune_run_without_autoswitch Thread { - autotunePlugin.atLog("[Automation] Run Autotune $profileName, ${daysBack.value} days, Autoswitch $autoSwitch") - autotunePlugin.aapsAutotune(daysBack.value, autoSwitch, profileName) - if (!autotunePlugin.lastRunSuccess) { - message = R.string.autotune_run_with_error - aapsLogger.error(LTag.AUTOMATION, "Error during Autotune Run") + if (!autotunePlugin.calculationRunning) { + autotunePlugin.atLog("[Automation] Run Autotune $profileName, ${daysBack.value} days, Autoswitch $autoSwitch") + autotunePlugin.aapsAutotune(daysBack.value, autoSwitch, profileName) + if (!autotunePlugin.lastRunSuccess) { + message = R.string.autotune_run_with_error + aapsLogger.error(LTag.AUTOMATION, "Error during Autotune Run") + } + callback.result(PumpEnactResult(injector).success(autotunePlugin.lastRunSuccess).comment(message)).run() + } else { + message = R.string.autotune_run_cancelled + aapsLogger.debug(LTag.AUTOMATION, "Autotune run detected, Autotune Run Cancelled") + callback.result(PumpEnactResult(injector).success(false).comment(message)).run() } - callback.result(PumpEnactResult(injector).success(autotunePlugin.lastRunSuccess).comment(message)).run() }.start() return } diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt index 83ebcd2530..93c20f71d5 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt @@ -6,4 +6,5 @@ interface Autotune { fun atLog(message: String) var lastRunSuccess: Boolean + var calculationRunning: Boolean } \ No newline at end of file diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 30974f7512..3f5832b0cd 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -600,6 +600,7 @@ Autotune runned without profile switch Autotune runned and profile automatically switched Error during last Autotune run + Another run of Autotune is detected, run cancelled %1$d day