From 72d4feffc49dbb1b6d035845e38e746bf21dda58 Mon Sep 17 00:00:00 2001 From: Philoul Date: Sun, 5 Jun 2022 20:29:04 +0200 Subject: [PATCH 01/18] Improve InputWeekDay (easy check with timestamp and external access to WeekdayPicker view) --- .../general/automation/elements/InputWeekDay.kt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt index c34a53444c..0f9ec5c159 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputWeekDay.kt @@ -49,7 +49,7 @@ class InputWeekDay : Element() { } val weekdays = BooleanArray(DayOfWeek.values().size) - + var view: WeekdayPicker? = null init { for (day in DayOfWeek.values()) set(day, false) } @@ -65,6 +65,11 @@ class InputWeekDay : Element() { fun isSet(day: DayOfWeek): Boolean = weekdays[day.ordinal] + fun isSet(timestamp: Long): Boolean { + val scheduledDayOfWeek = Calendar.getInstance().also { it.time = Date(timestamp) } + return isSet(Objects.requireNonNull(DayOfWeek.fromCalendarInt(scheduledDayOfWeek[Calendar.DAY_OF_WEEK]))) + } + fun getSelectedDays(): List { val selectedDays: MutableList = ArrayList() for (i in weekdays.indices) { @@ -76,11 +81,12 @@ class InputWeekDay : Element() { } override fun addToLayout(root: LinearLayout) { + view = WeekdayPicker(root.context).apply { + setSelectedDays(getSelectedDays()) + setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) } + } root.addView( - WeekdayPicker(root.context).apply { - setSelectedDays(getSelectedDays()) - setOnWeekdaysChangeListener { i: Int, selected: Boolean -> set(DayOfWeek.fromCalendarInt(i), selected) } - } + view ) } } From f69fb5d64bad1ba10152c58626712243ae729784 Mon Sep 17 00:00:00 2001 From: Philoul Date: Sun, 5 Jun 2022 20:30:58 +0200 Subject: [PATCH 02/18] Add Weekdaypicker in AutotuneFragment and ActionRunAutotune to allow Autotune on specific days of the week --- .../general/autotune/AutotuneFragment.kt | 33 ++++- .../general/autotune/AutotunePlugin.kt | 115 ++++++++++++------ app/src/main/res/layout/autotune_fragment.xml | 59 +++++++-- app/src/main/res/xml/pref_autotune.xml | 3 +- .../automation/actions/ActionRunAutotune.kt | 11 +- .../androidaps/interfaces/Autotune.kt | 2 +- core/src/main/res/values/strings.xml | 1 + 7 files changed, 171 insertions(+), 53 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..613a07a7ba 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 @@ -29,8 +29,10 @@ 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.runOnUiThread +import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.UserEntryLogger +import info.nightscout.androidaps.plugins.general.automation.elements.InputWeekDay import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.MidnightTime @@ -67,6 +69,10 @@ class AutotuneFragment : DaggerFragment() { private lateinit var profileStore: ProfileStore private var profileName = "" private lateinit var profile: ATProfile + private val days get() = autotunePlugin.days + private val daysBack get() = SafeParse.stringToInt(binding.tuneDays.text) + private val calcDays get() = autotunePlugin.calcDays(daysBack) + // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! @@ -79,6 +85,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 + sp.putBoolean(R.string.key_autotune_additional_log, false) // put to false additional log autotunePlugin.lastRun = sp.getLong(R.string.key_autotune_last_run, 0) if (autotunePlugin.lastNbDays.isEmpty()) autotunePlugin.lastNbDays = sp.getInt(R.string.key_autotune_default_tune_days, 5).toString() @@ -88,12 +95,22 @@ class AutotuneFragment : DaggerFragment() { profileFunction.getProfile()?.let { currentProfile -> profile = ATProfile(profileStore.getSpecificProfile(profileName)?.let { ProfileSealed.Pure(it) } ?:currentProfile, LocalInsulin(""), injector) } + days.addToLayout(binding.selectWeekDays) + days.view?.setOnWeekdaysChangeListener { i: Int, selected: Boolean -> + if (autotunePlugin.calculationRunning) + days.view?.setSelectedDays(days.getSelectedDays()) + else { + days.set(InputWeekDay.DayOfWeek.fromCalendarInt(i), selected) + resetParam() + updateGui() + } + } binding.tuneDays.setParams( savedInstanceState?.getDouble("tunedays") ?: defaultValue, 1.0, 30.0, 1.0, DecimalFormat("0"), false, null, textWatcher) + binding.autotuneRun.setOnClickListener { - val daysBack = SafeParse.stringToInt(binding.tuneDays.text) autotunePlugin.calculationRunning = true autotunePlugin.lastNbDays = daysBack.toString() log("Run Autotune $profileName, $daysBack days") @@ -102,6 +119,13 @@ class AutotuneFragment : DaggerFragment() { }.start() updateGui() } + + binding.showWeekDaysCheckbox.setOnCheckedChangeListener { _, isChecked -> + run { + binding.selectWeekDays.visibility = isChecked.toVisibility() + } + } + binding.profileList.onItemClickListener = AdapterView.OnItemClickListener { _, _, _, _ -> if (!autotunePlugin.calculationRunning) { @@ -263,6 +287,7 @@ class AutotuneFragment : DaggerFragment() { updateGui() }, { fabricPrivacy.logException(it) }) checkNewDay() + binding.selectWeekDays.visibility = binding.showWeekDaysCheckbox.isChecked.toVisibility() binding.tuneDays.value = autotunePlugin.lastNbDays.toDouble() updateGui() } @@ -292,6 +317,7 @@ class AutotuneFragment : DaggerFragment() { else { binding.profileList.setText(profileList[0], false) } + days.view?.setSelectedDays(days.getSelectedDays()) binding.autotuneRun.visibility = View.GONE binding.autotuneCheckInputProfile.visibility = View.GONE binding.autotuneCopylocal.visibility = View.GONE @@ -312,11 +338,13 @@ class AutotuneFragment : DaggerFragment() { binding.autotuneCompare.visibility = View.VISIBLE } else -> { - if (profile.isValid) + if (profile.isValid && calcDays > 0) binding.autotuneRun.visibility = View.VISIBLE binding.autotuneCheckInputProfile.visibility = View.VISIBLE } } + binding.calcDays.text = calcDays.toString() + binding.calcDays.visibility = (daysBack != calcDays).toVisibility() binding.tuneLastrun.text = dateUtil.dateAndTimeString(autotunePlugin.lastRun) showResults() } @@ -328,6 +356,7 @@ class AutotuneFragment : DaggerFragment() { binding.tuneWarning.text = rh.gs(R.string.autotune_warning_after_run) } else if (!runToday || autotunePlugin.result.isEmpty()) { //if new day reinit result, default days, warning and button's visibility resetParam(!runToday) + days.setAll(true) } } 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..699611fe87 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 @@ -10,6 +10,7 @@ import info.nightscout.androidaps.database.entities.ValueWithUnit import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.general.automation.elements.InputWeekDay import info.nightscout.androidaps.plugins.general.autotune.data.ATProfile import info.nightscout.androidaps.plugins.general.autotune.data.PreppedGlucose import info.nightscout.androidaps.plugins.general.autotune.events.EventAutotuneUpdateGui @@ -27,11 +28,10 @@ import java.util.* import javax.inject.Inject import javax.inject.Singleton -/** - * adaptation from oref0 autotune started by philoul on 2020 (complete refactoring of AutotunePlugin initialised by Rumen Georgiev on 1/29/2018.) +/* + * adaptation from oref0 autotune developed by philoul on 2022 (complete refactoring of AutotunePlugin initialised by Rumen Georgiev on 1/29/2018.) * * TODO: replace Thread by Worker - * TODO: future version: Allow day of the week selection to tune specifics days (training days, working days, WE days) */ @Singleton @@ -72,9 +72,23 @@ class AutotunePlugin @Inject constructor( @Volatile var tunedProfile: ATProfile? = null private var preppedGlucose: PreppedGlucose? = null private lateinit var profile: Profile + val days = InputWeekDay() val autotuneStartHour: Int = 4 - override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String): String { + override fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String, weekDays: BooleanArray?): String { + weekDays?.let { + for (i in weekDays.indices) + days.weekdays[i] = weekDays[i] + } + val calcDays = calcDays(daysBack) + val sb = StringBuilder() + sb.append("Selected days: ") + var counter = 0 + for (i in days.getSelectedDays()) { + if (counter++ > 0) sb.append(",") + sb.append(Objects.requireNonNull(InputWeekDay.DayOfWeek.fromCalendarInt(i))) + } + log(sb.toString()) tunedProfile = null updateButtonVisibility = View.GONE lastRunSuccess = false @@ -111,41 +125,56 @@ class AutotunePlugin @Inject constructor( } autotuneFS.exportPumpProfile(pumpProfile) + if (calcDays==0) { + result = rh.gs(R.string.autotune_error_more_days) + log(result) + calculationRunning = false + tunedProfile = null + autotuneFS.exportResult(result) + autotuneFS.exportLogAndZip(lastRun) + rxBus.send(EventAutotuneUpdateGui()) + return result + } + var currentCalcDay = 0 for (i in 0 until daysBack) { val from = starttime + i * 24 * 60 * 60 * 1000L // get 24 hours BG values from 4 AM to 4 AM next day val to = from + 24 * 60 * 60 * 1000L - log("Tune day " + (i + 1) + " of " + daysBack) - tunedProfile?.let { tunedProfile -> - autotuneIob.initializeData(from, to, tunedProfile) //autotuneIob contains BG and Treatments data from history (<=> query for ns-treatments and ns-entries) - autotuneFS.exportEntries(autotuneIob) //<=> ns-entries.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine - autotuneFS.exportTreatments(autotuneIob) //<=> ns-treatments.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine (include treatments ,tempBasal and extended - preppedGlucose = autotunePrep.categorize(tunedProfile) //<=> autotune.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine - } + if (days.isSet(from)) { + currentCalcDay++ - if (preppedGlucose == null || tunedProfile == null) { - result = rh.gs(R.string.autotune_error) - log(result) - calculationRunning = false - rxBus.send(EventAutotuneUpdateGui()) - tunedProfile = null - autotuneFS.exportResult(result) - autotuneFS.exportLogAndZip(lastRun) - return result + log("Tune day " + (i + 1) + " of " + daysBack + " (" + currentCalcDay + " of " + calcDays + ")") + tunedProfile?.let { tunedProfile -> + autotuneIob.initializeData(from, to, tunedProfile) //autotuneIob contains BG and Treatments data from history (<=> query for ns-treatments and ns-entries) + autotuneFS.exportEntries(autotuneIob) //<=> ns-entries.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine + autotuneFS.exportTreatments(autotuneIob) //<=> ns-treatments.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine (include treatments ,tempBasal and extended + preppedGlucose = autotunePrep.categorize(tunedProfile) //<=> autotune.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine + } + + if (preppedGlucose == null || tunedProfile == null) { + result = rh.gs(R.string.autotune_error) + log(result) + calculationRunning = false + rxBus.send(EventAutotuneUpdateGui()) + tunedProfile = null + autotuneFS.exportResult(result) + autotuneFS.exportLogAndZip(lastRun) + return result + } + preppedGlucose?.let { preppedGlucose -> //preppedGlucose and tunedProfile should never be null here + autotuneFS.exportPreppedGlucose(preppedGlucose) + tunedProfile = autotuneCore.tuneAllTheThings(preppedGlucose, tunedProfile!!, pumpProfile) + } + // localInsulin = LocalInsulin("TunedInsulin", tunedProfile!!.peak, tunedProfile!!.dia) // Todo: Add tune Insulin option + autotuneFS.exportTunedProfile(tunedProfile!!) //<=> newprofile.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine + if (currentCalcDay < calcDays) { + log("Partial result for day ${i + 1}".trimIndent()) + result = rh.gs(R.string.autotune_partial_result, currentCalcDay, calcDays) + rxBus.send(EventAutotuneUpdateGui()) + } + logResult = showResults(tunedProfile, pumpProfile) + if (detailedLog) + autotuneFS.exportLog(lastRun, i + 1) } - preppedGlucose?.let { preppedGlucose -> //preppedGlucose and tunedProfile should never be null here - autotuneFS.exportPreppedGlucose(preppedGlucose) - tunedProfile = autotuneCore.tuneAllTheThings(preppedGlucose, tunedProfile!!, pumpProfile) - } - // localInsulin = LocalInsulin("TunedInsulin", tunedProfile!!.peak, tunedProfile!!.dia) // Todo: Add tune Insulin option - autotuneFS.exportTunedProfile(tunedProfile!!) //<=> newprofile.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine - if (i < daysBack - 1) { - log("Partial result for day ${i + 1}".trimIndent()) - result = rh.gs(R.string.autotune_partial_result, i + 1, daysBack) - rxBus.send(EventAutotuneUpdateGui()) - } - logResult = showResults(tunedProfile, pumpProfile) - if (detailedLog) - autotuneFS.exportLog(lastRun, i + 1) } result = rh.gs(R.string.autotune_result, dateUtil.dateAndTimeString(lastRun)) if (!detailedLog) @@ -161,7 +190,8 @@ class AutotunePlugin @Inject constructor( updateProfile(tunedP) uel.log( UserEntry.Action.STORE_PROFILE, - UserEntry.Sources.Autotune, + UserEntry.Sources.Automation, + rh.gs(R.string.autotune), ValueWithUnit.SimpleString(tunedP.profilename) ) updateButtonVisibility = View.GONE @@ -178,8 +208,8 @@ class AutotunePlugin @Inject constructor( log("Profile Switch succeed ${tunedP.profilename}") uel.log( UserEntry.Action.PROFILE_SWITCH, - UserEntry.Sources.Autotune, - "Autotune AutoSwitch", + UserEntry.Sources.Automation, + rh.gs(R.string.autotune), ValueWithUnit.SimpleString(tunedP.profilename)) } rxBus.send(EventLocalProfileChanged()) @@ -296,6 +326,17 @@ class AutotunePlugin @Inject constructor( localProfilePlugin.storeSettings() } + fun calcDays(daysBack:Int): Int { + var endTime = MidnightTime.calc(dateUtil.now()) + autotuneStartHour * 60 * 60 * 1000L + if (endTime > dateUtil.now()) endTime -= T.days(1).msecs() // Check if 4 AM is before now + val starttime = endTime - daysBack * T.days(1).msecs() + var result = 0 + for (i in 0 until daysBack) { + if (days.isSet(starttime + i * T.days(1).msecs())) + result++ + } + return result + } private fun log(message: String) { atLog("[Plugin] $message") diff --git a/app/src/main/res/layout/autotune_fragment.xml b/app/src/main/res/layout/autotune_fragment.xml index 003699e6ba..717e673bb7 100644 --- a/app/src/main/res/layout/autotune_fragment.xml +++ b/app/src/main/res/layout/autotune_fragment.xml @@ -1,4 +1,5 @@ - + - + + + + + + + + + + + android:visibility="gone" + android:orientation="vertical"> - + diff --git a/app/src/main/res/xml/pref_autotune.xml b/app/src/main/res/xml/pref_autotune.xml index 35abef7d12..fc5f6dbc72 100644 --- a/app/src/main/res/xml/pref_autotune.xml +++ b/app/src/main/res/xml/pref_autotune.xml @@ -36,11 +36,12 @@ android:key="@string/key_autotune_circadian_ic_isf" android:summary="@string/autotune_circadian_ic_isf_summary" android:title="@string/autotune_circadian_ic_isf_title" /> - + \ No newline at end of file diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionRunAutotune.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionRunAutotune.kt index 27b26108f0..0769de1859 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionRunAutotune.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionRunAutotune.kt @@ -12,6 +12,7 @@ import info.nightscout.androidaps.interfaces.ResourceHelper import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration import info.nightscout.androidaps.plugins.general.automation.elements.InputProfileName +import info.nightscout.androidaps.plugins.general.automation.elements.InputWeekDay import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder import info.nightscout.androidaps.queue.Callback @@ -28,11 +29,11 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { @Inject lateinit var profileFunction: ProfileFunction @Inject lateinit var activePlugin: ActivePlugin @Inject lateinit var sp: SP - @Inject lateinit var uel: UserEntryLogger var defaultValue = 0 private var inputProfileName = InputProfileName(rh, activePlugin, "", true) private var daysBack = InputDuration(0, InputDuration.TimeUnit.DAYS) + private val days = InputWeekDay().also { it.setAll(true) } override fun friendlyName(): Int = R.string.autotune_run override fun shortDescription(): String = resourceHelper.gs(R.string.autotune_profile_name, inputProfileName.value) @@ -44,7 +45,7 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { var message = if (autoSwitch) R.string.autotune_run_with_autoswitch else R.string.autotune_run_without_autoswitch Thread { autotunePlugin.atLog("[Automation] Run Autotune $profileName, ${daysBack.value} days, Autoswitch $autoSwitch") - autotunePlugin.aapsAutotune(daysBack.value, autoSwitch, profileName) + autotunePlugin.aapsAutotune(daysBack.value, autoSwitch, profileName, days.weekdays) if (!autotunePlugin.lastRunSuccess) { message = R.string.autotune_run_with_error aapsLogger.error(LTag.AUTOMATION, "Error during Autotune Run") @@ -61,6 +62,7 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { LayoutBuilder() .add(LabelWithElement(rh, rh.gs(R.string.autotune_select_profile), "", inputProfileName)) .add(LabelWithElement(rh, rh.gs(R.string.autotune_tune_days), "", daysBack)) + .add(days) .build(root) } @@ -70,6 +72,9 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { val data = JSONObject() .put("profileToTune", inputProfileName.value) .put("tunedays", daysBack.value) + for (i in days.weekdays.indices) { + data.put(InputWeekDay.DayOfWeek.values()[i].name, days.weekdays[i]) + } return JSONObject() .put("type", this.javaClass.name) .put("data", data) @@ -78,6 +83,8 @@ class ActionRunAutotune(injector: HasAndroidInjector) : Action(injector) { override fun fromJSON(data: String): Action { val o = JSONObject(data) + for (i in days.weekdays.indices) + days.weekdays[i] = JsonHelper.safeGetBoolean(o, InputWeekDay.DayOfWeek.values()[i].name,true) inputProfileName.value = JsonHelper.safeGetString(o, "profileToTune", "") defaultValue = JsonHelper.safeGetInt(o, "tunedays") if (defaultValue == 0) diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt index b90934475e..fbce082882 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt @@ -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 = "", days: BooleanArray? = null): String fun atLog(message: String) var lastRunSuccess: Boolean diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 28411ab493..af26fd9b83 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -572,6 +572,7 @@ Autotune works with only one IC value, your profile has %1$d values. Average value is %2$.2fg/U Autotune works with only one ISF value, your profile has %1$d values. Average value is %2$.1f%3$s/U Error in input data, try to run again autotune or reduce the number of days + Error in input data, increase the number of days Autotune calculation started, please be patient Check the results carefully before using it! Partial result day %1$d / %2$d tuned From 0abbf55e90321fc90b695a1ba8a73ba7f539da02 Mon Sep 17 00:00:00 2001 From: Philoul Date: Wed, 8 Jun 2022 00:34:53 +0200 Subject: [PATCH 03/18] Fix Crash L137 --- .../general/autotune/AutotunePlugin.kt | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) 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 699611fe87..588c846be2 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 @@ -143,37 +143,39 @@ class AutotunePlugin @Inject constructor( currentCalcDay++ log("Tune day " + (i + 1) + " of " + daysBack + " (" + currentCalcDay + " of " + calcDays + ")") - tunedProfile?.let { tunedProfile -> - autotuneIob.initializeData(from, to, tunedProfile) //autotuneIob contains BG and Treatments data from history (<=> query for ns-treatments and ns-entries) + tunedProfile?.let { it -> + autotuneIob.initializeData(from, to, it) //autotuneIob contains BG and Treatments data from history (<=> query for ns-treatments and ns-entries) autotuneFS.exportEntries(autotuneIob) //<=> ns-entries.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine autotuneFS.exportTreatments(autotuneIob) //<=> ns-treatments.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine (include treatments ,tempBasal and extended - preppedGlucose = autotunePrep.categorize(tunedProfile) //<=> autotune.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine + preppedGlucose = autotunePrep.categorize(it) //<=> autotune.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine + preppedGlucose?.let { preppedGlucose -> //preppedGlucose and tunedProfile should never be null here + autotuneFS.exportPreppedGlucose(preppedGlucose) + tunedProfile = autotuneCore.tuneAllTheThings(preppedGlucose, it, pumpProfile).also { tunedProfile -> + autotuneFS.exportTunedProfile(tunedProfile!!) //<=> newprofile.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine + if (currentCalcDay < calcDays) { + log("Partial result for day ${i + 1}".trimIndent()) + result = rh.gs(R.string.autotune_partial_result, currentCalcDay, calcDays) + rxBus.send(EventAutotuneUpdateGui()) + } + logResult = showResults(tunedProfile, pumpProfile) + if (detailedLog) + autotuneFS.exportLog(lastRun, i + 1) + } + } + ?: { + log("preppedGlucose is null on day ${i + 1}") + tunedProfile = null + } } - - if (preppedGlucose == null || tunedProfile == null) { + if (tunedProfile == null) { result = rh.gs(R.string.autotune_error) - log(result) + log("TunedProfile is null on day ${i + 1}") calculationRunning = false rxBus.send(EventAutotuneUpdateGui()) - tunedProfile = null autotuneFS.exportResult(result) autotuneFS.exportLogAndZip(lastRun) return result } - preppedGlucose?.let { preppedGlucose -> //preppedGlucose and tunedProfile should never be null here - autotuneFS.exportPreppedGlucose(preppedGlucose) - tunedProfile = autotuneCore.tuneAllTheThings(preppedGlucose, tunedProfile!!, pumpProfile) - } - // localInsulin = LocalInsulin("TunedInsulin", tunedProfile!!.peak, tunedProfile!!.dia) // Todo: Add tune Insulin option - autotuneFS.exportTunedProfile(tunedProfile!!) //<=> newprofile.yyyymmdd.json files exported for results compare with oref0 autotune on virtual machine - if (currentCalcDay < calcDays) { - log("Partial result for day ${i + 1}".trimIndent()) - result = rh.gs(R.string.autotune_partial_result, currentCalcDay, calcDays) - rxBus.send(EventAutotuneUpdateGui()) - } - logResult = showResults(tunedProfile, pumpProfile) - if (detailedLog) - autotuneFS.exportLog(lastRun, i + 1) } } result = rh.gs(R.string.autotune_result, dateUtil.dateAndTimeString(lastRun)) From 9be6424c25df159e858a2d491b41576857cf8ec7 Mon Sep 17 00:00:00 2001 From: Philoul Date: Wed, 8 Jun 2022 07:42:00 +0200 Subject: [PATCH 04/18] Fix loadLastRun --- .../general/autotune/AutotunePlugin.kt | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) 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 1e5a83fe7a..bd14feee11 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 @@ -351,31 +351,34 @@ class AutotunePlugin @Inject constructor( 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) + try { + 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 + } catch (e: Exception) { } - result = JsonHelper.safeGetString(json, "result", "") - updateButtonVisibility = JsonHelper.safeGetInt(json, "updateButtonVisibility") - lastRunSuccess = true } fun calcDays(daysBack:Int): Int { From cee29b4116b8fbd0245e29d2b49c86d9c8f26557 Mon Sep 17 00:00:00 2001 From: Philoul Date: Wed, 8 Jun 2022 08:00:01 +0200 Subject: [PATCH 05/18] Autotune fix load result during calculation running --- .../androidaps/plugins/general/autotune/AutotuneFragment.kt | 6 ++++-- 1 file changed, 4 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 07efaefb74..d76d870bb9 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 @@ -281,8 +281,10 @@ class AutotuneFragment : DaggerFragment() { } binding.tuneLastrun.setOnClickListener { - autotunePlugin.loadLastRun() - updateGui() + if (!autotunePlugin.calculationRunning) { + autotunePlugin.loadLastRun() + updateGui() + } } binding.tuneLastrun.paintFlags = binding.tuneLastrun.paintFlags or Paint.UNDERLINE_TEXT_FLAG } From d716c25690b51a85f8f877f0b6070970a017d098 Mon Sep 17 00:00:00 2001 From: Philoul Date: Wed, 8 Jun 2022 08:00:40 +0200 Subject: [PATCH 06/18] Save days of the week --- .../androidaps/plugins/general/autotune/AutotunePlugin.kt | 5 +++++ 1 file changed, 5 insertions(+) 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 bd14feee11..5ab81d8ab6 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 @@ -343,6 +343,9 @@ class AutotunePlugin @Inject constructor( json.put("tunedProfileName", tunedProfile?.profilename) json.put("tunedPeak", tunedProfile?.peak) json.put("tunedDia", tunedProfile?.dia) + for (i in days.weekdays.indices) { + json.put(InputWeekDay.DayOfWeek.values()[i].name, days.weekdays[i]) + } json.put("result", result) json.put("updateButtonVisibility", updateButtonVisibility) sp.putString(R.string.key_autotune_last_run, json.toString()) @@ -374,6 +377,8 @@ class AutotunePlugin @Inject constructor( atProfile.profilename = tunedProfileName atProfile.circadianProfile = ProfileSealed.Pure(circadianTuned) } + for (i in days.weekdays.indices) + days.weekdays[i] = JsonHelper.safeGetBoolean(json, InputWeekDay.DayOfWeek.values()[i].name,true) result = JsonHelper.safeGetString(json, "result", "") updateButtonVisibility = JsonHelper.safeGetInt(json, "updateButtonVisibility") lastRunSuccess = true From ebc368cc7226a34d207e2d4abe3e454093dfada5 Mon Sep 17 00:00:00 2001 From: Philoul Date: Wed, 8 Jun 2022 23:05:56 +0200 Subject: [PATCH 07/18] 3.0.0.2-autotune-dev-m --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 132de499a8..7573e55876 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -111,7 +111,7 @@ android { defaultConfig { multiDexEnabled true versionCode 1500 - version "3.0.0.2-dev-m" + version "3.0.0.2-autotune-dev-m" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"' From a33bdb6bff0041d3603a175e0e7867150267027a Mon Sep 17 00:00:00 2001 From: Philoul Date: Thu, 30 Jun 2022 21:11:31 +0200 Subject: [PATCH 08/18] Set defaults on profile selection and new day only --- .../plugins/general/autotune/AutotuneFragment.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 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 ba1efbb778..c5d267de73 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 @@ -103,7 +103,7 @@ class AutotuneFragment : DaggerFragment() { days.view?.setSelectedDays(days.getSelectedDays()) else { days.set(InputWeekDay.DayOfWeek.fromCalendarInt(i), selected) - resetParam() + resetParam(false) updateGui() } } @@ -135,7 +135,7 @@ class AutotuneFragment : DaggerFragment() { profile = ATProfile(profileStore.getSpecificProfile(profileName)?.let { ProfileSealed.Pure(it) } ?: currentProfile, LocalInsulin(""), injector) } autotunePlugin.selectedProfile = profileName - resetParam() + resetParam(true) } updateGui() } @@ -364,10 +364,8 @@ class AutotuneFragment : DaggerFragment() { val runToday = autotunePlugin.lastRun > MidnightTime.calc(dateUtil.now() - autotunePlugin.autotuneStartHour * 3600 * 1000L) + autotunePlugin.autotuneStartHour * 3600 * 1000L if (runToday && autotunePlugin.result != "") { binding.tuneWarning.text = rh.gs(R.string.autotune_warning_after_run) - } else if (!runToday || autotunePlugin.result.isEmpty()) { //if new day re-init result, default days, warning and button's visibility + } else if (!runToday || autotunePlugin.result.isEmpty()) //if new day re-init result, default days, warning and button's visibility resetParam(!runToday) - days.setAll(true) - } } private fun addWarnings(): String { @@ -392,10 +390,12 @@ class AutotuneFragment : DaggerFragment() { return warning } - private fun resetParam(resetDay: Boolean = true) { + private fun resetParam(resetDay: Boolean) { binding.tuneWarning.text = addWarnings() - if (resetDay) + if (resetDay) { autotunePlugin.lastNbDays = sp.getInt(R.string.key_autotune_default_tune_days, 5).toString() + days.setAll(true) + } autotunePlugin.result = "" binding.autotuneResults.removeAllViews() autotunePlugin.tunedProfile = null From e9bd7b0314955cf36c4ac9814c63bcab429461c5 Mon Sep 17 00:00:00 2001 From: Philoul Date: Mon, 4 Jul 2022 08:53:19 +0200 Subject: [PATCH 09/18] Improve Layout alignment --- app/src/main/res/layout/autotune_fragment.xml | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/app/src/main/res/layout/autotune_fragment.xml b/app/src/main/res/layout/autotune_fragment.xml index 0f2064c374..8fa62fdbac 100644 --- a/app/src/main/res/layout/autotune_fragment.xml +++ b/app/src/main/res/layout/autotune_fragment.xml @@ -38,7 +38,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical" - android:layout_weight="2" + android:layout_weight="3.5" android:gravity="end" android:paddingStart="5dp" android:paddingEnd="5dp" @@ -74,42 +74,41 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical" - android:layout_weight="2" + android:layout_weight="3.5" android:gravity="end" android:paddingStart="5dp" android:paddingEnd="5dp" android:text="@string/autotune_tune_days" android:textSize="14sp" /> - + + + + + android:layout_gravity="center_vertical" + android:layout_weight="1" + android:gravity="start" + android:paddingStart="5dp" + android:paddingEnd="5dp" + android:text="(5)" + android:textSize="14sp" + android:visibility="visible" /> - - - - - + + + Date: Mon, 4 Jul 2022 22:43:50 +0200 Subject: [PATCH 10/18] Improve Autotune Layout --- .../androidaps/plugins/general/autotune/AutotuneFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c5d267de73..4895dc48c8 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 @@ -355,7 +355,7 @@ class AutotuneFragment : DaggerFragment() { } } binding.calcDays.text = calcDays.toString() - binding.calcDays.visibility = (daysBack != calcDays).toVisibility() + binding.calcDays.visibility = if (daysBack == calcDays) View.INVISIBLE else View.VISIBLE binding.tuneLastrun.text = dateUtil.dateAndTimeString(autotunePlugin.lastRun) showResults() } From 954a3118db21b3d5c960777f9a579f788c842ab3 Mon Sep 17 00:00:00 2001 From: Philoul Date: Sun, 17 Jul 2022 17:40:32 +0200 Subject: [PATCH 11/18] Fix build after dev merge --- .../androidaps/plugins/general/autotune/AutotunePlugin.kt | 2 +- .../main/java/info/nightscout/androidaps/interfaces/Autotune.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 869ee8001f..b57a52d5f0 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 @@ -149,7 +149,7 @@ class AutotunePlugin @Inject constructor( autotuneFS.exportResult(result) autotuneFS.exportLogAndZip(lastRun) rxBus.send(EventAutotuneUpdateGui()) - return result + return } var currentCalcDay = 0 for (i in 0 until daysBack) { diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt index 310be83e1b..9d1b468999 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/Autotune.kt @@ -2,7 +2,7 @@ package info.nightscout.androidaps.interfaces interface Autotune { - fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String = "", days: BooleanArray? = null) + fun aapsAutotune(daysBack: Int, autoSwitch: Boolean, profileToTune: String = "", weekDays: BooleanArray? = null) fun atLog(message: String) var lastRunSuccess: Boolean From 11af3a9e664945dabe0b897d97f27d719f9d856b Mon Sep 17 00:00:00 2001 From: Philoul Date: Thu, 28 Jul 2022 00:35:23 +0200 Subject: [PATCH 12/18] Fix Merge --- .../androidaps/plugins/general/autotune/AutotuneFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5fb9dddc8d..4073d32ece 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 @@ -134,7 +134,7 @@ class AutotuneFragment : DaggerFragment() { profile = ATProfile(profileStore.getSpecificProfile(profileName)?.let { ProfileSealed.Pure(it) } ?: currentProfile, LocalInsulin(""), injector) } autotunePlugin.selectedProfile = profileName - resetParam() + resetParam(true) binding.tuneDays.value = autotunePlugin.lastNbDays.toDouble() } updateGui() From e201ff256fae6a3f270191827a988cc4b6ea3535 Mon Sep 17 00:00:00 2001 From: Philoul Date: Mon, 14 Nov 2022 09:32:02 +0100 Subject: [PATCH 13/18] Autotune Fix Conflicts after dev merge --- .../nightscout/automation/actions/ActionRunAutotune.kt | 1 - .../nightscout/automation/elements/InputWeekDay.kt | 5 +++-- plugins/build.gradle | 1 + .../plugins/general/autotune/AutotuneFragment.kt | 10 +--------- .../plugins/general/autotune/AutotunePlugin.kt | 7 ++++--- 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/automation/src/main/java/info/nightscout/automation/actions/ActionRunAutotune.kt b/automation/src/main/java/info/nightscout/automation/actions/ActionRunAutotune.kt index 6ae0f9f244..5328fb452e 100644 --- a/automation/src/main/java/info/nightscout/automation/actions/ActionRunAutotune.kt +++ b/automation/src/main/java/info/nightscout/automation/actions/ActionRunAutotune.kt @@ -11,7 +11,6 @@ import info.nightscout.automation.elements.LabelWithElement import info.nightscout.automation.elements.LayoutBuilder import info.nightscout.interfaces.autotune.Autotune import info.nightscout.interfaces.plugin.ActivePlugin -import info.nightscout.androidaps.plugins.general.automation.elements.InputWeekDay import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.pump.PumpEnactResult import info.nightscout.interfaces.queue.Callback diff --git a/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt b/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt index d50439d9f7..c46f7de5be 100644 --- a/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt +++ b/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt @@ -4,7 +4,8 @@ import android.widget.LinearLayout import androidx.annotation.StringRes import info.nightscout.automation.ui.WeekdayPicker import info.nightscout.automation.R -import java.util.Calendar +import java.util.* +import kotlin.collections.ArrayList class InputWeekDay : Element() { @@ -67,7 +68,7 @@ class InputWeekDay : Element() { fun isSet(timestamp: Long): Boolean { val scheduledDayOfWeek = Calendar.getInstance().also { it.time = Date(timestamp) } - return isSet(Objects.requireNonNull(DayOfWeek.fromCalendarInt(scheduledDayOfWeek[Calendar.DAY_OF_WEEK]))) + return isSet(DayOfWeek.fromCalendarInt(scheduledDayOfWeek[Calendar.DAY_OF_WEEK])) } fun getSelectedDays(): List { diff --git a/plugins/build.gradle b/plugins/build.gradle index c9433aab96..f0cce76d73 100644 --- a/plugins/build.gradle +++ b/plugins/build.gradle @@ -53,4 +53,5 @@ dependencies { // Overview api 'com.google.android.flexbox:flexbox:3.0.0' + implementation project(path: ':automation') } \ No newline at end of file diff --git a/plugins/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt b/plugins/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt index 79ea58e9b8..21bdbebffd 100644 --- a/plugins/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt +++ b/plugins/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt @@ -21,16 +21,8 @@ import dagger.android.HasAndroidInjector import dagger.android.support.DaggerFragment import info.nightscout.androidaps.data.ProfileSealed import info.nightscout.androidaps.logging.UserEntryLogger -import info.nightscout.androidaps.plugins.bus.RxBus -import info.nightscout.androidaps.plugins.general.autotune.data.ATProfile -import info.nightscout.androidaps.plugins.general.autotune.events.EventAutotuneUpdateGui -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.FabricPrivacy -import info.nightscout.androidaps.utils.MidnightTime -import info.nightscout.androidaps.utils.Round import info.nightscout.androidaps.utils.alertDialogs.OKDialog.showConfirmation +import info.nightscout.automation.elements.InputWeekDay import info.nightscout.core.fabric.FabricPrivacy import info.nightscout.core.profile.ProfileStoreObject import info.nightscout.core.profile.fromMgdlToUnits diff --git a/plugins/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt b/plugins/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt index c0688bc597..c6d75526d9 100644 --- a/plugins/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt +++ b/plugins/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt @@ -5,10 +5,10 @@ import dagger.android.HasAndroidInjector import info.nightscout.androidaps.data.ProfileSealed import info.nightscout.androidaps.extensions.pureProfileFromJson import info.nightscout.androidaps.logging.UserEntryLogger +import info.nightscout.automation.elements.InputWeekDay import info.nightscout.core.profile.ProfileStoreObject import info.nightscout.database.entities.UserEntry import info.nightscout.database.entities.ValueWithUnit -import info.nightscout.androidaps.plugins.general.automation.elements.InputWeekDay import info.nightscout.interfaces.BuildHelper import info.nightscout.interfaces.autotune.Autotune import info.nightscout.interfaces.insulin.Insulin @@ -36,9 +36,10 @@ import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.T import org.json.JSONException import org.json.JSONObject -import java.util.TimeZone +import java.util.* import javax.inject.Inject import javax.inject.Singleton +import kotlin.collections.ArrayList /* * adaptation from oref0 autotune developed by philoul on 2022 (complete refactoring of AutotunePlugin initialised by Rumen Georgiev on 1/29/2018.) @@ -107,7 +108,7 @@ class AutotunePlugin @Inject constructor( var counter = 0 for (i in days.getSelectedDays()) { if (counter++ > 0) sb.append(",") - sb.append(Objects.requireNonNull(InputWeekDay.DayOfWeek.fromCalendarInt(i))) + sb.append(InputWeekDay.DayOfWeek.fromCalendarInt(i)) } log(sb.toString()) tunedProfile = null From d359c8563b7daca5e4da96f3cbfe050aa37c8406 Mon Sep 17 00:00:00 2001 From: Philoul Date: Wed, 16 Nov 2022 23:16:33 +0100 Subject: [PATCH 14/18] Fix AutotuneFragment InputWeekDay --- plugins/aps/build.gradle | 1 + plugins/main/build.gradle | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/aps/build.gradle b/plugins/aps/build.gradle index 8aa25674c8..1291a22c01 100644 --- a/plugins/aps/build.gradle +++ b/plugins/aps/build.gradle @@ -29,4 +29,5 @@ dependencies { // APS api 'org.mozilla:rhino:1.7.14' + implementation project(path: ':plugins:automation') } \ No newline at end of file diff --git a/plugins/main/build.gradle b/plugins/main/build.gradle index 887b732dd4..55629955bc 100644 --- a/plugins/main/build.gradle +++ b/plugins/main/build.gradle @@ -54,5 +54,4 @@ dependencies { // Overview api 'com.google.android.flexbox:flexbox:3.0.0' - implementation project(path: ':automation') } \ No newline at end of file From 798075c6f94f481c10b979e503d9e7374aea1500 Mon Sep 17 00:00:00 2001 From: Philoul Date: Thu, 17 Nov 2022 08:59:01 +0100 Subject: [PATCH 15/18] InputWeekDay.kt update import --- .../java/info/nightscout/automation/elements/InputWeekDay.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt index c46f7de5be..cf13f3b8a2 100644 --- a/plugins/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt +++ b/plugins/automation/src/main/java/info/nightscout/automation/elements/InputWeekDay.kt @@ -4,7 +4,8 @@ import android.widget.LinearLayout import androidx.annotation.StringRes import info.nightscout.automation.ui.WeekdayPicker import info.nightscout.automation.R -import java.util.* +import java.util.Calendar +import java.util.Date import kotlin.collections.ArrayList class InputWeekDay : Element() { From 15058fe830f462ade62c4a93f7aa6926b2b52615 Mon Sep 17 00:00:00 2001 From: Philoul Date: Tue, 29 Nov 2022 23:56:00 +0100 Subject: [PATCH 16/18] Autotune Fix Import --- .../nightscout/plugins/general/autotune/AutotuneFragment.kt | 4 ---- .../nightscout/plugins/general/autotune/AutotunePlugin.kt | 2 -- 2 files changed, 6 deletions(-) diff --git a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt index 790b013052..d1eb4dd907 100644 --- a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt +++ b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotuneFragment.kt @@ -19,12 +19,8 @@ import android.widget.TableRow import android.widget.TextView import dagger.android.HasAndroidInjector import dagger.android.support.DaggerFragment -import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.automation.elements.InputWeekDay -import info.nightscout.core.fabric.FabricPrivacy -import info.nightscout.core.profile.fromMgdlToUnits import info.nightscout.core.profile.ProfileSealed -import info.nightscout.core.profile.ProfileStoreObject import info.nightscout.core.ui.dialogs.OKDialog import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.database.entities.UserEntry diff --git a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt index 07c166f35e..51e10f9e81 100644 --- a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt +++ b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt @@ -2,11 +2,9 @@ package info.nightscout.plugins.general.autotune import android.view.View import dagger.android.HasAndroidInjector -import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.automation.elements.InputWeekDay import info.nightscout.core.extensions.pureProfileFromJson import info.nightscout.core.profile.ProfileSealed -import info.nightscout.core.profile.ProfileStoreObject import info.nightscout.database.entities.UserEntry import info.nightscout.database.entities.ValueWithUnit import info.nightscout.interfaces.Config From 476578ba3909f7b0c0d17acd11edea9848fe3ed9 Mon Sep 17 00:00:00 2001 From: Philoul Date: Thu, 1 Dec 2022 21:46:55 +0100 Subject: [PATCH 17/18] Fix tests --- plugins/aps/src/main/res/layout/autotune_fragment.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/aps/src/main/res/layout/autotune_fragment.xml b/plugins/aps/src/main/res/layout/autotune_fragment.xml index 40b2e3ba9e..ae6d6e7b6f 100644 --- a/plugins/aps/src/main/res/layout/autotune_fragment.xml +++ b/plugins/aps/src/main/res/layout/autotune_fragment.xml @@ -117,7 +117,6 @@ android:paddingStart="5dp" android:paddingEnd="5dp" android:checked="false" - android:contentDescription="@string/show_calculation" android:drawableEnd="@drawable/ic_visibility" /> From 51e1d4ad04ce82ba1b4d0de8be879d75100b01b8 Mon Sep 17 00:00:00 2001 From: Philoul Date: Wed, 7 Dec 2022 00:10:47 +0100 Subject: [PATCH 18/18] Autotune Additional string --- core/ui/src/main/res/values/strings.xml | 1 + .../info/nightscout/plugins/general/autotune/AutotunePlugin.kt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/core/ui/src/main/res/values/strings.xml b/core/ui/src/main/res/values/strings.xml index ab131ced6f..2087b1df1c 100644 --- a/core/ui/src/main/res/values/strings.xml +++ b/core/ui/src/main/res/values/strings.xml @@ -436,6 +436,7 @@ Selected profile has %1$d IC values. Autotune will use %2$.2f g/U Selected profile has %1$d ISF values. Autotune will use %2$.1f %3$s/U Error in input data, try to run again autotune or reduce the number of days + Error in input data, increase the number of days Autotune calculation started, please be patient Check the results carefully before using it! Partial result day %1$d / %2$d tuned diff --git a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt index b38affb589..4c2b4ca94e 100644 --- a/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt +++ b/plugins/aps/src/main/java/info/nightscout/plugins/general/autotune/AutotunePlugin.kt @@ -154,7 +154,7 @@ class AutotunePlugin @Inject constructor( autotuneFS.exportPumpProfile(pumpProfile) if (calcDays==0) { - result = rh.gs(R.string.autotune_error_more_days) + result = rh.gs(info.nightscout.core.ui.R.string.autotune_error_more_days) log(result) calculationRunning = false tunedProfile = null