From 358de54322a96d53fb0805e20701dcc7e812b95c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 29 May 2017 21:45:59 +0200 Subject: [PATCH] redesign uploading treatments to NS --- .../androidaps/data/DetailedBolusInfo.java | 31 ++++++++++++++++ .../androidaps/data/PumpEnactResult.java | 2 +- .../androidaps/db/CareportalEvent.java | 4 ++ .../androidaps/interfaces/PumpInterface.java | 3 +- .../interfaces/TreatmentsInterface.java | 3 +- .../plugins/Actions/dialogs/FillDialog.java | 7 +++- .../ConfigBuilder/ConfigBuilderPlugin.java | 37 +++++++++++++++++-- .../Overview/Dialogs/NewTreatmentDialog.java | 16 ++++++-- .../Overview/Dialogs/WizardDialog.java | 22 ++++++----- .../plugins/Overview/OverviewFragment.java | 24 ++++++------ .../plugins/PumpDanaR/DanaRPlugin.java | 20 ++++++---- .../PumpDanaRKorean/DanaRKoreanPlugin.java | 20 ++++++---- .../plugins/PumpDanaRv2/DanaRv2Plugin.java | 20 ++++++---- .../androidaps/plugins/PumpMDI/MDIPlugin.java | 8 ++-- .../PumpVirtual/VirtualPumpPlugin.java | 16 ++++---- .../SmsCommunicatorPlugin.java | 5 ++- .../plugins/Treatments/TreatmentsPlugin.java | 20 +++++++++- .../plugins/Wear/ActionStringHandler.java | 11 +++++- .../java/info/nightscout/utils/NSUpload.java | 25 ++++++++----- 19 files changed, 211 insertions(+), 83 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java diff --git a/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java new file mode 100644 index 0000000000..7cf513e659 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java @@ -0,0 +1,31 @@ +package info.nightscout.androidaps.data; + +import android.content.Context; + +import org.json.JSONObject; + +import java.util.Date; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.Source; +import info.nightscout.androidaps.interfaces.InsulinInterface; + +/** + * Created by mike on 29.05.2017. + */ + +public class DetailedBolusInfo { + public long date = new Date().getTime(); + public InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin(); + public String eventType = CareportalEvent.MEALBOLUS; + public double insulin = 0; + public double carbs = 0; + public int source = Source.NONE; + public double glucose = 0; // Bg value in current units + public String glucoseType = ""; // NS values: Manual, Finger, Sensor + public int carbTime = 0; // time shift of carbs in minutes + public JSONObject boluscalc = null; // additional bolus wizard info + public Context context = null; // context for progress dialog + public boolean addToTreatments = true; +} diff --git a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java index a2a5b91870..bd3529124b 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java +++ b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java @@ -25,7 +25,7 @@ public class PumpEnactResult extends Object { public boolean isTempCancel = false; // if true we are caceling temp basal // Result of treatment delivery public Double bolusDelivered = 0d; // real value of delivered insulin - public Integer carbsDelivered = 0; // real value of delivered carbs + public Double carbsDelivered = 0d; // real value of delivered carbs public boolean queued = false; diff --git a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java index ee2e63dffa..8411799081 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java +++ b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java @@ -40,6 +40,10 @@ public class CareportalEvent { @DatabaseField public String json; + public static final String CARBCORRECTION = "Carb Correction"; + public static final String BOLUSWIZARD = "Bolus Wizard"; + public static final String CORRECTIONBOLUS = "Correction Bolus"; + public static final String MEALBOLUS = "Meal Bolus"; public static final String COMBOBOLUS = "Combo Bolus"; public static final String TEMPBASAL = "Temp Basal"; public static final String TEMPORARYTARGET = "Temporary Target"; diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index def7dcddd9..1e9fcc6e35 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -6,6 +6,7 @@ import org.json.JSONObject; import java.util.Date; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; @@ -30,7 +31,7 @@ public interface PumpInterface { double getBaseBasalRate(); // base basal rate, not temp basal - PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context); + PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo); void stopBolusDelivering(); PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes); PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java index acaf1262ec..c0c58a1117 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.interfaces; import java.util.List; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.db.ExtendedBolus; @@ -48,7 +49,7 @@ public interface TreatmentsInterface { void addToHistoryExtendedBolusStop(long time); OverlappingIntervals getExtendedBolusesFromHistory(); - void addTreatmentToHistory(Treatment treatment); + void addTreatmentToHistory(DetailedBolusInfo detailedBolusInfo); TempTarget getTempTargetFromHistory(long time); OverlappingIntervals getTempTargetsFromHistory(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java index dd623c38e9..0e26766a40 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -26,6 +26,7 @@ import java.text.DecimalFormat; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; @@ -159,7 +160,11 @@ public class FillDialog extends DialogFragment implements OnClickListener { mHandler.post(new Runnable() { @Override public void run() { - PumpEnactResult result = pump.deliverTreatment(MainApp.getConfigBuilder().getActiveInsulin(), finalInsulinAfterConstraints, 0, context, false); + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.insulin = finalInsulinAfterConstraints; + detailedBolusInfo.context = context; + detailedBolusInfo.addToTreatments = false; + PumpEnactResult result = pump.deliverTreatment(detailedBolusInfo); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 3adea2f8bc..2b5c82ce77 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -19,6 +19,7 @@ import java.util.List; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.PumpEnactResult; @@ -402,7 +403,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain else return 0d; } - +/* public PumpEnactResult deliverTreatmentFromBolusWizard(InsulinInterface insulinType, Context context, Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { mWakeLock.acquire(); PumpEnactResult result; @@ -438,7 +439,32 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain mWakeLock.release(); return result; } +*/ + @Override + public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { + mWakeLock.acquire(); + PumpEnactResult result; + detailedBolusInfo.insulin = applyBolusConstraints(detailedBolusInfo.insulin); + detailedBolusInfo.carbs = applyCarbsConstraints((int) detailedBolusInfo.carbs); + BolusProgressDialog bolusProgressDialog = null; + if (detailedBolusInfo.context != null) { + bolusProgressDialog = new BolusProgressDialog(); + bolusProgressDialog.setInsulin(detailedBolusInfo.insulin); + bolusProgressDialog.show(((AppCompatActivity) detailedBolusInfo.context).getSupportFragmentManager(), "BolusProgress"); + } + + MainApp.bus().post(new EventBolusRequested(detailedBolusInfo.insulin)); + + result = activePump.deliverTreatment(detailedBolusInfo); + + BolusProgressDialog.bolusEnded = true; + MainApp.bus().post(new EventDismissBolusprogressIfRunning(result)); + + mWakeLock.release(); + return result; + } +/* @Override public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context) { return deliverTreatment(insulinType, insulin, carbs, context, true); @@ -487,7 +513,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return result; } - +*/ @Override public void stopBolusDelivering() { activePump.stopBolusDelivering(); @@ -904,8 +930,11 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } @Override - public void addTreatmentToHistory(Treatment treatment) { - activeTreatments.addTreatmentToHistory(treatment); + public void addTreatmentToHistory(DetailedBolusInfo detailedBolusInfo) { + if (!detailedBolusInfo.addToTreatments) + return; + activeTreatments.addTreatmentToHistory(detailedBolusInfo); + NSUpload.uploadBolusWizardRecord(detailedBolusInfo); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index 8144d39ff5..d15c7ec2ef 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -24,7 +24,9 @@ import java.text.DecimalFormat; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.utils.PlusMinusEditText; import info.nightscout.utils.SafeParse; @@ -86,7 +88,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene try { Double insulin = SafeParse.stringToDouble(this.insulin.getText().toString()); - Integer carbs = SafeParse.stringToInt(this.carbs.getText().toString()); + final Integer carbs = SafeParse.stringToInt(this.carbs.getText().toString()); String confirmMessage = getString(R.string.entertreatmentquestion) + "\n"; @@ -98,8 +100,8 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene if (insulinAfterConstraints - insulin != 0 || carbsAfterConstraints != carbs) confirmMessage += "\n" + getString(R.string.constraintapllied); - final Double finalInsulinAfterConstraints = insulinAfterConstraints; - final Integer finalCarbsAfterConstraints = carbsAfterConstraints; + final double finalInsulinAfterConstraints = insulinAfterConstraints; + final int finalCarbsAfterConstraints = carbsAfterConstraints; final Context context = getContext(); AlertDialog.Builder builder = new AlertDialog.Builder(context); @@ -113,7 +115,13 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene mHandler.post(new Runnable() { @Override public void run() { - PumpEnactResult result = pump.deliverTreatment(MainApp.getConfigBuilder().getActiveInsulin(), finalInsulinAfterConstraints, finalCarbsAfterConstraints, context); + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + if (finalInsulinAfterConstraints == 0) detailedBolusInfo.eventType = CareportalEvent.CARBCORRECTION; + if (finalCarbsAfterConstraints == 0) detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS; + detailedBolusInfo.insulin = finalInsulinAfterConstraints; + detailedBolusInfo.carbs = finalCarbsAfterConstraints; + detailedBolusInfo.context = context; + PumpEnactResult result = pump.deliverTreatment(detailedBolusInfo); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index 117257bbf9..14d03bd00b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -42,8 +42,10 @@ import java.util.Date; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventRefreshGui; @@ -308,16 +310,16 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com mHandler.post(new Runnable() { @Override public void run() { - PumpEnactResult result = pump.deliverTreatmentFromBolusWizard( - MainApp.getConfigBuilder().getActiveInsulin(), - context, - finalInsulinAfterConstraints, - finalCarbsAfterConstraints, - bg, - "Manual", - carbTime, - boluscalcJSON - ); + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.eventType = CareportalEvent.BOLUSWIZARD; + detailedBolusInfo.insulin = finalInsulinAfterConstraints; + detailedBolusInfo.carbs = finalCarbsAfterConstraints; + detailedBolusInfo.context = context; + detailedBolusInfo.glucose = bg; + detailedBolusInfo.glucoseType = "Manual"; + detailedBolusInfo.carbTime = carbTime; + detailedBolusInfo.boluscalc = boluscalcJSON; + PumpEnactResult result = pump.deliverTreatment(detailedBolusInfo); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 0227416874..c0806e54d5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.Overview; import android.annotation.SuppressLint; import android.app.Activity; +import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; import android.graphics.DashPathEffect; @@ -62,10 +63,12 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; @@ -645,8 +648,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, final Double finalInsulinAfterConstraints = insulinAfterConstraints; final Integer finalCarbsAfterConstraints = carbsAfterConstraints; - - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + final Context context = getContext(); + AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(MainApp.sResources.getString(R.string.confirmation)); builder.setMessage(confirmMessage); builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { @@ -656,16 +659,13 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, sHandler.post(new Runnable() { @Override public void run() { - PumpEnactResult result = pump.deliverTreatmentFromBolusWizard( - MainApp.getConfigBuilder().getActiveInsulin(), - getContext(), - finalInsulinAfterConstraints, - finalCarbsAfterConstraints, - actualBg.valueToUnits(profile.getUnits()), - "Manual", - 0, - boluscalcJSON - ); + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.eventType = CareportalEvent.BOLUSWIZARD; + detailedBolusInfo.insulin = finalInsulinAfterConstraints; + detailedBolusInfo.carbs = finalCarbsAfterConstraints; + detailedBolusInfo.context = context; + detailedBolusInfo.boluscalc = boluscalcJSON; + PumpEnactResult result = pump.deliverTreatment(detailedBolusInfo); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java index 882e2333df..1faf2f5f7e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/DanaRPlugin.java @@ -22,6 +22,7 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; @@ -296,26 +297,29 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf } @Override - public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context) { + public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - insulin = configBuilderPlugin.applyBolusConstraints(insulin); - if (insulin > 0 || carbs > 0) { - Treatment t = new Treatment(insulinType); + detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); + if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { + Treatment t = new Treatment(detailedBolusInfo.insulinInterface); boolean connectionOK = false; - if (insulin > 0 || carbs > 0) connectionOK = sExecutionService.bolus(insulin, carbs, t); + if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, t); PumpEnactResult result = new PumpEnactResult(); result.success = connectionOK; result.bolusDelivered = t.insulin; - result.carbsDelivered = carbs; + result.carbsDelivered = detailedBolusInfo.carbs; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); if (Config.logPumpActions) - log.debug("deliverTreatment: OK. Asked: " + insulin + " Delivered: " + result.bolusDelivered); + log.debug("deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered); + detailedBolusInfo.insulin = t.insulin; + detailedBolusInfo.date = new Date().getTime(); + MainApp.getConfigBuilder().addTreatmentToHistory(detailedBolusInfo); return result; } else { PumpEnactResult result = new PumpEnactResult(); result.success = false; result.bolusDelivered = 0d; - result.carbsDelivered = 0; + result.carbsDelivered = 0d; result.comment = MainApp.instance().getString(R.string.danar_invalidinput); log.error("deliverTreatment: Invalid input"); return result; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java index 158b7e882f..632f66db83 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRKorean/DanaRKoreanPlugin.java @@ -22,6 +22,7 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; @@ -299,26 +300,29 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints } @Override - public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context) { + public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - insulin = configBuilderPlugin.applyBolusConstraints(insulin); - if (insulin > 0 || carbs > 0) { - Treatment t = new Treatment(insulinType); + detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); + if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { + Treatment t = new Treatment(detailedBolusInfo.insulinInterface); boolean connectionOK = false; - if (insulin > 0 || carbs > 0) connectionOK = sExecutionService.bolus(insulin, carbs, t); + if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, t); PumpEnactResult result = new PumpEnactResult(); result.success = connectionOK; result.bolusDelivered = t.insulin; - result.carbsDelivered = carbs; + result.carbsDelivered = detailedBolusInfo.carbs; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); if (Config.logPumpActions) - log.debug("deliverTreatment: OK. Asked: " + insulin + " Delivered: " + result.bolusDelivered); + log.debug("deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered); + detailedBolusInfo.insulin = t.insulin; + detailedBolusInfo.date = new Date().getTime(); + MainApp.getConfigBuilder().addTreatmentToHistory(detailedBolusInfo); return result; } else { PumpEnactResult result = new PumpEnactResult(); result.success = false; result.bolusDelivered = 0d; - result.carbsDelivered = 0; + result.carbsDelivered = 0d; result.comment = MainApp.instance().getString(R.string.danar_invalidinput); log.error("deliverTreatment: Invalid input"); return result; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java index 28734b76cc..15ca7cd558 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/DanaRv2Plugin.java @@ -24,6 +24,7 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; @@ -282,26 +283,29 @@ public class DanaRv2Plugin implements PluginBase, PumpInterface, ConstraintsInte } @Override - public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context) { + public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { ConfigBuilderPlugin configBuilderPlugin = MainApp.getConfigBuilder(); - insulin = configBuilderPlugin.applyBolusConstraints(insulin); - if (insulin > 0 || carbs > 0) { - Treatment t = new Treatment(insulinType); + detailedBolusInfo.insulin = configBuilderPlugin.applyBolusConstraints(detailedBolusInfo.insulin); + if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) { + Treatment t = new Treatment(detailedBolusInfo.insulinInterface); boolean connectionOK = false; - if (insulin > 0 || carbs > 0) connectionOK = sExecutionService.bolus(insulin, carbs, t); + if (detailedBolusInfo.insulin > 0 || detailedBolusInfo.carbs > 0) connectionOK = sExecutionService.bolus(detailedBolusInfo.insulin, (int) detailedBolusInfo.carbs, t); PumpEnactResult result = new PumpEnactResult(); result.success = connectionOK; result.bolusDelivered = t.insulin; - result.carbsDelivered = carbs; + result.carbsDelivered = detailedBolusInfo.carbs; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); if (Config.logPumpActions) - log.debug("deliverTreatment: OK. Asked: " + insulin + " Delivered: " + result.bolusDelivered); + log.debug("deliverTreatment: OK. Asked: " + detailedBolusInfo.insulin + " Delivered: " + result.bolusDelivered); + detailedBolusInfo.insulin = t.insulin; + detailedBolusInfo.date = new Date().getTime(); + MainApp.getConfigBuilder().addTreatmentToHistory(detailedBolusInfo); return result; } else { PumpEnactResult result = new PumpEnactResult(); result.success = false; result.bolusDelivered = 0d; - result.carbsDelivered = 0; + result.carbsDelivered = 0d; result.comment = MainApp.instance().getString(R.string.danar_invalidinput); log.error("deliverTreatment: Invalid input"); return result; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java index 380e796aca..f2d31b90c6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java @@ -13,6 +13,7 @@ import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; @@ -164,12 +165,13 @@ public class MDIPlugin implements PluginBase, PumpInterface { } @Override - public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context) { + public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { PumpEnactResult result = new PumpEnactResult(); result.success = true; - result.bolusDelivered = insulin; - result.carbsDelivered = carbs; + result.bolusDelivered = detailedBolusInfo.insulin; + result.carbsDelivered = detailedBolusInfo.carbs; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); + MainApp.getConfigBuilder().addTreatmentToHistory(detailedBolusInfo); return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java index cc86325848..6eb658b8a8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java @@ -15,6 +15,7 @@ import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; @@ -189,23 +190,23 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { } @Override - public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context) { + public PumpEnactResult deliverTreatment(DetailedBolusInfo detailedBolusInfo) { PumpEnactResult result = new PumpEnactResult(); result.success = true; - result.bolusDelivered = insulin; - result.carbsDelivered = carbs; + result.bolusDelivered = detailedBolusInfo.insulin; + result.carbsDelivered = detailedBolusInfo.carbs; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); Double delivering = 0d; - while (delivering < insulin) { + while (delivering < detailedBolusInfo.insulin) { try { Thread.sleep(200); } catch (InterruptedException e) { } EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivering), delivering); - bolusingEvent.percent = Math.min((int) (delivering / insulin * 100), 100); + bolusingEvent.percent = Math.min((int) (delivering / detailedBolusInfo.insulin * 100), 100); MainApp.bus().post(bolusingEvent); delivering += 0.1d; } @@ -214,7 +215,7 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { } catch (InterruptedException e) { } EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivered), insulin); + bolusingEvent.status = String.format(MainApp.sResources.getString(R.string.bolusdelivered), detailedBolusInfo.insulin); bolusingEvent.percent = 100; MainApp.bus().post(bolusingEvent); try { @@ -222,9 +223,10 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { } catch (InterruptedException e) { } if (Config.logPumpComm) - log.debug("Delivering treatment insulin: " + insulin + "U carbs: " + carbs + "g " + result); + log.debug("Delivering treatment insulin: " + detailedBolusInfo.insulin + "U carbs: " + detailedBolusInfo.carbs + "g " + result); MainApp.bus().post(new EventVirtualPumpUpdateGui()); lastDataTime = new Date(); + MainApp.getConfigBuilder().addTreatmentToHistory(detailedBolusInfo); return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index e10a92205b..344b9aaf00 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -21,6 +21,7 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.PumpEnactResult; @@ -465,7 +466,9 @@ public class SmsCommunicatorPlugin implements PluginBase { PumpInterface pumpInterface = MainApp.getConfigBuilder(); if (pumpInterface != null) { danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); - PumpEnactResult result = pumpInterface.deliverTreatment(ConfigBuilderPlugin.getActiveInsulin(), bolusWaitingForConfirmation.bolusRequested, 0, null); + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.insulin = bolusWaitingForConfirmation.bolusRequested; + PumpEnactResult result = pumpInterface.deliverTreatment(detailedBolusInfo); if (result.success) { reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusdelivered), result.bolusDelivered); if (danaRPlugin != null) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 19be0ac511..0e892fbc67 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -14,6 +14,7 @@ import java.util.List; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; @@ -403,9 +404,24 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { } @Override - public void addTreatmentToHistory(Treatment treatment) { - log.debug("Adding new Treatment record" + treatment); + public void addTreatmentToHistory(DetailedBolusInfo detailedBolusInfo) { + Treatment treatment = new Treatment(detailedBolusInfo.insulinInterface); + treatment.date = detailedBolusInfo.date; + treatment.insulin = detailedBolusInfo.insulin; + if (detailedBolusInfo.carbTime == 0) + treatment.carbs = detailedBolusInfo.carbs; + treatment.source = detailedBolusInfo.source; + treatment.mealBolus = treatment.carbs > 0; MainApp.getDbHelper().createOrUpdate(treatment); + log.debug("Adding new Treatment record" + treatment); + if (detailedBolusInfo.carbTime != 0) { + Treatment carbsTreatment = new Treatment(detailedBolusInfo.insulinInterface); + carbsTreatment.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60 * 1000L; + carbsTreatment.carbs = detailedBolusInfo.carbs; + carbsTreatment.source = detailedBolusInfo.source; + MainApp.getDbHelper().createOrUpdate(carbsTreatment); + log.debug("Adding new Treatment record" + carbsTreatment); + } } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index 4a03ee1972..36e6f43899 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -12,6 +12,7 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.DatabaseHelper; @@ -368,7 +369,10 @@ public class ActionStringHandler { handler.post(new Runnable() { @Override public void run() { - PumpEnactResult result = MainApp.getConfigBuilder().deliverTreatment(MainApp.getConfigBuilder().getActiveInsulin(), amount, 0, null, false); + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.insulin = amount; + detailedBolusInfo.addToTreatments = false; + PumpEnactResult result = MainApp.getConfigBuilder().deliverTreatment(detailedBolusInfo); if (!result.success) { sendError(MainApp.sResources.getString(R.string.treatmentdeliveryerror) + "\n" + @@ -384,7 +388,10 @@ public class ActionStringHandler { handler.post(new Runnable() { @Override public void run() { - PumpEnactResult result = MainApp.getConfigBuilder().deliverTreatment(MainApp.getConfigBuilder().getActiveInsulin(), amount, carbs, null, true); + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.insulin = amount; + detailedBolusInfo.carbs = carbs; + PumpEnactResult result = MainApp.getConfigBuilder().deliverTreatment(detailedBolusInfo); if (!result.success) { sendError(MainApp.sResources.getString(R.string.treatmentdeliveryerror) + "\n" + diff --git a/app/src/main/java/info/nightscout/utils/NSUpload.java b/app/src/main/java/info/nightscout/utils/NSUpload.java index af26c76642..c0cc3a985a 100644 --- a/app/src/main/java/info/nightscout/utils/NSUpload.java +++ b/app/src/main/java/info/nightscout/utils/NSUpload.java @@ -16,6 +16,7 @@ import java.util.Date; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.TemporaryBasal; @@ -251,18 +252,22 @@ public class NSUpload { } } - public static void uploadBolusWizardRecord(Treatment t, double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { + public static void uploadBolusWizardRecord(DetailedBolusInfo detailedBolusInfo) { JSONObject data = new JSONObject(); try { - data.put("eventType", "Bolus Wizard"); - if (t.insulin != 0d) data.put("insulin", t.insulin); - if (t.carbs != 0d) data.put("carbs", t.carbs.intValue()); - data.put("created_at", DateUtil.toISOString(t.date)); - data.put("date", t.date); - if (glucose != 0d) data.put("glucose", glucose); - data.put("glucoseType", glucoseType); - data.put("boluscalc", boluscalc); - if (carbTime != 0) data.put("preBolus", carbTime); + data.put("eventType", detailedBolusInfo.eventType); + if (detailedBolusInfo.insulin != 0d) data.put("insulin", detailedBolusInfo.insulin); + if (detailedBolusInfo.carbs != 0d) data.put("carbs", (int) detailedBolusInfo.carbs); + data.put("created_at", DateUtil.toISOString(detailedBolusInfo.date)); + data.put("date", detailedBolusInfo.date); + if (detailedBolusInfo.glucose != 0d) + data.put("glucose", detailedBolusInfo.glucose); + if (!detailedBolusInfo.glucoseType.equals("")) + data.put("glucoseType", detailedBolusInfo.glucoseType); + if (detailedBolusInfo.boluscalc != null) + data.put("boluscalc", detailedBolusInfo.boluscalc); + if (detailedBolusInfo.carbTime != 0) + data.put("preBolus", detailedBolusInfo.carbTime); } catch (JSONException e) { e.printStackTrace(); }