fix comparing bolus size granularity

This commit is contained in:
Milos Kozak 2019-06-21 11:41:50 +02:00
parent af0d4f23b1
commit 7b15cfb395
6 changed files with 74 additions and 121 deletions

View file

@ -13,10 +13,10 @@ import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin;
import info.nightscout.androidaps.plugins.aps.openAPSMA.OpenAPSMAPlugin; import info.nightscout.androidaps.plugins.aps.openAPSMA.OpenAPSMAPlugin;
import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin;
@ -196,7 +196,7 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
if (pump != null) { if (pump != null) {
double rounded = Round.roundTo(insulin.value(), pump.getPumpDescription().pumpType.determineCorrectBolusSize(insulin.value())); double rounded = pump.getPumpDescription().pumpType.determineCorrectBolusSize(insulin.value());
insulin.setIfDifferent(rounded, MainApp.gs(R.string.pumplimit), this); insulin.setIfDifferent(rounded, MainApp.gs(R.string.pumplimit), this);
} }
return insulin; return insulin;
@ -213,7 +213,7 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface {
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
if (pump != null) { if (pump != null) {
double rounded = Round.roundTo(insulin.value(), pump.getPumpDescription().pumpType.determineCorrectExtendedBolusSize(insulin.value())); double rounded = pump.getPumpDescription().pumpType.determineCorrectExtendedBolusSize(insulin.value());
insulin.setIfDifferent(rounded, MainApp.gs(R.string.pumplimit), this); insulin.setIfDifferent(rounded, MainApp.gs(R.string.pumplimit), this);
} }
return insulin; return insulin;

View file

@ -787,7 +787,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (wizard.getCalculatedTotalInsulin() > 0d && quickWizardEntry.carbs() > 0d) { if (wizard.getCalculatedTotalInsulin() > 0d && quickWizardEntry.carbs() > 0d) {
Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(quickWizardEntry.carbs())).value(); Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(quickWizardEntry.carbs())).value();
if (Math.abs(wizard.getInsulinAfterConstraints() - wizard.getCalculatedTotalInsulin()) >= pump.getPumpDescription().pumpType.determineCorrectBolusSize(wizard.getInsulinAfterConstraints()) || !carbsAfterConstraints.equals(quickWizardEntry.carbs())) { if (Math.abs(wizard.getInsulinAfterConstraints() - wizard.getCalculatedTotalInsulin()) >= pump.getPumpDescription().pumpType.determineCorrectBolusStepSize(wizard.getInsulinAfterConstraints()) || !carbsAfterConstraints.equals(quickWizardEntry.carbs())) {
OKDialog.show(getContext(), MainApp.gs(R.string.treatmentdeliveryerror), MainApp.gs(R.string.constraints_violation) + "\n" + MainApp.gs(R.string.changeyourinput), null); OKDialog.show(getContext(), MainApp.gs(R.string.treatmentdeliveryerror), MainApp.gs(R.string.constraints_violation) + "\n" + MainApp.gs(R.string.changeyourinput), null);
return; return;
} }

View file

@ -225,7 +225,7 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener
} }
} }
if (Math.abs(insulinAfterConstraints - insulin) > pump.getPumpDescription().pumpType.determineCorrectBolusSize(insulinAfterConstraints)) if (Math.abs(insulinAfterConstraints - insulin) > pump.getPumpDescription().pumpType.determineCorrectBolusStepSize(insulinAfterConstraints))
actions.add("<font color='" + MainApp.gc(R.color.warning) + "'>" + MainApp.gs(R.string.bolusconstraintapplied) + "</font>"); actions.add("<font color='" + MainApp.gc(R.color.warning) + "'>" + MainApp.gs(R.string.bolusconstraintapplied) + "</font>");
int eatingSoonTTDuration = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration); int eatingSoonTTDuration = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration);

View file

