Merge pull request #1842 from MilosKozak/bw2

BolusWizard privacy
This commit is contained in:
Milos Kozak 2019-06-21 13:33:01 +02:00 committed by GitHub
commit 423e7e763e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 58 deletions

View file

@ -124,10 +124,7 @@ public class QuickWizardEntry {
trend = true;
}
BolusWizard wizard = new BolusWizard(profile, profileName, tempTarget, carbs(), cob, bg, 0d, 100, true, useCOB() == YES, bolusIOB, basalIOB, superBolus, useTempTarget() == YES, trend);
wizard.doCalc();
wizard.setNotes("QuickWizard");
return wizard;
return new BolusWizard(profile, profileName, tempTarget, carbs(), cob, bg, 0d, 100, true, useCOB() == YES, bolusIOB, basalIOB, superBolus, useTempTarget() == YES, trend, "QuickWizard");
}
public String buttonText() {

View file

@ -391,10 +391,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
c_cob = cobInfo.displayCob;
}
wizard = new BolusWizard(specificProfile, profileName, tempTarget, carbsAfterConstraint, c_cob, c_bg, corrAfterConstraint, 100d, bgCheckbox.isChecked(), cobCheckbox.isChecked(), bolusIobCheckbox.isChecked(), basalIobCheckbox.isChecked(), superbolusCheckbox.isChecked(), ttCheckbox.isChecked(), bgtrendCheckbox.isChecked());
wizard.doCalc();
wizard.setNotes(notesEdit.getText().toString());
wizard.setCarbTime(SafeParse.stringToInt(editCarbTime.getText()));
wizard = new BolusWizard(specificProfile, profileName, tempTarget, carbsAfterConstraint, c_cob, c_bg, corrAfterConstraint, 100d, bgCheckbox.isChecked(), cobCheckbox.isChecked(), bolusIobCheckbox.isChecked(), basalIobCheckbox.isChecked(), superbolusCheckbox.isChecked(), ttCheckbox.isChecked(), bgtrendCheckbox.isChecked(), notesEdit.getText().toString(), SafeParse.stringToInt(editCarbTime.getText()));
bg.setText(c_bg + " ISF: " + DecimalFormatter.to1Decimal(wizard.getSens()));
bgInsulin.setText(StringUtils.formatInsulin(wizard.getInsulinFromBG()));
@ -402,8 +399,8 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
carbs.setText(DecimalFormatter.to0Decimal(c_carbs) + "g IC: " + DecimalFormatter.to1Decimal(wizard.getIc()));
carbsInsulin.setText(StringUtils.formatInsulin(wizard.getInsulinFromCarbs()));
bolusIobInsulin.setText(StringUtils.formatInsulin(wizard.getInsulingFromBolusIOB()));
basalIobInsulin.setText(StringUtils.formatInsulin(wizard.getInsulingFromBasalsIOB()));
bolusIobInsulin.setText(StringUtils.formatInsulin(wizard.getInsulinFromBolusIOB()));
basalIobInsulin.setText(StringUtils.formatInsulin(wizard.getInsulinFromBasalsIOB()));
correctionInsulin.setText(StringUtils.formatInsulin(wizard.getInsulinFromCorrection()));

View file

