apply constraints on new treatment dialog

This commit is contained in:
Milos Kozak 2016-06-20 21:43:29 +02:00
parent 7cb0e0e588
commit 4645c2bc4d
11 changed files with 120 additions and 36 deletions

View file

@ -22,5 +22,6 @@ public class Config {
public static final boolean logConstraintsChanges = true; public static final boolean logConstraintsChanges = true;
// Developing mode only - never turn on // Developing mode only - never turn on
// TODO: remove fakeGlucoseData
public static final boolean fakeGlucoseData = true; public static final boolean fakeGlucoseData = true;
} }

View file

@ -13,8 +13,9 @@ public class Result extends Object implements Parcelable{
public Double absolute = -1d; // absolute rate [U/h] , isPercent = false public Double absolute = -1d; // absolute rate [U/h] , isPercent = false
public Integer percent = -1; // percent of current basal [%] (100% = current basal), isPercent = true public Integer percent = -1; // percent of current basal [%] (100% = current basal), isPercent = true
public boolean isPercent = false; // if true percent is used, otherwise absolute public boolean isPercent = false; // if true percent is used, otherwise absolute
// Result of bolus delivery // Result of treatment delivery
public Double bolusDelivered = 0d; // real value of delivered insulin public Double bolusDelivered = 0d; // real value of delivered insulin
public Integer carbsDelivered = 0; // real value of delivered carbs
public String log() { public String log() {
return "Success: " + success + " Enacted: " + enacted + " Comment: " + comment + " Duration: " + duration + " Absolute: " + absolute + " Percent: " + percent + " IsPercent: " + isPercent; return "Success: " + success + " Enacted: " + enacted + " Comment: " + comment + " Duration: " + duration + " Absolute: " + absolute + " Percent: " + percent + " IsPercent: " + isPercent;

View file

@ -12,4 +12,7 @@ public interface ConstraintsInterface {
APSResult applyBasalConstraints(APSResult request); APSResult applyBasalConstraints(APSResult request);
Double applyBasalConstraints(Double absoluteRate); Double applyBasalConstraints(Double absoluteRate);
Integer applyBasalConstraints(Integer percentRate); Integer applyBasalConstraints(Integer percentRate);
Double applyBolusConstraints(Double insulin);
Integer applyCarbsConstraints(Integer carbs);
} }

View file

@ -26,7 +26,7 @@ public interface PumpInterface {
double getTempBasalRemainingMinutes(); double getTempBasalRemainingMinutes();
TempBasal getTempBasal(); TempBasal getTempBasal();
Result deliverTreatment(Double insulin, Double carbs); Result deliverTreatment(Double insulin, Integer carbs);
Result setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes); Result setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes);
Result setTempBasalPercent(Integer percent, Integer durationInMinutes); Result setTempBasalPercent(Integer percent, Integer durationInMinutes);
Result setExtendedBolus(Double insulin, Integer durationInMinutes); Result setExtendedBolus(Double insulin, Integer durationInMinutes);

View file

@ -249,23 +249,16 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
} }
@Override @Override
public Result deliverTreatment(Double insulin, Double carbs) { public Result deliverTreatment(Double insulin, Integer carbs) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); insulin = applyBolusConstraints(insulin);
Double maxbolus = Double.parseDouble(SP.getString("treatmentssafety_maxbolus", "3")); carbs = applyCarbsConstraints(carbs);
Double maxcarbs = Double.parseDouble(SP.getString("treatmentssafety_maxcarbs", "48"));
if (insulin > maxbolus || carbs > maxcarbs) {
Result failResult = new Result();
failResult.success = false;
failResult.comment = MainApp.instance().getString(R.string.constraints_violation);
return failResult;
}
Result result = activePump.deliverTreatment(insulin, carbs); Result result = activePump.deliverTreatment(insulin, carbs);
if (result.success) { if (result.success) {
Treatment t = new Treatment(); Treatment t = new Treatment();
t.insulin = result.bolusDelivered; t.insulin = result.bolusDelivered;
t.carbs = carbs; t.carbs = (double) result.carbsDelivered;
t.created_at = new Date(); t.created_at = new Date();
try { try {
MainApp.instance().getDbHelper().getDaoTreatments().create(t); MainApp.instance().getDbHelper().getDaoTreatments().create(t);
@ -713,6 +706,30 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
return rateAfterConstrain; return rateAfterConstrain;
} }
@Override
public Double applyBolusConstraints(Double insulin) {
Double insulinAfterConstrain = insulin;
ArrayList<PluginBase> constraintsPlugins = MainActivity.getSpecificPluginsList(PluginBase.CONSTRAINTS);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled()) continue;
insulinAfterConstrain = constrain.applyBolusConstraints(insulinAfterConstrain);
}
return insulinAfterConstrain;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
Integer carbsAfterConstrain = carbs;
ArrayList<PluginBase> constraintsPlugins = MainActivity.getSpecificPluginsList(PluginBase.CONSTRAINTS);
for (PluginBase p : constraintsPlugins) {
ConstraintsInterface constrain = (ConstraintsInterface) p;
if (!p.isEnabled()) continue;
carbsAfterConstrain = constrain.applyCarbsConstraints(carbsAfterConstrain);
}
return carbsAfterConstrain;
}
public static void uploadTempBasalStartAbsolute(Double absolute, double durationInMinutes) { public static void uploadTempBasalStartAbsolute(Double absolute, double durationInMinutes) {
try { try {
Context context = MainApp.instance().getApplicationContext(); Context context = MainApp.instance().getApplicationContext();

View file

@ -341,4 +341,14 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
return percentRate; return percentRate;
} }
@Override
public Double applyBolusConstraints(Double insulin) {
return insulin;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
return carbs;
}
} }

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.Overview.Dialogs; package info.nightscout.androidaps.plugins.Overview.Dialogs;
import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -7,6 +8,7 @@ import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.*; import android.view.*;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.animation.Interpolator;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
@ -14,6 +16,7 @@ import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Result; import info.nightscout.androidaps.data.Result;
import info.nightscout.androidaps.interfaces.PumpInterface;
public class NewTreatmentDialog extends DialogFragment implements OnClickListener { public class NewTreatmentDialog extends DialogFragment implements OnClickListener {
@ -41,30 +44,47 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.treatments_newtreatment_deliverbutton: case R.id.treatments_newtreatment_deliverbutton:
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
Double maxbolus = Double.parseDouble(SP.getString("treatmentssafety_maxbolus", "3"));
Double maxcarbs = Double.parseDouble(SP.getString("treatmentssafety_maxcarbs", "48"));
try { try {
String insulinText = this.insulin.getText().toString().replace(",", "."); String insulinText = this.insulin.getText().toString().replace(",", ".");
String carbsText = this.carbs.getText().toString().replace(",", "."); String carbsText = this.carbs.getText().toString().replace(",", ".");
Double insulin = Double.parseDouble(!insulinText.equals("") ? insulinText : "0"); Double insulin = Double.parseDouble(!insulinText.equals("") ? insulinText : "0");
Double carbs = Double.parseDouble(!carbsText.equals("") ? carbsText : "0"); Integer carbs = Integer.parseInt(!carbsText.equals("") ? carbsText : "0");
if (insulin > maxbolus) {
this.insulin.setText(""); String confirmMessage = getString(R.string.entertreatmentquestion);
} else if (carbs > maxcarbs) {
this.carbs.setText(""); Double insulinAfterConstraints = MainActivity.getConfigBuilder().applyBolusConstraints(insulin);
} else if (insulin > 0d || carbs > 0d) { Integer carbsAfterConstraints = MainActivity.getConfigBuilder().applyCarbsConstraints(carbs);
dismiss();
Result result = MainActivity.getConfigBuilder().getActivePump().deliverTreatment(insulin, carbs); confirmMessage += getString(R.string.bolus) + ": " + insulinAfterConstraints + "U";
if (!result.success) { confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g";
AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); if (insulinAfterConstraints != insulin || carbsAfterConstraints != carbs)
builder.setTitle(this.getContext().getString(R.string.treatmentdeliveryerror)); confirmMessage += "\n" + getString(R.string.constraintapllied);
builder.setMessage(result.comment);
builder.setPositiveButton(this.getContext().getString(R.string.ok), null); final Double finalInsulinAfterConstraints = insulinAfterConstraints;
builder.show(); final Integer finalCarbsAfterConstraints = carbsAfterConstraints;
AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());
builder.setTitle(this.getContext().getString(R.string.confirmation));
builder.setMessage(confirmMessage);
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) {
PumpInterface pump = MainActivity.getConfigBuilder().getActivePump();
Result result = pump.deliverTreatment(finalInsulinAfterConstraints, finalCarbsAfterConstraints);
if (!result.success) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getContext().getString(R.string.treatmentdeliveryerror));
builder.setMessage(result.comment);
builder.setPositiveButton(getContext().getString(R.string.ok), null);
builder.show();
}
}
} }
} });
builder.setNegativeButton(getString(R.string.cancel), null);
builder.show();
dismiss();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -42,7 +42,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
public static final DecimalFormat numberFormat = new DecimalFormat("0.00"); public static final DecimalFormat numberFormat = new DecimalFormat("0.00");
public static final DecimalFormat intFormat = new DecimalFormat("0"); public static final DecimalFormat intFormat = new DecimalFormat("0");
Double calculatedCarbs = 0d; Integer calculatedCarbs = 0;
Double calculatedTotalInsulin = 0d; Double calculatedTotalInsulin = 0d;
final private TextWatcher textWatcher = new TextWatcher() { final private TextWatcher textWatcher = new TextWatcher() {
@ -181,9 +181,9 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
String i_correction = this.correctionInput.getText().toString().replace(",", "."); String i_correction = this.correctionInput.getText().toString().replace(",", ".");
Double c_bg = 0d; Double c_bg = 0d;
try { c_bg = Double.parseDouble(i_bg.equals("") ? "0" : i_bg); } catch (Exception e) {} try { c_bg = Double.parseDouble(i_bg.equals("") ? "0" : i_bg); } catch (Exception e) {}
Double c_carbs = 0d; Integer c_carbs = 0;
try { c_carbs = Double.parseDouble(i_carbs.equals("") ? "0" : i_carbs); } catch (Exception e) {} try { c_carbs = Integer.parseInt(i_carbs.equals("") ? "0" : i_carbs); } catch (Exception e) {}
c_carbs = ((Long)Math.round(c_carbs)).doubleValue(); c_carbs = (Integer) Math.round(c_carbs);
Double c_correction = 0d; Double c_correction = 0d;
try { c_correction = Double.parseDouble(i_correction.equals("") ? "0" : i_correction); } catch (Exception e) {} try { c_correction = Double.parseDouble(i_correction.equals("") ? "0" : i_correction); } catch (Exception e) {}
if(c_correction > maxbolus) { if(c_correction > maxbolus) {

View file

@ -154,4 +154,32 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI
return percentRateAfterConst; return percentRateAfterConst;
} }
@Override
public Double applyBolusConstraints(Double insulin) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
try {
Double maxBolus = Double.parseDouble(SP.getString("treatmentssafety_maxbolus", "3"));
if (insulin < 0) insulin = 0d;
if (insulin > maxBolus) insulin = maxBolus;
} catch (Exception e) {
insulin = 0d;
}
return insulin;
}
@Override
public Integer applyCarbsConstraints(Integer carbs) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
try {
Integer maxCarbs = Integer.parseInt(SP.getString("treatmentssafety_maxcarbs", "48"));
if (carbs < 0) carbs = 0;
if (carbs > maxCarbs) carbs = maxCarbs;
} catch (Exception e) {
carbs = 0;
}
return carbs;
}
} }

View file

@ -220,10 +220,11 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
} }
@Override @Override
public Result deliverTreatment(Double insulin, Double carbs) { public Result deliverTreatment(Double insulin, Integer carbs) {
Result result = new Result(); Result result = new Result();
result.success = true; result.success = true;
result.bolusDelivered = insulin; result.bolusDelivered = insulin;
result.carbsDelivered = carbs;
result.comment = getString(R.string.virtualpump_resultok); result.comment = getString(R.string.virtualpump_resultok);
if (Config.logPumpComm) if (Config.logPumpComm)

View file

@ -137,5 +137,8 @@
<string name="overview_calculator_label">Calculator</string> <string name="overview_calculator_label">Calculator</string>
<string name="constraintapllied">Constraint applied!</string> <string name="constraintapllied">Constraint applied!</string>
<string name="confirmation">Confirmation</string> <string name="confirmation">Confirmation</string>
<string name="entertreatmentquestion">Enter new treatment:</string>
<string name="bolus">Bolus</string>
<string name="carbs">Carbs</string>
</resources> </resources>