diff --git a/app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js b/app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js index ce13f03387..d65a328136 100644 --- a/app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js +++ b/app/src/main/assets/OpenAPSSMBDynamicISF/determine-basal.js @@ -284,18 +284,32 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_ else { console.error("Time now is "+now+"; "); } + console.error("++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + console.error("++ Dynamic ISF Beta 1.3 - Based on rolling 24 hours ++"); + console.error("++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + if (meal_data.TDDAIMI7){ var tdd7 = meal_data.TDDAIMI7; + } + else{ + var tdd7 = ((basal * 12)*100)/21; } - else{ - var tdd7 = ((basal * 12)*100)/21; - } - var tdd_pump_now = meal_data.TDDPUMP; - var tdd_pump = ( tdd_pump_now / (now / 24)); - var TDD = (tdd7 * 0.4) + (tdd_pump * 0.6); - console.error("Pump extrapolated TDD = "+tdd_pump+"; "); + console.error("7-day average TDD is: " +tdd7+ "; "); + + + if (meal_data.TDDLast24){ + var tdd_24 = meal_data.TDDLast24; + } + else { + var tdd_24 = (( basal * 24 ) * 2.8); + } + + var TDD = (tdd7 * 0.3) + (tdd_24 * 0.7); + + console.error("Rolling 24 hour TDD = "+tdd_24+"; "); + console.error("Weighted Average TDD = "+TDD"; "); //if (tdd7 > 0){ - if ( tdd_pump > tdd7 && now < 5 || now < 7 && TDD < ( 0.8 * tdd7 ) ){ + /*if ( tdd_pump > tdd7 && now < 5 || now < 7 && TDD < ( 0.8 * tdd7 ) ){ TDD = ( 0.8 * tdd7 ); console.log("Excess or too low insulin from pump so TDD set to "+TDD+" based on 75% of TDD7; "); rT.reason += "TDD: " +TDD+ " due to low or high tdd from pump; "; @@ -316,7 +330,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_ else { console.log("TDD 7 ="+tdd7+", TDD Pump ="+tdd_pump+" and TDD = "+TDD+";"); rT.reason += "TDD: " +TDD+ " based on standard pump 60/tdd7 40 split; "; - } + }*/ var variable_sens = (277700 / (TDD * bg)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMBDynamicISF/DetermineBasalAdapterSMBDynamicISFJS.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMBDynamicISF/DetermineBasalAdapterSMBDynamicISFJS.kt index 950a3e9f26..4a84093b6a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMBDynamicISF/DetermineBasalAdapterSMBDynamicISFJS.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMBDynamicISF/DetermineBasalAdapterSMBDynamicISFJS.kt @@ -260,6 +260,7 @@ class DetermineBasalAdapterSMBDynamicISFJS internal constructor(private val scri this.mealData.put("TDDAIMI7", tddCalculator.averageTDD(tddCalculator.calculate(7)).totalAmount) this.mealData.put("TDDPUMP", tddCalculator.calculateDaily().totalAmount) + this.mealData.put("TDDLast24", tddCalculator!!.calculate24Daily().totalAmount) if (constraintChecker.isAutosensModeEnabled().value()) { autosensData.put("ratio", autosensDataRatio) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt index 0f08ccea8a..fc7cfe2bb0 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt @@ -122,7 +122,53 @@ class TddCalculator @Inject constructor( aapsLogger.debug(LTag.CORE, tdd.toString()) return tdd } + fun calculate24Daily():TotalDailyDose { + 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) + tdd.basalAmount += absoluteRate / T.mins(5).msecs().toDouble() * calculationStep.toDouble() + + if (!activePlugin.activePump.isFakingTempsByExtendedBoluses) { + // they are not included in TBRs + val eb = iobCobCalculator.getExtendedBolus(t) + val absoluteEbRate = eb?.rate ?: 0.0 + tdd.bolusAmount += absoluteEbRate / T.mins(5).msecs().toDouble() * calculationStep.toDouble() + } + //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 + } fun averageTDD(tdds: LongSparseArray): TotalDailyDose { val totalTdd = TotalDailyDose(timestamp = dateUtil.now()) for (i in 0 until tdds.size()) {