Synchronized aapsAutotune call to avoid multi-thread conflicts

This commit is contained in:
Philoul 2022-07-08 23:13:12 +02:00
parent 32ccd8583c
commit 6b1f495c28
2 changed files with 24 additions and 11 deletions

View file

@ -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 {

View file

@ -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