Improve COB handling

This commit is contained in:
Milos Kozak 2021-09-19 09:59:35 +02:00
parent fb9f5220d3
commit b80e2a7648
7 changed files with 31 additions and 29 deletions

View file

@ -680,14 +680,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
activity?.let { OKDialog.show(it, resourceHelper.gs(R.string.iob), overviewData.iobDialogText) }
}
// cob
var cobText: String = resourceHelper.gs(R.string.value_unavailable_short)
overviewData.cobInfo?.let { cobInfo ->
if (cobInfo.displayCob != null) {
cobText = resourceHelper.gs(R.string.format_carbs, cobInfo.displayCob!!.toInt())
if (cobInfo.futureCarbs > 0) cobText += "(" + DecimalFormatter.to0Decimal(cobInfo.futureCarbs) + ")"
}
}
binding.infoLayout.cob.text = cobText
var cobText = overviewData.cobInfo?.displayText(resourceHelper, dateUtil, buildHelper.isDev()) ?: resourceHelper.gs(R.string.value_unavailable_short)
val constraintsProcessed = loopPlugin.lastRun?.constraintsProcessed
val lastRun = loopPlugin.lastRun
@ -695,7 +688,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
if (constraintsProcessed.carbsReq > 0) {
//only display carbsreq when carbs have not been entered recently
if (overviewData.lastCarbsTime < lastRun.lastAPSRun) {
cobText = cobText + " | " + constraintsProcessed.carbsReq + " " + resourceHelper.gs(R.string.required)
cobText += " | " + constraintsProcessed.carbsReq + " " + resourceHelper.gs(R.string.required)
}
if (carbAnimation?.isRunning == false)
carbAnimation?.start()
@ -704,6 +697,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
carbAnimation?.selectDrawable(0)
}
}
binding.infoLayout.cob.text = cobText
}
OverviewData.Property.TEMPORARY_TARGET -> {

View file

@ -23,7 +23,6 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventBucke
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.Translator
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.sharedPreferences.SP
@ -45,8 +44,6 @@ class OverviewPlugin @Inject constructor(
resourceHelper: ResourceHelper,
private val config: Config,
private val dateUtil: DateUtil,
private val translator: Translator,
// private val profiler: Profiler,
private val profileFunction: ProfileFunction,
private val iobCobCalculator: IobCobCalculator,
private val repository: AppRepository,
@ -234,7 +231,6 @@ class OverviewPlugin @Inject constructor(
overviewBus.send(EventUpdateOverview(from, OverviewData.Property.TIME))
overviewBus.send(EventUpdateOverview(from, OverviewData.Property.TEMPORARY_BASAL))
overviewBus.send(EventUpdateOverview(from, OverviewData.Property.EXTENDED_BOLUS))
overviewBus.send(EventUpdateOverview(from, OverviewData.Property.IOB_COB))
overviewBus.send(EventUpdateOverview(from, OverviewData.Property.TEMPORARY_TARGET))
overviewBus.send(EventUpdateOverview(from, OverviewData.Property.SENSITIVITY))
loadAsData(from)
@ -244,6 +240,7 @@ class OverviewPlugin @Inject constructor(
overviewData.prepareTreatmentsData(from)
overviewData.prepareIobAutosensData(from)
overviewBus.send(EventUpdateOverview(from, OverviewData.Property.GRAPH))
overviewBus.send(EventUpdateOverview(from, OverviewData.Property.IOB_COB))
aapsLogger.debug(LTag.UI, "refreshLoop finished")
runningRefresh = false
}
@ -305,7 +302,7 @@ class OverviewPlugin @Inject constructor(
private fun loadIobCobResults(from: String) {
overviewData.bolusIob = iobCobCalculator.calculateIobFromBolus().round()
overviewData.basalIob = iobCobCalculator.calculateIobFromTempBasalsIncludingConvertedExtended().round()
overviewData.cobInfo = iobCobCalculator.getCobInfo(false, "Overview COB")
overviewData.cobInfo = iobCobCalculator.getCobInfo(true, "Overview COB")
val lastCarbs = repository.getLastCarbsRecordWrapped().blockingGet()
overviewData.lastCarbsTime = if (lastCarbs is ValueWrapper.Existing) lastCarbs.value.timestamp else 0L

View file

@ -270,18 +270,19 @@ class IobCobCalculatorPlugin @Inject constructor(
var displayCob: Double? = null
var futureCarbs = 0.0
val now = dateUtil.now()
val carbs = repository.getCarbsDataFromTimeExpanded(now, true).blockingGet()
var timestamp = now
val carbs = repository.getCarbsDataFromTimeExpanded(autosensData?.time ?: now, true).blockingGet()
if (autosensData != null) {
displayCob = autosensData.cob
carbs.forEach { carb ->
if (ads.roundUpTime(carb.timestamp) > ads.roundUpTime(autosensData.time) && carb.timestamp <= now) {
if (carb.timestamp > autosensData.time && carb.timestamp <= now)
displayCob += carb.amount
}
}
timestamp = autosensData.time
}
// Future carbs
carbs.forEach { carb -> if (carb.timestamp > now) futureCarbs += carb.amount }
return CobInfo(displayCob, futureCarbs)
return CobInfo(timestamp, displayCob, futureCarbs)
}
override fun getMealDataWithWaitingForCalculationFinish(): MealData {
@ -579,7 +580,7 @@ class IobCobCalculatorPlugin @Inject constructor(
return total
}
open fun getCalculationToTimeTempBasals(toTime: Long, lastAutosensResult: AutosensResult, exercise_mode: Boolean, half_basal_exercise_target: Int, isTempTarget: Boolean): IobTotal {
fun getCalculationToTimeTempBasals(toTime: Long, lastAutosensResult: AutosensResult, exercise_mode: Boolean, half_basal_exercise_target: Int, isTempTarget: Boolean): IobTotal {
val total = IobTotal(toTime)
val pumpInterface = activePlugin.activePump
val now = dateUtil.now()

View file

@ -84,11 +84,9 @@ class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjec
bg = lastBG.valueToUnits(profileFunction.getUnits())
}
// COB
var cob = 0.0
if (useCOB() == YES) {
val cobInfo = iobCobCalculator.getCobInfo(_synchronized, "QuickWizard COB")
if (cobInfo.displayCob != null) cob = cobInfo.displayCob!!
}
val cob =
if (useCOB() == YES) iobCobCalculator.getCobInfo(_synchronized, "QuickWizard COB").displayCob ?: 0.0
else 0.0
// Bolus IOB
var bolusIOB = false
if (useBolusIOB() == YES) {
@ -121,7 +119,7 @@ class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjec
trend = true
}
val percentage = sp.getInt(R.string.key_boluswizard_percentage, 100)
return BolusWizard(injector).doCalc(profile, profileName, tempTarget, carbs(), cob, bg, 0.0, percentage, true, useCOB() == YES, bolusIOB, basalIOB, superBolus, useTempTarget() == YES, trend, false, buttonText(), quickWizard = true) //tbc, ok if only quickwizard, but if other sources elsewhere use Sources.QuickWiard
return BolusWizard(injector).doCalc(profile, profileName, tempTarget, carbs(), cob, bg, 0.0, percentage, true, useCOB() == YES, bolusIOB, basalIOB, superBolus, useTempTarget() == YES, trend, false, buttonText(), quickWizard = true) //tbc, ok if only quickwizard, but if other sources elsewhere use Sources.QuickWizard
}
fun buttonText(): String = safeGetString(storage, "buttonText", "")

View file

@ -98,7 +98,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
val bgList: MutableList<GlucoseValue> = ArrayList()
bgList.add(reading)
`when`(iobCobCalculator.getCobInfo(false, "SMS COB")).thenReturn(CobInfo(10.0, 2.0))
`when`(iobCobCalculator.getCobInfo(false, "SMS COB")).thenReturn(CobInfo(0, 10.0, 2.0))
`when`(iobCobCalculator.ads).thenReturn(autosensDataStore)
`when`(autosensDataStore.lastBg()).thenReturn(reading)

View file

@ -1,9 +1,12 @@
package info.nightscout.androidaps.plugins.iob.iobCobCalculator
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.resources.ResourceHelper
/** All COB up to now, including carbs not yet processed by IobCob calculation. */
class CobInfo(val displayCob: Double?, val futureCarbs: Double) {
class CobInfo(val timestamp: Long, val displayCob: Double?, val futureCarbs: Double) {
fun generateCOBString(): String {
var cobStringResult = "--g"
@ -15,4 +18,13 @@ class CobInfo(val displayCob: Double?, val futureCarbs: Double) {
}
return cobStringResult
}
}
fun displayText(resourceHelper: ResourceHelper, dateUtil: DateUtil, isDev: Boolean): String? =
if (displayCob != null) {
var cobText = resourceHelper.gs(R.string.format_carbs, displayCob.toInt())
if (futureCarbs > 0) cobText += "(" + DecimalFormatter.to0Decimal(futureCarbs) + ")"
// This is only temporary for debugging
if (isDev) cobText += "\n" + dateUtil.timeStringWithSeconds(timestamp)
cobText
} else null
}

View file

@ -119,7 +119,7 @@ class DateUtil @Inject constructor(private val context: Context) {
return DateTime(mills).toString(DateTimeFormat.forPattern(format))
}
private fun timeStringWithSeconds(mills: Long): String {
fun timeStringWithSeconds(mills: Long): String {
var format = "hh:mm:ssa"
if (android.text.format.DateFormat.is24HourFormat(context)) {
format = "HH:mm:ss"