Merge pull request #1900 from Philoul/Fix/CrashAutotune2

Autotune Crash during sort
This commit is contained in:
Milos Kozak 2022-07-09 18:02:31 +02:00 committed by GitHub
commit df76c8cee1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 15 deletions

View file

@ -39,7 +39,7 @@ open class AutotuneIob @Inject constructor(
private val autotuneFS: AutotuneFS
) {
private val nsTreatments = ArrayList<NsTreatment>()
private var nsTreatments = ArrayList<NsTreatment>()
private var dia: Double = Constants.defaultDIA
var boluses: ArrayList<Bolus> = ArrayList()
var meals = ArrayList<Carbs>()
@ -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)
}

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