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;
// Developing mode only - never turn on
// TODO: remove fakeGlucoseData
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 Integer percent = -1; // percent of current basal [%] (100% = current basal), isPercent = true
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 Integer carbsDelivered = 0; // real value of delivered carbs
public String log() {
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);
Double applyBasalConstraints(Double absoluteRate);
Integer applyBasalConstraints(Integer percentRate);
Double applyBolusConstraints(Double insulin);
Integer applyCarbsConstraints(Integer carbs);
}

View file

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

View file

@ -249,23 +249,16 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
}
@Override
public Result deliverTreatment(Double insulin, Double carbs) {
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"));
public Result deliverTreatment(Double insulin, Integer carbs) {
insulin = applyBolusConstraints(insulin);
carbs = applyCarbsConstraints(carbs);
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);
if (result.success) {
Treatment t = new Treatment();
t.insulin = result.bolusDelivered;
t.carbs = carbs;
t.carbs = (double) result.carbsDelivered;
t.created_at = new Date();
try {
MainApp.instance().getDbHelper().getDaoTreatments().create(t);
@ -713,6 +706,30 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
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) {
try {
Context context = MainApp.instance().getApplicationContext();

View file

@ -341,4 +341,14 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
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;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
@ -7,6 +8,7 @@ import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.animation.Interpolator;
import android.widget.Button;
import android.widget.TextView;
@ -14,6 +16,7 @@ import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Result;
import info.nightscout.androidaps.interfaces.PumpInterface;
public class NewTreatmentDialog extends DialogFragment implements OnClickListener {
@ -41,30 +44,47 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene
public void onClick(View view) {
switch (view.getId()) {
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 {
String insulinText = this.insulin.getText().toString().replace(",", ".");
String carbsText = this.carbs.getText().toString().replace(",", ".");
Double insulin = Double.parseDouble(!insulinText.equals("") ? insulinText : "0");
Double carbs = Double.parseDouble(!carbsText.equals("") ? carbsText : "0");
if (insulin > maxbolus) {
this.insulin.setText("");
} else if (carbs > maxcarbs) {
this.carbs.setText("");
} else if (insulin > 0d || carbs > 0d) {
dismiss();
Result result = MainActivity.getConfigBuilder().getActivePump().deliverTreatment(insulin, carbs);
if (!result.success) {
AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());
builder.setTitle(this.getContext().getString(R.string.treatmentdeliveryerror));
builder.setMessage(result.comment);
builder.setPositiveButton(this.getContext().getString(R.string.ok), null);
builder.show();
Integer carbs = Integer.parseInt(!carbsText.equals("") ? carbsText : "0");
String confirmMessage = getString(R.string.entertreatmentquestion);
Double insulinAfterConstraints = MainActivity.getConfigBuilder().applyBolusConstraints(insulin);
Integer carbsAfterConstraints = MainActivity.getConfigBuilder().applyCarbsConstraints(carbs);
confirmMessage += getString(R.string.bolus) + ": " + insulinAfterConstraints + "U";
confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g";
if (insulinAfterConstraints != insulin || carbsAfterConstraints != carbs)
confirmMessage += "\n" + getString(R.string.constraintapllied);
final Double finalInsulinAfterConstraints = insulinAfterConstraints;
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) {
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 intFormat = new DecimalFormat("0");
Double calculatedCarbs = 0d;
Integer calculatedCarbs = 0;
Double calculatedTotalInsulin = 0d;
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(",", ".");
Double c_bg = 0d;
try { c_bg = Double.parseDouble(i_bg.equals("") ? "0" : i_bg); } catch (Exception e) {}
Double c_carbs = 0d;
try { c_carbs = Double.parseDouble(i_carbs.equals("") ? "0" : i_carbs); } catch (Exception e) {}
c_carbs = ((Long)Math.round(c_carbs)).doubleValue();
Integer c_carbs = 0;
try { c_carbs = Integer.parseInt(i_carbs.equals("") ? "0" : i_carbs); } catch (Exception e) {}
c_carbs = (Integer) Math.round(c_carbs);
Double c_correction = 0d;
try { c_correction = Double.parseDouble(i_correction.equals("") ? "0" : i_correction); } catch (Exception e) {}
if(c_correction > maxbolus) {

View file

@ -154,4 +154,32 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI
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
public Result deliverTreatment(Double insulin, Double carbs) {
public Result deliverTreatment(Double insulin, Integer carbs) {
Result result = new Result();
result.success = true;
result.bolusDelivered = insulin;
result.carbsDelivered = carbs;
result.comment = getString(R.string.virtualpump_resultok);
if (Config.logPumpComm)

View file

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