2016-10-11 22:47:48 +02:00
|
|
|
package info.nightscout.utils;
|
|
|
|
|
2017-05-12 21:07:29 +02:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2017-04-14 14:50:18 +02:00
|
|
|
import info.nightscout.androidaps.data.GlucoseStatus;
|
2017-01-04 22:33:17 +01:00
|
|
|
import info.nightscout.androidaps.data.IobTotal;
|
2017-06-02 10:25:49 +02:00
|
|
|
import info.nightscout.androidaps.data.Profile;
|
2017-09-09 15:34:00 +02:00
|
|
|
import info.nightscout.androidaps.db.TempTarget;
|
|
|
|
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
|
2017-11-20 23:39:12 +01:00
|
|
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
2016-10-11 22:47:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 11.10.2016.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class BolusWizard {
|
|
|
|
// Inputs
|
2017-12-26 00:27:34 +01:00
|
|
|
private Profile specificProfile = null;
|
|
|
|
private TempTarget tempTarget;
|
2017-02-10 06:07:31 +01:00
|
|
|
public Integer carbs = 0;
|
2017-12-26 00:27:34 +01:00
|
|
|
private Double bg = 0d;
|
|
|
|
private Double correction;
|
|
|
|
private Boolean includeBolusIOB = true;
|
|
|
|
private Boolean includeBasalIOB = true;
|
|
|
|
public Boolean superBolus = false;
|
|
|
|
private Boolean trend = false;
|
2016-10-11 22:47:48 +02:00
|
|
|
|
|
|
|
// Intermediate
|
|
|
|
public Double sens = 0d;
|
|
|
|
public Double ic = 0d;
|
|
|
|
|
2017-04-14 14:50:18 +02:00
|
|
|
public GlucoseStatus glucoseStatus;
|
|
|
|
|
2016-10-11 22:47:48 +02:00
|
|
|
public Double targetBGLow = 0d;
|
|
|
|
public Double targetBGHigh = 0d;
|
|
|
|
public Double bgDiff = 0d;
|
|
|
|
|
|
|
|
public Double insulinFromBG = 0d;
|
|
|
|
public Double insulinFromCarbs = 0d;
|
|
|
|
public Double insulingFromBolusIOB = 0d;
|
|
|
|
public Double insulingFromBasalsIOB = 0d;
|
|
|
|
public Double insulinFromCorrection = 0d;
|
2017-04-14 14:50:18 +02:00
|
|
|
public Double insulinFromSuperBolus = 0d;
|
|
|
|
public Double insulinFromCOB = 0d;
|
|
|
|
public Double insulinFromTrend = 0d;
|
2016-10-11 22:47:48 +02:00
|
|
|
|
|
|
|
// Result
|
|
|
|
public Double calculatedTotalInsulin = 0d;
|
2017-07-25 22:04:28 +02:00
|
|
|
public Double totalBeforePercentageAdjustment = 0d;
|
2016-10-11 22:47:48 +02:00
|
|
|
public Double carbsEquivalent = 0d;
|
|
|
|
|
2017-09-09 15:34:00 +02:00
|
|
|
public Double doCalc(Profile specificProfile, TempTarget tempTarget, Integer carbs, Double cob, Double bg, Double correction, Boolean includeBolusIOB, Boolean includeBasalIOB, Boolean superBolus, Boolean trend) {
|
|
|
|
return doCalc(specificProfile, tempTarget, carbs, cob, bg, correction, 100d, includeBolusIOB, includeBasalIOB, superBolus, trend);
|
2017-07-25 22:04:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-09 15:34:00 +02:00
|
|
|
public Double doCalc(Profile specificProfile, TempTarget tempTarget, Integer carbs, Double cob, Double bg, Double correction, double percentageCorrection, Boolean includeBolusIOB, Boolean includeBasalIOB, Boolean superBolus, Boolean trend) {
|
2016-10-11 22:47:48 +02:00
|
|
|
this.specificProfile = specificProfile;
|
2017-09-09 15:34:00 +02:00
|
|
|
this.tempTarget = tempTarget;
|
2016-10-11 22:47:48 +02:00
|
|
|
this.carbs = carbs;
|
|
|
|
this.bg = bg;
|
|
|
|
this.correction = correction;
|
2017-09-09 15:34:00 +02:00
|
|
|
this.includeBolusIOB = includeBolusIOB;
|
|
|
|
this.includeBasalIOB = includeBasalIOB;
|
2017-04-14 14:50:18 +02:00
|
|
|
this.superBolus = superBolus;
|
|
|
|
this.trend = trend;
|
2016-10-11 22:47:48 +02:00
|
|
|
|
|
|
|
// Insulin from BG
|
2017-06-02 10:25:49 +02:00
|
|
|
sens = specificProfile.getIsf();
|
|
|
|
targetBGLow = specificProfile.getTargetLow();
|
|
|
|
targetBGHigh = specificProfile.getTargetHigh();
|
2017-09-09 15:34:00 +02:00
|
|
|
if (tempTarget != null) {
|
2017-09-12 01:23:38 +02:00
|
|
|
targetBGLow = Profile.fromMgdlToUnits(tempTarget.low, specificProfile.getUnits());
|
|
|
|
targetBGHigh = Profile.fromMgdlToUnits(tempTarget.high, specificProfile.getUnits());
|
2017-09-09 15:34:00 +02:00
|
|
|
}
|
2017-12-22 00:17:46 +01:00
|
|
|
if (bg >= targetBGLow && bg <= targetBGHigh) {
|
|
|
|
bgDiff = 0d;
|
|
|
|
} else if (bg <= targetBGLow) {
|
2016-10-11 22:47:48 +02:00
|
|
|
bgDiff = bg - targetBGLow;
|
|
|
|
} else {
|
|
|
|
bgDiff = bg - targetBGHigh;
|
|
|
|
}
|
|
|
|
insulinFromBG = bg != 0d ? bgDiff / sens : 0d;
|
|
|
|
|
2017-04-14 14:50:18 +02:00
|
|
|
// Insulin from 15 min trend
|
|
|
|
glucoseStatus = GlucoseStatus.getGlucoseStatusData();
|
2017-04-24 19:24:53 +02:00
|
|
|
if (glucoseStatus != null && trend) {
|
2017-06-02 10:25:49 +02:00
|
|
|
insulinFromTrend = (Profile.fromMgdlToUnits(glucoseStatus.short_avgdelta, specificProfile.getUnits()) * 3) / sens;
|
2017-04-14 14:50:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 22:47:48 +02:00
|
|
|
// Insuling from carbs
|
2017-06-02 10:25:49 +02:00
|
|
|
ic = specificProfile.getIc();
|
2016-10-11 22:47:48 +02:00
|
|
|
insulinFromCarbs = carbs / ic;
|
2017-04-24 17:46:20 +02:00
|
|
|
insulinFromCOB = cob / ic;
|
2016-10-11 22:47:48 +02:00
|
|
|
|
|
|
|
// Insulin from IOB
|
2017-02-23 20:00:33 +01:00
|
|
|
// IOB calculation
|
2017-05-12 21:07:29 +02:00
|
|
|
TreatmentsInterface treatments = MainApp.getConfigBuilder();
|
2017-05-11 18:54:50 +02:00
|
|
|
treatments.updateTotalIOBTreatments();
|
2017-05-12 16:27:33 +02:00
|
|
|
IobTotal bolusIob = treatments.getLastCalculationTreatments().round();
|
|
|
|
treatments.updateTotalIOBTempBasals();
|
|
|
|
IobTotal basalIob = treatments.getLastCalculationTempBasals().round();
|
2016-10-11 22:47:48 +02:00
|
|
|
|
|
|
|
insulingFromBolusIOB = includeBolusIOB ? -bolusIob.iob : 0d;
|
|
|
|
insulingFromBasalsIOB = includeBasalIOB ? -basalIob.basaliob : 0d;
|
|
|
|
|
|
|
|
// Insulin from correction
|
|
|
|
insulinFromCorrection = correction;
|
|
|
|
|
2017-04-14 14:50:18 +02:00
|
|
|
// Insulin from superbolus for 2h. Get basal rate now and after 1h
|
|
|
|
if (superBolus) {
|
2017-06-02 10:25:49 +02:00
|
|
|
insulinFromSuperBolus = specificProfile.getBasal();
|
2017-06-15 23:12:12 +02:00
|
|
|
long timeAfter1h = System.currentTimeMillis();
|
2017-04-14 14:50:18 +02:00
|
|
|
timeAfter1h += 60L * 60 * 1000;
|
2017-06-11 17:22:54 +02:00
|
|
|
insulinFromSuperBolus += specificProfile.getBasal(timeAfter1h);
|
2017-04-14 14:50:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 22:47:48 +02:00
|
|
|
// Total
|
2017-09-09 15:34:00 +02:00
|
|
|
calculatedTotalInsulin = insulinFromBG + insulinFromTrend + insulinFromCarbs + insulingFromBolusIOB + insulingFromBasalsIOB + insulinFromCorrection + insulinFromSuperBolus + insulinFromCOB;
|
2017-07-25 22:04:28 +02:00
|
|
|
|
2017-09-09 15:34:00 +02:00
|
|
|
// Percentage adjustment
|
|
|
|
totalBeforePercentageAdjustment = calculatedTotalInsulin;
|
|
|
|
if (calculatedTotalInsulin > 0) {
|
|
|
|
calculatedTotalInsulin = calculatedTotalInsulin * percentageCorrection / 100d;
|
|
|
|
}
|
2016-10-11 22:47:48 +02:00
|
|
|
|
|
|
|
if (calculatedTotalInsulin < 0) {
|
|
|
|
carbsEquivalent = -calculatedTotalInsulin * ic;
|
|
|
|
calculatedTotalInsulin = 0d;
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:39:12 +01:00
|
|
|
double bolusStep = ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep;
|
2017-07-14 16:45:31 +02:00
|
|
|
calculatedTotalInsulin = Round.roundTo(calculatedTotalInsulin, bolusStep);
|
2016-10-11 22:47:48 +02:00
|
|
|
|
|
|
|
return calculatedTotalInsulin;
|
|
|
|
}
|
|
|
|
}
|