@ -230,20 +230,13 @@ public class ActionStringHandler {
BolusWizard bolusWizard = new BolusWizard(profile, profileName, TreatmentsPlugin.getPlugin().getTempTargetFromHistory(),
carbsAfterConstraints, cobInfo.displayCob, bgReading.valueToUnits(profile.getUnits()),
0d, percentage, useBG, useCOB, useBolusIOB, useBasalIOB, false, useTT, useTrend);
bolusWizard.doCalc();
Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(bolusWizard.getCalculatedTotalInsulin())).value();
if (Math.abs(insulinAfterConstraints - bolusWizard.getCalculatedTotalInsulin()) >= 0.01) {
if (Math.abs(bolusWizard.getInsulinAfterConstraints() - bolusWizard.getCalculatedTotalInsulin()) >= 0.01) {
sendError("Insulin constraint violation!" +
"\nCannot deliver " + format.format(bolusWizard.getCalculatedTotalInsulin()) + "!");
return;
}
if (bolusWizard.getCalculatedTotalInsulin() < 0) {
bolusWizard.setCalculatedTotalInsulin(0d);
}
if (bolusWizard.getCalculatedTotalInsulin() <= 0 && bolusWizard.getCarbs() <= 0) {
rAction = "info";
rTitle = "INFO";
@ -259,9 +252,9 @@ public class ActionStringHandler {
rMessage += "\nFrom" + formatInt.format(cobInfo.displayCob) + "g COB : " + format.format(bolusWizard.getInsulinFromCOB()) + "U";
if (useBG) rMessage += "\nFrom BG: " + format.format(bolusWizard.getInsulinFromBG()) + "U";
if (useBolusIOB)
rMessage += "\nBolus IOB: " + format.format(bolusWizard.getInsulingFromBolusIOB()) + "U";
rMessage += "\nBolus IOB: " + format.format(bolusWizard.getInsulinFromBolusIOB()) + "U";
if (useBasalIOB)
rMessage += "\nBasal IOB: " + format.format(bolusWizard.getInsulingFromBasalsIOB()) + "U";
rMessage += "\nBasal IOB: " + format.format(bolusWizard.getInsulinFromBasalsIOB()) + "U";
if (useTrend)
rMessage += "\nFrom 15' trend: " + format.format(bolusWizard.getInsulinFromTrend()) + "U";
if (percentage != 100) {

View file

@ -29,57 +29,94 @@ import org.json.JSONObject
import org.slf4j.LoggerFactory
import java.util.*
class BolusWizard(val profile: Profile,
val profileName: String,
val tempTarget: TempTarget?,
val carbs: Int,
val cob: Double,
val bg: Double,
val correction: Double,
val percentageCorrection: Double = 100.0,
val useBg: Boolean,
val useCob: Boolean,
val includeBolusIOB: Boolean,
val includeBasalIOB: Boolean,
val useSuperBolus: Boolean,
val useTT: Boolean,
val useTrend: Boolean) {
class BolusWizard @JvmOverloads constructor(val profile: Profile,
val profileName: String,
val tempTarget: TempTarget?,
val carbs: Int,
val cob: Double,
val bg: Double,
val correction: Double,
private val percentageCorrection: Double = 100.0,
private val useBg: Boolean,
private val useCob: Boolean,
private val includeBolusIOB: Boolean,
private val includeBasalIOB: Boolean,
private val useSuperBolus: Boolean,
private val useTT: Boolean,
private val useTrend: Boolean,
val notes: String = "",
private val carbTime: Int = 0
) {
private val log = LoggerFactory.getLogger(L.CORE)
// Intermediate
var sens = 0.0
private set
var ic = 0.0
private set
var glucoseStatus: GlucoseStatus? = null
private set
var targetBGLow = 0.0
private set
var targetBGHigh = 0.0
private set
var bgDiff = 0.0
private set
var insulinFromBG = 0.0
private set
var insulinFromCarbs = 0.0
var insulingFromBolusIOB = 0.0
var insulingFromBasalsIOB = 0.0
private set
var insulinFromBolusIOB = 0.0
private set
var insulinFromBasalsIOB = 0.0
private set
var insulinFromCorrection = 0.0
private set
var insulinFromSuperBolus = 0.0
private set
var insulinFromCOB = 0.0
private set
var insulinFromTrend = 0.0
private set
var trend = 0.0
var carbTime = 0
private set
var notes = ""
var accepted = false
private set
// Result
var calculatedTotalInsulin: Double = 0.0
private set
var totalBeforePercentageAdjustment: Double = 0.0
private set
var carbsEquivalent: Double = 0.0
private set
var insulinAfterConstraints: Double = 0.0
private set
fun doCalc(): Double {
init {
doCalc()
}
private fun doCalc() {
// Insulin from BG
sens = profile.isf
@ -102,11 +139,14 @@ class BolusWizard(val profile: Profile,
// Insulin from 15 min trend
glucoseStatus = GlucoseStatus.getGlucoseStatusData()
if (glucoseStatus != null && useTrend) {
trend = glucoseStatus!!.short_avgdelta
insulinFromTrend = Profile.fromMgdlToUnits(trend, profile.units) * 3 / sens
glucoseStatus?.let {
if (useTrend) {
trend = it.short_avgdelta
insulinFromTrend = Profile.fromMgdlToUnits(trend, profile.units) * 3 / sens
}
}
// Insuling from carbs
ic = profile.ic
insulinFromCarbs = carbs / ic
@ -120,8 +160,8 @@ class BolusWizard(val profile: Profile,
treatments.updateTotalIOBTempBasals()
val basalIob = treatments.lastCalculationTempBasals.round()
insulingFromBolusIOB = if (includeBolusIOB) -bolusIob.iob else 0.0
insulingFromBasalsIOB = if (includeBasalIOB) -basalIob.basaliob else 0.0
insulinFromBolusIOB = if (includeBolusIOB) -bolusIob.iob else 0.0
insulinFromBasalsIOB = if (includeBasalIOB) -basalIob.basaliob else 0.0
// Insulin from correction
insulinFromCorrection = correction
@ -135,7 +175,7 @@ class BolusWizard(val profile: Profile,
}
// Total
calculatedTotalInsulin = insulinFromBG + insulinFromTrend + insulinFromCarbs + insulingFromBolusIOB + insulingFromBasalsIOB + insulinFromCorrection + insulinFromSuperBolus + insulinFromCOB
calculatedTotalInsulin = insulinFromBG + insulinFromTrend + insulinFromCarbs + insulinFromBolusIOB + insulinFromBasalsIOB + insulinFromCorrection + insulinFromSuperBolus + insulinFromCOB
// Percentage adjustment
totalBeforePercentageAdjustment = calculatedTotalInsulin
@ -154,8 +194,6 @@ class BolusWizard(val profile: Profile,
insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(Constraint(calculatedTotalInsulin)).value()
log.debug(this.toString())
return calculatedTotalInsulin
}
fun nsJSON(): JSONObject {
@ -168,9 +206,9 @@ class BolusWizard(val profile: Profile,
boluscalcJSON.put("targetBGHigh", targetBGHigh)
boluscalcJSON.put("isf", sens)
boluscalcJSON.put("ic", ic)
boluscalcJSON.put("iob", -(insulingFromBolusIOB + insulingFromBasalsIOB))
boluscalcJSON.put("bolusiob", insulingFromBolusIOB)
boluscalcJSON.put("basaliob", insulingFromBasalsIOB)
boluscalcJSON.put("iob", -(insulinFromBolusIOB + insulinFromBasalsIOB))
boluscalcJSON.put("bolusiob", insulinFromBolusIOB)
boluscalcJSON.put("basaliob", insulinFromBasalsIOB)
boluscalcJSON.put("bolusiobused", includeBolusIOB)
boluscalcJSON.put("basaliobused", includeBasalIOB)
boluscalcJSON.put("bg", bg)
@ -198,7 +236,7 @@ class BolusWizard(val profile: Profile,
return boluscalcJSON
}
fun confirmMessageAfterConstraints(pump: PumpInterface): String {
private fun confirmMessageAfterConstraints(pump: PumpInterface): String {
var confirmMessage = MainApp.gs(R.string.entertreatmentquestion)
if (insulinAfterConstraints > 0)

View file

@ -38,9 +38,9 @@ public class BolusWizardTest {
Profile profile = setupProfile(4d, 8d, 20d, 12d);
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 4.2, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForBg42 = bw.doCalc();
Double bolusForBg42 = bw.getCalculatedTotalInsulin();
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForBg54 = bw.doCalc();
Double bolusForBg54 = bw.getCalculatedTotalInsulin();
Assert.assertEquals(bolusForBg42, bolusForBg54);
}
@ -49,9 +49,9 @@ public class BolusWizardTest {
Profile profile = setupProfile(4d, 8d, 20d, 12d);
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 9.8, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForHighBg = bw.doCalc();
Double bolusForHighBg = bw.getCalculatedTotalInsulin();
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForBgInRange = bw.doCalc();
Double bolusForBgInRange = bw.getCalculatedTotalInsulin();
Assert.assertTrue(bolusForHighBg > bolusForBgInRange);
}
@ -60,9 +60,9 @@ public class BolusWizardTest {
Profile profile = setupProfile(4d, 8d, 20d, 12d);
BolusWizard bw = new BolusWizard(profile, "", null, 20, 0.0, 3.6, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForLowBg = bw.doCalc();
Double bolusForLowBg = bw.getCalculatedTotalInsulin();
bw = new BolusWizard(profile, "", null, 20, 0.0, 5.4, 0d, 100d, true, true, true, true, false, false, false);
Double bolusForBgInRange = bw.doCalc();
Double bolusForBgInRange = bw.getCalculatedTotalInsulin();
Assert.assertTrue(bolusForLowBg < bolusForBgInRange);
}