@ -141,7 +141,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene
if (recordOnlyCheckbox.isChecked()) { if (recordOnlyCheckbox.isChecked()) {
confirmMessage += "<br/><font color='" + MainApp.gc(R.color.warning) + "'>" + MainApp.gs(R.string.bolusrecordedonly) + "</font>"; confirmMessage += "<br/><font color='" + MainApp.gc(R.color.warning) + "'>" + MainApp.gs(R.string.bolusrecordedonly) + "</font>";
} }
if (Math.abs(insulinAfterConstraints - insulin) > pump.getPumpDescription().pumpType.determineCorrectBolusSize(insulinAfterConstraints) || !Objects.equals(carbsAfterConstraints, carbs)) if (Math.abs(insulinAfterConstraints - insulin) > pump.getPumpDescription().pumpType.determineCorrectBolusStepSize(insulinAfterConstraints) || !Objects.equals(carbsAfterConstraints, carbs))
confirmMessage += "<br/><font color='" + MainApp.gc(R.color.warning) + "'>" + MainApp.gs(R.string.bolusconstraintapplied) + "</font>"; confirmMessage += "<br/><font color='" + MainApp.gc(R.color.warning) + "'>" + MainApp.gs(R.string.bolusconstraintapplied) + "</font>";
} }
if (carbsAfterConstraints > 0) if (carbsAfterConstraints > 0)

View file

