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 5e8b4e52e8..698f0161e4 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java @@ -30,6 +30,7 @@ public interface TreatmentsInterface { List getTreatmentsFromHistory(); List getTreatments5MinBackFromHistory(long time); + long getLastSMBTime(); // real basals (not faked by extended bolus) boolean isInHistoryRealTempBasalInProgress(); 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 6bc0e1e807..30ee04de8a 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 @@ -27,6 +27,7 @@ import info.nightscout.androidaps.data.Intervals; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.ProfileIntervals; import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.TempTarget; @@ -442,6 +443,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain bolusProgressDialog = new BolusProgressDialog(); bolusProgressDialog.setInsulin(detailedBolusInfo.insulin); bolusProgressDialog.show(((AppCompatActivity) detailedBolusInfo.context).getSupportFragmentManager(), "BolusProgress"); + } else if (detailedBolusInfo.isSMB) { + // do not show bolus progress dialog } else { Intent i = new Intent(); i.putExtra("insulin", detailedBolusInfo.insulin); @@ -588,6 +591,23 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain log.debug("applyAPSRequest: setTempBasalAbsolute()"); result = setTempBasalAbsolute(request.rate, request.duration); } + + if (request.smb != 0) { + long lastSMBTime = getLastSMBTime(); + if (lastSMBTime != 0 && lastSMBTime + 4.5 * 60 * 1000 > System.currentTimeMillis()) { + log.debug("SMS requsted but still in 5 min interval"); + } else { + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS; + detailedBolusInfo.insulin = request.smb; + detailedBolusInfo.isSMB = true; + PumpEnactResult smbResult = deliverTreatment(detailedBolusInfo); + if (smbResult.success) + return result; + else + return smbResult; + } + } return result; } @@ -797,6 +817,11 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return activeTreatments.getTreatments5MinBackFromHistory(time); } + @Override + public long getLastSMBTime() { + return activeTreatments.getLastSMBTime(); + } + @Override public boolean isInHistoryRealTempBasalInProgress() { return activeTreatments.isInHistoryRealTempBasalInProgress(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java index 69e161524f..5e89a5a0f3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/APSResult.java @@ -10,6 +10,7 @@ import org.json.JSONObject; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.utils.DecimalFormatter; @@ -21,6 +22,10 @@ public class APSResult { public double rate; public int duration; public boolean changeRequested = false; + public IobTotal iob; + public double smb = 0d; // super micro bolus in units + public long deliverAt = 0; + @Override public String toString() { final ConfigBuilderPlugin configBuilder = MainApp.getConfigBuilder(); @@ -31,6 +36,7 @@ public class APSResult { return MainApp.sResources.getString(R.string.rate) + ": " + DecimalFormatter.to2Decimal(rate) + " U/h " + "(" + DecimalFormatter.to2Decimal(rate/configBuilder.getBaseBasalRate() *100) + "%)\n" + MainApp.sResources.getString(R.string.duration) + ": " + DecimalFormatter.to0Decimal(duration) + " min\n" + + (smb != 0 ? ("SMB" + ": " + DecimalFormatter.to2Decimal(smb) + " U") : "") + MainApp.sResources.getString(R.string.reason) + ": " + reason; } else return MainApp.sResources.getString(R.string.nochangerequested); @@ -45,6 +51,7 @@ public class APSResult { ret = "" + MainApp.sResources.getString(R.string.rate) + ": " + DecimalFormatter.to2Decimal(rate) + " U/h " + "(" + DecimalFormatter.to2Decimal(rate/configBuilder.getBaseBasalRate() *100) + "%)
" + "" + MainApp.sResources.getString(R.string.duration) + ": " + DecimalFormatter.to2Decimal(duration) + " min
" + + (smb != 0 ? ("" + "SMB" + ": " + DecimalFormatter.to2Decimal(smb) + " U") : "") + "" + MainApp.sResources.getString(R.string.reason) + ": " + reason.replace("<", "<").replace(">", ">"); return Html.fromHtml(ret); } else @@ -60,6 +67,7 @@ public class APSResult { newResult.rate = rate; newResult.duration = duration; newResult.changeRequested = changeRequested; + newResult.iob = iob; return newResult; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java index 6e459a03b2..54a9cd0795 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java @@ -260,6 +260,7 @@ public class LoopPlugin implements PluginBase { // check rate for constrais final APSResult resultAfterConstraints = result.clone(); resultAfterConstraints.rate = constraintsInterface.applyBasalConstraints(resultAfterConstraints.rate); + resultAfterConstraints.smb = constraintsInterface.applyBolusConstraints(resultAfterConstraints.smb); if (lastRun == null) lastRun = new LastRun(); lastRun.request = result; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalResultAMA.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalResultAMA.java index c2a55ff894..12c7e8ac22 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalResultAMA.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/DetermineBasalResultAMA.java @@ -19,7 +19,6 @@ public class DetermineBasalResultAMA extends APSResult { public JSONObject json = new JSONObject(); public double eventualBG; public double snoozeBG; - public IobTotal iob; public DetermineBasalResultAMA(V8Object result, JSONObject j) { date = new Date(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResultMA.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResultMA.java index a0cd1a75b0..e5154a8fa2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResultMA.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResultMA.java @@ -17,7 +17,6 @@ public class DetermineBasalResultMA extends APSResult { public double eventualBG; public double snoozeBG; public String mealAssist; - public IobTotal iob; public DetermineBasalResultMA(V8Object result, JSONObject j) { json = j; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/DetermineBasalResultSMB.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/DetermineBasalResultSMB.java index 8a6c29d4fe..f512d44535 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/DetermineBasalResultSMB.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/DetermineBasalResultSMB.java @@ -20,9 +20,6 @@ public class DetermineBasalResultSMB extends APSResult { public JSONObject json = new JSONObject(); public double eventualBG; public double snoozeBG; - public IobTotal iob; - public double smbValue; - public long deliverAt; public DetermineBasalResultSMB(JSONObject result) { date = new Date(); @@ -54,9 +51,9 @@ public class DetermineBasalResultSMB extends APSResult { } if (result.has("units")) { changeRequested = true; - smbValue = result.getDouble("units"); + smb = result.getDouble("units"); } else { - smbValue = 0.0; + smb = 0d; //changeRequested as above } if (result.has("deliverAt")) { @@ -85,7 +82,8 @@ public class DetermineBasalResultSMB extends APSResult { newResult.changeRequested = changeRequested; newResult.rate = rate; newResult.duration = duration; - newResult.smbValue = smbValue; + newResult.smb = smb; + newResult.deliverAt = deliverAt; try { newResult.json = new JSONObject(json.toString()); 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 75ee736a8b..608887b9ac 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 @@ -268,6 +268,19 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { return in5minback; } + @Override + public long getLastSMBTime() { + long last = 0; + for (Integer pos = 0; pos < treatments.size(); pos++) { + Treatment t = treatments.get(pos); + if (!t.isValid) + continue; + if (t.isSMB && t.date > last) + last = t.date; + } + return last; + } + @Override public boolean isInHistoryRealTempBasalInProgress() { return getRealTempBasalFromHistory(System.currentTimeMillis()) != null; diff --git a/app/src/main/java/info/nightscout/utils/NSUpload.java b/app/src/main/java/info/nightscout/utils/NSUpload.java index 09ecbc9370..9e28910def 100644 --- a/app/src/main/java/info/nightscout/utils/NSUpload.java +++ b/app/src/main/java/info/nightscout/utils/NSUpload.java @@ -193,17 +193,8 @@ public class NSUpload { apsResult.json().put("timestamp", DateUtil.toISOString(lastRun.lastAPSRun)); deviceStatus.suggested = apsResult.json(); - if (lastRun.request instanceof DetermineBasalResultMA) { - DetermineBasalResultMA result = (DetermineBasalResultMA) lastRun.request; - deviceStatus.iob = result.iob.json(); - deviceStatus.iob.put("time", DateUtil.toISOString(lastRun.lastAPSRun)); - } - - if (lastRun.request instanceof DetermineBasalResultAMA) { - DetermineBasalResultAMA result = (DetermineBasalResultAMA) lastRun.request; - deviceStatus.iob = result.iob.json(); - deviceStatus.iob.put("time", DateUtil.toISOString(lastRun.lastAPSRun)); - } + deviceStatus.iob = lastRun.request.iob.json(); + deviceStatus.iob.put("time", DateUtil.toISOString(lastRun.lastAPSRun)); if (lastRun.setByPump != null && lastRun.setByPump.enacted) { // enacted deviceStatus.enacted = lastRun.request.json();