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

144 lines
6.4 KiB
Java
Raw Normal View History

2016-06-11 20:45:40 +02:00
package info.nightscout.androidaps.data;
2016-06-19 13:17:16 +02:00
import android.os.Parcel;
import android.os.Parcelable;
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 info.nightscout.androidaps.MainApp;
2016-07-11 17:49:09 +02:00
import info.nightscout.androidaps.R;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.Round;
public class PumpEnactResult extends Object implements Parcelable {
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
public Integer carbsDelivered = 0; // real value of delivered carbs
2016-06-11 20:45:40 +02:00
public String log() {
2016-06-20 20:45:55 +02:00
return "Success: " + success + " Enacted: " + enacted + " Comment: " + comment + " Duration: " + duration + " Absolute: " + absolute + " Percent: " + percent + " IsPercent: " + isPercent;
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" +
MainApp.sResources.getString(R.string.tempcancel);
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;
if (enacted) {
if (isTempCancel) {
ret += "<br><b>" + MainApp.sResources.getString(R.string.enacted) + "</b>: " + enacted;
ret += "<br><b>" + MainApp.sResources.getString(R.string.comment) + "</b>: " + comment +
"<br>" + MainApp.sResources.getString(R.string.tempcancel);
} else if (isPercent) {
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 {
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.absolute) + "</b>: " + absolute + " U/h";
}
} else {
ret += "<br><b>" + MainApp.sResources.getString(R.string.comment) + "</b>: " + comment;
}
return Html.fromHtml(ret);
}
2016-06-19 13:17:16 +02:00
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(success ? 1 : 0);
dest.writeInt(enacted ? 1 : 0);
2016-06-20 20:45:55 +02:00
dest.writeInt(isPercent ? 1 : 0);
2016-06-19 13:17:16 +02:00
dest.writeString(comment);
dest.writeInt(duration);
dest.writeDouble(absolute);
dest.writeInt(percent);
}
public final Parcelable.Creator<PumpEnactResult> CREATOR = new Parcelable.Creator<PumpEnactResult>() {
public PumpEnactResult createFromParcel(Parcel in) {
return new PumpEnactResult(in);
2016-06-19 13:17:16 +02:00
}
public PumpEnactResult[] newArray(int size) {
return new PumpEnactResult[size];
2016-06-19 13:17:16 +02:00
}
};
protected PumpEnactResult(Parcel in) {
2016-06-19 13:17:16 +02:00
success = in.readInt() == 1 ? true : false;
enacted = in.readInt() == 1 ? true : false;
2016-06-20 20:45:55 +02:00
isPercent = in.readInt() == 1 ? true : false;
2016-06-19 13:17:16 +02:00
duration = in.readInt();
comment = in.readString();
absolute = in.readDouble();
percent = in.readInt();
}
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
Double abs = Round.roundTo(MainApp.getConfigBuilder().getActiveProfile().getProfile().getBasal(NSProfile.secondsFromMidnight()) * percent / 100, 0.01);
result.put("rate", abs);
result.put("duration", duration);
} else {
result.put("rate", absolute);
result.put("duration", duration);
}
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
2016-06-11 20:45:40 +02:00
}