diff --git a/app/src/main/java/info/nightscout/androidaps/utils/TIR.kt b/app/src/main/java/info/nightscout/androidaps/utils/TIR.kt index 9d0d662f5e..4ceb9d7504 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/TIR.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/TIR.kt @@ -1,5 +1,14 @@ package info.nightscout.androidaps.utils -class TIR(val low: Double, val high: Double) { +class TIR(val date: Long, val lowThreshold: Double, val highThreshold: Double) { + private var below = 0; + private var inRange = 0; + private var above = 0; + private var error = 0; + private var count = 0; + fun error() = run { error++ } + fun below() = run { below++; count++ } + fun inRange() = run { inRange++; count++ } + fun above() = run { above++; count++ } } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/TirCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/TirCalculator.kt index ddfe9b9216..08a8c70cd7 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/TirCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/TirCalculator.kt @@ -1,9 +1,25 @@ package info.nightscout.androidaps.utils import android.util.LongSparseArray +import info.nightscout.androidaps.MainApp object TirCalculator { - fun calculate(days: Long): LongSparseArray { - return LongSparseArray() + fun calculate(days: Long, lowMgdl: Double, highMgdl: Double): LongSparseArray { + if (lowMgdl < 39) throw RuntimeException("Low below 39") + if (lowMgdl > highMgdl) throw RuntimeException("Low > High") + val startTime = MidnightTime.calc(DateUtil.now()) - T.days(days).msecs() + val endTime = MidnightTime.calc(DateUtil.now()) + + val bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(startTime, endTime, true) + val result = LongSparseArray() + for (bg in bgReadings) { + val midnight = MidnightTime.calc(bg.date) + val tir = result[midnight] ?: TIR(midnight, lowMgdl, highMgdl) + if (bg.value < 39) tir.error() + if (bg.value >= 39 && bg.value < lowMgdl) tir.below() + if (bg.value in lowMgdl..highMgdl) tir.inRange() + if (bg.value > highMgdl) tir.inRange() + } + return result } } \ No newline at end of file