Merge branch 'smb-apsresult' into 'smb060'

Clean up parsing OpenAPS result and applying it

See merge request MilosKozak/AndroidAPS!383
This commit is contained in:
Milos Kozak 2018-01-29 10:46:40 +00:00
commit e55f88a255
12 changed files with 145 additions and 136 deletions

View file

@ -357,13 +357,8 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr
/** /**
* expect absolute request and allow both absolute and percent response based on pump capabilities * expect absolute request and allow both absolute and percent response based on pump capabilities
*
* @param request
* @return true if command is going to be executed
* false if error
*/ */
public void applyAPSRequest(APSResult request, Callback callback) {
public boolean applyAPSRequest(APSResult request, Callback callback) {
PumpInterface pump = getActivePump(); PumpInterface pump = getActivePump();
request.rate = applyBasalConstraints(request.rate); request.rate = applyBasalConstraints(request.rate);
@ -372,7 +367,7 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr
if (callback != null) { if (callback != null) {
callback.result(new PumpEnactResult().comment(MainApp.sResources.getString(R.string.pumpNotInitialized)).enacted(false).success(false)).run(); callback.result(new PumpEnactResult().comment(MainApp.sResources.getString(R.string.pumpNotInitialized)).enacted(false).success(false)).run();
} }
return false; return;
} }
if (pump.isSuspended()) { if (pump.isSuspended()) {
@ -380,46 +375,41 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr
if (callback != null) { if (callback != null) {
callback.result(new PumpEnactResult().comment(MainApp.sResources.getString(R.string.pumpsuspended)).enacted(false).success(false)).run(); callback.result(new PumpEnactResult().comment(MainApp.sResources.getString(R.string.pumpsuspended)).enacted(false).success(false)).run();
} }
return false; return;
} }
if (Config.logCongigBuilderActions) if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: " + request.toString()); log.debug("applyAPSRequest: " + request.toString());
if ((request.rate == 0 && request.duration == 0) || Math.abs(request.rate - pump.getBaseBasalRate()) < pump.getPumpDescription().basalStep) {
if (isTempBasalInProgress()) { if (request.tempBasalReqested) {
if ((request.rate == 0 && request.duration == 0) || Math.abs(request.rate - pump.getBaseBasalRate()) < pump.getPumpDescription().basalStep) {
if (isTempBasalInProgress()) {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: cancelTempBasal()");
getCommandQueue().cancelTempBasal(false, callback);
} else {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: Basal set correctly");
if (callback != null) {
callback.result(new PumpEnactResult().absolute(request.rate).duration(0).enacted(false).success(true).comment("Basal set correctly")).run();
}
}
} else if (isTempBasalInProgress()
&& getTempBasalRemainingMinutesFromHistory() > 5
&& Math.abs(request.rate - getTempBasalAbsoluteRateHistory()) < pump.getPumpDescription().basalStep) {
if (Config.logCongigBuilderActions) if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: cancelTempBasal()"); log.debug("applyAPSRequest: Temp basal set correctly");
getCommandQueue().cancelTempBasal(false, callback); if (callback != null) {
if (request.smb == 0) callback.result(new PumpEnactResult().absolute(getTempBasalAbsoluteRateHistory()).duration(getTempBasalFromHistory(System.currentTimeMillis()).getPlannedRemainingMinutes()).enacted(false).success(true).comment("Temp basal set correctly")).run();
return true; }
} else { } else {
if (Config.logCongigBuilderActions) if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: Basal set correctly"); log.debug("applyAPSRequest: setTempBasalAbsolute()");
if (callback != null) { getCommandQueue().tempBasalAbsolute(request.rate, request.duration, false, callback);
callback.result(new PumpEnactResult().absolute(request.rate).duration(0).enacted(false).success(true).comment("Basal set correctly")).run();
}
if (request.smb == 0)
return false;
} }
} else if (isTempBasalInProgress()
&& getTempBasalRemainingMinutesFromHistory() > 5
&& Math.abs(request.rate - getTempBasalAbsoluteRateHistory()) < pump.getPumpDescription().basalStep) {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: Temp basal set correctly");
if (callback != null) {
callback.result(new PumpEnactResult().absolute(getTempBasalAbsoluteRateHistory()).duration(getTempBasalFromHistory(System.currentTimeMillis()).getPlannedRemainingMinutes()).enacted(false).success(true).comment("Temp basal set correctly")).run();
}
if (request.smb == 0)
return false;
} else {
if (Config.logCongigBuilderActions)
log.debug("applyAPSRequest: setTempBasalAbsolute()");
getCommandQueue().tempBasalAbsolute(request.rate, request.duration, false, callback);
if (request.smb == 0) return true;
} }
log.debug("SMB requested in config is: " + request.smb);
if (request.smb != 0) { if (request.bolusRequested) {
long lastBolusTime = getLastBolusTime(); long lastBolusTime = getLastBolusTime();
if (lastBolusTime != 0 && lastBolusTime + 3 * 60 * 1000 > System.currentTimeMillis()) { if (lastBolusTime != 0 && lastBolusTime + 3 * 60 * 1000 > System.currentTimeMillis()) {
log.debug("SMB requsted but still in 3 min interval"); log.debug("SMB requsted but still in 3 min interval");
@ -430,17 +420,11 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr
detailedBolusInfo.isSMB = true; detailedBolusInfo.isSMB = true;
detailedBolusInfo.source = Source.USER; detailedBolusInfo.source = Source.USER;
detailedBolusInfo.deliverAt = request.deliverAt; detailedBolusInfo.deliverAt = request.deliverAt;
boolean smbDelivered = getCommandQueue().bolus(detailedBolusInfo, callback); getCommandQueue().bolus(detailedBolusInfo, callback);
if (smbDelivered)
return true;
else
return false;
} }
} }
return true;
} }
/** /**
* Constraints interface * Constraints interface
**/ **/

