diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/bgQualityCheck/BgQualityCheckPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/bgQualityCheck/BgQualityCheckPlugin.kt index 18bc5e173d..4b0c9c8e4b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/bgQualityCheck/BgQualityCheckPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/bgQualityCheck/BgQualityCheckPlugin.kt @@ -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): Constraint { + // Fallback to LGS if BG values are doubled + override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint = 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) {