Cache TDD calculations

This commit is contained in:
Milos Kozak 2022-06-09 15:22:02 +02:00
parent 16c5b27bdd
commit 845879d916
7 changed files with 261 additions and 189 deletions

View file

@ -39,7 +39,6 @@ import info.nightscout.androidaps.events.EventPreferenceChange
import info.nightscout.androidaps.events.EventRebuildTabs
import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.logging.UserEntryLogger
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker
import info.nightscout.androidaps.plugins.constraints.signatureVerifier.SignatureVerifierPlugin
import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtils

View file

@ -30,6 +30,7 @@ import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.workflow.CalculationWorkflow
import info.nightscout.androidaps.events.Event
import info.nightscout.androidaps.utils.MidnightTime
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP
@ -130,7 +131,17 @@ class IobCobCalculatorPlugin @Inject constructor(
calculationWorkflow.stopCalculation(CalculationWorkflow.MAIN_CALCULATION, reason)
clearCache()
ads.reset()
calculationWorkflow.runCalculation(CalculationWorkflow.MAIN_CALCULATION,this, overviewData, reason, System.currentTimeMillis(), bgDataReload = false, limitDataToOldestAvailable = true, cause = event, runLoop = true)
calculationWorkflow.runCalculation(
CalculationWorkflow.MAIN_CALCULATION,
this,
overviewData,
reason,
System.currentTimeMillis(),
bgDataReload = false,
limitDataToOldestAvailable = true,
cause = event,
runLoop = true
)
}
override fun clearCache() {
@ -363,9 +374,11 @@ class IobCobCalculatorPlugin @Inject constructor(
scheduledHistoryPost?.cancel(false)
// prepare task for execution in 1 sec
scheduledEvent = event
scheduledHistoryPost = historyWorker.schedule({
scheduledHistoryPost = historyWorker.schedule(
{
synchronized(this) {
aapsLogger.debug(LTag.AUTOSENS, "Running newHistoryData")
repository.clearCachedData(MidnightTime.calc(event.oldDataTimestamp))
newHistoryData(
event.oldDataTimestamp,
event.reloadBgData,
@ -374,7 +387,8 @@ class IobCobCalculatorPlugin @Inject constructor(
scheduledEvent = null
scheduledHistoryPost = null
}
}, 1L, TimeUnit.SECONDS)
}, 1L, TimeUnit.SECONDS
)
} else {
// asked reload is newer -> adjust params only
scheduledEvent?.let {

View file

@ -11,6 +11,8 @@ import android.widget.TableRow
import android.widget.TextView
import info.nightscout.androidaps.R
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.database.ValueWrapper
import info.nightscout.androidaps.database.embedments.InterfaceIDs
import info.nightscout.androidaps.database.entities.Bolus
import info.nightscout.androidaps.database.entities.TotalDailyDose
import info.nightscout.androidaps.extensions.convertedToAbsolute
@ -19,10 +21,10 @@ import info.nightscout.androidaps.extensions.toTableRowHeader
import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.interfaces.IobCobCalculator
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.MidnightTime
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import javax.inject.Inject
@ -38,10 +40,19 @@ class TddCalculator @Inject constructor(
) {
fun calculate(days: Long): LongSparseArray<TotalDailyDose> {
val startTime = MidnightTime.calc(dateUtil.now() - T.days(days).msecs())
var startTime = MidnightTime.calc(dateUtil.now() - T.days(days).msecs())
val endTime = MidnightTime.calc(dateUtil.now())
val result = LongSparseArray<TotalDailyDose>()
// Try to load cached values
while (startTime < endTime) {
val tdd = repository.getCalculatedTotalDailyDose(startTime).blockingGet()
if (tdd is ValueWrapper.Existing) result.put(startTime, tdd.value)
else break
startTime += T.hours(24).msecs()
}
if (endTime > startTime) {
repository.getBolusesDataFromTimeToTime(startTime, endTime, true).blockingGet()
.filter { it.type != Bolus.Type.PRIMING }
.forEach { t ->
@ -75,11 +86,16 @@ class TddCalculator @Inject constructor(
}
result.put(midnight, tdd)
}
}
for (i in 0 until result.size()) {
val tdd = result.valueAt(i)
tdd.totalAmount = tdd.bolusAmount + tdd.basalAmount
if (tdd.interfaceIDs.pumpType != InterfaceIDs.PumpType.CACHE) {
tdd.interfaceIDs.pumpType = InterfaceIDs.PumpType.CACHE
aapsLogger.debug(LTag.CORE, "Storing TDD $tdd")
repository.createTotalDailyDose(tdd)
}
}
aapsLogger.debug(LTag.CORE, result.toString())
return result
}
@ -91,22 +107,13 @@ class TddCalculator @Inject constructor(
repository.getBolusesDataFromTimeToTime(startTime, endTime, true).blockingGet()
.filter { it.type != Bolus.Type.PRIMING }
.forEach { t ->
//val midnight = MidnightTime.calc(t.timestamp)
//val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight)
tdd.bolusAmount += t.amount
//result.put(midnight, tdd)
}
repository.getCarbsDataFromTimeToTimeExpanded(startTime, endTime, true).blockingGet().forEach { t ->
//val midnight = MidnightTime.calc(t.timestamp)
//val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight)
tdd.carbs += t.amount
//result.put(midnight, tdd)
}
val calculationStep = T.mins(5).msecs()
for (t in startTime until endTime step calculationStep) {
//val midnight = MidnightTime.calc(t)
//val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight)
val tbr = iobCobCalculator.getTempBasalIncludingConvertedExtended(t)
val profile = profileFunction.getProfile(t) ?: continue
val absoluteRate = tbr?.convertedToAbsolute(t, profile) ?: profile.getBasal(t)
@ -118,13 +125,8 @@ class TddCalculator @Inject constructor(
val absoluteEbRate = eb?.rate ?: 0.0
tdd.bolusAmount += absoluteEbRate / 60.0 * 5.0
}
//result.put(midnight, tdd)
}
//for (i in 0 until tdd.size()) {
//val tdd = result.valueAt(i)
tdd.totalAmount = tdd.bolusAmount + tdd.basalAmount
//}
aapsLogger.debug(LTag.CORE, tdd.toString())
return tdd
}
@ -133,27 +135,16 @@ class TddCalculator @Inject constructor(
val startTime = dateUtil.now() - T.hours(hour = 24).msecs()
val endTime = dateUtil.now()
val tdd = TotalDailyDose(timestamp = startTime)
//val result = TotalDailyDose()
repository.getBolusesDataFromTimeToTime(startTime, endTime, true).blockingGet()
.filter { it.type != Bolus.Type.PRIMING }
.forEach { t ->
//val midnight = MidnightTime.calc(t.timestamp)
//val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight)
tdd.bolusAmount += t.amount
//result.put(midnight, tdd)
}
repository.getCarbsDataFromTimeToTimeExpanded(startTime, endTime, true).blockingGet().forEach { t ->
//val midnight = MidnightTime.calc(t.timestamp)
//val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight)
tdd.carbs += t.amount
//result.put(midnight, tdd)
}
val calculationStep = T.mins(5).msecs()
//val tempBasals = iobCobCalculator.getTempBasalIncludingConvertedExtendedForRange(startTime, endTime, calculationStep)
for (t in startTime until endTime step calculationStep) {
//val midnight = MidnightTime.calc(t)
//val tdd = result[midnight] ?: TotalDailyDose(timestamp = midnight)
val tbr = iobCobCalculator.getTempBasalIncludingConvertedExtended(t)
val profile = profileFunction.getProfile(t) ?: continue
val absoluteRate = tbr?.convertedToAbsolute(t, profile) ?: profile.getBasal(t)
@ -165,13 +156,8 @@ class TddCalculator @Inject constructor(
val absoluteEbRate = eb?.rate ?: 0.0
tdd.bolusAmount += absoluteEbRate / 60.0 * 5.0
}
//result.put(midnight, tdd)
}
//for (i in 0 until tdd.size()) {
//val tdd = result.valueAt(i)
tdd.totalAmount = tdd.bolusAmount + tdd.basalAmount
//}
aapsLogger.debug(LTag.CORE, tdd.toString())
return tdd
}

View file

@ -11,7 +11,8 @@ import kotlin.math.min
@Suppress("unused")
enum class PumpType {
GENERIC_AAPS(description = "Generic AAPS",
GENERIC_AAPS(
description = "Generic AAPS",
manufacturer = ManufacturerType.AndroidAPS,
model = "VirtualPump",
bolusSize = 0.1,
@ -23,9 +24,11 @@ enum class PumpType {
baseBasalMinValue = 0.01,
baseBasalStep = 0.01,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.VirtualPumpCapabilities),
pumpCapability = PumpCapability.VirtualPumpCapabilities
),
CELLNOVO(description = "Cellnovo",
CELLNOVO(
description = "Cellnovo",
manufacturer = ManufacturerType.Cellnovo,
model = "Cellnovo",
bolusSize = 0.05,
@ -37,9 +40,11 @@ enum class PumpType {
baseBasalMinValue = 0.05,
baseBasalStep = 0.05,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.VirtualPumpCapabilities),
pumpCapability = PumpCapability.VirtualPumpCapabilities
),
ACCU_CHEK_COMBO(description = "Accu-Chek Combo",
ACCU_CHEK_COMBO(
description = "Accu-Chek Combo",
manufacturer = ManufacturerType.Roche,
model = "Combo",
bolusSize = 0.1,
@ -53,8 +58,10 @@ enum class PumpType {
baseBasalSpecialSteps = DoseStepSize.ComboBasal,
pumpCapability = PumpCapability.ComboCapabilities,
source = Sources.Combo,
supportBatteryLevel = false),
ACCU_CHEK_SPIRIT(description = "Accu-Chek Spirit",
supportBatteryLevel = false
),
ACCU_CHEK_SPIRIT(
description = "Accu-Chek Spirit",
manufacturer = ManufacturerType.Roche,
model = "Spirit",
bolusSize = 0.1,
@ -66,8 +73,10 @@ enum class PumpType {
baseBasalMinValue = 0.01,
baseBasalStep = 0.1,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.VirtualPumpCapabilities),
ACCU_CHEK_INSIGHT_VIRTUAL(description = "Accu-Chek Insight",
pumpCapability = PumpCapability.VirtualPumpCapabilities
),
ACCU_CHEK_INSIGHT_VIRTUAL(
description = "Accu-Chek Insight",
manufacturer = ManufacturerType.Roche,
model = "Insight",
bolusSize = 0.05,
@ -79,8 +88,10 @@ enum class PumpType {
baseBasalMinValue = 0.02,
baseBasalStep = 0.01,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.InsightCapabilities),
ACCU_CHEK_INSIGHT(description = "Accu-Chek Insight",
pumpCapability = PumpCapability.InsightCapabilities
),
ACCU_CHEK_INSIGHT(
description = "Accu-Chek Insight",
manufacturer = ManufacturerType.Roche,
model = "Insight",
bolusSize = 0.01,
@ -94,8 +105,10 @@ enum class PumpType {
baseBasalStep = 0.01,
baseBasalSpecialSteps = DoseStepSize.InsightBasal,
pumpCapability = PumpCapability.InsightCapabilities,
source = Sources.Insight),
ACCU_CHEK_SOLO(description = "Accu-Chek Solo",
source = Sources.Insight
),
ACCU_CHEK_SOLO(
description = "Accu-Chek Solo",
manufacturer = ManufacturerType.Roche,
model = "Solo",
bolusSize = 0.01,
@ -108,9 +121,11 @@ enum class PumpType {
baseBasalMaxValue = null,
baseBasalStep = 0.01,
baseBasalSpecialSteps = DoseStepSize.InsightBolus,
pumpCapability = PumpCapability.InsightCapabilities),
pumpCapability = PumpCapability.InsightCapabilities
),
ANIMAS_VIBE(description = "Animas Vibe",
ANIMAS_VIBE(
description = "Animas Vibe",
manufacturer = ManufacturerType.Animas,
model = "Vibe",
bolusSize = 0.05,
@ -123,9 +138,11 @@ enum class PumpType {
baseBasalMaxValue = 5.0,
baseBasalStep = 0.0,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.VirtualPumpCapabilities),
pumpCapability = PumpCapability.VirtualPumpCapabilities
),
ANIMAS_PING(description = "Animas Ping", model = "Ping", parent = ANIMAS_VIBE),
DANA_R(description = "DanaR",
DANA_R(
description = "DanaR",
manufacturer = ManufacturerType.Sooil,
model = "DanaR",
bolusSize = 0.05,
@ -138,8 +155,10 @@ enum class PumpType {
baseBasalStep = 0.01,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.DanaCapabilities,
source = Sources.DanaR),
DANA_R_KOREAN(description = "DanaR Korean",
source = Sources.DanaR
),
DANA_R_KOREAN(
description = "DanaR Korean",
manufacturer = ManufacturerType.Sooil,
model = "DanaRKorean",
bolusSize = 0.05,
@ -152,8 +171,10 @@ enum class PumpType {
baseBasalStep = 0.01,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.DanaCapabilities,
source = Sources.DanaRC),
DANA_RS(description = "DanaRS",
source = Sources.DanaRC
),
DANA_RS(
description = "DanaRS",
manufacturer = ManufacturerType.Sooil,
model = "DanaRS",
bolusSize = 0.05,
@ -166,11 +187,13 @@ enum class PumpType {
baseBasalStep = 0.01,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.DanaWithHistoryCapabilities,
source = Sources.DanaRS),
source = Sources.DanaRS
),
DANA_RS_KOREAN(description = "DanaRSKorean", model = "DanaRSKorean", parent = DANA_RS),
DANA_I(description = "DanaI", model = "DanaI", parent = DANA_RS, source = Sources.DanaI),
DANA_RV2(description = "DanaRv2", model = "DanaRv2", parent = DANA_RS, source = Sources.DanaRv2),
OMNIPOD_EROS(description = "Omnipod Eros",
OMNIPOD_EROS(
description = "Omnipod Eros",
manufacturer = ManufacturerType.Insulet,
model = "Eros",
bolusSize = 0.05,
@ -188,8 +211,10 @@ enum class PumpType {
isPatchPump = true,
useHardwareLink = true,
supportBatteryLevel = false,
source = Sources.OmnipodEros),
OMNIPOD_DASH(description = "Omnipod Dash",
source = Sources.OmnipodEros
),
OMNIPOD_DASH(
description = "Omnipod Dash",
manufacturer = ManufacturerType.Insulet,
model = "Dash",
bolusSize = 0.05,
@ -205,8 +230,10 @@ enum class PumpType {
isPatchPump = true,
pumpCapability = PumpCapability.OmnipodCapabilities,
hasCustomUnreachableAlertCheck = false,
supportBatteryLevel = false),
MEDTRONIC_512_712(description = "Medtronic 512/712",
supportBatteryLevel = false
),
MEDTRONIC_512_712(
description = "Medtronic 512/712",
manufacturer = ManufacturerType.Medtronic,
model = "512/712",
bolusSize = 0.1,
@ -219,14 +246,20 @@ enum class PumpType {
baseBasalStep = 0.05,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.MedtronicCapabilities,
source = Sources.Medtronic),
MEDTRONIC_515_715(description = "Medtronic 515/715",
source = Sources.Medtronic
),
MEDTRONIC_515_715(
description = "Medtronic 515/715",
model = "515/715",
parent = MEDTRONIC_512_712),
MEDTRONIC_522_722(description = "Medtronic 522/722",
parent = MEDTRONIC_512_712
),
MEDTRONIC_522_722(
description = "Medtronic 522/722",
model = "522/722",
parent = MEDTRONIC_512_712),
MEDTRONIC_523_723_REVEL(description = "Medtronic 523/723 (Revel)",
parent = MEDTRONIC_512_712
),
MEDTRONIC_523_723_REVEL(
description = "Medtronic 523/723 (Revel)",
manufacturer = ManufacturerType.Medtronic,
model = "523/723 (Revel)",
bolusSize = 0.05,
@ -239,9 +272,11 @@ enum class PumpType {
baseBasalStep = 0.025,
baseBasalSpecialSteps = DoseStepSize.MedtronicVeoBasal,
pumpCapability = PumpCapability.MedtronicCapabilities,
source = Sources.Medtronic),
source = Sources.Medtronic
),
MEDTRONIC_554_754_VEO(description = "Medtronic 554/754 (Veo)", model = "554/754 (Veo)", parent = MEDTRONIC_523_723_REVEL),
MEDTRONIC_640G(description = "Medtronic 640G",
MEDTRONIC_640G(
description = "Medtronic 640G",
manufacturer = ManufacturerType.Medtronic,
model = "640G",
bolusSize = 0.025,
@ -253,9 +288,11 @@ enum class PumpType {
baseBasalMinValue = 0.025,
baseBasalStep = 0.025,
baseBasalSpecialSteps = DoseStepSize.MedtronicVeoBasal,
pumpCapability = PumpCapability.VirtualPumpCapabilities),
pumpCapability = PumpCapability.VirtualPumpCapabilities
),
TANDEM_T_SLIM(description = "Tandem t:slim",
TANDEM_T_SLIM(
description = "Tandem t:slim",
manufacturer = ManufacturerType.Tandem,
model = "t:slim",
bolusSize = 0.01,
@ -267,12 +304,14 @@ enum class PumpType {
baseBasalMinValue = 0.1,
baseBasalStep = 0.001,
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.VirtualPumpCapabilities),
pumpCapability = PumpCapability.VirtualPumpCapabilities
),
TANDEM_T_FLEX(description = "Tandem t:flex", model = "t:flex", parent = TANDEM_T_SLIM),
TANDEM_T_SLIM_G4(description = "Tandem t:slim G4", model = "t:slim G4", parent = TANDEM_T_SLIM),
TANDEM_T_SLIM_X2(description = "Tandem t:slim X2", model = "t:slim X2", parent = TANDEM_T_SLIM),
YPSOPUMP(description = "YpsoPump",
YPSOPUMP(
description = "YpsoPump",
manufacturer = ManufacturerType.Ypsomed,
model = "Ypsopump",
bolusSize = 0.1,
@ -285,26 +324,39 @@ enum class PumpType {
baseBasalMaxValue = 40.0,
baseBasalStep = 0.01,
baseBasalSpecialSteps = DoseStepSize.YpsopumpBasal,
pumpCapability = PumpCapability.YpsomedCapabilities),
MDI(description = "MDI",
pumpCapability = PumpCapability.YpsomedCapabilities
),
MDI(
description = "MDI",
manufacturer = ManufacturerType.AndroidAPS,
bolusSize = 0.5,
model = "MDI",
tbrSettings = DoseSettings(1.0, 15, 24 * 60, 0.0, 500.0),
extendedBolusSettings = DoseSettings(0.1, 15, 12 * 60, 0.1),
pumpCapability = PumpCapability.MDI),
pumpCapability = PumpCapability.MDI
),
// Not real pump. Used for User as a source
USER(description = "USER",
USER(
description = "USER",
manufacturer = ManufacturerType.AndroidAPS,
model = "USER",
tbrSettings = DoseSettings(1.0, 15, 24 * 60, 0.0, 500.0),
extendedBolusSettings = DoseSettings(0.1, 15, 12 * 60, 0.1),
pumpCapability = PumpCapability.MDI,
source = Sources.MDI),
source = Sources.MDI
),
// Not real, cached value
CACHE(
description = "CACHE",
model = "CACHE",
parent = USER
),
//Diaconn Pump
DIACONN_G8(description = "Diaconn G8",
DIACONN_G8(
description = "Diaconn G8",
manufacturer = ManufacturerType.G2e,
model = "DiaconnG8",
bolusSize = 0.01,
@ -319,7 +371,8 @@ enum class PumpType {
baseBasalSpecialSteps = null,
pumpCapability = PumpCapability.DiaconnCapabilities,
source = Sources.DiaconnG8,
useHardwareLink = true);
useHardwareLink = true
);
val description: String
var manufacturer: ManufacturerType? = null
@ -407,6 +460,7 @@ enum class PumpType {
InterfaceIDs.PumpType.MDI -> MDI
InterfaceIDs.PumpType.USER -> USER
InterfaceIDs.PumpType.DIACONN_G8 -> DIACONN_G8
InterfaceIDs.PumpType.CACHE -> TODO()
}
}
@ -418,7 +472,8 @@ enum class PumpType {
parent.model = model
}
constructor(description: String,
constructor(
description: String,
manufacturer: ManufacturerType,
model: String,
bolusSize: Double = 0.0,
@ -436,7 +491,8 @@ enum class PumpType {
isPatchPump: Boolean = false,
supportBatteryLevel: Boolean = true,
useHardwareLink: Boolean = false,
source: Sources = Sources.VirtualPump) {
source: Sources = Sources.VirtualPump
) {
this.description = description
this.manufacturer = manufacturer
this.model = model
@ -463,12 +519,14 @@ enum class PumpType {
val eb = extendedBolusSettings ?: return "INVALID"
val tbr = tbrSettings ?: return "INVALID"
val extendedNote = if (hasExtendedBasals) rh.gs(R.string.def_extended_note) else ""
return String.format(i18nTemplate,
return String.format(
i18nTemplate,
getStep("" + bolusSize, specialBolusSize),
eb.step, eb.durationStep, eb.maxDuration / 60,
getStep(baseBasalRange(), baseBasalSpecialSteps),
tbr.minDose.toString() + unit + "-" + tbr.maxDose + unit, tbr.step.toString() + unit,
tbr.durationStep, tbr.maxDuration / 60, extendedNote)
tbr.durationStep, tbr.maxDuration / 60, extendedNote
)
}
private fun baseBasalRange(): String =
@ -494,8 +552,10 @@ enum class PumpType {
fun determineCorrectBasalSize(basalAmount: Double): Double {
val tSettings = tbrSettings ?: throw IllegalStateException()
return Round.roundTo(min(basalAmount, tSettings.maxDose), baseBasalSpecialSteps?.getStepSizeForAmount(basalAmount)
?: baseBasalStep)
return Round.roundTo(
min(basalAmount, tSettings.maxDose), baseBasalSpecialSteps?.getStepSizeForAmount(basalAmount)
?: baseBasalStep
)
}
fun toDbPumpType(): InterfaceIDs.PumpType =
@ -531,5 +591,6 @@ enum class PumpType {
MDI -> InterfaceIDs.PumpType.MDI
USER -> InterfaceIDs.PumpType.USER
DIACONN_G8 -> InterfaceIDs.PumpType.DIACONN_G8
CACHE -> InterfaceIDs.PumpType.CACHE
}
}

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.database
import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.androidaps.database.data.NewEntries
import info.nightscout.androidaps.database.embedments.InterfaceIDs
import info.nightscout.androidaps.database.entities.*
import info.nightscout.androidaps.database.interfaces.DBEntry
import info.nightscout.androidaps.database.transactions.Transaction
@ -59,6 +60,10 @@ import kotlin.math.roundToInt
fun clearDatabases() = database.clearAllTables()
fun clearCachedData(from: Long) {
database.totalDailyDoseDao.deleteNewerThan(from, InterfaceIDs.PumpType.CACHE)
}
//BG READINGS -- only valid records
fun compatGetBgReadingsDataFromTime(timestamp: Long, ascending: Boolean): Single<List<GlucoseValue>> =
database.glucoseValueDao.compatGetBgReadingsDataFromTime(timestamp)
@ -775,22 +780,26 @@ import kotlin.math.roundToInt
fun getOldestExtendedBolusRecord(): ExtendedBolus? =
database.extendedBolusDao.getOldestRecord()
// TotalDailyDose
fun getAllTotalDailyDoses(ascending: Boolean): Single<List<TotalDailyDose>> =
database.totalDailyDoseDao.getAllTotalDailyDoses()
.map { if (!ascending) it.reversed() else it }
fun getLastExtendedBolusIdWrapped(): Single<ValueWrapper<Long>> =
database.extendedBolusDao.getLastId()
.subscribeOn(Schedulers.io())
.toWrappedSingle()
// TotalDailyDose
fun getLastTotalDailyDoses(count: Int, ascending: Boolean): Single<List<TotalDailyDose>> =
database.totalDailyDoseDao.getLastTotalDailyDoses(count)
.map { if (!ascending) it.reversed() else it }
.subscribeOn(Schedulers.io())
fun getLastExtendedBolusIdWrapped(): Single<ValueWrapper<Long>> =
database.extendedBolusDao.getLastId()
fun getCalculatedTotalDailyDose(timestamp: Long): Single<ValueWrapper<TotalDailyDose>> =
database.totalDailyDoseDao.findByTimestamp(timestamp, InterfaceIDs.PumpType.CACHE)
.subscribeOn(Schedulers.io())
.toWrappedSingle()
fun createTotalDailyDose(tdd: TotalDailyDose) {
database.totalDailyDoseDao.insert(tdd)
}
// OFFLINE EVENT
/*
* returns a Pair of the next entity to sync and the ID of the "update".

View file

@ -2,11 +2,10 @@ package info.nightscout.androidaps.database.daos
import androidx.room.Dao
import androidx.room.Query
import info.nightscout.androidaps.database.TABLE_TEMPORARY_TARGETS
import info.nightscout.androidaps.database.TABLE_TOTAL_DAILY_DOSES
import info.nightscout.androidaps.database.embedments.InterfaceIDs
import info.nightscout.androidaps.database.entities.TemporaryTarget
import info.nightscout.androidaps.database.entities.TotalDailyDose
import io.reactivex.rxjava3.core.Maybe
import io.reactivex.rxjava3.core.Single
@Suppress("FunctionName")
@ -25,13 +24,16 @@ internal interface TotalDailyDoseDao : TraceableDao<TotalDailyDose> {
@Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE timestamp = :timestamp AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL")
fun findByPumpTimestamp(timestamp: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): TotalDailyDose?
@Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC")
fun getAllTotalDailyDoses(): Single<List<TotalDailyDose>>
@Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE timestamp = :timestamp AND pumpType = :pumpType AND referenceId IS NULL")
fun findByTimestamp(timestamp: Long, pumpType: InterfaceIDs.PumpType): Maybe<TotalDailyDose>
@Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE isValid = 1 AND referenceId IS NULL ORDER BY timestamp DESC LIMIT :count")
fun getLastTotalDailyDoses(count: Int): Single<List<TotalDailyDose>>
@Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE isValid = 1 AND referenceId IS NULL AND pumpType <> :exclude ORDER BY timestamp DESC LIMIT :count")
fun getLastTotalDailyDoses(count: Int, exclude: InterfaceIDs.PumpType = InterfaceIDs.PumpType.CACHE): Single<List<TotalDailyDose>>
@Query("SELECT * FROM $TABLE_TOTAL_DAILY_DOSES WHERE dateCreated > :since AND dateCreated <= :until LIMIT :limit OFFSET :offset")
suspend fun getNewEntriesSince(since: Long, until: Long, limit: Int, offset: Int): List<TotalDailyDose>
@Query("DELETE FROM $TABLE_TOTAL_DAILY_DOSES WHERE dateCreated >= :since AND pumpType = :pumpType")
fun deleteNewerThan(since: Long, pumpType: InterfaceIDs.PumpType)
}

View file

@ -42,7 +42,8 @@ data class InterfaceIDs(
YPSOPUMP,
MDI,
DIACONN_G8,
USER;
USER,
CACHE;
companion object {