BgQuality: RED state from last 50 min, LGS only

This commit is contained in:
Milos Kozak 2022-01-16 22:02:19 +01:00
parent 5c50d20f9e
commit 686bd350c3

View file

@ -3,8 +3,6 @@ package info.nightscout.androidaps.plugins.constraints.bgQualityCheck
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.interfaces.*
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventBucketedDataCreated import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventBucketedDataCreated
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
@ -12,11 +10,14 @@ import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.min
@Singleton @Singleton
class BgQualityCheckPlugin @Inject constructor( class BgQualityCheckPlugin @Inject constructor(
@ -63,23 +64,23 @@ class BgQualityCheckPlugin @Inject constructor(
var state: State = State.UNKNOWN var state: State = State.UNKNOWN
var message: String = "" var message: String = ""
// Return false if BG values are doubled // Fallback to LGS if BG values are doubled
@Suppress("ReplaceGetOrSet") override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> =
override fun isLoopInvocationAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
if (state == State.DOUBLED) if (state == State.DOUBLED)
value.set(aapsLogger, false, "Doubled values in BGSource", this) maxIob.set(aapsLogger, 0.0, "Doubled values in BGSource", this)
return value else
} maxIob
@Suppress("CascadeIf") @Suppress("CascadeIf")
fun processBgData() { fun processBgData() {
val readings = iobCobCalculator.ads.getBgReadingsDataTableCopy() val readings = iobCobCalculator.ads.getBgReadingsDataTableCopy()
for (i in readings.indices) for (i in readings.indices)
if (i < readings.size - 2) // Deltas are calculated from last ~50 min. Detect RED state only on this interval
if (i < min(readings.size - 2, 10))
if (abs(readings[i].timestamp - readings[i + 1].timestamp) <= T.secs(20).msecs()) { if (abs(readings[i].timestamp - readings[i + 1].timestamp) <= T.secs(20).msecs()) {
state = State.DOUBLED state = State.DOUBLED
aapsLogger.debug(LTag.CORE, "BG similar. Turning on red state.\n${readings[i]}\n${readings[i+1]}") aapsLogger.debug(LTag.CORE, "BG similar. Turning on red state.\n${readings[i]}\n${readings[i + 1]}")
message = rh.gs(R.string.bg_too_close, dateUtil.dateAndTimeAndSecondsString(readings[i].timestamp), dateUtil.dateAndTimeAndSecondsString(readings[i+1].timestamp)) message = rh.gs(R.string.bg_too_close, dateUtil.dateAndTimeAndSecondsString(readings[i].timestamp), dateUtil.dateAndTimeAndSecondsString(readings[i + 1].timestamp))
return return
} }
if (iobCobCalculator.ads.lastUsed5minCalculation == true) { if (iobCobCalculator.ads.lastUsed5minCalculation == true) {