Merge pull request #1902 from Philoul/Fix/AutotuneIobCrashes
AutotuneIob Fix Crash
This commit is contained in:
commit
b029fc4804
7 changed files with 45 additions and 16 deletions
|
@ -105,7 +105,7 @@ android {
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
multiDexEnabled true
|
multiDexEnabled true
|
||||||
versionCode 1500
|
versionCode 1500
|
||||||
version "3.0.0.2-dev-n"
|
version "3.0.0.2-autotune-dev-n0"
|
||||||
buildConfigField "String", "VERSION", '"' + version + '"'
|
buildConfigField "String", "VERSION", '"' + version + '"'
|
||||||
buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"'
|
buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"'
|
||||||
buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"'
|
buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"'
|
||||||
|
|
|
@ -100,7 +100,6 @@ class AutotuneFragment : DaggerFragment() {
|
||||||
)
|
)
|
||||||
binding.autotuneRun.setOnClickListener {
|
binding.autotuneRun.setOnClickListener {
|
||||||
val daysBack = SafeParse.stringToInt(binding.tuneDays.text)
|
val daysBack = SafeParse.stringToInt(binding.tuneDays.text)
|
||||||
autotunePlugin.calculationRunning = true
|
|
||||||
autotunePlugin.lastNbDays = daysBack.toString()
|
autotunePlugin.lastNbDays = daysBack.toString()
|
||||||
log("Run Autotune $profileName, $daysBack days")
|
log("Run Autotune $profileName, $daysBack days")
|
||||||
Thread {
|
Thread {
|
||||||
|
|
|
@ -59,14 +59,28 @@ open class AutotuneIob @Inject constructor(
|
||||||
initializeTreatmentData(from - range(), to)
|
initializeTreatmentData(from - range(), to)
|
||||||
initializeTempBasalData(from - range(), to, tunedProfile)
|
initializeTempBasalData(from - range(), to, tunedProfile)
|
||||||
initializeExtendedBolusData(from - range(), to, tunedProfile)
|
initializeExtendedBolusData(from - range(), to, tunedProfile)
|
||||||
tempBasals = ArrayList(tempBasals.toList().sortedWith { o1: TemporaryBasal, o2: TemporaryBasal -> (o2.timestamp - o1.timestamp).toInt() })
|
sortTempBasal()
|
||||||
// Without Neutral TBR, Autotune Web will ignore iob for periods without TBR running
|
addNeutralTempBasal(from - range(), to, tunedProfile) // Without Neutral TBR, Autotune Web will ignore iob for periods without TBR running
|
||||||
addNeutralTempBasal(from - range(), to, tunedProfile)
|
sortNsTreatments()
|
||||||
nsTreatments = ArrayList(nsTreatments.toList().sortedWith { o1: NsTreatment, o2: NsTreatment -> (o2.date - o1.date).toInt() })
|
sortBoluses()
|
||||||
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)
|
aapsLogger.debug(LTag.AUTOTUNE, "Nb Treatments: " + nsTreatments.size + " Nb meals: " + meals.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
private fun sortTempBasal() {
|
||||||
|
tempBasals = ArrayList(tempBasals.toList().sortedWith { o1: TemporaryBasal, o2: TemporaryBasal -> (o2.timestamp - o1.timestamp).toInt() })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
private fun sortNsTreatments() {
|
||||||
|
nsTreatments = ArrayList(nsTreatments.toList().sortedWith { o1: NsTreatment, o2: NsTreatment -> (o2.date - o1.date).toInt() })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
private fun sortBoluses() {
|
||||||
|
boluses = ArrayList(boluses.toList().sortedWith { o1: Bolus, o2: Bolus -> (o2.timestamp - o1.timestamp).toInt() })
|
||||||
|
}
|
||||||
|
|
||||||
private fun initializeBgreadings(from: Long, to: Long) {
|
private fun initializeBgreadings(from: Long, to: Long) {
|
||||||
glucose = repository.compatGetBgReadingsDataFromTime(from, to, false).blockingGet()
|
glucose = repository.compatGetBgReadingsDataFromTime(from, to, false).blockingGet()
|
||||||
}
|
}
|
||||||
|
@ -146,6 +160,7 @@ open class AutotuneIob @Inject constructor(
|
||||||
|
|
||||||
// addNeutralTempBasal will add a fake neutral TBR (100%) to have correct basal rate in exported file for periods without TBR running
|
// addNeutralTempBasal will add a fake neutral TBR (100%) to have correct basal rate in exported file for periods without TBR running
|
||||||
// to be able to compare results between oref0 algo and aaps
|
// to be able to compare results between oref0 algo and aaps
|
||||||
|
@Synchronized
|
||||||
private fun addNeutralTempBasal(from: Long, to: Long, tunedProfile: ATProfile) {
|
private fun addNeutralTempBasal(from: Long, to: Long, tunedProfile: ATProfile) {
|
||||||
var previousStart = to
|
var previousStart = to
|
||||||
for (i in tempBasals.indices) {
|
for (i in tempBasals.indices) {
|
||||||
|
@ -180,6 +195,7 @@ open class AutotuneIob @Inject constructor(
|
||||||
|
|
||||||
// toSplittedTimestampTB will split all TBR across hours in different TBR with correct absolute value to be sure to have correct basal rate
|
// toSplittedTimestampTB will split all TBR across hours in different TBR with correct absolute value to be sure to have correct basal rate
|
||||||
// even if profile rate is not the same
|
// even if profile rate is not the same
|
||||||
|
@Synchronized
|
||||||
private fun toSplittedTimestampTB(tb: TemporaryBasal, tunedProfile: ATProfile) {
|
private fun toSplittedTimestampTB(tb: TemporaryBasal, tunedProfile: ATProfile) {
|
||||||
var splittedTimestamp = tb.timestamp
|
var splittedTimestamp = tb.timestamp
|
||||||
val cutInMilliSec = T.mins(60).msecs() //30 min to compare with oref0, 60 min to improve accuracy
|
val cutInMilliSec = T.mins(60).msecs() //30 min to compare with oref0, 60 min to improve accuracy
|
||||||
|
@ -295,7 +311,7 @@ open class AutotuneIob @Inject constructor(
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
fun glucoseToJSON(): String {
|
fun glucoseToJSON(): String {
|
||||||
val glucoseJson = JSONArray()
|
val glucoseJson = JSONArray()
|
||||||
for (bgreading in glucose)
|
for (bgreading in glucose)
|
||||||
|
@ -303,6 +319,7 @@ open class AutotuneIob @Inject constructor(
|
||||||
return glucoseJson.toString(2)
|
return glucoseJson.toString(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
fun bolusesToJSON(): String {
|
fun bolusesToJSON(): String {
|
||||||
val bolusesJson = JSONArray()
|
val bolusesJson = JSONArray()
|
||||||
for (bolus in boluses)
|
for (bolus in boluses)
|
||||||
|
@ -310,6 +327,7 @@ open class AutotuneIob @Inject constructor(
|
||||||
return bolusesJson.toString(2)
|
return bolusesJson.toString(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
fun nsHistoryToJSON(): String {
|
fun nsHistoryToJSON(): String {
|
||||||
val json = JSONArray()
|
val json = JSONArray()
|
||||||
for (t in nsTreatments) {
|
for (t in nsTreatments) {
|
||||||
|
|
|
@ -22,6 +22,7 @@ import info.nightscout.androidaps.utils.MidnightTime
|
||||||
import info.nightscout.androidaps.utils.T
|
import info.nightscout.androidaps.utils.T
|
||||||
import info.nightscout.androidaps.interfaces.BuildHelper
|
import info.nightscout.androidaps.interfaces.BuildHelper
|
||||||
import info.nightscout.shared.logging.AAPSLogger
|
import info.nightscout.shared.logging.AAPSLogger
|
||||||
|
import info.nightscout.shared.logging.LTag
|
||||||
import info.nightscout.shared.sharedPreferences.SP
|
import info.nightscout.shared.sharedPreferences.SP
|
||||||
import org.json.JSONException
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
@ -65,7 +66,7 @@ class AutotunePlugin @Inject constructor(
|
||||||
), Autotune {
|
), Autotune {
|
||||||
@Volatile override var lastRunSuccess: Boolean = false
|
@Volatile override var lastRunSuccess: Boolean = false
|
||||||
@Volatile var result: String = ""
|
@Volatile var result: String = ""
|
||||||
@Volatile var calculationRunning: Boolean = false
|
@Volatile override var calculationRunning: Boolean = false
|
||||||
@Volatile var lastRun: Long = 0
|
@Volatile var lastRun: Long = 0
|
||||||
@Volatile var selectedProfile = ""
|
@Volatile var selectedProfile = ""
|
||||||
@Volatile var lastNbDays: String = ""
|
@Volatile var lastNbDays: String = ""
|
||||||
|
@ -76,9 +77,12 @@ class AutotunePlugin @Inject constructor(
|
||||||
private lateinit var profile: Profile
|
private lateinit var profile: Profile
|
||||||
val autotuneStartHour: Int = 4
|
val autotuneStartHour: Int = 4
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String) {
|
override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String) {
|
||||||
lastRunSuccess = false
|
lastRunSuccess = false
|
||||||
|
if (calculationRunning) {
|
||||||
|
aapsLogger.debug(LTag.AUTOMATION, "Autotune run detected, Autotune Run Cancelled")
|
||||||
|
return
|
||||||
|
}
|
||||||
calculationRunning = true
|
calculationRunning = true
|
||||||
tunedProfile = null
|
tunedProfile = null
|
||||||
updateButtonVisibility = View.GONE
|
updateButtonVisibility = View.GONE
|
||||||
|
|
|
@ -42,13 +42,19 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) {
|
||||||
val profileName = if (inputProfileName.value == rh.gs(R.string.active)) "" else inputProfileName.value
|
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
|
var message = if (autoSwitch) R.string.autotune_run_with_autoswitch else R.string.autotune_run_without_autoswitch
|
||||||
Thread {
|
Thread {
|
||||||
autotunePlugin.atLog("[Automation] Run Autotune $profileName, ${daysBack.value} days, Autoswitch $autoSwitch")
|
if (!autotunePlugin.calculationRunning) {
|
||||||
autotunePlugin.aapsAutotune(daysBack.value, autoSwitch, profileName)
|
autotunePlugin.atLog("[Automation] Run Autotune $profileName, ${daysBack.value} days, Autoswitch $autoSwitch")
|
||||||
if (!autotunePlugin.lastRunSuccess) {
|
autotunePlugin.aapsAutotune(daysBack.value, autoSwitch, profileName)
|
||||||
message = R.string.autotune_run_with_error
|
if (!autotunePlugin.lastRunSuccess) {
|
||||||
aapsLogger.error(LTag.AUTOMATION, "Error during Autotune Run")
|
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()
|
}.start()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,4 +6,5 @@ interface Autotune {
|
||||||
fun atLog(message: String)
|
fun atLog(message: String)
|
||||||
|
|
||||||
var lastRunSuccess: Boolean
|
var lastRunSuccess: Boolean
|
||||||
|
var calculationRunning: Boolean
|
||||||
}
|
}
|
|
@ -600,6 +600,7 @@
|
||||||
<string name="autotune_run_without_autoswitch">Autotune runned without profile switch</string>
|
<string name="autotune_run_without_autoswitch">Autotune runned without profile switch</string>
|
||||||
<string name="autotune_run_with_autoswitch">Autotune runned and profile automatically switched</string>
|
<string name="autotune_run_with_autoswitch">Autotune runned and profile automatically switched</string>
|
||||||
<string name="autotune_run_with_error">Error during last Autotune run</string>
|
<string name="autotune_run_with_error">Error during last Autotune run</string>
|
||||||
|
<string name="autotune_run_cancelled">Another run of Autotune is detected, run cancelled</string>
|
||||||
|
|
||||||
<plurals name="days">
|
<plurals name="days">
|
||||||
<item quantity="one">%1$d day</item>
|
<item quantity="one">%1$d day</item>
|
||||||
|
|
Loading…
Reference in a new issue