From 0aad00808c02a400b4af2acf68c612b94965766c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 27 Apr 2017 20:22:04 +0200 Subject: [PATCH] fix temp basal iob calculation --- .../nightscout/androidaps/data/IobTotal.java | 2 +- .../nightscout/androidaps/db/TempBasal.java | 62 ++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java index 941480e3a1..ed3096c945 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java +++ b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java @@ -20,7 +20,7 @@ public class IobTotal { public Double hightempinsulin; public Double netInsulin = 0d; // for calculations from temp basals only - public Double netRatio = 0d; // for calculations from temp basals only + public Double netRatio = 0d; // net ratio at start of temp basal long time; diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java index 43ec244d59..deccb18049 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TempBasal.java @@ -59,6 +59,64 @@ public class TempBasal { NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin(); + if (profile == null) + return result; + + int realDuration = getDurationToTime(time); + Double netBasalAmount = 0d; + + if (realDuration > 0) { + Double netBasalRate = 0d; + + int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d); + + result.netRatio = netBasalRate; + + double tempBolusSpacing = realDuration / aboutFiveMinIntervals; + for (Long j = 0L; j < aboutFiveMinIntervals; j++) { + // find middle of the interval + Long date = (long) (timeStart.getTime() + j * tempBolusSpacing * 60 * 1000 + 0.5d * tempBolusSpacing * 60 * 1000); + + Double basalRate = profile.getBasal(NSProfile.secondsFromMidnight(date)); + if (isExtended) { + netBasalRate = this.absolute; + } else { + if (this.isAbsolute) { + netBasalRate = this.absolute - basalRate; + } else { + netBasalRate = (this.percent - 100) / 100d * basalRate; + } + } + + double tempBolusSize = netBasalRate * tempBolusSpacing / 60d; + netBasalAmount += tempBolusSize; + + Treatment tempBolusPart = new Treatment(insulinInterface); + tempBolusPart.insulin = tempBolusSize; + tempBolusPart.created_at = new Date(date); + + Iob aIOB = insulinInterface.iobCalc(tempBolusPart, time, profile.getDia()); + result.basaliob += aIOB.iobContrib; + result.activity += aIOB.activityContrib; + Double dia_ago = time - profile.getDia() * 60 * 60 * 1000; + if (date > dia_ago && date <= time) { + result.netbasalinsulin += tempBolusPart.insulin; + if (tempBolusPart.insulin > 0) { + result.hightempinsulin += tempBolusPart.insulin; + } + } + } + } + result.netInsulin = netBasalAmount; + return result; + } + +/* + public IobTotal old_iobCalc(long time) { + IobTotal result = new IobTotal(time); + NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); + InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin(); + if (profile == null) return result; @@ -116,6 +174,7 @@ public class TempBasal { } return result; } +*/ // Determine end of basal public long getTimeEnd() { @@ -184,7 +243,8 @@ public class TempBasal { return false; } // closed end - if (timeStart.getTime() < time.getTime() && timeEnd.getTime() > time.getTime()) return true; // in interval + if (timeStart.getTime() < time.getTime() && timeEnd.getTime() > time.getTime()) + return true; // in interval return false; }