@ -7,12 +7,12 @@ import java.util.Map;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.pump.common.data.DoseSettings; import info.nightscout.androidaps.plugins.pump.common.data.DoseSettings;
import info.nightscout.androidaps.utils.Round;
/** /**
* Created by andy on 02/05/2018. * Created by andy on 02/05/2018.
* * <p>
* Most of this defintions is intended for VirtualPump only, but they can be used by other plugins. * Most of this defintions is intended for VirtualPump only, but they can be used by other plugins.
*/ */
@ -148,8 +148,7 @@ public enum PumpType {
private PumpType parent; private PumpType parent;
private static Map<String, PumpType> mapByDescription; private static Map<String, PumpType> mapByDescription;
static static {
{
mapByDescription = new HashMap<>(); mapByDescription = new HashMap<>();
for (PumpType pumpType : values()) { for (PumpType pumpType : values()) {
@ -158,14 +157,12 @@ public enum PumpType {
} }
PumpType(String description, PumpType parent) PumpType(String description, PumpType parent) {
{
this.description = description; this.description = description;
this.parent = parent; this.parent = parent;
} }
PumpType(String description, PumpType parent, PumpCapability pumpCapability) PumpType(String description, PumpType parent, PumpCapability pumpCapability) {
{
this.description = description; this.description = description;
this.parent = parent; this.parent = parent;
this.pumpCapability = pumpCapability; this.pumpCapability = pumpCapability;
@ -174,16 +171,14 @@ public enum PumpType {
PumpType(String description, double bolusSize, DoseStepSize specialBolusSize, // PumpType(String description, double bolusSize, DoseStepSize specialBolusSize, //
DoseSettings extendedBolusSettings, // DoseSettings extendedBolusSettings, //
PumpTempBasalType pumpTempBasalType, DoseSettings tbrSettings, PumpCapability specialBasalDurations, // PumpTempBasalType pumpTempBasalType, DoseSettings tbrSettings, PumpCapability specialBasalDurations, //
double baseBasalMinValue, double baseBasalStep, DoseStepSize baseBasalSpecialSteps, PumpCapability pumpCapability) double baseBasalMinValue, double baseBasalStep, DoseStepSize baseBasalSpecialSteps, PumpCapability pumpCapability) {
{
this(description, bolusSize, specialBolusSize, extendedBolusSettings, pumpTempBasalType, tbrSettings, specialBasalDurations, baseBasalMinValue, null, baseBasalStep, baseBasalSpecialSteps, pumpCapability); this(description, bolusSize, specialBolusSize, extendedBolusSettings, pumpTempBasalType, tbrSettings, specialBasalDurations, baseBasalMinValue, null, baseBasalStep, baseBasalSpecialSteps, pumpCapability);
} }
PumpType(String description, double bolusSize, DoseStepSize specialBolusSize, // PumpType(String description, double bolusSize, DoseStepSize specialBolusSize, //
DoseSettings extendedBolusSettings, // DoseSettings extendedBolusSettings, //
PumpTempBasalType pumpTempBasalType, DoseSettings tbrSettings, PumpCapability specialBasalDurations, // PumpTempBasalType pumpTempBasalType, DoseSettings tbrSettings, PumpCapability specialBasalDurations, //
double baseBasalMinValue, Double baseBasalMaxValue, double baseBasalStep, DoseStepSize baseBasalSpecialSteps, PumpCapability pumpCapability) double baseBasalMinValue, Double baseBasalMaxValue, double baseBasalStep, DoseStepSize baseBasalSpecialSteps, PumpCapability pumpCapability) {
{
this.description = description; this.description = description;
this.bolusSize = bolusSize; this.bolusSize = bolusSize;
this.specialBolusSize = specialBolusSize; this.specialBolusSize = specialBolusSize;
@ -261,20 +256,15 @@ public enum PumpType {
} }
private boolean isParentSet() private boolean isParentSet() {
{
return this.parent != null; return this.parent != null;
} }
public static PumpType getByDescription(String desc) public static PumpType getByDescription(String desc) {
{ if (mapByDescription.containsKey(desc)) {
if (mapByDescription.containsKey(desc))
{
return mapByDescription.get(desc); return mapByDescription.get(desc);
} } else {
else
{
return PumpType.GenericAAPS; return PumpType.GenericAAPS;
} }
} }
@ -298,16 +288,14 @@ public enum PumpType {
} }
private String getBaseBasalRange() private String getBaseBasalRange() {
{
Double maxValue = getBaseBasalMaxValue(); Double maxValue = getBaseBasalMaxValue();
return maxValue == null ? "" + getBaseBasalMinValue() : getBaseBasalMinValue() + "-" + maxValue; return maxValue == null ? "" + getBaseBasalMinValue() : getBaseBasalMinValue() + "-" + maxValue;
} }
private String getStep(String step, DoseStepSize stepSize) private String getStep(String step, DoseStepSize stepSize) {
{
if (stepSize != null) if (stepSize != null)
return step + " [" + stepSize.getDescription() + "] *"; return step + " [" + stepSize.getDescription() + "] *";
else else
@ -322,12 +310,9 @@ public enum PumpType {
public PumpCapability getSpecialBasalDurations() { public PumpCapability getSpecialBasalDurations() {
if (isParentSet()) if (isParentSet()) {
{
return parent.getSpecialBasalDurations(); return parent.getSpecialBasalDurations();
} } else {
else
{
return specialBasalDurations == null ? // return specialBasalDurations == null ? //
PumpCapability.BasalRate_Duration15and30minNotAllowed : specialBasalDurations; PumpCapability.BasalRate_Duration15and30minNotAllowed : specialBasalDurations;
} }
@ -338,20 +323,24 @@ public enum PumpType {
return bolusAmount; return bolusAmount;
} }
double bolusStepSize; double bolusStepSize = getBolusSize();
if (getSpecialBolusSize() == null) { if (getSpecialBolusSize() != null) {
bolusStepSize = getBolusSize();
} else {
DoseStepSize specialBolusSize = getSpecialBolusSize(); DoseStepSize specialBolusSize = getSpecialBolusSize();
bolusStepSize = specialBolusSize.getStepSizeForAmount(bolusAmount);
bolusStepSize = specialBolusSize.getStepSizeForAmount((double)bolusAmount);
} }
return Math.round(bolusAmount / bolusStepSize) * bolusStepSize; return Round.roundTo(bolusAmount, bolusStepSize);
} }
public double determineCorrectBolusStepSize(double bolusAmount) {
DoseStepSize specialBolusSize = getSpecialBolusSize();
if (specialBolusSize != null)
return specialBolusSize.getStepSizeForAmount(bolusAmount);
return getBolusSize();
}
public double determineCorrectExtendedBolusSize(double bolusAmount) { public double determineCorrectExtendedBolusSize(double bolusAmount) {
if (bolusAmount == 0.0d) { if (bolusAmount == 0.0d) {
return bolusAmount; return bolusAmount;
@ -371,7 +360,7 @@ public enum PumpType {
bolusAmount = extendedBolusSettings.getMaxDose(); bolusAmount = extendedBolusSettings.getMaxDose();
} }
return Math.round(bolusAmount / bolusStepSize) * bolusStepSize; return Round.roundTo(bolusAmount, bolusStepSize);
} }
@ -393,7 +382,7 @@ public enum PumpType {
if (basalAmount > getTbrSettings().getMaxDose()) if (basalAmount > getTbrSettings().getMaxDose())
basalAmount = getTbrSettings().getMaxDose().doubleValue(); basalAmount = getTbrSettings().getMaxDose().doubleValue();
return Math.round(basalAmount / basalStepSize) * basalStepSize; return Round.roundTo(basalAmount, basalStepSize);
} }
} }

View file

@ -157,43 +157,7 @@ class BolusWizard(val profile: Profile,
return calculatedTotalInsulin return calculatedTotalInsulin
} }
/*
fun log(): String {
val sb = StringBuilder()
sb.append("TempTarget=").append(tempTarget?.toString() ?: "null").append("; ")
sb.append("Carbs=").append(carbs).append("; ")
sb.append("Bg=").append(bg).append("; ")
sb.append("Cob=").append(cob).append("; ")
sb.append("Correction=").append(correction).append("; ")
sb.append("PercentageCorrection=").append(percentageCorrection).append("; ")
sb.append("IncludeBolusIOB=").append(includeBolusIOB).append("; ")
sb.append("IncludeBasalIOB=").append(includeBasalIOB).append("; ")
sb.append("Superbolus=").append(useSuperBolus).append("; ")
sb.append("Trend=").append(useTrend).append("; ")
sb.append("Profile=").append(profile.data?.toString()).append("; ")
sb.append("\n")
sb.append("targetBGLow=").append(targetBGLow).append("; ")
sb.append("targetBGHigh=").append(targetBGHigh).append("; ")
sb.append("bgDiff=").append(bgDiff).append("; ")
sb.append("insulinFromBG=").append(insulinFromBG).append("; ")
sb.append("insulinFromCarbs=").append(insulinFromCarbs).append("; ")
sb.append("insulingFromBolusIOB=").append(insulingFromBolusIOB).append("; ")
sb.append("insulingFromBasalsIOB=").append(insulingFromBasalsIOB).append("; ")
sb.append("insulinFromCorrection=").append(insulinFromCorrection).append("; ")
sb.append("insulinFromSuperBolus=").append(insulinFromSuperBolus).append("; ")
sb.append("insulinFromCOB=").append(insulinFromCOB).append("; ")
sb.append("insulinFromTrend=").append(insulinFromTrend).append("; ")
sb.append("\n")
sb.append("calculatedTotalInsulin=").append(calculatedTotalInsulin).append("; ")
sb.append("totalBeforePercentageAdjustment=").append(totalBeforePercentageAdjustment).append("; ")
sb.append("carbsEquivalent=").append(carbsEquivalent).append("; ")
return sb.toString()
}
*/
fun nsJSON(): JSONObject { fun nsJSON(): JSONObject {
val boluscalcJSON = JSONObject() val boluscalcJSON = JSONObject()
try { try {
@ -242,7 +206,7 @@ class BolusWizard(val profile: Profile,
if (carbs > 0) if (carbs > 0)
confirmMessage += "<br/>" + MainApp.gs(R.string.carbs) + ": " + "<font color='" + MainApp.gc(R.color.carbs) + "'>" + carbs + "g" + "</font>" confirmMessage += "<br/>" + MainApp.gs(R.string.carbs) + ": " + "<font color='" + MainApp.gc(R.color.carbs) + "'>" + carbs + "g" + "</font>"
if (Math.abs(insulinAfterConstraints - calculatedTotalInsulin) > pump.getPumpDescription().pumpType.determineCorrectBolusSize(insulinAfterConstraints)) { if (Math.abs(insulinAfterConstraints - calculatedTotalInsulin) > pump.getPumpDescription().pumpType.determineCorrectBolusStepSize(insulinAfterConstraints)) {
confirmMessage += "<br/><font color='" + MainApp.gc(R.color.warning) + "'>" + MainApp.gs(R.string.bolusconstraintapplied) + "</font>" confirmMessage += "<br/><font color='" + MainApp.gc(R.color.warning) + "'>" + MainApp.gs(R.string.bolusconstraintapplied) + "</font>"
} }