AndroidAPS/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java

170 lines
7.1 KiB
Java
Raw Normal View History

2016-06-11 20:45:40 +02:00
package info.nightscout.androidaps.data;
2016-07-11 17:49:09 +02:00
import android.text.Html;
import android.text.Spanned;
2016-06-19 13:17:16 +02:00
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
2016-07-11 17:49:09 +02:00
import info.nightscout.androidaps.R;
2016-07-13 18:57:23 +02:00
import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.Round;
2017-01-08 23:10:27 +01:00
public class PumpEnactResult extends Object {
private static Logger log = LoggerFactory.getLogger(PumpEnactResult.class);
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
public Integer duration = -1; // duration set [minutes]
public Double absolute = -1d; // absolute rate [U/h] , isPercent = false
public Integer percent = -1; // percent of current basal [%] (100% = current basal), isPercent = true
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
// Result of treatment delivery
2016-06-20 20:45:55 +02:00
public Double bolusDelivered = 0d; // real value of delivered insulin
2017-05-29 21:45:59 +02:00
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) {
this.success = success;
return this;
}
public PumpEnactResult enacted(boolean enacted) {
this.enacted = enacted;
return this;
}
public PumpEnactResult comment(String comment) {
this.comment = comment;
return this;
}
public PumpEnactResult duration(Integer duration) {
this.duration = duration;
return this;
}
public PumpEnactResult absolute(Double absolute) {
this.absolute = absolute;
return this;
}
public PumpEnactResult percent(Integer percent) {
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;
}
public PumpEnactResult bolusDelivered(Double bolusDelivered) {
this.bolusDelivered = bolusDelivered;
return this;
}
public PumpEnactResult carbsDelivered(Double carbsDelivered) {
this.carbsDelivered = carbsDelivered;
return this;
}
public PumpEnactResult queued(boolean queued) {
this.queued = queued;
return this;
}
2016-07-13 18:57:23 +02:00
public String log() {
return "Success: " + success + " Enacted: " + enacted + " Comment: " + comment + " Duration: " + duration + " Absolute: " + absolute + " Percent: " + percent + " IsPercent: " + isPercent + " Queued: " + queued;
2016-06-11 20:45:40 +02:00
}
2016-06-19 13:17:16 +02:00
public String toString() {
2016-07-11 17:49:09 +02:00
String ret = MainApp.sResources.getString(R.string.success) + ": " + success;
if (enacted) {
2016-06-23 17:07:38 +02:00
if (isTempCancel) {
2016-07-11 17:49:09 +02:00
ret += "\n" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted;
ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment + "\n" +
2016-07-12 17:56:01 +02:00
MainApp.sResources.getString(R.string.canceltemp);
2016-06-23 17:07:38 +02:00
} else if (isPercent) {
2016-07-11 17:49:09 +02:00
ret += "\n" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted;
ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment;
ret += "\n" + MainApp.sResources.getString(R.string.duration) + ": " + duration + " min";
ret += "\n" + MainApp.sResources.getString(R.string.percent) + ": " + percent + "%";
2016-06-20 20:45:55 +02:00
} else {
2016-07-11 17:49:09 +02:00
ret += "\n" + MainApp.sResources.getString(R.string.enacted) + ": " + enacted;
ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment;
ret += "\n" + MainApp.sResources.getString(R.string.duration) + ": " + duration + " min";
ret += "\n" + MainApp.sResources.getString(R.string.absolute) + ": " + absolute + " U/h";
2016-06-20 20:45:55 +02:00
}
} else {
2016-07-11 17:49:09 +02:00
ret += "\n" + MainApp.sResources.getString(R.string.comment) + ": " + comment;
}
return ret;
2016-06-19 13:17:16 +02:00
}
2016-07-11 17:49:09 +02:00
public Spanned toSpanned() {
String ret = "<b>" + MainApp.sResources.getString(R.string.success) + "</b>: " + success;
2016-07-13 18:57:23 +02:00
if (queued) {
ret = MainApp.sResources.getString(R.string.waitingforpumpresult);
} else if (enacted) {
2016-07-11 17:49:09 +02:00
if (isTempCancel) {
ret += "<br><b>" + MainApp.sResources.getString(R.string.enacted) + "</b>: " + enacted;
ret += "<br><b>" + MainApp.sResources.getString(R.string.comment) + "</b>: " + comment +
2016-07-12 17:56:01 +02:00
"<br>" + MainApp.sResources.getString(R.string.canceltemp);
} else if (isPercent && percent != -1) {
2016-07-11 17:49:09 +02:00
ret += "<br><b>" + MainApp.sResources.getString(R.string.enacted) + "</b>: " + enacted;
ret += "<br><b>" + MainApp.sResources.getString(R.string.comment) + "</b>: " + comment;
ret += "<br><b>" + MainApp.sResources.getString(R.string.duration) + "</b>: " + duration + " min";
ret += "<br><b>" + MainApp.sResources.getString(R.string.percent) + "</b>: " + percent + "%";
} else if (absolute != -1) {
2016-07-11 17:49:09 +02:00
ret += "<br><b>" + MainApp.sResources.getString(R.string.enacted) + "</b>: " + enacted;
ret += "<br><b>" + MainApp.sResources.getString(R.string.comment) + "</b>: " + comment;
ret += "<br><b>" + MainApp.sResources.getString(R.string.duration) + "</b>: " + duration + " min";
2016-07-13 18:57:23 +02:00
ret += "<br><b>" + MainApp.sResources.getString(R.string.absolute) + "</b>: " + DecimalFormatter.to2Decimal(absolute) + " U/h";
2016-07-11 17:49:09 +02:00
}
if (bolusDelivered > 0) {
ret += "<br><b>" + MainApp.sResources.getString(R.string.bolus) + "</b>: " + DecimalFormatter.to2Decimal(bolusDelivered) + " U";
}
2016-07-11 17:49:09 +02:00
} else {
ret += "<br><b>" + MainApp.sResources.getString(R.string.comment) + "</b>: " + comment;
}
return Html.fromHtml(ret);
}
public PumpEnactResult() {
}
2016-06-19 13:17:16 +02:00
public JSONObject json() {
JSONObject result = new JSONObject();
try {
if (isTempCancel) {
result.put("rate", 0);
result.put("duration", 0);
} else if (isPercent) {
// Nightscout is expecting absolute value
2017-06-02 10:25:49 +02:00
Double abs = Round.roundTo(MainApp.getConfigBuilder().getProfile().getBasal() * percent / 100, 0.01);
result.put("rate", abs);
result.put("duration", duration);
} else {
result.put("rate", absolute);
result.put("duration", duration);
}
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
return result;
}
2016-06-11 20:45:40 +02:00
}