AndroidAPS/app/src/main/java/info/nightscout/androidaps/data/QuickWizardEntry.java

238 lines
7 KiB
Java
Raw Normal View History

2017-12-26 00:27:34 +01:00
package info.nightscout.androidaps.data;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
2019-11-12 00:01:58 +01:00
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
2019-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.CobInfo;
2019-04-16 20:36:18 +02:00
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
2019-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.BolusWizard;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SP;
2017-12-26 00:27:34 +01:00
/**
* Created by mike on 25.12.2017.
*/
public class QuickWizardEntry {
private static Logger log = LoggerFactory.getLogger(QuickWizardEntry.class);
public JSONObject storage;
public int position;
public static final int YES = 0;
public static final int NO = 1;
public static final int POSITIVE_ONLY = 2;
public static final int NEGATIVE_ONLY = 3;
/*
{
buttonText: "Meal",
carbs: 36,
validFrom: 8 * 60 * 60, // seconds from midnight
validTo: 9 * 60 * 60, // seconds from midnight
useBG: 0,
useCOB: 0,
useBolusIOB: 0,
useBasalIOB: 0,
useTrend: 0,
useSuperBolus: 0,
useTemptarget: 0
}
*/
2019-06-16 09:00:47 +02:00
QuickWizardEntry() {
2017-12-26 00:27:34 +01:00
String emptyData = "{\"buttonText\":\"\",\"carbs\":0,\"validFrom\":0,\"validTo\":86340}";
try {
storage = new JSONObject(emptyData);
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
position = -1;
}
2019-06-16 09:00:47 +02:00
QuickWizardEntry(JSONObject entry, int position) {
2017-12-26 00:27:34 +01:00
storage = entry;
this.position = position;
}
2019-06-16 09:00:47 +02:00
Boolean isActive() {
2017-12-26 00:27:34 +01:00
return Profile.secondsFromMidnight() >= validFrom() && Profile.secondsFromMidnight() <= validTo();
}
2019-06-16 09:00:47 +02:00
public BolusWizard doCalc(Profile profile, String profileName, BgReading lastBG, boolean _synchronized) {
final TempTarget tempTarget = TreatmentsPlugin.getPlugin().getTempTargetFromHistory();
2017-12-26 00:27:34 +01:00
//BG
double bg = 0;
if (lastBG != null && useBG() == YES) {
2019-11-12 00:01:58 +01:00
bg = lastBG.valueToUnits(ProfileFunctions.getSystemUnits());
2017-12-26 00:27:34 +01:00
}
// COB
double cob = 0d;
if (useCOB() == YES) {
CobInfo cobInfo = IobCobCalculatorPlugin.getPlugin().getCobInfo(_synchronized, "QuickWizard COB");
if (cobInfo.displayCob != null)
cob = cobInfo.displayCob;
2017-12-26 00:27:34 +01:00
}
// Bolus IOB
boolean bolusIOB = false;
if (useBolusIOB() == YES) {
bolusIOB = true;
}
// Basal IOB
TreatmentsInterface treatments = TreatmentsPlugin.getPlugin();
2017-12-26 00:27:34 +01:00
treatments.updateTotalIOBTempBasals();
IobTotal basalIob = treatments.getLastCalculationTempBasals().round();
boolean basalIOB = false;
if (useBasalIOB() == YES) {
basalIOB = true;
} else if (useBasalIOB() == POSITIVE_ONLY && basalIob.iob > 0) {
basalIOB = true;
} else if (useBasalIOB() == NEGATIVE_ONLY && basalIob.iob < 0) {
basalIOB = true;
}
// SuperBolus
boolean superBolus = false;
if (useSuperBolus() == YES && SP.getBoolean(R.string.key_usesuperbolus, false)) {
superBolus = true;
}
final LoopPlugin loopPlugin = LoopPlugin.getPlugin();
if (loopPlugin.isEnabled(loopPlugin.getType()) && loopPlugin.isSuperBolus())
2017-12-26 00:27:34 +01:00
superBolus = false;
// Trend
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
boolean trend = false;
if (useTrend() == YES) {
trend = true;
} else if (useTrend() == POSITIVE_ONLY && glucoseStatus != null && glucoseStatus.short_avgdelta > 0) {
trend = true;
} else if (useTrend() == NEGATIVE_ONLY && glucoseStatus != null && glucoseStatus.short_avgdelta < 0) {
trend = true;
}
2019-06-21 13:23:37 +02:00
return new BolusWizard(profile, profileName, tempTarget, carbs(), cob, bg, 0d, 100, true, useCOB() == YES, bolusIOB, basalIOB, superBolus, useTempTarget() == YES, trend, "QuickWizard");
2017-12-26 00:27:34 +01:00
}
public String buttonText() {
try {
return storage.getString("buttonText");
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return "";
}
public Integer carbs() {
try {
return storage.getInt("carbs");
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return 0;
}
public Date validFromDate() {
return DateUtil.toDate(validFrom());
}
public Date validToDate() {
return DateUtil.toDate(validTo());
}
public Integer validFrom() {
try {
return storage.getInt("validFrom");
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return 0;
}
public Integer validTo() {
try {
return storage.getInt("validTo");
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return 0;
}
public int useBG() {
try {
return storage.getInt("useBG");
} catch (JSONException e) {
2018-01-03 22:28:48 +01:00
//log.error("Unhandled exception", e);
2017-12-26 00:27:34 +01:00
}
return YES;
}
public int useCOB() {
try {
return storage.getInt("useCOB");
} catch (JSONException e) {
2018-01-03 22:28:48 +01:00
//log.error("Unhandled exception", e);
2017-12-26 00:27:34 +01:00
}
return NO;
}
public int useBolusIOB() {
try {
return storage.getInt("useBolusIOB");
} catch (JSONException e) {
2018-01-03 22:28:48 +01:00
//log.error("Unhandled exception", e);
2017-12-26 00:27:34 +01:00
}
return YES;
}
public int useBasalIOB() {
try {
return storage.getInt("useBasalIOB");
} catch (JSONException e) {
2018-01-03 22:28:48 +01:00
//log.error("Unhandled exception", e);
2017-12-26 00:27:34 +01:00
}
return YES;
}
public int useTrend() {
try {
return storage.getInt("useTrend");
} catch (JSONException e) {
2018-01-03 22:28:48 +01:00
//log.error("Unhandled exception", e);
2017-12-26 00:27:34 +01:00
}
return NO;
}
public int useSuperBolus() {
try {
return storage.getInt("useSuperBolus");
} catch (JSONException e) {
2018-01-03 22:28:48 +01:00
//log.error("Unhandled exception", e);
2017-12-26 00:27:34 +01:00
}
return NO;
}
public int useTempTarget() {
try {
return storage.getInt("useTempTarget");
} catch (JSONException e) {
2018-01-03 22:28:48 +01:00
//log.error("Unhandled exception", e);
2017-12-26 00:27:34 +01:00
}
return NO;
}
}