From 6b1f495c283d3b66b2620582efb123054dff4029 Mon Sep 17 00:00:00 2001 From: Philoul Date: Fri, 8 Jul 2022 23:13:12 +0200 Subject: [PATCH 1/2] 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 From 4c52081a8fecce7420600fd95be71f74dc977025 Mon Sep 17 00:00:00 2001 From: Philoul Date: Sat, 9 Jul 2022 00:57:23 +0200 Subject: [PATCH 2/2] Prevent NPE (Trial) convert ArrayList to List to execute Sort --- .../androidaps/plugins/general/autotune/AutotuneIob.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneIob.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneIob.kt index bac89d9f9e..925d290ee4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneIob.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneIob.kt @@ -39,7 +39,7 @@ open class AutotuneIob @Inject constructor( private val autotuneFS: AutotuneFS ) { - private val nsTreatments = ArrayList() + private var nsTreatments = ArrayList() private var dia: Double = Constants.defaultDIA var boluses: ArrayList = ArrayList() var meals = ArrayList() @@ -59,11 +59,11 @@ open class AutotuneIob @Inject constructor( initializeTreatmentData(from - range(), to) initializeTempBasalData(from - range(), to, tunedProfile) initializeExtendedBolusData(from - range(), to, tunedProfile) - tempBasals.sortWith { o1: TemporaryBasal, o2: TemporaryBasal -> (o2.timestamp - o1.timestamp).toInt() } + tempBasals = ArrayList(tempBasals.toList().sortedWith { o1: TemporaryBasal, o2: TemporaryBasal -> (o2.timestamp - o1.timestamp).toInt() }) // Without Neutral TBR, Autotune Web will ignore iob for periods without TBR running addNeutralTempBasal(from - range(), to, tunedProfile) - nsTreatments.sortWith { o1: NsTreatment, o2: NsTreatment -> (o2.date - o1.date).toInt() } - this.boluses.sortWith { o1: Bolus, o2: Bolus -> (o2.timestamp - o1.timestamp).toInt() } + nsTreatments = ArrayList(nsTreatments.toList().sortedWith { o1: NsTreatment, o2: NsTreatment -> (o2.date - o1.date).toInt() }) + boluses = ArrayList(boluses.toList().sortedWith { o1: Bolus, o2: Bolus -> (o2.timestamp - o1.timestamp).toInt() }) aapsLogger.debug(LTag.AUTOTUNE, "Nb Treatments: " + nsTreatments.size + " Nb meals: " + meals.size) }