Wear: optimize loading treed arrow

This commit is contained in:
Milos Kozak 2023-10-27 13:45:35 +02:00
parent 90b101e77f
commit ff0d327a15
3 changed files with 5 additions and 88 deletions

View file

@ -1,7 +1,6 @@
package app.aaps.core.interfaces.utils
import app.aaps.core.interfaces.aps.AutosensDataStore
import app.aaps.core.interfaces.iob.InMemoryGlucoseValue
import app.aaps.database.entities.GlucoseValue
/**
@ -10,22 +9,6 @@ import app.aaps.database.entities.GlucoseValue
*/
interface TrendCalculator {
/**
* Provide or calculate trend
*
* @param glucoseValue BG
* @return TrendArrow
*/
fun getTrendArrow(glucoseValue: GlucoseValue?): GlucoseValue.TrendArrow
/**
* Provide or calculate trend
*
* @param glucoseValue BG
* @return TrendArrow
*/
fun getTrendArrow(glucoseValue: InMemoryGlucoseValue?): GlucoseValue.TrendArrow
/**
* Provide or calculate trend from newest bucketed data
*
@ -34,14 +17,6 @@ interface TrendCalculator {
*/
fun getTrendArrow(autosensDataStore: AutosensDataStore): GlucoseValue.TrendArrow?
/**
* Provide or calculate trend
*
* @param glucoseValue BG
* @return string description of TrendArrow
*/
fun getTrendDescription(glucoseValue: GlucoseValue?): String
/**
* Provide or calculate trend from newest bucketed data
*

View file

@ -3,75 +3,16 @@ package app.aaps.implementation.utils
import app.aaps.core.interfaces.aps.AutosensDataStore
import app.aaps.core.interfaces.iob.InMemoryGlucoseValue
import app.aaps.core.interfaces.resources.ResourceHelper
import app.aaps.core.interfaces.utils.T
import app.aaps.core.interfaces.utils.TrendCalculator
import app.aaps.database.entities.GlucoseValue
import app.aaps.database.impl.AppRepository
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class TrendCalculatorImpl @Inject constructor(
private val repository: AppRepository,
private val rh: ResourceHelper
) : TrendCalculator {
override fun getTrendArrow(glucoseValue: GlucoseValue?): GlucoseValue.TrendArrow =
when {
glucoseValue?.trendArrow == null -> GlucoseValue.TrendArrow.NONE
glucoseValue.trendArrow != GlucoseValue.TrendArrow.NONE -> glucoseValue.trendArrow
else -> calculateDirection(InMemoryGlucoseValue(glucoseValue))
}
override fun getTrendArrow(glucoseValue: InMemoryGlucoseValue?): GlucoseValue.TrendArrow =
when {
glucoseValue?.trendArrow == null -> GlucoseValue.TrendArrow.NONE
glucoseValue.trendArrow != GlucoseValue.TrendArrow.NONE -> glucoseValue.trendArrow
else -> calculateDirection(glucoseValue)
}
override fun getTrendDescription(glucoseValue: GlucoseValue?): String =
when (getTrendArrow(glucoseValue)) {
GlucoseValue.TrendArrow.DOUBLE_DOWN -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_double_down)
GlucoseValue.TrendArrow.SINGLE_DOWN -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_single_down)
GlucoseValue.TrendArrow.FORTY_FIVE_DOWN -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_forty_five_down)
GlucoseValue.TrendArrow.FLAT -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_flat)
GlucoseValue.TrendArrow.FORTY_FIVE_UP -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_forty_five_up)
GlucoseValue.TrendArrow.SINGLE_UP -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_single_up)
GlucoseValue.TrendArrow.DOUBLE_UP -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_double_up)
GlucoseValue.TrendArrow.NONE -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_none)
else -> rh.gs(app.aaps.core.ui.R.string.a11y_arrow_unknown)
}
private fun calculateDirection(glucoseValue: InMemoryGlucoseValue): GlucoseValue.TrendArrow {
val toTime = glucoseValue.timestamp
val readings = repository.compatGetBgReadingsDataFromTime(toTime - T.mins(10).msecs(), toTime, false).blockingGet()
if (readings.size < 2)
return GlucoseValue.TrendArrow.NONE
val current = readings[0]
val previous = readings[1]
// Avoid division by 0
val slope =
if (current.timestamp == previous.timestamp) 0.0
else (previous.value - current.value) / (previous.timestamp - current.timestamp)
val slopeByMinute = slope * 60000
return when {
slopeByMinute <= -3.5 -> GlucoseValue.TrendArrow.DOUBLE_DOWN
slopeByMinute <= -2 -> GlucoseValue.TrendArrow.SINGLE_DOWN
slopeByMinute <= -1 -> GlucoseValue.TrendArrow.FORTY_FIVE_DOWN
slopeByMinute <= 1 -> GlucoseValue.TrendArrow.FLAT
slopeByMinute <= 2 -> GlucoseValue.TrendArrow.FORTY_FIVE_UP
slopeByMinute <= 3.5 -> GlucoseValue.TrendArrow.SINGLE_UP
slopeByMinute <= 40 -> GlucoseValue.TrendArrow.DOUBLE_UP
else -> GlucoseValue.TrendArrow.NONE
}
}
override fun getTrendArrow(autosensDataStore: AutosensDataStore): GlucoseValue.TrendArrow? {
val data = autosensDataStore.getBucketedDataTableCopy() ?: return null
if (data.size == 0) return null

View file

@ -2,6 +2,7 @@ package app.aaps.plugins.sync.wear.wearintegration
import android.app.NotificationManager
import android.content.Context
import app.aaps.core.interfaces.aps.AutosensDataStore
import app.aaps.core.interfaces.aps.Loop
import app.aaps.core.interfaces.configuration.Config
import app.aaps.core.interfaces.configuration.Constants
@ -741,7 +742,7 @@ class DataHandlerMobile @Inject constructor(
fun resendData(from: String) {
aapsLogger.debug(LTag.WEAR, "Sending data to wear from $from")
// SingleBg
iobCobCalculator.ads.lastBg()?.let { rxBus.send(EventMobileToWear(getSingleBG(it))) }
iobCobCalculator.ads.lastBg()?.let { rxBus.send(EventMobileToWear(getSingleBG(it, iobCobCalculator.ads))) }
// Preferences
rxBus.send(
EventMobileToWear(
@ -769,7 +770,7 @@ class DataHandlerMobile @Inject constructor(
)
// GraphData
iobCobCalculator.ads.getBucketedDataTableCopy()?.let { bucketedData ->
rxBus.send(EventMobileToWear(EventData.GraphData(ArrayList(bucketedData.map { getSingleBG(it) }))))
rxBus.send(EventMobileToWear(EventData.GraphData(ArrayList(bucketedData.map { getSingleBG(it, null) }))))
}
// Treatments
sendTreatments()
@ -969,7 +970,7 @@ class DataHandlerMobile @Inject constructor(
return deltaStringDetailed
}
private fun getSingleBG(glucoseValue: InMemoryGlucoseValue): EventData.SingleBg {
private fun getSingleBG(glucoseValue: InMemoryGlucoseValue, autosensDataStore: AutosensDataStore?): EventData.SingleBg {
val glucoseStatus = glucoseStatusProvider.getGlucoseStatusData(true)
val units = profileFunction.getUnits()
val lowLine = profileUtil.convertToMgdl(defaultValueHelper.determineLowLine(), units)
@ -979,7 +980,7 @@ class DataHandlerMobile @Inject constructor(
timeStamp = glucoseValue.timestamp,
sgvString = profileUtil.stringInCurrentUnitsDetect(glucoseValue.value),
glucoseUnits = units.asText,
slopeArrow = trendCalculator.getTrendArrow(glucoseValue).symbol,
slopeArrow = (autosensDataStore?.let { ads -> trendCalculator.getTrendArrow(ads) } ?: GlucoseValue.TrendArrow.NONE).symbol,
delta = glucoseStatus?.let { deltaString(it.delta, it.delta * Constants.MGDL_TO_MMOLL, units) } ?: "--",
deltaDetailed = glucoseStatus?.let { deltaStringDetailed(it.delta, it.delta * Constants.MGDL_TO_MMOLL, units) } ?: "--",
avgDelta = glucoseStatus?.let { deltaString(it.shortAvgDelta, it.shortAvgDelta * Constants.MGDL_TO_MMOLL, units) } ?: "--",