AndroidAPS/app/src/main/java/info/nightscout/utils/BolusWizard.java

127 lines
4.5 KiB
Java
Raw Normal View History

2016-10-11 22:47:48 +02:00
package info.nightscout.utils;
import org.json.JSONObject;
2017-02-23 20:00:33 +01:00
import java.util.Date;
2016-10-11 22:47:48 +02:00
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.GlucoseStatus;
2016-10-11 22:47:48 +02:00
import info.nightscout.androidaps.interfaces.TempBasalsInterface;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
2017-01-04 22:33:17 +01:00
import info.nightscout.androidaps.data.IobTotal;
2017-02-23 20:00:33 +01:00
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
2017-02-17 13:18:36 +01:00
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
2016-10-11 22:47:48 +02:00
/**
* Created by mike on 11.10.2016.
*/
public class BolusWizard {
// Inputs
JSONObject specificProfile = null;
2017-02-10 06:07:31 +01:00
public Integer carbs = 0;
2016-10-11 22:47:48 +02:00
Double bg = 0d;
Double correction;
Boolean includeBolusIOB = true;
Boolean includeBasalIOB = true;
Boolean superBolus = false;
Boolean trend = false;
2016-10-11 22:47:48 +02:00
// Intermediate
public Double sens = 0d;
public Double ic = 0d;
public GlucoseStatus glucoseStatus;
2016-10-11 22:47:48 +02:00
public Double targetBGLow = 0d;
public Double targetBGHigh = 0d;
public Double bgDiff = 0d;
IobTotal bolusIob;
IobTotal basalIob;
public Double insulinFromBG = 0d;
public Double insulinFromCarbs = 0d;
public Double insulingFromBolusIOB = 0d;
public Double insulingFromBasalsIOB = 0d;
public Double insulinFromCorrection = 0d;
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;
public Double carbsEquivalent = 0d;
public Double doCalc(JSONObject specificProfile, Integer carbs, Double cob, Double bg, Double correction, Boolean includeBolusIOB, Boolean includeBasalIOB, Boolean superBolus, Boolean trend) {
2016-10-11 22:47:48 +02:00
this.specificProfile = specificProfile;
this.carbs = carbs;
this.bg = bg;
this.correction = correction;
this.superBolus = superBolus;
this.trend = trend;
2016-10-11 22:47:48 +02:00
2017-02-23 20:00:33 +01:00
NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
2016-10-11 22:47:48 +02:00
// Insulin from BG
sens = profile.getIsf(specificProfile, NSProfile.secondsFromMidnight());
targetBGLow = profile.getTargetLow(specificProfile, NSProfile.secondsFromMidnight());
targetBGHigh = profile.getTargetHigh(specificProfile, NSProfile.secondsFromMidnight());
if (bg <= targetBGLow) {
bgDiff = bg - targetBGLow;
} else {
bgDiff = bg - targetBGHigh;
}
insulinFromBG = bg != 0d ? bgDiff / sens : 0d;
// Insulin from 15 min trend
glucoseStatus = GlucoseStatus.getGlucoseStatusData();
2017-04-24 19:24:53 +02:00
if (glucoseStatus != null && trend) {
insulinFromTrend = (NSProfile.fromMgdlToUnits(glucoseStatus.short_avgdelta, profile.getUnits()) * 3) / sens;
}
2016-10-11 22:47:48 +02:00
// Insuling from carbs
ic = profile.getIc(specificProfile, NSProfile.secondsFromMidnight());
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
TreatmentsInterface treatments = ConfigBuilderPlugin.getActiveTreatments();
2016-10-11 22:47:48 +02:00
treatments.updateTotalIOB();
2017-02-23 20:00:33 +01:00
IobTotal bolusIob = treatments.getLastCalculation();
TempBasalsInterface tempBasals = ConfigBuilderPlugin.getActiveTempBasals();
IobTotal basalIob = new IobTotal(new Date().getTime());
if (tempBasals != null) {
tempBasals.updateTotalIOB();
basalIob = tempBasals.getLastCalculation().round();
}
2016-10-11 22:47:48 +02:00
insulingFromBolusIOB = includeBolusIOB ? -bolusIob.iob : 0d;
insulingFromBasalsIOB = includeBasalIOB ? -basalIob.basaliob : 0d;
// Insulin from correction
insulinFromCorrection = correction;
// Insulin from superbolus for 2h. Get basal rate now and after 1h
if (superBolus) {
insulinFromSuperBolus = profile.getBasal(NSProfile.secondsFromMidnight());
long timeAfter1h = new Date().getTime();
timeAfter1h += 60L * 60 * 1000;
insulinFromSuperBolus += profile.getBasal(NSProfile.secondsFromMidnight(new Date(timeAfter1h)));
}
2016-10-11 22:47:48 +02:00
// Total
calculatedTotalInsulin = insulinFromBG + insulinFromTrend + insulinFromCarbs + insulingFromBolusIOB + insulingFromBasalsIOB + insulinFromCorrection + insulinFromSuperBolus + insulinFromCOB;
2016-10-11 22:47:48 +02:00
if (calculatedTotalInsulin < 0) {
carbsEquivalent = -calculatedTotalInsulin * ic;
calculatedTotalInsulin = 0d;
}
calculatedTotalInsulin = Round.roundTo(calculatedTotalInsulin, 0.05d);
return calculatedTotalInsulin;
}
}