2017-06-02 10:25:49 +02:00
|
|
|
package info.nightscout.androidaps.data;
|
|
|
|
|
2017-06-13 15:06:41 +02:00
|
|
|
import android.support.v4.util.LongSparseArray;
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
|
|
|
|
import info.nightscout.androidaps.Constants;
|
|
|
|
import info.nightscout.androidaps.MainApp;
|
|
|
|
import info.nightscout.androidaps.R;
|
2017-12-22 20:17:31 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.PumpDescription;
|
|
|
|
import info.nightscout.androidaps.interfaces.PumpInterface;
|
2017-06-02 10:25:49 +02:00
|
|
|
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
|
2018-01-21 13:37:38 +01:00
|
|
|
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
|
2017-12-30 19:56:37 +01:00
|
|
|
import info.nightscout.utils.DateUtil;
|
2017-06-02 10:25:49 +02:00
|
|
|
import info.nightscout.utils.DecimalFormatter;
|
2018-02-22 13:30:36 +01:00
|
|
|
import info.nightscout.utils.FabricPrivacy;
|
2017-06-02 10:25:49 +02:00
|
|
|
|
|
|
|
public class Profile {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(Profile.class);
|
|
|
|
|
|
|
|
private JSONObject json;
|
2018-03-18 15:02:21 +01:00
|
|
|
private String units;
|
|
|
|
private double dia;
|
|
|
|
private TimeZone timeZone;
|
2017-11-15 11:23:20 +01:00
|
|
|
private JSONArray isf;
|
2018-03-18 15:02:21 +01:00
|
|
|
private LongSparseArray<Double> isf_v; // oldest at index 0
|
2017-11-15 11:23:20 +01:00
|
|
|
private JSONArray ic;
|
2018-03-18 15:02:21 +01:00
|
|
|
private LongSparseArray<Double> ic_v; // oldest at index 0
|
2017-11-15 11:23:20 +01:00
|
|
|
private JSONArray basal;
|
2018-03-18 15:02:21 +01:00
|
|
|
private LongSparseArray<Double> basal_v; // oldest at index 0
|
2017-11-15 11:23:20 +01:00
|
|
|
private JSONArray targetLow;
|
2018-03-18 15:02:21 +01:00
|
|
|
private LongSparseArray<Double> targetLow_v; // oldest at index 0
|
2017-11-15 11:23:20 +01:00
|
|
|
private JSONArray targetHigh;
|
2018-03-18 15:02:21 +01:00
|
|
|
private LongSparseArray<Double> targetHigh_v; // oldest at index 0
|
2017-06-02 10:25:49 +02:00
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
private int percentage;
|
|
|
|
private int timeshift;
|
2017-09-23 23:12:08 +02:00
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
protected boolean isValid;
|
|
|
|
protected boolean isValidated;
|
|
|
|
|
|
|
|
// Default constructor for tests
|
|
|
|
protected Profile() {
|
|
|
|
}
|
2018-01-21 13:37:38 +01:00
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
// Constructor from profileStore JSON
|
2017-06-02 10:25:49 +02:00
|
|
|
public Profile(JSONObject json, String units) {
|
2018-03-18 15:02:21 +01:00
|
|
|
init(json, 100, 0);
|
2017-06-02 10:25:49 +02:00
|
|
|
if (this.units == null) {
|
|
|
|
if (units != null)
|
|
|
|
this.units = units;
|
|
|
|
else {
|
2018-02-22 13:30:36 +01:00
|
|
|
FabricPrivacy.log("Profile failover failed too");
|
2017-06-02 10:25:49 +02:00
|
|
|
this.units = Constants.MGDL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 23:12:08 +02:00
|
|
|
public Profile(JSONObject json, int percentage, int timeshift) {
|
2018-03-18 15:02:21 +01:00
|
|
|
init(json, percentage, timeshift);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void init(JSONObject json, int percentage, int timeshift) {
|
|
|
|
units = null;
|
|
|
|
dia = Constants.defaultDIA;
|
|
|
|
timeZone = TimeZone.getDefault();
|
|
|
|
isf_v = null;
|
|
|
|
ic_v = null;
|
|
|
|
basal_v = null;
|
|
|
|
targetLow_v = null;
|
|
|
|
targetHigh_v = null;
|
|
|
|
|
|
|
|
isValid = true;
|
|
|
|
isValidated = false;
|
|
|
|
|
2017-09-23 23:12:08 +02:00
|
|
|
this.percentage = percentage;
|
|
|
|
this.timeshift = timeshift;
|
2017-06-02 10:25:49 +02:00
|
|
|
this.json = json;
|
|
|
|
try {
|
|
|
|
if (json.has("units"))
|
|
|
|
units = json.getString("units").toLowerCase();
|
|
|
|
if (json.has("dia"))
|
|
|
|
dia = json.getDouble("dia");
|
|
|
|
if (json.has("dia"))
|
|
|
|
dia = json.getDouble("dia");
|
|
|
|
if (json.has("timezone"))
|
|
|
|
timeZone = TimeZone.getTimeZone(json.getString("timezone"));
|
|
|
|
isf = json.getJSONArray("sens");
|
|
|
|
ic = json.getJSONArray("carbratio");
|
|
|
|
basal = json.getJSONArray("basal");
|
|
|
|
targetLow = json.getJSONArray("target_low");
|
|
|
|
targetHigh = json.getJSONArray("target_high");
|
|
|
|
} catch (JSONException e) {
|
2017-08-20 11:17:05 +02:00
|
|
|
log.error("Unhandled exception", e);
|
2018-01-21 13:37:38 +01:00
|
|
|
isValid = false;
|
|
|
|
isValidated = true;
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String log() {
|
|
|
|
String ret = "\n";
|
|
|
|
for (Integer hour = 0; hour < 24; hour++) {
|
2018-03-18 15:02:21 +01:00
|
|
|
double value = getBasalTimeFromMidnight((Integer) (hour * 60 * 60));
|
2017-06-02 10:25:49 +02:00
|
|
|
ret += "NS basal value for " + hour + ":00 is " + value + "\n";
|
|
|
|
}
|
|
|
|
ret += "NS units: " + getUnits();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JSONObject getData() {
|
2017-07-03 11:14:30 +02:00
|
|
|
if (!json.has("units"))
|
|
|
|
try {
|
|
|
|
json.put("units", units);
|
|
|
|
} catch (JSONException e) {
|
2017-08-20 11:17:05 +02:00
|
|
|
log.error("Unhandled exception", e);
|
2017-07-03 11:14:30 +02:00
|
|
|
}
|
2017-06-02 10:25:49 +02:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2017-06-16 09:14:57 +02:00
|
|
|
public double getDia() {
|
2017-06-02 10:25:49 +02:00
|
|
|
return dia;
|
|
|
|
}
|
|
|
|
|
|
|
|
// mmol or mg/dl
|
2018-03-18 15:02:21 +01:00
|
|
|
public void setUnits(String units) {
|
|
|
|
this.units = units;
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
public String getUnits() {
|
|
|
|
return units;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TimeZone getTimeZone() {
|
|
|
|
return timeZone;
|
|
|
|
}
|
|
|
|
|
2017-06-13 15:06:41 +02:00
|
|
|
private LongSparseArray<Double> convertToSparseArray(JSONArray array) {
|
2018-03-17 23:18:34 +01:00
|
|
|
if (array == null) {
|
|
|
|
isValid = false;
|
|
|
|
return new LongSparseArray<>();
|
|
|
|
}
|
|
|
|
|
2017-10-05 00:54:35 +02:00
|
|
|
double multiplier = getMultiplier(array);
|
|
|
|
|
2017-06-13 15:06:41 +02:00
|
|
|
LongSparseArray<Double> sparse = new LongSparseArray<>();
|
|
|
|
for (Integer index = 0; index < array.length(); index++) {
|
|
|
|
try {
|
2017-12-14 21:49:11 +01:00
|
|
|
final JSONObject o = array.getJSONObject(index);
|
2017-12-30 19:56:37 +01:00
|
|
|
long tas = 0;
|
|
|
|
try {
|
|
|
|
tas = getShitfTimeSecs((int) o.getLong("timeAsSeconds"));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
String time = o.getString("time");
|
|
|
|
tas = getShitfTimeSecs(DateUtil.toSeconds(time));
|
|
|
|
//log.debug(">>>>>>>>>>>> Used recalculated timeAsSecons: " + time + " " + tas);
|
|
|
|
}
|
2018-01-04 11:06:57 +01:00
|
|
|
double value = o.getDouble("value") * multiplier;
|
2017-06-13 15:06:41 +02:00
|
|
|
sparse.put(tas, value);
|
|
|
|
} catch (JSONException e) {
|
2017-08-20 11:17:05 +02:00
|
|
|
log.error("Unhandled exception", e);
|
2017-12-30 19:56:37 +01:00
|
|
|
log.error(json.toString());
|
2017-06-13 15:06:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 00:54:35 +02:00
|
|
|
|
|
|
|
// check if start is at 0 (midnight)
|
|
|
|
// and add last value before midnight if not
|
|
|
|
if (sparse.keyAt(0) != 0) {
|
|
|
|
sparse.put(0, sparse.valueAt(sparse.size() - 1));
|
|
|
|
}
|
2017-06-13 15:06:41 +02:00
|
|
|
return sparse;
|
|
|
|
}
|
|
|
|
|
2018-01-21 15:56:07 +01:00
|
|
|
public synchronized boolean isValid(String from) {
|
2018-01-21 13:37:38 +01:00
|
|
|
if (!isValid)
|
|
|
|
return false;
|
|
|
|
if (!isValidated) {
|
|
|
|
if (basal_v == null)
|
|
|
|
basal_v = convertToSparseArray(basal);
|
|
|
|
validate(basal_v);
|
|
|
|
if (isf_v == null)
|
|
|
|
isf_v = convertToSparseArray(isf);
|
|
|
|
validate(isf_v);
|
|
|
|
if (ic_v == null)
|
|
|
|
ic_v = convertToSparseArray(ic);
|
|
|
|
validate(ic_v);
|
|
|
|
if (targetLow_v == null)
|
|
|
|
targetLow_v = convertToSparseArray(targetLow);
|
|
|
|
validate(targetLow_v);
|
|
|
|
if (targetHigh_v == null)
|
|
|
|
targetHigh_v = convertToSparseArray(targetHigh);
|
|
|
|
validate(targetHigh_v);
|
|
|
|
isValidated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
// Check for hours alignment
|
2018-03-11 16:03:21 +01:00
|
|
|
PumpInterface pump = MainApp.getConfigBuilder().getActivePump();
|
2018-03-11 16:38:30 +01:00
|
|
|
if (pump != null && !pump.getPumpDescription().is30minBasalRatesCapable) {
|
2018-03-11 16:03:21 +01:00
|
|
|
for (int index = 0; index < basal_v.size(); index++) {
|
|
|
|
long secondsFromMidnight = basal_v.keyAt(index);
|
|
|
|
if (secondsFromMidnight % 3600 != 0) {
|
|
|
|
Notification notification = new Notification(Notification.BASAL_PROFILE_NOT_ALIGNED_TO_HOURS, String.format(MainApp.gs(R.string.basalprofilenotaligned), from), Notification.NORMAL);
|
|
|
|
MainApp.bus().post(new EventNewNotification(notification));
|
|
|
|
}
|
2018-01-21 13:37:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for minimal basal value
|
|
|
|
if (pump != null) {
|
|
|
|
PumpDescription description = pump.getPumpDescription();
|
|
|
|
for (int i = 0; i < basal_v.size(); i++) {
|
|
|
|
if (basal_v.valueAt(i) < description.basalMinimumRate) {
|
|
|
|
basal_v.setValueAt(i, description.basalMinimumRate);
|
2018-03-18 15:02:21 +01:00
|
|
|
sendBelowMinimumNotification(from);
|
2018-01-21 13:37:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if pump not available (at start)
|
|
|
|
// do not store converted array
|
|
|
|
basal_v = null;
|
2018-01-22 21:49:01 +01:00
|
|
|
isValidated = false;
|
2018-01-21 13:37:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return isValid;
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
protected void sendBelowMinimumNotification(String from) {
|
|
|
|
MainApp.bus().post(new EventNewNotification(new Notification(Notification.MINIMAL_BASAL_VALUE_REPLACED, String.format(MainApp.gs(R.string.minimalbasalvaluereplaced), from), Notification.NORMAL)));
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:37:38 +01:00
|
|
|
private void validate(LongSparseArray array) {
|
|
|
|
if (array.size() == 0) {
|
|
|
|
isValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int index = 0; index < array.size(); index++) {
|
|
|
|
if (array.valueAt(index).equals(0d)) {
|
|
|
|
isValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
/*
|
2017-06-02 10:25:49 +02:00
|
|
|
private Double getValueToTime(JSONArray array, Integer timeAsSeconds) {
|
|
|
|
Double lastValue = null;
|
|
|
|
|
|
|
|
for (Integer index = 0; index < array.length(); index++) {
|
|
|
|
try {
|
|
|
|
JSONObject o = array.getJSONObject(index);
|
|
|
|
Integer tas = o.getInt("timeAsSeconds");
|
|
|
|
Double value = o.getDouble("value");
|
|
|
|
if (lastValue == null) lastValue = value;
|
|
|
|
if (timeAsSeconds < tas) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastValue = value;
|
|
|
|
} catch (JSONException e) {
|
2017-08-20 11:17:05 +02:00
|
|
|
log.error("Unhandled exception", e);
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return lastValue;
|
|
|
|
}
|
2018-03-18 15:02:21 +01:00
|
|
|
*/
|
2017-06-02 10:25:49 +02:00
|
|
|
|
2017-09-23 23:12:08 +02:00
|
|
|
Integer getShitfTimeSecs(Integer originalTime) {
|
2017-10-05 00:54:35 +02:00
|
|
|
Integer shiftedTime = originalTime + timeshift * 60 * 60;
|
2017-10-07 20:42:56 +02:00
|
|
|
shiftedTime = (shiftedTime + 24 * 60 * 60) % (24 * 60 * 60);
|
2017-10-05 00:54:35 +02:00
|
|
|
if (timeshift != 0)
|
|
|
|
log.debug("(Sec) Original time: " + originalTime + " ShiftedTime: " + shiftedTime);
|
2017-09-23 23:12:08 +02:00
|
|
|
return shiftedTime;
|
|
|
|
}
|
|
|
|
|
2017-11-15 11:23:20 +01:00
|
|
|
private double getMultiplier(LongSparseArray<Double> array) {
|
2017-09-23 23:12:08 +02:00
|
|
|
double multiplier = 1d;
|
|
|
|
|
|
|
|
if (array == isf_v)
|
|
|
|
multiplier = 100d / percentage;
|
|
|
|
else if (array == ic_v)
|
|
|
|
multiplier = 100d / percentage;
|
|
|
|
else if (array == basal_v)
|
|
|
|
multiplier = percentage / 100d;
|
|
|
|
else
|
|
|
|
log.error("Unknown array type");
|
|
|
|
return multiplier;
|
|
|
|
}
|
|
|
|
|
2017-11-15 11:23:20 +01:00
|
|
|
private double getMultiplier(JSONArray array) {
|
2017-09-23 23:12:08 +02:00
|
|
|
double multiplier = 1d;
|
|
|
|
|
|
|
|
if (array == isf)
|
|
|
|
multiplier = 100d / percentage;
|
|
|
|
else if (array == ic)
|
|
|
|
multiplier = 100d / percentage;
|
|
|
|
else if (array == basal)
|
|
|
|
multiplier = percentage / 100d;
|
|
|
|
else if (array == targetLow)
|
|
|
|
multiplier = 1d;
|
|
|
|
else if (array == targetHigh)
|
|
|
|
multiplier = 1d;
|
|
|
|
else
|
|
|
|
log.error("Unknown array type");
|
|
|
|
return multiplier;
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
private double getValueToTime(LongSparseArray<Double> array, Integer timeAsSeconds) {
|
2017-06-13 15:06:41 +02:00
|
|
|
Double lastValue = null;
|
|
|
|
|
|
|
|
for (Integer index = 0; index < array.size(); index++) {
|
|
|
|
long tas = array.keyAt(index);
|
|
|
|
double value = array.valueAt(index);
|
|
|
|
if (lastValue == null) lastValue = value;
|
2017-10-05 00:54:35 +02:00
|
|
|
if (timeAsSeconds < tas) {
|
2017-06-13 15:06:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastValue = value;
|
|
|
|
}
|
2017-10-05 00:54:35 +02:00
|
|
|
return lastValue;
|
2017-09-23 23:12:08 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
protected String format_HH_MM(Integer timeAsSeconds) {
|
2017-09-23 23:12:08 +02:00
|
|
|
String time;
|
|
|
|
int hour = timeAsSeconds / 60 / 60;
|
2017-10-20 11:34:33 +02:00
|
|
|
int minutes = (timeAsSeconds - hour * 60 * 60) / 60;
|
2017-09-23 23:12:08 +02:00
|
|
|
DecimalFormat df = new DecimalFormat("00");
|
|
|
|
time = df.format(hour) + ":" + df.format(minutes);
|
|
|
|
return time;
|
2017-06-13 15:06:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 00:54:35 +02:00
|
|
|
private String getValuesList(LongSparseArray<Double> array, LongSparseArray<Double> array2, DecimalFormat format, String units) {
|
2017-06-02 10:25:49 +02:00
|
|
|
String retValue = "";
|
|
|
|
|
2017-10-05 00:54:35 +02:00
|
|
|
for (Integer index = 0; index < array.size(); index++) {
|
|
|
|
retValue += format_HH_MM((int) array.keyAt(index));
|
|
|
|
retValue += " ";
|
|
|
|
retValue += format.format(array.valueAt(index));
|
|
|
|
if (array2 != null) {
|
|
|
|
retValue += " - ";
|
|
|
|
retValue += format.format(array2.valueAt(index));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
2017-10-05 00:54:35 +02:00
|
|
|
retValue += " " + units;
|
|
|
|
if (index + 1 < array.size())
|
|
|
|
retValue += "\n";
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
return retValue;
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getIsf() {
|
|
|
|
return getIsfTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getIsf(long time) {
|
|
|
|
return getIsfTimeFromMidnight(secondsFromMidnight(time));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
double getIsfTimeFromMidnight(int timeAsSeconds) {
|
2017-06-13 15:06:41 +02:00
|
|
|
if (isf_v == null)
|
|
|
|
isf_v = convertToSparseArray(isf);
|
|
|
|
return getValueToTime(isf_v, timeAsSeconds);
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getIsfList() {
|
2018-03-17 23:18:34 +01:00
|
|
|
if (isf_v == null)
|
|
|
|
isf_v = convertToSparseArray(isf);
|
2017-10-05 00:54:35 +02:00
|
|
|
return getValuesList(isf_v, null, new DecimalFormat("0.0"), getUnits() + "/U");
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getIc() {
|
|
|
|
return getIcTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getIc(long time) {
|
|
|
|
return getIcTimeFromMidnight(secondsFromMidnight(time));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getIcTimeFromMidnight(int timeAsSeconds) {
|
2017-06-13 15:06:41 +02:00
|
|
|
if (ic_v == null)
|
|
|
|
ic_v = convertToSparseArray(ic);
|
|
|
|
return getValueToTime(ic_v, timeAsSeconds);
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getIcList() {
|
2018-03-17 23:18:34 +01:00
|
|
|
if (ic_v == null)
|
|
|
|
ic_v = convertToSparseArray(ic);
|
2018-03-18 15:02:21 +01:00
|
|
|
return getValuesList(ic_v, null, new DecimalFormat("0.0"), "g/U");
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getBasal() {
|
|
|
|
return getBasalTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getBasal(long time) {
|
|
|
|
return getBasalTimeFromMidnight(secondsFromMidnight(time));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public synchronized double getBasalTimeFromMidnight(int timeAsSeconds) {
|
2017-12-22 20:17:31 +01:00
|
|
|
if (basal_v == null) {
|
2017-06-13 15:06:41 +02:00
|
|
|
basal_v = convertToSparseArray(basal);
|
2017-12-22 20:17:31 +01:00
|
|
|
}
|
2017-06-13 15:06:41 +02:00
|
|
|
return getValueToTime(basal_v, timeAsSeconds);
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getBasalList() {
|
2018-02-11 14:40:07 +01:00
|
|
|
if (basal_v == null)
|
|
|
|
basal_v = convertToSparseArray(basal);
|
2018-03-18 15:02:21 +01:00
|
|
|
return getValuesList(basal_v, null, new DecimalFormat("0.00"), "U/h");
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class BasalValue {
|
2018-03-18 15:02:21 +01:00
|
|
|
public BasalValue(int timeAsSeconds, double value) {
|
2017-06-02 10:25:49 +02:00
|
|
|
this.timeAsSeconds = timeAsSeconds;
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public int timeAsSeconds;
|
|
|
|
public double value;
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 15:56:07 +01:00
|
|
|
public synchronized BasalValue[] getBasalValues() {
|
2018-01-14 21:42:36 +01:00
|
|
|
if (basal_v == null)
|
|
|
|
basal_v = convertToSparseArray(basal);
|
2017-10-05 00:54:35 +02:00
|
|
|
BasalValue[] ret = new BasalValue[basal_v.size()];
|
2017-06-02 10:25:49 +02:00
|
|
|
|
2017-10-05 00:54:35 +02:00
|
|
|
for (Integer index = 0; index < basal_v.size(); index++) {
|
|
|
|
Integer tas = (int) basal_v.keyAt(index);
|
2018-03-18 15:02:21 +01:00
|
|
|
double value = basal_v.valueAt(index);
|
2017-10-05 00:54:35 +02:00
|
|
|
ret[index] = new BasalValue(tas, value);
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
2017-10-05 00:54:35 +02:00
|
|
|
return ret;
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 14:59:18 +01:00
|
|
|
public double getTarget(){
|
|
|
|
return getTarget(secondsFromMidnight(System.currentTimeMillis()));
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
protected double getTarget(int timeAsSeconds) {
|
|
|
|
return (getTargetLowTimeFromMidnight(timeAsSeconds) + getTargetHighTimeFromMidnight(timeAsSeconds))/2;
|
2018-03-08 14:59:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getTargetLow() {
|
|
|
|
return getTargetLowTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getTargetLow(long time) {
|
|
|
|
return getTargetLowTimeFromMidnight(secondsFromMidnight(time));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getTargetLowTimeFromMidnight(int timeAsSeconds) {
|
2017-10-05 00:54:35 +02:00
|
|
|
if (targetLow_v == null)
|
|
|
|
targetLow_v = convertToSparseArray(targetLow);
|
|
|
|
return getValueToTime(targetLow_v, timeAsSeconds);
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getTargetHigh() {
|
|
|
|
return getTargetHighTimeFromMidnight(secondsFromMidnight(System.currentTimeMillis()));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getTargetHigh(long time) {
|
|
|
|
return getTargetHighTimeFromMidnight(secondsFromMidnight(time));
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public double getTargetHighTimeFromMidnight(int timeAsSeconds) {
|
2017-10-05 00:54:35 +02:00
|
|
|
if (targetHigh_v == null)
|
|
|
|
targetHigh_v = convertToSparseArray(targetHigh);
|
|
|
|
return getValueToTime(targetHigh_v, timeAsSeconds);
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getTargetList() {
|
2018-03-17 23:18:34 +01:00
|
|
|
if (targetLow_v == null)
|
|
|
|
targetLow_v = convertToSparseArray(targetLow);
|
|
|
|
if (targetHigh_v == null)
|
|
|
|
targetHigh_v = convertToSparseArray(targetHigh);
|
2017-10-05 00:54:35 +02:00
|
|
|
return getValuesList(targetLow_v, targetHigh_v, new DecimalFormat("0.0"), getUnits());
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public double getMaxDailyBasal() {
|
2018-03-18 15:02:21 +01:00
|
|
|
double max = 0d;
|
|
|
|
for (int hour = 0; hour < 24; hour++) {
|
|
|
|
double value = getBasalTimeFromMidnight((Integer) (hour * 60 * 60));
|
2017-06-02 10:25:49 +02:00
|
|
|
if (value > max) max = value;
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public static int secondsFromMidnight() {
|
2017-06-02 10:25:49 +02:00
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
|
long now = c.getTimeInMillis();
|
|
|
|
c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
c.set(Calendar.MINUTE, 0);
|
|
|
|
c.set(Calendar.SECOND, 0);
|
|
|
|
c.set(Calendar.MILLISECOND, 0);
|
|
|
|
long passed = now - c.getTimeInMillis();
|
|
|
|
return (int) (passed / 1000);
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public static int secondsFromMidnight(long date) {
|
2017-06-02 10:25:49 +02:00
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
|
c.setTimeInMillis(date);
|
|
|
|
c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
c.set(Calendar.MINUTE, 0);
|
|
|
|
c.set(Calendar.SECOND, 0);
|
|
|
|
c.set(Calendar.MILLISECOND, 0);
|
|
|
|
long passed = date - c.getTimeInMillis();
|
|
|
|
return (int) (passed / 1000);
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public static double toMgdl(double value, String units) {
|
2017-06-02 10:25:49 +02:00
|
|
|
if (units.equals(Constants.MGDL)) return value;
|
|
|
|
else return value * Constants.MMOLL_TO_MGDL;
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public static double toMmol(double value, String units) {
|
2017-08-20 19:42:41 +02:00
|
|
|
if (units.equals(Constants.MGDL)) return value * Constants.MGDL_TO_MMOLL;
|
|
|
|
else return value;
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public static double fromMgdlToUnits(double value, String units) {
|
2017-06-02 10:25:49 +02:00
|
|
|
if (units.equals(Constants.MGDL)) return value;
|
|
|
|
else return value * Constants.MGDL_TO_MMOLL;
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:02:21 +01:00
|
|
|
public static double toUnits(Double valueInMgdl, Double valueInMmol, String units) {
|
2017-06-02 10:25:49 +02:00
|
|
|
if (units.equals(Constants.MGDL)) return valueInMgdl;
|
|
|
|
else return valueInMmol;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String toUnitsString(Double valueInMgdl, Double valueInMmol, String units) {
|
|
|
|
if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(valueInMgdl);
|
|
|
|
else return DecimalFormatter.to1Decimal(valueInMmol);
|
|
|
|
}
|
2017-08-15 16:04:46 +02:00
|
|
|
|
2017-08-20 19:42:41 +02:00
|
|
|
// targets are stored in mg/dl but profile vary
|
|
|
|
public static String toTargetRangeString(double low, double high, String sourceUnits, String units) {
|
|
|
|
double lowMgdl = toMgdl(low, sourceUnits);
|
|
|
|
double highMgdl = toMgdl(high, sourceUnits);
|
|
|
|
double lowMmol = toMmol(low, sourceUnits);
|
|
|
|
double highMmol = toMmol(high, sourceUnits);
|
|
|
|
if (low == high)
|
|
|
|
return toUnitsString(lowMgdl, lowMmol, units);
|
|
|
|
else
|
|
|
|
return toUnitsString(lowMgdl, lowMmol, units) + " - " + toUnitsString(highMgdl, highMmol, units);
|
|
|
|
|
2017-08-15 16:04:46 +02:00
|
|
|
}
|
2017-10-08 14:41:51 +02:00
|
|
|
|
|
|
|
public double percentageBasalSum() {
|
|
|
|
double result = 0d;
|
|
|
|
for (int i = 0; i < 24; i++) {
|
2018-03-18 15:02:21 +01:00
|
|
|
result += getBasalTimeFromMidnight(i * 60 * 60);
|
2017-10-08 14:41:51 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double baseBasalSum() {
|
|
|
|
double result = 0d;
|
|
|
|
for (int i = 0; i < 24; i++) {
|
2018-03-18 15:02:21 +01:00
|
|
|
result += getBasalTimeFromMidnight(i * 60 * 60) / getMultiplier(basal_v);
|
2017-10-08 14:41:51 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPercentage() {
|
|
|
|
return percentage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTimeshift() {
|
|
|
|
return timeshift;
|
|
|
|
}
|
2017-06-02 10:25:49 +02:00
|
|
|
}
|