From 6b1f495c283d3b66b2620582efb123054dff4029 Mon Sep 17 00:00:00 2001 From: Philoul Date: Fri, 8 Jul 2022 23:13:12 +0200 Subject: [PATCH] Synchronized aapsAutotune call to avoid multi-thread conflicts --- .../general/autotune/AutotunePlugin.kt | 33 +++++++++++++------ .../androidaps/interfaces/Autotune.kt | 2 +- 2 files changed, 24 insertions(+), 11 deletions(-) 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 5f926c28c5..e379f22026 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 @@ -76,21 +76,31 @@ class AutotunePlugin @Inject constructor( private lateinit var profile: Profile val autotuneStartHour: Int = 4 - override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String): String { + @Synchronized + override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String) { + lastRunSuccess = false + calculationRunning = true tunedProfile = null updateButtonVisibility = View.GONE - lastRunSuccess = false var logResult = "" result = "" if (profileFunction.getProfile() == null) { result = rh.gs(R.string.profileswitch_ismissing) - return result + rxBus.send(EventAutotuneUpdateGui()) + calculationRunning = false + return } val detailedLog = sp.getBoolean(R.string.key_autotune_additional_log, false) calculationRunning = true lastNbDays = "" + daysBack lastRun = dateUtil.now() - val profileStore = activePlugin.activeProfileSource.profile ?: return rh.gs(R.string.profileswitch_ismissing) + val profileStore = activePlugin.activeProfileSource.profile + if (profileStore == null) { + result = rh.gs(R.string.profileswitch_ismissing) + rxBus.send(EventAutotuneUpdateGui()) + calculationRunning = false + return + } selectedProfile = if (profileToTune.isEmpty()) profileFunction.getProfileName() else profileToTune profileFunction.getProfile()?.let { currentProfile -> profile = profileStore.getSpecificProfile(profileToTune)?.let { ProfileSealed.Pure(it) } ?: currentProfile @@ -144,11 +154,11 @@ class AutotunePlugin @Inject constructor( if (tunedProfile == null) { result = rh.gs(R.string.autotune_error) log("TunedProfile is null on day ${i + 1}") - calculationRunning = false - rxBus.send(EventAutotuneUpdateGui()) autotuneFS.exportResult(result) autotuneFS.exportLogAndZip(lastRun) - return result + rxBus.send(EventAutotuneUpdateGui()) + calculationRunning = false + return } } result = rh.gs(R.string.autotune_result, dateUtil.dateAndTimeString(lastRun)) @@ -193,13 +203,16 @@ class AutotunePlugin @Inject constructor( } tunedProfile?.let { - lastRunSuccess = true saveLastRun() + lastRunSuccess = true rxBus.send(EventAutotuneUpdateGui()) calculationRunning = false - return result + return } - return rh.gs(R.string.autotune_error) + result = rh.gs(R.string.autotune_error) + rxBus.send(EventAutotuneUpdateGui()) + calculationRunning = false + return } private fun showResults(tunedProfile: ATProfile?, pumpProfile: ATProfile): String { 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 b90934475e..83ebcd2530 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt @@ -2,7 +2,7 @@ package info.nightscout.androidaps.interfaces interface Autotune { - fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String = ""): String + fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String = "") fun atLog(message: String) var lastRunSuccess: Boolean