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 info.nightscout.androidaps.R
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.iob.iobCobCalculator.events.EventBucketedDataCreated
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.resources.ResourceHelper
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.rxkotlin.plusAssign
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.math.abs
import kotlin.math.min
@Singleton
class BgQualityCheckPlugin @Inject constructor(
@ -63,23 +64,23 @@ class BgQualityCheckPlugin @Inject constructor(
var state: State = State.UNKNOWN
var message: String = ""
// Return false if BG values are doubled
@Suppress("ReplaceGetOrSet")
override fun isLoopInvocationAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
// Fallback to LGS if BG values are doubled
override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> =
if (state == State.DOUBLED)
value.set(aapsLogger, false, "Doubled values in BGSource", this)
return value
}
maxIob.set(aapsLogger, 0.0, "Doubled values in BGSource", this)
else
maxIob
@Suppress("CascadeIf")
fun processBgData() {
val readings = iobCobCalculator.ads.getBgReadingsDataTableCopy()
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()) {
state = State.DOUBLED
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))
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))
return
}
if (iobCobCalculator.ads.lastUsed5minCalculation == true) {