From 02d8e9928e848d6491b9cf82bea8a9d66c91646c Mon Sep 17 00:00:00 2001 From: Philoul Date: Mon, 6 Jun 2022 12:53:00 +0200 Subject: [PATCH] Save Autotune lastRun and Load LastRun after AAPS restart --- .../general/autotune/AutotuneFragment.kt | 4 +- .../general/autotune/AutotunePlugin.kt | 51 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt index b968c7fc88..85e7d2fb87 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/autotune/AutotuneFragment.kt @@ -79,7 +79,7 @@ class AutotuneFragment : DaggerFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) 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()) 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() @@ -144,6 +144,7 @@ class AutotuneFragment : DaggerFragment() { autotunePlugin.tunedProfile?.profilename = localName autotunePlugin.updateProfile(autotunePlugin.tunedProfile) autotunePlugin.updateButtonVisibility = View.GONE + autotunePlugin.saveLastRun() uel.log( UserEntry.Action.STORE_PROFILE, UserEntry.Sources.Autotune, @@ -163,6 +164,7 @@ class AutotuneFragment : DaggerFragment() { autotunePlugin.tunedProfile?.profilename = "" autotunePlugin.updateProfile(autotunePlugin.pumpProfile) autotunePlugin.updateButtonVisibility = View.VISIBLE + autotunePlugin.saveLastRun() uel.log( UserEntry.Action.STORE_PROFILE, UserEntry.Sources.Autotune, 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 9a7839f600..948904944c 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 @@ -7,6 +7,7 @@ import info.nightscout.androidaps.data.LocalInsulin import info.nightscout.androidaps.data.ProfileSealed import info.nightscout.androidaps.database.entities.UserEntry import info.nightscout.androidaps.database.entities.ValueWithUnit +import info.nightscout.androidaps.extensions.pureProfileFromJson import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.UserEntryLogger 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.events.EventLocalProfileChanged import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.JsonHelper import info.nightscout.androidaps.utils.MidnightTime import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.buildHelper.BuildHelper @@ -187,7 +189,7 @@ class AutotunePlugin @Inject constructor( } } lastRunSuccess = true - sp.putLong(R.string.key_autotune_last_run, lastRun) + saveLastRun() rxBus.send(EventAutotuneUpdateGui()) calculationRunning = false tunedProfile?.let { @@ -296,6 +298,53 @@ class AutotunePlugin @Inject constructor( 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) { atLog("[Plugin] $message")