Save Autotune lastRun and Load LastRun after AAPS restart

This commit is contained in:
Philoul 2022-06-06 12:53:00 +02:00
parent 7fac5a87f3
commit 02d8e9928e
2 changed files with 53 additions and 2 deletions

View file

@ -79,7 +79,7 @@ class AutotuneFragment : DaggerFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
sp.putBoolean(R.string.key_autotune_tune_insulin_curve, false) // put to false tune insulin curve sp.putBoolean(R.string.key_autotune_tune_insulin_curve, false) // put to false tune insulin curve
autotunePlugin.lastRun = sp.getLong(R.string.key_autotune_last_run, 0) autotunePlugin.loadLastRun()
if (autotunePlugin.lastNbDays.isEmpty()) if (autotunePlugin.lastNbDays.isEmpty())
autotunePlugin.lastNbDays = sp.getInt(R.string.key_autotune_default_tune_days, 5).toString() autotunePlugin.lastNbDays = sp.getInt(R.string.key_autotune_default_tune_days, 5).toString()
val defaultValue = sp.getInt(R.string.key_autotune_default_tune_days, 5).toDouble() val defaultValue = sp.getInt(R.string.key_autotune_default_tune_days, 5).toDouble()
@ -144,6 +144,7 @@ class AutotuneFragment : DaggerFragment() {
autotunePlugin.tunedProfile?.profilename = localName autotunePlugin.tunedProfile?.profilename = localName
autotunePlugin.updateProfile(autotunePlugin.tunedProfile) autotunePlugin.updateProfile(autotunePlugin.tunedProfile)
autotunePlugin.updateButtonVisibility = View.GONE autotunePlugin.updateButtonVisibility = View.GONE
autotunePlugin.saveLastRun()
uel.log( uel.log(
UserEntry.Action.STORE_PROFILE, UserEntry.Action.STORE_PROFILE,
UserEntry.Sources.Autotune, UserEntry.Sources.Autotune,
@ -163,6 +164,7 @@ class AutotuneFragment : DaggerFragment() {
autotunePlugin.tunedProfile?.profilename = "" autotunePlugin.tunedProfile?.profilename = ""
autotunePlugin.updateProfile(autotunePlugin.pumpProfile) autotunePlugin.updateProfile(autotunePlugin.pumpProfile)
autotunePlugin.updateButtonVisibility = View.VISIBLE autotunePlugin.updateButtonVisibility = View.VISIBLE
autotunePlugin.saveLastRun()
uel.log( uel.log(
UserEntry.Action.STORE_PROFILE, UserEntry.Action.STORE_PROFILE,
UserEntry.Sources.Autotune, UserEntry.Sources.Autotune,

View file

@ -7,6 +7,7 @@ import info.nightscout.androidaps.data.LocalInsulin
import info.nightscout.androidaps.data.ProfileSealed import info.nightscout.androidaps.data.ProfileSealed
import info.nightscout.androidaps.database.entities.UserEntry import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.ValueWithUnit import info.nightscout.androidaps.database.entities.ValueWithUnit
import info.nightscout.androidaps.extensions.pureProfileFromJson
import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.logging.UserEntryLogger
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
@ -16,6 +17,7 @@ import info.nightscout.androidaps.plugins.general.autotune.events.EventAutotuneU
import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin
import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.JsonHelper
import info.nightscout.androidaps.utils.MidnightTime import info.nightscout.androidaps.utils.MidnightTime
import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.buildHelper.BuildHelper import info.nightscout.androidaps.utils.buildHelper.BuildHelper
@ -187,7 +189,7 @@ class AutotunePlugin @Inject constructor(
} }
} }
lastRunSuccess = true lastRunSuccess = true
sp.putLong(R.string.key_autotune_last_run, lastRun) saveLastRun()
rxBus.send(EventAutotuneUpdateGui()) rxBus.send(EventAutotuneUpdateGui())
calculationRunning = false calculationRunning = false
tunedProfile?.let { tunedProfile?.let {
@ -296,6 +298,53 @@ class AutotunePlugin @Inject constructor(
localProfilePlugin.storeSettings() localProfilePlugin.storeSettings()
} }
fun saveLastRun() {
val json = JSONObject()
json.put("lastNbDays", lastNbDays)
json.put("lastRun",lastRun)
json.put("pumpProfile", pumpProfile.profile.toPureNsJson(dateUtil))
json.put("pumpProfileName", pumpProfile.profilename)
json.put("pumpPeak", pumpProfile.peak)
json.put("pumpDia", pumpProfile.dia)
json.put("tunedProfile", tunedProfile?.profile?.toPureNsJson(dateUtil))
json.put("tunedCircadianProfile", tunedProfile?.circadianProfile?.toPureNsJson(dateUtil))
json.put("tunedProfileName", tunedProfile?.profilename)
json.put("tunedPeak", tunedProfile?.peak)
json.put("tunedDia", tunedProfile?.dia)
json.put("result", result)
json.put("updateButtonVisibility", updateButtonVisibility)
sp.putString(R.string.key_autotune_last_run, json.toString())
}
fun loadLastRun() {
result = ""
lastRunSuccess = false
val json = JSONObject(sp.getString(R.string.key_autotune_last_run, ""))
lastNbDays = JsonHelper.safeGetString(json, "lastNbDays", "")
lastRun = JsonHelper.safeGetLong(json, "lastRun")
val pumpPeak = JsonHelper.safeGetInt(json, "pumpPeak")
val pumpDia = JsonHelper.safeGetDouble(json, "pumpDia")
var localInsulin = LocalInsulin("PumpInsulin", pumpPeak, pumpDia)
selectedProfile = JsonHelper.safeGetString(json, "pumpProfileName", "")
val profile = JsonHelper.safeGetJSONObject(json, "pumpProfile", null)?.let { pureProfileFromJson(it, dateUtil) }
?:return
pumpProfile = ATProfile(ProfileSealed.Pure(profile), localInsulin, injector).also { it.profilename = selectedProfile }
val tunedPeak = JsonHelper.safeGetInt(json, "tunedPeak")
val tunedDia = JsonHelper.safeGetDouble(json, "tunedDia")
localInsulin = LocalInsulin("PumpInsulin", tunedPeak, tunedDia)
val tunedProfileName = JsonHelper.safeGetString(json, "tunedProfileName", "")
val tuned = JsonHelper.safeGetJSONObject(json, "tunedProfile", null)?.let { pureProfileFromJson(it, dateUtil) }
?: return
val circadianTuned = JsonHelper.safeGetJSONObject(json, "tunedCircadianProfile", null)?.let { pureProfileFromJson(it, dateUtil) }
?: return
tunedProfile = ATProfile(ProfileSealed.Pure(tuned), localInsulin, injector).also { atProfile ->
atProfile.profilename = tunedProfileName
atProfile.circadianProfile = ProfileSealed.Pure(circadianTuned)
}
result = JsonHelper.safeGetString(json, "result", "")
updateButtonVisibility = JsonHelper.safeGetInt(json, "updateButtonVisibility")
lastRunSuccess = true
}
private fun log(message: String) { private fun log(message: String) {
atLog("[Plugin] $message") atLog("[Plugin] $message")