2016-06-11 20:45:40 +02:00
|
|
|
package info.nightscout.androidaps.data;
|
|
|
|
|
2016-06-24 17:16:17 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2017-08-20 11:17:05 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2016-06-24 17:16:17 +02:00
|
|
|
|
|
|
|
import info.nightscout.androidaps.MainApp;
|
2016-07-11 17:49:09 +02:00
|
|
|
import info.nightscout.androidaps.R;
|
2018-07-30 15:46:20 +02:00
|
|
|
import info.nightscout.androidaps.logging.L;
|
2020-01-13 19:22:28 +01:00
|
|
|
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
|
2019-03-22 23:08:13 +01:00
|
|
|
import info.nightscout.androidaps.utils.DecimalFormatter;
|
|
|
|
import info.nightscout.androidaps.utils.Round;
|
2016-06-24 17:16:17 +02:00
|
|
|
|
2018-02-02 23:59:05 +01:00
|
|
|
public class PumpEnactResult {
|
2020-01-13 19:22:28 +01:00
|
|
|
private static Logger log = StacktraceLoggerWrapper.getLogger(L.APS);
|
2017-08-20 11:17:05 +02:00
|
|
|
|
2016-06-20 20:45:55 +02:00
|
|
|
public boolean success = false; // request was processed successfully (but possible no change was needed)
|
|
|
|
public boolean enacted = false; // request was processed successfully and change has been made
|
2016-06-11 20:45:40 +02:00
|
|
|
public String comment = "";
|
|
|
|
|
2016-06-20 20:45:55 +02:00
|
|
|
// Result of basal change
|
2018-03-29 18:44:16 +02:00
|
|
|
public int duration = -1; // duration set [minutes]
|
2018-03-15 18:30:18 +01:00
|
|
|
public double absolute = -1d; // absolute rate [U/h] , isPercent = false
|
|
|
|
public int percent = -1; // percent of current basal [%] (100% = current basal), isPercent = true
|
2016-06-20 20:45:55 +02:00
|
|
|
public boolean isPercent = false; // if true percent is used, otherwise absolute
|
2016-06-23 17:07:38 +02:00
|
|
|
public boolean isTempCancel = false; // if true we are caceling temp basal
|
2016-06-20 21:43:29 +02:00
|
|
|
// Result of treatment delivery
|
2018-03-29 18:44:16 +02:00
|
|
|
public double bolusDelivered = 0d; // real value of delivered insulin
|
|
|
|
public double carbsDelivered = 0d; // real value of delivered carbs
|
2016-06-11 20:45:40 +02:00
|
|
|
|
2016-07-13 18:57:23 +02:00
|
|
|
public boolean queued = false;
|
|
|
|
|
2017-10-31 12:47:41 +01:00
|
|
|
public PumpEnactResult success(boolean success) {
|
2018-03-29 18:44:16 +02:00
|
|
|
this.success = success;
|
2017-10-31 12:47:41 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PumpEnactResult enacted(boolean enacted) {
|
|
|
|
this.enacted = enacted;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PumpEnactResult comment(String comment) {
|
|
|
|
this.comment = comment;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-18 23:17:00 +02:00
|
|
|
public PumpEnactResult comment(int comment) {
|
|
|
|
this.comment = MainApp.gs(comment);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:30:18 +01:00
|
|
|
public PumpEnactResult duration(int duration) {
|
2017-10-31 12:47:41 +01:00
|
|
|
this.duration = duration;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:30:18 +01:00
|
|
|
public PumpEnactResult absolute(double absolute) {
|
2017-10-31 12:47:41 +01:00
|
|
|
this.absolute = absolute;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:30:18 +01:00
|
|
|
public PumpEnactResult percent(int percent) {
|
2017-11-03 14:40:01 +01:00
|
|
|
this.percent = percent;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-10-31 12:47:41 +01:00
|
|
|
public PumpEnactResult isPercent(boolean isPercent) {
|
|
|
|
this.isPercent = isPercent;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PumpEnactResult isTempCancel(boolean isTempCancel) {
|
|
|
|
this.isTempCancel = isTempCancel;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:30:18 +01:00
|
|
|
public PumpEnactResult bolusDelivered(double bolusDelivered) {
|
2017-10-31 12:47:41 +01:00
|
|
|
this.bolusDelivered = bolusDelivered;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:30:18 +01:00
|
|
|
public PumpEnactResult carbsDelivered(double carbsDelivered) {
|
2017-10-31 12:47:41 +01:00
|
|
|
this.carbsDelivered = carbsDelivered;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PumpEnactResult queued(boolean queued) {
|
|
|
|
this.queued = queued;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:44:16 +02:00
|
|
|
public String log() {
|
|
|
|
return "Success: " + success +
|
|
|
|
" Enacted: " + enacted +
|
|
|
|
" Comment: " + comment +
|
|
|
|
" Duration: " + duration +
|
|
|
|
" Absolute: " + absolute +
|
|
|
|
" Percent: " + percent +
|
|
|
|
" IsPercent: " + isPercent +
|
|
|
|
" IsTempCancel: " + isTempCancel +
|
|
|
|
" bolusDelivered: " + bolusDelivered +
|
|
|
|
" carbsDelivered: " + carbsDelivered +
|
|
|
|
" Queued: " + queued;
|
2016-06-11 20:45:40 +02:00
|
|
|
}
|
2016-06-19 13:17:16 +02:00
|
|
|
|
|
|
|
public String toString() {
|
2018-03-29 18:44:16 +02:00
|
|
|
String ret = MainApp.gs(R.string.success) + ": " + success;
|
2016-06-19 20:06:00 +02:00
|
|
|
if (enacted) {
|
2018-03-15 18:30:18 +01:00
|
|
|
if (bolusDelivered > 0) {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted;
|
|
|
|
ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment;
|
2018-08-03 13:15:29 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.configbuilder_insulin)
|
2018-03-19 13:10:09 +01:00
|
|
|
+ ": " + bolusDelivered + " " + MainApp.gs(R.string.insulin_unit_shortname);
|
2018-03-15 18:30:18 +01:00
|
|
|
} else if (isTempCancel) {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted;
|
2018-03-19 11:08:58 +01:00
|
|
|
if (!comment.isEmpty())
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment;
|
|
|
|
ret += "\n" + MainApp.gs(R.string.canceltemp);
|
2016-06-23 17:07:38 +02:00
|
|
|
} else if (isPercent) {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted;
|
2018-03-19 11:08:58 +01:00
|
|
|
if (!comment.isEmpty())
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment;
|
|
|
|
ret += "\n" + MainApp.gs(R.string.duration) + ": " + duration + " min";
|
|
|
|
ret += "\n" + MainApp.gs(R.string.percent) + ": " + percent + "%";
|
2016-06-20 20:45:55 +02:00
|
|
|
} else {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.enacted) + ": " + enacted;
|
2018-03-19 11:08:58 +01:00
|
|
|
if (!comment.isEmpty())
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment;
|
|
|
|
ret += "\n" + MainApp.gs(R.string.duration) + ": " + duration + " min";
|
|
|
|
ret += "\n" + MainApp.gs(R.string.absolute) + ": " + absolute + " U/h";
|
2016-06-20 20:45:55 +02:00
|
|
|
}
|
2016-06-19 20:06:00 +02:00
|
|
|
} else {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "\n" + MainApp.gs(R.string.comment) + ": " + comment;
|
2016-06-19 20:06:00 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2016-06-19 13:17:16 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 18:44:16 +02:00
|
|
|
public String toHtml() {
|
|
|
|
String ret = "<b>" + MainApp.gs(R.string.success) + "</b>: " + success;
|
2016-07-13 18:57:23 +02:00
|
|
|
if (queued) {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret = MainApp.gs(R.string.waitingforpumpresult);
|
2016-07-13 18:57:23 +02:00
|
|
|
} else if (enacted) {
|
2018-03-15 18:30:18 +01:00
|
|
|
if (bolusDelivered > 0) {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.enacted) + "</b>: " + enacted;
|
2018-03-19 11:08:58 +01:00
|
|
|
if (!comment.isEmpty())
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.comment) + "</b>: " + comment;
|
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.smb_shortname) + "</b>: " + bolusDelivered + " " + MainApp.gs(R.string.insulin_unit_shortname);
|
2018-03-15 18:30:18 +01:00
|
|
|
} else if (isTempCancel) {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.enacted) + "</b>: " + enacted;
|
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.comment) + "</b>: " + comment +
|
|
|
|
"<br>" + MainApp.gs(R.string.canceltemp);
|
2018-01-27 19:51:35 +01:00
|
|
|
} else if (isPercent && percent != -1) {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.enacted) + "</b>: " + enacted;
|
2018-03-19 11:08:58 +01:00
|
|
|
if (!comment.isEmpty())
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.comment) + "</b>: " + comment;
|
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.duration) + "</b>: " + duration + " min";
|
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.percent) + "</b>: " + percent + "%";
|
2018-01-27 19:51:35 +01:00
|
|
|
} else if (absolute != -1) {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.enacted) + "</b>: " + enacted;
|
2018-03-19 11:08:58 +01:00
|
|
|
if (!comment.isEmpty())
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.comment) + "</b>: " + comment;
|
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.duration) + "</b>: " + duration + " min";
|
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.absolute) + "</b>: " + DecimalFormatter.to2Decimal(absolute) + " U/h";
|
2016-07-11 17:49:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-29 18:44:16 +02:00
|
|
|
ret += "<br><b>" + MainApp.gs(R.string.comment) + "</b>: " + comment;
|
2016-07-11 17:49:09 +02:00
|
|
|
}
|
2018-03-29 18:44:16 +02:00
|
|
|
return ret;
|
2016-06-24 17:16:17 +02:00
|
|
|
}
|
2016-06-19 13:17:16 +02:00
|
|
|
|
2018-03-16 16:53:38 +01:00
|
|
|
public JSONObject json(Profile profile) {
|
2016-06-24 17:16:17 +02:00
|
|
|
JSONObject result = new JSONObject();
|
|
|
|
try {
|
2018-03-15 18:30:18 +01:00
|
|
|
if (bolusDelivered > 0) {
|
|
|
|
result.put("smb", bolusDelivered);
|
|
|
|
} else if (isTempCancel) {
|
2016-06-24 17:16:17 +02:00
|
|
|
result.put("rate", 0);
|
|
|
|
result.put("duration", 0);
|
|
|
|
} else if (isPercent) {
|
|
|
|
// Nightscout is expecting absolute value
|
2018-03-16 16:53:38 +01:00
|
|
|
Double abs = Round.roundTo(profile.getBasal() * percent / 100, 0.01);
|
2016-06-24 17:16:17 +02:00
|
|
|
result.put("rate", abs);
|
|
|
|
result.put("duration", duration);
|
|
|
|
} else {
|
|
|
|
result.put("rate", absolute);
|
|
|
|
result.put("duration", duration);
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
2017-08-20 11:17:05 +02:00
|
|
|
log.error("Unhandled exception", e);
|
2016-06-24 17:16:17 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-01-10 23:14:58 +01:00
|
|
|
|
|
|
|
public static PumpEnactResult error(String message) {
|
|
|
|
return new PumpEnactResult().enacted(false).success(false).comment(message);
|
|
|
|
}
|
2016-06-11 20:45:40 +02:00
|
|
|
}
|