View file

@ -31,7 +31,8 @@ public class APSResult {
public String reason; public String reason;
public double rate; public double rate;
public int duration; public int duration;
public boolean changeRequested = false; public boolean tempBasalReqested = false;
public boolean bolusRequested = false;
public IobTotal iob; public IobTotal iob;
public JSONObject json = new JSONObject(); public JSONObject json = new JSONObject();
public boolean hasPredictions = false; public boolean hasPredictions = false;
@ -41,30 +42,49 @@ public class APSResult {
@Override @Override
public String toString() { public String toString() {
final PumpInterface pump = ConfigBuilderPlugin.getActivePump(); final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
if (changeRequested) { if (isChangeRequested()) {
String ret;
// rate
if (rate == 0 && duration == 0) if (rate == 0 && duration == 0)
return MainApp.sResources.getString(R.string.canceltemp); ret = MainApp.sResources.getString(R.string.canceltemp) + "\n";
else if (rate == -1)
ret = MainApp.sResources.getString(R.string.let_temp_basal_run) + "\n";
else else
return MainApp.sResources.getString(R.string.rate) + ": " + DecimalFormatter.to2Decimal(rate) + " U/h " + ret = MainApp.sResources.getString(R.string.rate) + ": " + DecimalFormatter.to2Decimal(rate) + " U/h " +
"(" + DecimalFormatter.to2Decimal(rate / pump.getBaseBasalRate() * 100) + "%)\n" + "(" + DecimalFormatter.to2Decimal(rate / pump.getBaseBasalRate() * 100) + "%) \n" +
MainApp.sResources.getString(R.string.duration) + ": " + DecimalFormatter.to0Decimal(duration) + " min\n" + MainApp.sResources.getString(R.string.duration) + ": " + DecimalFormatter.to2Decimal(duration) + " min\n";
(smb != 0 ? ("SMB: " + DecimalFormatter.to2Decimal(smb) + " U\n") : "") +
MainApp.sResources.getString(R.string.reason) + ": " + reason; // smb
if (smb != 0)
ret += ("SMB: " + DecimalFormatter.to2Decimal(smb) + " U\n");
// reason
ret += MainApp.sResources.getString(R.string.reason) + ": " + reason;
return ret;
} else } else
return MainApp.sResources.getString(R.string.nochangerequested); return MainApp.sResources.getString(R.string.nochangerequested);
} }
public Spanned toSpanned() { public Spanned toSpanned() {
final PumpInterface pump = ConfigBuilderPlugin.getActivePump(); final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
if (changeRequested) { if (isChangeRequested()) {
String ret = ""; String ret;
if (rate == 0 && duration == 0) ret = MainApp.sResources.getString(R.string.canceltemp); // rate
if (rate == 0 && duration == 0)
ret = MainApp.sResources.getString(R.string.canceltemp) + "<br>";
else if (rate == -1)
ret = MainApp.sResources.getString(R.string.let_temp_basal_run) + "<br>";
else else
ret = "<b>" + MainApp.sResources.getString(R.string.rate) + "</b>: " + DecimalFormatter.to2Decimal(rate) + " U/h " + ret = "<b>" + MainApp.sResources.getString(R.string.rate) + "</b>: " + DecimalFormatter.to2Decimal(rate) + " U/h " +
"(" + DecimalFormatter.to2Decimal(rate / pump.getBaseBasalRate() * 100) + "%) <br>" + "(" + DecimalFormatter.to2Decimal(rate / pump.getBaseBasalRate() * 100) + "%) <br>" +
"<b>" + MainApp.sResources.getString(R.string.duration) + "</b>: " + DecimalFormatter.to2Decimal(duration) + " min<br>" + "<b>" + MainApp.sResources.getString(R.string.duration) + "</b>: " + DecimalFormatter.to2Decimal(duration) + " min<br>";
(smb != 0 ? ("<b>" + "SMB" + "</b>: " + DecimalFormatter.to2Decimal(smb) + " U<br>") : "") +
"<b>" + MainApp.sResources.getString(R.string.reason) + "</b>: " + reason.replace("<", "&lt;").replace(">", "&gt;"); // smb
if (smb != 0)
ret += ("<b>" + "SMB" + "</b>: " + DecimalFormatter.to2Decimal(smb) + " U<br>");
// reason
ret += "<b>" + MainApp.sResources.getString(R.string.reason) + "</b>: " + reason.replace("<", "&lt;").replace(">", "&gt;");
return Html.fromHtml(ret); return Html.fromHtml(ret);
} else } else
return Html.fromHtml(MainApp.sResources.getString(R.string.nochangerequested)); return Html.fromHtml(MainApp.sResources.getString(R.string.nochangerequested));
@ -75,10 +95,11 @@ public class APSResult {
public APSResult clone() { public APSResult clone() {
APSResult newResult = new APSResult(); APSResult newResult = new APSResult();
newResult.reason = new String(reason); newResult.reason = reason;
newResult.rate = rate; newResult.rate = rate;
newResult.duration = duration; newResult.duration = duration;
newResult.changeRequested = changeRequested; newResult.tempBasalReqested = tempBasalReqested;
newResult.bolusRequested = bolusRequested;
newResult.iob = iob; newResult.iob = iob;
return newResult; return newResult;
} }
@ -86,7 +107,7 @@ public class APSResult {
public JSONObject json() { public JSONObject json() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
try { try {
if (changeRequested) { if (isChangeRequested()) {
json.put("rate", rate); json.put("rate", rate);
json.put("duration", duration); json.put("duration", duration);
json.put("reason", reason); json.put("reason", reason);
@ -194,4 +215,7 @@ public class APSResult {
return latest; return latest;
} }
public boolean isChangeRequested() {
return tempBasalReqested || bolusRequested;
}
} }

View file

@ -315,7 +315,7 @@ public class LoopPlugin implements PluginBase {
} }
if (constraintsInterface.isClosedModeEnabled()) { if (constraintsInterface.isClosedModeEnabled()) {
if (result.changeRequested) { if (result.isChangeRequested()) {
final PumpEnactResult waiting = new PumpEnactResult(); final PumpEnactResult waiting = new PumpEnactResult();
final PumpEnactResult previousResult = lastRun.setByPump; final PumpEnactResult previousResult = lastRun.setByPump;
waiting.queued = true; waiting.queued = true;
@ -339,7 +339,7 @@ public class LoopPlugin implements PluginBase {
lastRun.source = null; lastRun.source = null;
} }
} else { } else {
if (result.changeRequested && allowNotification) { if (result.isChangeRequested() && allowNotification) {
NotificationCompat.Builder builder = NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainApp.instance().getApplicationContext()); new NotificationCompat.Builder(MainApp.instance().getApplicationContext());
builder.setSmallIcon(R.drawable.notif_icon) builder.setSmallIcon(R.drawable.notif_icon)

View file

@ -22,7 +22,7 @@ public class DetermineBasalResultAMA extends APSResult {
json = j; json = j;
if (result.containsKey("error")) { if (result.containsKey("error")) {
reason = result.get("error").toString(); reason = result.get("error").toString();
changeRequested = false; tempBasalReqested = false;
rate = -1; rate = -1;
duration = -1; duration = -1;
} else { } else {
@ -32,19 +32,20 @@ public class DetermineBasalResultAMA extends APSResult {
if (result.containsKey("rate")) { if (result.containsKey("rate")) {
rate = (Double) result.get("rate"); rate = (Double) result.get("rate");
if (rate < 0d) rate = 0d; if (rate < 0d) rate = 0d;
changeRequested = true; tempBasalReqested = true;
} else { } else {
rate = -1; rate = -1;
changeRequested = false; tempBasalReqested = false;
} }
if (result.containsKey("duration")) { if (result.containsKey("duration")) {
duration = ((Double) result.get("duration")).intValue(); duration = ((Double) result.get("duration")).intValue();
//changeRequested as above //changeRequested as above
} else { } else {
duration = -1; duration = -1;
changeRequested = false; tempBasalReqested = false;
} }
} }
bolusRequested = false;
} }
public DetermineBasalResultAMA() { public DetermineBasalResultAMA() {
@ -54,13 +55,12 @@ public class DetermineBasalResultAMA extends APSResult {
@Override @Override
public DetermineBasalResultAMA clone() { public DetermineBasalResultAMA clone() {
DetermineBasalResultAMA newResult = new DetermineBasalResultAMA(); DetermineBasalResultAMA newResult = new DetermineBasalResultAMA();
newResult.reason = new String(reason); newResult.reason = reason;
newResult.rate = rate; newResult.rate = rate;
newResult.duration = duration; newResult.duration = duration;
newResult.changeRequested = changeRequested; newResult.tempBasalReqested = tempBasalReqested;
newResult.rate = rate; newResult.rate = rate;
newResult.duration = duration; newResult.duration = duration;
newResult.changeRequested = changeRequested;
try { try {
newResult.json = new JSONObject(json.toString()); newResult.json = new JSONObject(json.toString());

View file

@ -30,7 +30,6 @@ import info.nightscout.utils.NSUpload;
import info.nightscout.utils.Profiler; import info.nightscout.utils.Profiler;
import info.nightscout.utils.Round; import info.nightscout.utils.Round;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
import info.nightscout.utils.SafeParse;
import info.nightscout.utils.ToastUtils; import info.nightscout.utils.ToastUtils;
/** /**
@ -244,15 +243,15 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface {
Profiler.log(log, "AMA calculation", start); Profiler.log(log, "AMA calculation", start);
// Fix bug determine basal // Fix bug determine basal
if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress())
determineBasalResultAMA.changeRequested = false; determineBasalResultAMA.tempBasalReqested = false;
// limit requests on openloop mode // limit requests on openloop mode
if (!MainApp.getConfigBuilder().isClosedModeEnabled()) { if (!MainApp.getConfigBuilder().isClosedModeEnabled()) {
if (MainApp.getConfigBuilder().isTempBasalInProgress() && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) { if (MainApp.getConfigBuilder().isTempBasalInProgress() && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) {
// going to cancel // going to cancel
} else if (MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultAMA.rate - MainApp.getConfigBuilder().getTempBasalAbsoluteRateHistory()) < 0.1) { } else if (MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultAMA.rate - MainApp.getConfigBuilder().getTempBasalAbsoluteRateHistory()) < 0.1) {
determineBasalResultAMA.changeRequested = false; determineBasalResultAMA.tempBasalReqested = false;
} else if (!MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultAMA.rate - ConfigBuilderPlugin.getActivePump().getBaseBasalRate()) < 0.1) } else if (!MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultAMA.rate - ConfigBuilderPlugin.getActivePump().getBaseBasalRate()) < 0.1)
determineBasalResultAMA.changeRequested = false; determineBasalResultAMA.tempBasalReqested = false;
} }
determineBasalResultAMA.iob = iobArray[0]; determineBasalResultAMA.iob = iobArray[0];

View file

@ -6,7 +6,6 @@ import org.mozilla.javascript.NativeObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.APSResult;
public class DetermineBasalResultMA extends APSResult { public class DetermineBasalResultMA extends APSResult {
@ -21,7 +20,7 @@ public class DetermineBasalResultMA extends APSResult {
json = j; json = j;
if (result.containsKey("error")) { if (result.containsKey("error")) {
reason = (String) result.get("error"); reason = (String) result.get("error");
changeRequested = false; tempBasalReqested = false;
rate = -1; rate = -1;
duration = -1; duration = -1;
mealAssist = ""; mealAssist = "";
@ -32,17 +31,17 @@ public class DetermineBasalResultMA extends APSResult {
if (result.containsKey("rate")) { if (result.containsKey("rate")) {
rate = (Double) result.get("rate"); rate = (Double) result.get("rate");
if (rate < 0d) rate = 0d; if (rate < 0d) rate = 0d;
changeRequested = true; tempBasalReqested = true;
} else { } else {
rate = -1; rate = -1;
changeRequested = false; tempBasalReqested = false;
} }
if (result.containsKey("duration")) { if (result.containsKey("duration")) {
duration = ((Double) result.get("duration")).intValue(); duration = ((Double) result.get("duration")).intValue();
//changeRequested as above //changeRequested as above
} else { } else {
duration = -1; duration = -1;
changeRequested = false; tempBasalReqested = false;
} }
if (result.containsKey("mealAssist")) { if (result.containsKey("mealAssist")) {
mealAssist = result.get("mealAssist").toString(); mealAssist = result.get("mealAssist").toString();
@ -59,10 +58,10 @@ public class DetermineBasalResultMA extends APSResult {
newResult.reason = new String(reason); newResult.reason = new String(reason);
newResult.rate = rate; newResult.rate = rate;
newResult.duration = duration; newResult.duration = duration;
newResult.changeRequested = changeRequested; newResult.tempBasalReqested = isChangeRequested();
newResult.rate = rate; newResult.rate = rate;
newResult.duration = duration; newResult.duration = duration;
newResult.changeRequested = changeRequested; newResult.tempBasalReqested = isChangeRequested();
try { try {
newResult.json = new JSONObject(json.toString()); newResult.json = new JSONObject(json.toString());
@ -71,7 +70,7 @@ public class DetermineBasalResultMA extends APSResult {
} }
newResult.eventualBG = eventualBG; newResult.eventualBG = eventualBG;
newResult.snoozeBG = snoozeBG; newResult.snoozeBG = snoozeBG;
newResult.mealAssist = new String(mealAssist); newResult.mealAssist = mealAssist;
return newResult; return newResult;
} }

View file

@ -229,15 +229,15 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface {
DetermineBasalResultMA determineBasalResultMA = determineBasalAdapterMAJS.invoke(); DetermineBasalResultMA determineBasalResultMA = determineBasalAdapterMAJS.invoke();
// Fix bug determinef basal // Fix bug determinef basal
if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) if (determineBasalResultMA.rate == 0d && determineBasalResultMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress())
determineBasalResultMA.changeRequested = false; determineBasalResultMA.tempBasalReqested = false;
// limit requests on openloop mode // limit requests on openloop mode
if (!MainApp.getConfigBuilder().isClosedModeEnabled()) { if (!MainApp.getConfigBuilder().isClosedModeEnabled()) {
if (MainApp.getConfigBuilder().isTempBasalInProgress() && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) { if (MainApp.getConfigBuilder().isTempBasalInProgress() && determineBasalResultMA.rate == 0 && determineBasalResultMA.duration == 0) {
// going to cancel // going to cancel
} else if (MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultMA.rate - MainApp.getConfigBuilder().getTempBasalAbsoluteRateHistory()) < 0.1) { } else if (MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultMA.rate - MainApp.getConfigBuilder().getTempBasalAbsoluteRateHistory()) < 0.1) {
determineBasalResultMA.changeRequested = false; determineBasalResultMA.tempBasalReqested = false;
} else if (!MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultMA.rate - ConfigBuilderPlugin.getActivePump().getBaseBasalRate()) < 0.1) } else if (!MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultMA.rate - ConfigBuilderPlugin.getActivePump().getBaseBasalRate()) < 0.1)
determineBasalResultMA.changeRequested = false; determineBasalResultMA.tempBasalReqested = false;
} }
determineBasalResultMA.iob = iobTotal; determineBasalResultMA.iob = iobTotal;

View file

@ -2,6 +2,8 @@ package info.nightscout.androidaps.plugins.OpenAPSSMB;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date; import java.util.Date;
@ -9,8 +11,12 @@ import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
public class DetermineBasalResultSMB extends APSResult { public class DetermineBasalResultSMB extends APSResult {
private static final Logger log = LoggerFactory.getLogger(DetermineBasalResultSMB.class);
public double eventualBG; public double eventualBG;
public double snoozeBG; public double snoozeBG;
public double insulinReq;
public double carbsReq;
public DetermineBasalResultSMB(JSONObject result) { public DetermineBasalResultSMB(JSONObject result) {
this(); this();
@ -19,46 +25,42 @@ public class DetermineBasalResultSMB extends APSResult {
try { try {
if (result.has("error")) { if (result.has("error")) {
reason = result.getString("error"); reason = result.getString("error");
changeRequested = false; return;
}
reason = result.getString("reason");
if (result.has("eventualBG")) eventualBG = result.getDouble("eventualBG");
if (result.has("snoozeBG")) snoozeBG = result.getDouble("snoozeBG");
if (result.has("insulinReq")) insulinReq = result.getDouble("insulinReq");
if (result.has("carbsReq")) carbsReq = result.getDouble("carbsReq");
if (result.has("rate") && result.has("duration")) {
tempBasalReqested = true;
rate = result.getDouble("rate");
if (rate < 0d) rate = 0d;
duration = result.getInt("duration");
} else {
rate = -1; rate = -1;
duration = -1; duration = -1;
}
if (result.has("units")) {
bolusRequested = true;
smb = result.getDouble("units");
} else { } else {
reason = result.getString("reason"); smb = 0d;
if (result.has("eventualBG")) eventualBG = result.getDouble("eventualBG"); }
if (result.has("snoozeBG")) snoozeBG = result.getDouble("snoozeBG");
if (result.has("rate")) { if (result.has("deliverAt")) {
rate = result.getDouble("rate"); String date = result.getString("deliverAt");
if (rate < 0d) rate = 0d; try {
changeRequested = true; deliverAt = DateUtil.fromISODateString(date).getTime();
} else { } catch (Exception e) {
rate = -1; log.warn("Error parsing 'deliverAt' date: " + date, e);
changeRequested = false;
}
if (result.has("duration")) {
duration = result.getInt("duration");
//changeRequested as above
} else {
duration = -1;
changeRequested = false;
}
if (result.has("units")) {
changeRequested = true;
smb = result.getDouble("units");
} else {
smb = 0d;
//changeRequested as above
}
if (result.has("deliverAt")) {
String date = result.getString("deliverAt");
try {
deliverAt = DateUtil.fromISODateString(date).getTime();
} catch (Exception e) {
e.printStackTrace();
}
} }
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); log.error("Error parsing determine-basal result JSON", e);
} }
} }
@ -69,10 +71,11 @@ public class DetermineBasalResultSMB extends APSResult {
@Override @Override
public DetermineBasalResultSMB clone() { public DetermineBasalResultSMB clone() {
DetermineBasalResultSMB newResult = new DetermineBasalResultSMB(); DetermineBasalResultSMB newResult = new DetermineBasalResultSMB();
newResult.reason = new String(reason); newResult.reason = reason;
newResult.rate = rate; newResult.rate = rate;
newResult.duration = duration; newResult.duration = duration;
newResult.changeRequested = changeRequested; newResult.tempBasalReqested = tempBasalReqested;
newResult.bolusRequested = bolusRequested;
newResult.rate = rate; newResult.rate = rate;
newResult.duration = duration; newResult.duration = duration;
newResult.smb = smb; newResult.smb = smb;
@ -81,7 +84,7 @@ public class DetermineBasalResultSMB extends APSResult {
try { try {
newResult.json = new JSONObject(json.toString()); newResult.json = new JSONObject(json.toString());
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); log.error("Error clone parsing determine-basal result", e);
} }
newResult.eventualBG = eventualBG; newResult.eventualBG = eventualBG;
newResult.snoozeBG = snoozeBG; newResult.snoozeBG = snoozeBG;
@ -92,10 +95,9 @@ public class DetermineBasalResultSMB extends APSResult {
@Override @Override
public JSONObject json() { public JSONObject json() {
try { try {
JSONObject ret = new JSONObject(this.json.toString()); return new JSONObject(this.json.toString());
return ret;
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); log.error("Error converting determine-basal result to JSON", e);
} }
return null; return null;
} }

View file

@ -246,17 +246,18 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface {
DetermineBasalResultSMB determineBasalResultSMB = determineBasalAdapterSMBJS.invoke(); DetermineBasalResultSMB determineBasalResultSMB = determineBasalAdapterSMBJS.invoke();
Profiler.log(log, "SMB calculation", start); Profiler.log(log, "SMB calculation", start);
// TODO still needed with oref1?
// Fix bug determine basal // Fix bug determine basal
if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress()) if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress())
determineBasalResultSMB.changeRequested = false; determineBasalResultSMB.tempBasalReqested = false;
// limit requests on openloop mode // limit requests on openloop mode
if (!MainApp.getConfigBuilder().isClosedModeEnabled()) { if (!MainApp.getConfigBuilder().isClosedModeEnabled()) {
if (MainApp.getConfigBuilder().isTempBasalInProgress() && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) { if (MainApp.getConfigBuilder().isTempBasalInProgress() && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) {
// going to cancel // going to cancel
} else if (MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultSMB.rate - MainApp.getConfigBuilder().getTempBasalAbsoluteRateHistory()) < 0.1) { } else if (MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultSMB.rate - MainApp.getConfigBuilder().getTempBasalAbsoluteRateHistory()) < 0.1) {
determineBasalResultSMB.changeRequested = false; determineBasalResultSMB.tempBasalReqested = false;
} else if (!MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultSMB.rate - pump.getBaseBasalRate()) < 0.1) { } else if (!MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultSMB.rate - pump.getBaseBasalRate()) < 0.1) {
determineBasalResultSMB.changeRequested = false; determineBasalResultSMB.tempBasalReqested = false;
} }
} }
@ -278,11 +279,11 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface {
} }
// safety checks // safety checks
public static boolean checkOnlyHardLimits(Double value, String valueName, double lowLimit, double highLimit) { private static boolean checkOnlyHardLimits(Double value, String valueName, double lowLimit, double highLimit) {
return value.equals(verifyHardLimits(value, valueName, lowLimit, highLimit)); return value.equals(verifyHardLimits(value, valueName, lowLimit, highLimit));
} }
public static Double verifyHardLimits(Double value, String valueName, double lowLimit, double highLimit) { private static Double verifyHardLimits(Double value, String valueName, double lowLimit, double highLimit) {
Double newvalue = value; Double newvalue = value;
if (newvalue < lowLimit || newvalue > highLimit) { if (newvalue < lowLimit || newvalue > highLimit) {
newvalue = Math.max(newvalue, lowLimit); newvalue = Math.max(newvalue, lowLimit);

View file

@ -93,7 +93,6 @@ import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification; import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification;
import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastAckAlarm; import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastAckAlarm;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus;
import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA;
import info.nightscout.androidaps.plugins.Overview.Dialogs.CalibrationDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.CalibrationDialog;
import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity; import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog;
@ -674,7 +673,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (ConfigBuilderPlugin.getActiveLoop() != null) { if (ConfigBuilderPlugin.getActiveLoop() != null) {
ConfigBuilderPlugin.getActiveLoop().invoke("Accept temp button", false); ConfigBuilderPlugin.getActiveLoop().invoke("Accept temp button", false);
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun; final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
if (finalLastRun != null && finalLastRun.lastAPSRun != null && finalLastRun.constraintsProcessed.changeRequested) { if (finalLastRun != null && finalLastRun.lastAPSRun != null && finalLastRun.constraintsProcessed.isChangeRequested()) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getContext().getString(R.string.confirmation)); builder.setTitle(getContext().getString(R.string.confirmation));
builder.setMessage(getContext().getString(R.string.setbasalquestion) + "\n" + finalLastRun.constraintsProcessed); builder.setMessage(getContext().getString(R.string.setbasalquestion) + "\n" + finalLastRun.constraintsProcessed);
@ -1105,7 +1104,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
boolean showAcceptButton = !MainApp.getConfigBuilder().isClosedModeEnabled(); // Open mode needed boolean showAcceptButton = !MainApp.getConfigBuilder().isClosedModeEnabled(); // Open mode needed
showAcceptButton = showAcceptButton && finalLastRun != null && finalLastRun.lastAPSRun != null; // aps result must exist showAcceptButton = showAcceptButton && finalLastRun != null && finalLastRun.lastAPSRun != null; // aps result must exist
showAcceptButton = showAcceptButton && (finalLastRun.lastOpenModeAccept == null || finalLastRun.lastOpenModeAccept.getTime() < finalLastRun.lastAPSRun.getTime()); // never accepted or before last result showAcceptButton = showAcceptButton && (finalLastRun.lastOpenModeAccept == null || finalLastRun.lastOpenModeAccept.getTime() < finalLastRun.lastAPSRun.getTime()); // never accepted or before last result
showAcceptButton = showAcceptButton && finalLastRun.constraintsProcessed.changeRequested; // change is requested showAcceptButton = showAcceptButton && finalLastRun.constraintsProcessed.isChangeRequested(); // change is requested
if (showAcceptButton && pump.isInitialized() && !pump.isSuspended() && ConfigBuilderPlugin.getActiveLoop() != null) { if (showAcceptButton && pump.isInitialized() && !pump.isSuspended() && ConfigBuilderPlugin.getActiveLoop() != null) {
acceptTempLayout.setVisibility(View.VISIBLE); acceptTempLayout.setVisibility(View.VISIBLE);

View file

@ -503,7 +503,7 @@ public class ActionStringHandler {
return "Last result not available!"; return "Last result not available!";
} }
if (!result.changeRequested) { if (!result.isChangeRequested()) {
ret += MainApp.sResources.getString(R.string.nochangerequested) + "\n"; ret += MainApp.sResources.getString(R.string.nochangerequested) + "\n";
} else if (result.rate == 0 && result.duration == 0) { } else if (result.rate == 0 && result.duration == 0) {
ret += MainApp.sResources.getString(R.string.canceltemp) + "\n"; ret += MainApp.sResources.getString(R.string.canceltemp) + "\n";

View file

@ -887,5 +887,6 @@
<string name="enablesmbwithtemptarget_summary">Enable SMB when there is temp target active (eating soon, exercise)</string> <string name="enablesmbwithtemptarget_summary">Enable SMB when there is temp target active (eating soon, exercise)</string>
<string name="enablesmbwithhightemptarget">Enable SMB with high temp targets</string> <string name="enablesmbwithhightemptarget">Enable SMB with high temp targets</string>
<string name="enablesmbwithhightemptarget_summary">Enable SMB when there is high temp target active (exercise)</string> <string name="enablesmbwithhightemptarget_summary">Enable SMB when there is high temp target active (exercise)</string>
<string name="let_temp_basal_run">Let current temp basal run</string>
</resources> </resources>