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 732c8ff07f..a2a5b91870 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java +++ b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java @@ -27,9 +27,6 @@ public class PumpEnactResult extends Object { public Double bolusDelivered = 0d; // real value of delivered insulin public Integer carbsDelivered = 0; // real value of delivered carbs - public Double originalExtendedAmount = null; // amount of extended bolus insulin if uploading as temp basal - public Boolean isFakedTempBasal = null; // true if canceling extended bolus but uploading as temp basal - public boolean queued = false; public String log() { diff --git a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java index 7bb7a1fa4c..027f5c1e3e 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java @@ -61,6 +61,19 @@ public class TemporaryBasal implements Interval { this.absoluteRate = basal + extendedBolus.absoluteRate(); } + public TemporaryBasal clone() { + TemporaryBasal t = new TemporaryBasal(); + t.date = date; + t.isValid = isValid; + t.source = source; + t._id = _id; + t.durationInMinutes = durationInMinutes; + t.isAbsolute = isAbsolute; + t.percentRate = percentRate; + t.absoluteRate = absoluteRate; + return t; + } + // -------- Interval interface --------- Long cuttedEnd = null; 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 e87a429a02..3adea2f8bc 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 @@ -406,50 +406,34 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain public PumpEnactResult deliverTreatmentFromBolusWizard(InsulinInterface insulinType, Context context, Double insulin, Integer carbs, Double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { mWakeLock.acquire(); PumpEnactResult result; - if (activePump != null) { - insulin = applyBolusConstraints(insulin); - carbs = applyCarbsConstraints(carbs); + insulin = applyBolusConstraints(insulin); + carbs = applyCarbsConstraints(carbs); - BolusProgressDialog bolusProgressDialog = null; - if (context != null) { - bolusProgressDialog = new BolusProgressDialog(); - bolusProgressDialog.setInsulin(insulin); - bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress"); - } + BolusProgressDialog bolusProgressDialog = null; + if (context != null) { + bolusProgressDialog = new BolusProgressDialog(); + bolusProgressDialog.setInsulin(insulin); + bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress"); + } - MainApp.bus().post(new EventBolusRequested(insulin)); + MainApp.bus().post(new EventBolusRequested(insulin)); - result = activePump.deliverTreatment(insulinType, insulin, carbs, context); + result = activePump.deliverTreatment(insulinType, insulin, carbs, context); - BolusProgressDialog.bolusEnded = true; + BolusProgressDialog.bolusEnded = true; - MainApp.bus().post(new EventDismissBolusprogressIfRunning(result)); + MainApp.bus().post(new EventDismissBolusprogressIfRunning(result)); - if (result.success) { - Treatment t = new Treatment(insulinType); - t.insulin = result.bolusDelivered; - if (carbTime == 0) - t.carbs = (double) result.carbsDelivered; // with different carbTime record will come back from nightscout - t.date = new Date().getDate(); - t.mealBolus = result.carbsDelivered > 0; - addTreatmentToHistory(t); - t.carbs = (double) result.carbsDelivered; - NSUpload.uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc); - } - } else { - if (Config.logCongigBuilderActions) - log.debug("Creating treatment: " + insulin + " carbs: " + carbs); + if (result.success) { Treatment t = new Treatment(insulinType); - t.insulin = insulin; - t.carbs = (double) carbs; - t.date = new Date().getDate(); - t.mealBolus = t.carbs > 0; + t.insulin = result.bolusDelivered; + if (carbTime == 0) + t.carbs = (double) result.carbsDelivered; // with different carbTime record will come back from nightscout + t.date = new Date().getTime(); + t.mealBolus = result.carbsDelivered > 0; addTreatmentToHistory(t); - NSUpload.uploadTreatment(t); - result = new PumpEnactResult(); - result.success = true; - result.bolusDelivered = insulin; - result.carbsDelivered = carbs; + t.carbs = (double) result.carbsDelivered; + NSUpload.uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc); } mWakeLock.release(); return result; @@ -463,47 +447,41 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain public PumpEnactResult deliverTreatment(InsulinInterface insulinType, Double insulin, Integer carbs, Context context, boolean createTreatment) { mWakeLock.acquire(); PumpEnactResult result; - if (activePump != null) { - insulin = applyBolusConstraints(insulin); - carbs = applyCarbsConstraints(carbs); + insulin = applyBolusConstraints(insulin); + carbs = applyCarbsConstraints(carbs); - BolusProgressDialog bolusProgressDialog = null; - if (context != null) { - bolusProgressDialog = new BolusProgressDialog(); - bolusProgressDialog.setInsulin(insulin); - bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress"); - } else { - Intent i = new Intent(); - i.putExtra("insulin", insulin.doubleValue()); - i.setClass(MainApp.instance(), BolusProgressHelperActivity.class); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - - MainApp.bus().post(new EventBolusRequested(insulin)); - - result = activePump.deliverTreatment(insulinType, insulin, carbs, context); - - BolusProgressDialog.bolusEnded = true; - - MainApp.bus().post(new EventDismissBolusprogressIfRunning(result)); - - if (Config.logCongigBuilderActions) - log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered); - - if (result.success && createTreatment) { - Treatment t = new Treatment(insulinType); - t.insulin = result.bolusDelivered; - t.carbs = (double) result.carbsDelivered; - t.date = new Date().getTime(); - t.mealBolus = t.carbs > 0; - addTreatmentToHistory(t); - NSUpload.uploadTreatment(t); - } + BolusProgressDialog bolusProgressDialog = null; + if (context != null) { + bolusProgressDialog = new BolusProgressDialog(); + bolusProgressDialog.setInsulin(insulin); + bolusProgressDialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "BolusProgress"); } else { - log.error("activePump==null"); - result = new PumpEnactResult(); - result.success = false; + Intent i = new Intent(); + i.putExtra("insulin", insulin.doubleValue()); + i.setClass(MainApp.instance(), BolusProgressHelperActivity.class); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); + } + + MainApp.bus().post(new EventBolusRequested(insulin)); + + result = activePump.deliverTreatment(insulinType, insulin, carbs, context); + + BolusProgressDialog.bolusEnded = true; + + MainApp.bus().post(new EventDismissBolusprogressIfRunning(result)); + + if (Config.logCongigBuilderActions) + log.debug("deliverTreatment insulin: " + insulin + " carbs: " + carbs + " success: " + result.success + " enacted: " + result.enacted + " bolusDelivered: " + result.bolusDelivered); + + if (result.success && createTreatment) { + Treatment t = new Treatment(insulinType); + t.insulin = result.bolusDelivered; + t.carbs = (double) result.carbsDelivered; + t.date = new Date().getTime(); + t.mealBolus = t.carbs > 0; + addTreatmentToHistory(t); + NSUpload.uploadTreatment(t); } mWakeLock.release(); return result; @@ -528,13 +506,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain PumpEnactResult result = activePump.setTempBasalAbsolute(rateAfterConstraints, durationInMinutes); if (Config.logCongigBuilderActions) log.debug("setTempBasalAbsolute rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted); - if (result.enacted && result.success) { - if (result.isPercent) { - NSUpload.uploadTempBasalStartPercent(result.percent, result.duration); - } else { - NSUpload.uploadTempBasalStartAbsolute(result.absolute, result.duration, result.originalExtendedAmount); - } - } return result; } @@ -551,9 +522,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain PumpEnactResult result = activePump.setTempBasalPercent(percentAfterConstraints, durationInMinutes); if (Config.logCongigBuilderActions) log.debug("setTempBasalPercent percent: " + percentAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted); - if (result.enacted && result.success) { - NSUpload.uploadTempBasalStartPercent(result.percent, result.duration); - } return result; } @@ -563,9 +531,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain PumpEnactResult result = activePump.setExtendedBolus(rateAfterConstraints, durationInMinutes); if (Config.logCongigBuilderActions) log.debug("setExtendedBolus rate: " + rateAfterConstraints + " durationInMinutes: " + durationInMinutes + " success: " + result.success + " enacted: " + result.enacted); - if (result.enacted && result.success) { - NSUpload.uploadExtendedBolus(result.bolusDelivered, result.duration); - } return result; } @@ -574,9 +539,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain PumpEnactResult result = activePump.cancelTempBasal(); if (Config.logCongigBuilderActions) log.debug("cancelTempBasal success: " + result.success + " enacted: " + result.enacted); - if (result.enacted && result.success) { - NSUpload.uploadTempBasalEnd(result.isFakedTempBasal); - } return result; } @@ -585,9 +547,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain PumpEnactResult result = activePump.cancelExtendedBolus(); if (Config.logCongigBuilderActions) log.debug("cancelExtendedBolus success: " + result.success + " enacted: " + result.enacted); - if (result.enacted && result.success) { - NSUpload.uploadExtendedBolusEnd(); - } return result; } @@ -898,11 +857,17 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain @Override public void addToHistoryTempBasalStart(TemporaryBasal tempBasal) { activeTreatments.addToHistoryTempBasalStart(tempBasal); + if (tempBasal.isAbsolute) + NSUpload.uploadTempBasalStartAbsolute(tempBasal, null); + else + NSUpload.uploadTempBasalStartPercent(tempBasal); + } @Override public void addToHistoryTempBasalStop(long time) { activeTreatments.addToHistoryTempBasalStop(time); + NSUpload.uploadTempBasalEnd(time, false); } @Override @@ -918,11 +883,19 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain @Override public void addToHistoryExtendedBolusStart(ExtendedBolus extendedBolus) { activeTreatments.addToHistoryExtendedBolusStart(extendedBolus); + if (activePump.isFakingTempsByExtendedBoluses()) + NSUpload.uploadTempBasalStartAbsolute(new TemporaryBasal(extendedBolus), extendedBolus.insulin); + else + NSUpload.uploadExtendedBolus(extendedBolus); } @Override public void addToHistoryExtendedBolusStop(long time) { activeTreatments.addToHistoryExtendedBolusStop(time); + if (activePump.isFakingTempsByExtendedBoluses()) + NSUpload.uploadTempBasalEnd(time, true); + else + NSUpload.uploadExtendedBolusEnd(time); } @Override 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 60ab6244aa..882e2333df 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 @@ -472,7 +472,6 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf if (Config.logPumpActions) log.debug("setTempBasalAbsolute: Extended bolus set ok"); result.absolute = result.absolute + getBaseBasalRate(); - result.originalExtendedAmount = extendedAmount; return result; } // We should never end here @@ -578,7 +577,6 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf return cancelRealTempBasal(); if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress() && useExtendedBoluses) { PumpEnactResult cancelEx = cancelExtendedBolus(); - cancelEx.isFakedTempBasal = true; return cancelEx; } PumpEnactResult result = new PumpEnactResult(); 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 6075168ef4..158b7e882f 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 @@ -475,7 +475,6 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints if (Config.logPumpActions) log.debug("setTempBasalAbsolute: Extended bolus set ok"); result.absolute = result.absolute + getBaseBasalRate(); - result.originalExtendedAmount = extendedAmount; return result; } // We should never end here @@ -581,7 +580,6 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints return cancelRealTempBasal(); if (MainApp.getConfigBuilder().isInHistoryExtendedBoluslInProgress() && useExtendedBoluses) { PumpEnactResult cancelEx = cancelExtendedBolus(); - cancelEx.isFakedTempBasal = true; return cancelEx; } PumpEnactResult result = new PumpEnactResult(); diff --git a/app/src/main/java/info/nightscout/utils/NSUpload.java b/app/src/main/java/info/nightscout/utils/NSUpload.java index 4da4fe0a34..af26c76642 100644 --- a/app/src/main/java/info/nightscout/utils/NSUpload.java +++ b/app/src/main/java/info/nightscout/utils/NSUpload.java @@ -17,11 +17,14 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.ExtendedBolus; +import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.DeviceStatus; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.data.DbLogger; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA; import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResultMA; @@ -32,16 +35,16 @@ import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResultMA; public class NSUpload { private static Logger log = LoggerFactory.getLogger(NSUpload.class); - public static void uploadTempBasalStartAbsolute(Double absolute, double durationInMinutes, Double originalExtendedAmount) { + public static void uploadTempBasalStartAbsolute(TemporaryBasal temporaryBasal, Double originalExtendedAmount) { try { Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.TEMPBASAL); - data.put("duration", durationInMinutes); - data.put("absolute", absolute); - data.put("created_at", DateUtil.toISOString(new Date())); + data.put("duration", temporaryBasal.durationInMinutes); + data.put("absolute", temporaryBasal.absoluteRate); + data.put("created_at", DateUtil.toISOString(temporaryBasal.date)); data.put("enteredBy", MainApp.instance().getString(R.string.app_name)); - data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + absolute + "u/h " + durationInMinutes + " min"); // ECOR + data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + temporaryBasal.absoluteRate + "u/h " + temporaryBasal.durationInMinutes + " min"); // ECOR if (originalExtendedAmount != null) data.put("originalExtendedAmount", originalExtendedAmount); // for back synchronization Bundle bundle = new Bundle(); @@ -58,22 +61,27 @@ public class NSUpload { } } - public static void uploadTempBasalStartPercent(Integer percent, double durationInMinutes) { + public static void uploadTempBasalStartPercent(TemporaryBasal temporaryBasal) { try { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); boolean useAbsolute = SP.getBoolean("ns_sync_use_absolute", false); if (useAbsolute) { - double absolute = MainApp.getConfigBuilder().getBaseBasalRate() * percent / 100d; - uploadTempBasalStartAbsolute(absolute, durationInMinutes, null); + TemporaryBasal t = temporaryBasal.clone(); + t.isAbsolute = true; + NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + if (profile != null) { + t.absoluteRate = profile.getBasal(NSProfile.secondsFromMidnight(temporaryBasal.date)) * temporaryBasal.percentRate / 100d; + uploadTempBasalStartAbsolute(t, null); + } } else { Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.TEMPBASAL); - data.put("duration", durationInMinutes); - data.put("percent", percent - 100); - data.put("created_at", DateUtil.toISOString(new Date())); + data.put("duration", temporaryBasal.durationInMinutes); + data.put("percent", temporaryBasal.percentRate - 100); + data.put("created_at", DateUtil.toISOString(temporaryBasal.date)); data.put("enteredBy", MainApp.instance().getString(R.string.app_name)); - data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + percent + "% " + durationInMinutes + " min"); // ECOR + data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + temporaryBasal.percentRate + "% " + temporaryBasal.durationInMinutes + " min"); // ECOR Bundle bundle = new Bundle(); bundle.putString("action", "dbAdd"); bundle.putString("collection", "treatments"); @@ -89,15 +97,15 @@ public class NSUpload { } } - public static void uploadTempBasalEnd(Boolean isFakedTempBasal) { + public static void uploadTempBasalEnd(long time, boolean isFakedTempBasal) { try { Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.TEMPBASAL); - data.put("created_at", DateUtil.toISOString(new Date())); + data.put("created_at", DateUtil.toISOString(time)); data.put("enteredBy", MainApp.instance().getString(R.string.app_name)); data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalendnote)); // ECOR - if (isFakedTempBasal != null) + if (isFakedTempBasal) data.put("isFakedTempBasal", isFakedTempBasal); Bundle bundle = new Bundle(); bundle.putString("action", "dbAdd"); @@ -113,17 +121,17 @@ public class NSUpload { } } - public static void uploadExtendedBolus(Double insulin, double durationInMinutes) { + public static void uploadExtendedBolus(ExtendedBolus extendedBolus) { try { Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.COMBOBOLUS); - data.put("duration", durationInMinutes); + data.put("duration", extendedBolus.durationInMinutes); data.put("splitNow", 0); data.put("splitExt", 100); - data.put("enteredinsulin", insulin); - data.put("relative", insulin); - data.put("created_at", DateUtil.toISOString(new Date())); + data.put("enteredinsulin", extendedBolus.insulin); + data.put("relative", extendedBolus.insulin); + data.put("created_at", DateUtil.toISOString(extendedBolus.date)); data.put("enteredBy", MainApp.instance().getString(R.string.app_name)); Bundle bundle = new Bundle(); bundle.putString("action", "dbAdd"); @@ -139,7 +147,7 @@ public class NSUpload { } } - public static void uploadExtendedBolusEnd() { + public static void uploadExtendedBolusEnd(long time) { try { Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); @@ -149,7 +157,7 @@ public class NSUpload { data.put("splitExt", 100); data.put("enteredinsulin", 0); data.put("relative", 0); - data.put("created_at", DateUtil.toISOString(new Date())); + data.put("created_at", DateUtil.toISOString(time)); data.put("enteredBy", MainApp.instance().getString(R.string.app_name)); Bundle bundle = new Bundle(); bundle.putString("action", "dbAdd");