Clean up parsing of SMB result and applying the requests.

This commit is contained in:
Johannes Mockenhaupt 2018-01-27 06:36:04 +01:00
parent 0655e4e676
commit 6fa1badcdb
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
11 changed files with 113 additions and 124 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
*
* @param request
* @return true if command is going to be executed
* false if error
*/
public boolean applyAPSRequest(APSResult request, Callback callback) {
public void applyAPSRequest(APSResult request, Callback callback) {
PumpInterface pump = getActivePump();
request.rate = applyBasalConstraints(request.rate);
@ -372,7 +367,7 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr
if (callback != null) {
callback.result(new PumpEnactResult().comment(MainApp.sResources.getString(R.string.pumpNotInitialized)).enacted(false).success(false)).run();
}
return false;
return;
}
if (pump.isSuspended()) {
@ -380,46 +375,41 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr
if (callback != null) {
callback.result(new PumpEnactResult().comment(MainApp.sResources.getString(R.string.pumpsuspended)).enacted(false).success(false)).run();
}
return false;
return;
}
if (Config.logCongigBuilderActions)
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.tbrRequested) {
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)
log.debug("applyAPSRequest: cancelTempBasal()");
getCommandQueue().cancelTempBasal(false, callback);
if (request.smb == 0)
return true;
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();
}
} 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();
}
if (request.smb == 0)
return false;
log.debug("applyAPSRequest: setTempBasalAbsolute()");
getCommandQueue().tempBasalAbsolute(request.rate, request.duration, false, callback);
}
} 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();
if (lastBolusTime != 0 && lastBolusTime + 3 * 60 * 1000 > System.currentTimeMillis()) {
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.source = Source.USER;
detailedBolusInfo.deliverAt = request.deliverAt;
boolean smbDelivered = getCommandQueue().bolus(detailedBolusInfo, callback);
if (smbDelivered)
return true;
else
return false;
getCommandQueue().bolus(detailedBolusInfo, callback);
}
}
return true;
}
/**
* Constraints interface
**/

View file

@ -31,7 +31,8 @@ public class APSResult {
public String reason;
public double rate;
public int duration;
public boolean changeRequested = false;
public boolean tbrRequested = false;
public boolean bolusRequested = false;
public IobTotal iob;
public JSONObject json = new JSONObject();
public boolean hasPredictions = false;
@ -41,7 +42,7 @@ public class APSResult {
@Override
public String toString() {
final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
if (changeRequested) {
if (isChangeRequested()) {
String ret;
// rate
if (rate == 0 && duration == 0)
@ -66,7 +67,7 @@ public class APSResult {
public Spanned toSpanned() {
final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
if (changeRequested) {
if (isChangeRequested()) {
String ret;
// rate
if (rate == 0 && duration == 0)
@ -94,10 +95,11 @@ public class APSResult {
public APSResult clone() {
APSResult newResult = new APSResult();
newResult.reason = new String(reason);
newResult.reason = reason;
newResult.rate = rate;
newResult.duration = duration;
newResult.changeRequested = changeRequested;
newResult.tbrRequested = tbrRequested;
newResult.bolusRequested = bolusRequested;
newResult.iob = iob;
return newResult;
}
@ -105,7 +107,7 @@ public class APSResult {
public JSONObject json() {
JSONObject json = new JSONObject();
try {
if (changeRequested) {
if (isChangeRequested()) {
json.put("rate", rate);
json.put("duration", duration);
json.put("reason", reason);
@ -213,4 +215,7 @@ public class APSResult {
return latest;
}
public boolean isChangeRequested() {
return tbrRequested || bolusRequested;
}
}

View file

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

View file

@ -22,7 +22,7 @@ public class DetermineBasalResultAMA extends APSResult {
json = j;
if (result.containsKey("error")) {
reason = result.get("error").toString();
changeRequested = false;
tbrRequested = false;
rate = -1;
duration = -1;
} else {
@ -32,19 +32,20 @@ public class DetermineBasalResultAMA extends APSResult {
if (result.containsKey("rate")) {
rate = (Double) result.get("rate");
if (rate < 0d) rate = 0d;
changeRequested = true;
tbrRequested = true;
} else {
rate = -1;
changeRequested = false;
tbrRequested = false;
}
if (result.containsKey("duration")) {
duration = ((Double) result.get("duration")).intValue();
//changeRequested as above
} else {
duration = -1;
changeRequested = false;
tbrRequested = false;
}
}
bolusRequested = false;
}
public DetermineBasalResultAMA() {
@ -54,13 +55,12 @@ public class DetermineBasalResultAMA extends APSResult {
@Override
public DetermineBasalResultAMA clone() {
DetermineBasalResultAMA newResult = new DetermineBasalResultAMA();
newResult.reason = new String(reason);
newResult.reason = reason;
newResult.rate = rate;
newResult.duration = duration;
newResult.changeRequested = changeRequested;
newResult.tbrRequested = tbrRequested;
newResult.rate = rate;
newResult.duration = duration;
newResult.changeRequested = changeRequested;
try {
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.Round;
import info.nightscout.utils.SP;
import info.nightscout.utils.SafeParse;
import info.nightscout.utils.ToastUtils;
/**
@ -244,15 +243,15 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface {
Profiler.log(log, "AMA calculation", start);
// Fix bug determine basal
if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress())
determineBasalResultAMA.changeRequested = false;
determineBasalResultAMA.tbrRequested = false;
// limit requests on openloop mode
if (!MainApp.getConfigBuilder().isClosedModeEnabled()) {
if (MainApp.getConfigBuilder().isTempBasalInProgress() && determineBasalResultAMA.rate == 0 && determineBasalResultAMA.duration == 0) {
// going to cancel
} else if (MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultAMA.rate - MainApp.getConfigBuilder().getTempBasalAbsoluteRateHistory()) < 0.1) {
determineBasalResultAMA.changeRequested = false;
determineBasalResultAMA.tbrRequested = false;
} else if (!MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultAMA.rate - ConfigBuilderPlugin.getActivePump().getBaseBasalRate()) < 0.1)
determineBasalResultAMA.changeRequested = false;
determineBasalResultAMA.tbrRequested = false;
}
determineBasalResultAMA.iob = iobArray[0];

View file

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

View file

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

View file

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

View file

@ -246,17 +246,18 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface {
DetermineBasalResultSMB determineBasalResultSMB = determineBasalAdapterSMBJS.invoke();
Profiler.log(log, "SMB calculation", start);
// TODO still needed with oref1?
// Fix bug determine basal
if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !MainApp.getConfigBuilder().isTempBasalInProgress())
determineBasalResultSMB.changeRequested = false;
determineBasalResultSMB.tbrRequested = false;
// limit requests on openloop mode
if (!MainApp.getConfigBuilder().isClosedModeEnabled()) {
if (MainApp.getConfigBuilder().isTempBasalInProgress() && determineBasalResultSMB.rate == 0 && determineBasalResultSMB.duration == 0) {
// going to cancel
} else if (MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultSMB.rate - MainApp.getConfigBuilder().getTempBasalAbsoluteRateHistory()) < 0.1) {
determineBasalResultSMB.changeRequested = false;
determineBasalResultSMB.tbrRequested = false;
} else if (!MainApp.getConfigBuilder().isTempBasalInProgress() && Math.abs(determineBasalResultSMB.rate - pump.getBaseBasalRate()) < 0.1) {
determineBasalResultSMB.changeRequested = false;
determineBasalResultSMB.tbrRequested = false;
}
}
@ -278,11 +279,11 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface {
}
// 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));
}
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;
if (newvalue < lowLimit || newvalue > highLimit) {
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.NSClientInternal.broadcasts.BroadcastAckAlarm;
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.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog;
@ -674,7 +673,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (ConfigBuilderPlugin.getActiveLoop() != null) {
ConfigBuilderPlugin.getActiveLoop().invoke("Accept temp button", false);
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());
builder.setTitle(getContext().getString(R.string.confirmation));
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
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.constraintsProcessed.changeRequested; // change is requested
showAcceptButton = showAcceptButton && finalLastRun.constraintsProcessed.isChangeRequested(); // change is requested
if (showAcceptButton && pump.isInitialized() && !pump.isSuspended() && ConfigBuilderPlugin.getActiveLoop() != null) {
acceptTempLayout.setVisibility(View.VISIBLE);

View file

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