optimize dia
This commit is contained in:
parent
2a1fe205ee
commit
d3de51cc88
|
@ -1,5 +1,7 @@
|
||||||
package info.nightscout.androidaps.data;
|
package info.nightscout.androidaps.data;
|
||||||
|
|
||||||
|
import android.support.v4.util.LongSparseArray;
|
||||||
|
|
||||||
import com.crashlytics.android.Crashlytics;
|
import com.crashlytics.android.Crashlytics;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
@ -30,8 +32,11 @@ public class Profile {
|
||||||
double dia = Constants.defaultDIA;
|
double dia = Constants.defaultDIA;
|
||||||
TimeZone timeZone = TimeZone.getDefault();
|
TimeZone timeZone = TimeZone.getDefault();
|
||||||
JSONArray isf;
|
JSONArray isf;
|
||||||
|
private LongSparseArray<Double> isf_v = null; // oldest at index 0
|
||||||
JSONArray ic;
|
JSONArray ic;
|
||||||
|
private LongSparseArray<Double> ic_v = null; // oldest at index 0
|
||||||
JSONArray basal;
|
JSONArray basal;
|
||||||
|
private LongSparseArray<Double> basal_v = null; // oldest at index 0
|
||||||
JSONArray targetLow;
|
JSONArray targetLow;
|
||||||
JSONArray targetHigh;
|
JSONArray targetHigh;
|
||||||
|
|
||||||
|
@ -136,6 +141,21 @@ public class Profile {
|
||||||
return timeZone;
|
return timeZone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private LongSparseArray<Double> convertToSparseArray(JSONArray array) {
|
||||||
|
LongSparseArray<Double> sparse = new LongSparseArray<>();
|
||||||
|
for (Integer index = 0; index < array.length(); index++) {
|
||||||
|
try {
|
||||||
|
JSONObject o = array.getJSONObject(index);
|
||||||
|
long tas = o.getLong("timeAsSeconds");
|
||||||
|
Double value = o.getDouble("value");
|
||||||
|
sparse.put(tas, value);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sparse;
|
||||||
|
}
|
||||||
|
|
||||||
private Double getValueToTime(JSONArray array, Integer timeAsSeconds) {
|
private Double getValueToTime(JSONArray array, Integer timeAsSeconds) {
|
||||||
Double lastValue = null;
|
Double lastValue = null;
|
||||||
|
|
||||||
|
@ -156,6 +176,21 @@ public class Profile {
|
||||||
return lastValue;
|
return lastValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Double getValueToTime(LongSparseArray<Double> array, long timeAsSeconds) {
|
||||||
|
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;
|
||||||
|
if (timeAsSeconds < tas) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lastValue = value;
|
||||||
|
}
|
||||||
|
return lastValue;
|
||||||
|
}
|
||||||
|
|
||||||
private String getValuesList(JSONArray array, JSONArray array2, DecimalFormat format, String units) {
|
private String getValuesList(JSONArray array, JSONArray array2, DecimalFormat format, String units) {
|
||||||
String retValue = "";
|
String retValue = "";
|
||||||
|
|
||||||
|
@ -188,7 +223,9 @@ public class Profile {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getIsf(Integer timeAsSeconds) {
|
public Double getIsf(Integer timeAsSeconds) {
|
||||||
return getValueToTime(isf, timeAsSeconds);
|
if (isf_v == null)
|
||||||
|
isf_v = convertToSparseArray(isf);
|
||||||
|
return getValueToTime(isf_v, timeAsSeconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getIsfList() {
|
public String getIsfList() {
|
||||||
|
@ -204,7 +241,9 @@ public class Profile {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getIc(Integer timeAsSeconds) {
|
public Double getIc(Integer timeAsSeconds) {
|
||||||
return getValueToTime(ic, timeAsSeconds);
|
if (ic_v == null)
|
||||||
|
ic_v = convertToSparseArray(ic);
|
||||||
|
return getValueToTime(ic_v, timeAsSeconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getIcList() {
|
public String getIcList() {
|
||||||
|
@ -220,7 +259,9 @@ public class Profile {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getBasal(Integer timeAsSeconds) {
|
public Double getBasal(Integer timeAsSeconds) {
|
||||||
return getValueToTime(basal, timeAsSeconds);
|
if (basal_v == null)
|
||||||
|
basal_v = convertToSparseArray(basal);
|
||||||
|
return getValueToTime(basal_v, timeAsSeconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBasalList() {
|
public String getBasalList() {
|
||||||
|
|
|
@ -180,7 +180,7 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
|
||||||
int realDuration = getDurationToTime(time);
|
int realDuration = getDurationToTime(time);
|
||||||
|
|
||||||
if (realDuration > 0) {
|
if (realDuration > 0) {
|
||||||
Double dia_ago = time - profile.getDia() * 60 * 60 * 1000;
|
Double dia_ago = time - dia * 60 * 60 * 1000;
|
||||||
int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d);
|
int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d);
|
||||||
double spacing = realDuration / aboutFiveMinIntervals;
|
double spacing = realDuration / aboutFiveMinIntervals;
|
||||||
|
|
||||||
|
@ -191,11 +191,11 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
|
||||||
if (calcdate > dia_ago && calcdate <= time) {
|
if (calcdate > dia_ago && calcdate <= time) {
|
||||||
double tempBolusSize = absoluteRate() * spacing / 60d;
|
double tempBolusSize = absoluteRate() * spacing / 60d;
|
||||||
|
|
||||||
Treatment tempBolusPart = new Treatment(insulinInterface);
|
Treatment tempBolusPart = new Treatment(insulinInterface, dia);
|
||||||
tempBolusPart.insulin = tempBolusSize;
|
tempBolusPart.insulin = tempBolusSize;
|
||||||
tempBolusPart.date = calcdate;
|
tempBolusPart.date = calcdate;
|
||||||
|
|
||||||
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, profile.getDia());
|
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, dia);
|
||||||
result.iob += aIOB.iobContrib;
|
result.iob += aIOB.iobContrib;
|
||||||
result.activity += aIOB.activityContrib;
|
result.activity += aIOB.activityContrib;
|
||||||
result.extendedBolusInsulin += tempBolusPart.insulin;
|
result.extendedBolusInsulin += tempBolusPart.insulin;
|
||||||
|
|
|
@ -179,8 +179,8 @@ public class TemporaryBasal implements Interval {
|
||||||
|
|
||||||
if (realDuration > 0) {
|
if (realDuration > 0) {
|
||||||
Double netBasalRate = 0d;
|
Double netBasalRate = 0d;
|
||||||
|
double dia = profile.getDia();
|
||||||
Double dia_ago = time - profile.getDia() * 60 * 60 * 1000;
|
Double dia_ago = time - dia * 60 * 60 * 1000;
|
||||||
int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d);
|
int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d);
|
||||||
double tempBolusSpacing = realDuration / aboutFiveMinIntervals;
|
double tempBolusSpacing = realDuration / aboutFiveMinIntervals;
|
||||||
|
|
||||||
|
@ -202,11 +202,11 @@ public class TemporaryBasal implements Interval {
|
||||||
double tempBolusSize = netBasalRate * tempBolusSpacing / 60d;
|
double tempBolusSize = netBasalRate * tempBolusSpacing / 60d;
|
||||||
netBasalAmount += tempBolusSize;
|
netBasalAmount += tempBolusSize;
|
||||||
|
|
||||||
Treatment tempBolusPart = new Treatment(insulinInterface);
|
Treatment tempBolusPart = new Treatment(insulinInterface, dia);
|
||||||
tempBolusPart.insulin = tempBolusSize;
|
tempBolusPart.insulin = tempBolusSize;
|
||||||
tempBolusPart.date = calcdate;
|
tempBolusPart.date = calcdate;
|
||||||
|
|
||||||
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, profile.getDia());
|
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, dia);
|
||||||
result.basaliob += aIOB.iobContrib;
|
result.basaliob += aIOB.iobContrib;
|
||||||
result.activity += aIOB.activityContrib;
|
result.activity += aIOB.activityContrib;
|
||||||
result.netbasalinsulin += tempBolusPart.insulin;
|
result.netbasalinsulin += tempBolusPart.insulin;
|
||||||
|
|
|
@ -65,6 +65,11 @@ public class Treatment implements DataPointWithLabelInterface {
|
||||||
dia = insulin.getDia();
|
dia = insulin.getDia();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Treatment(InsulinInterface insulin, double dia) {
|
||||||
|
insulinInterfaceID = insulin.getId();
|
||||||
|
this.dia = dia;
|
||||||
|
}
|
||||||
|
|
||||||
public long getMillisecondsFromStart() {
|
public long getMillisecondsFromStart() {
|
||||||
return new Date().getTime() - date;
|
return new Date().getTime() - date;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ public class ActivityGraph extends GraphView {
|
||||||
double dia = insulin.getDia();
|
double dia = insulin.getDia();
|
||||||
int hours = (int) Math.floor(dia + 1);
|
int hours = (int) Math.floor(dia + 1);
|
||||||
|
|
||||||
Treatment t = new Treatment(insulin);
|
Treatment t = new Treatment(insulin, dia);
|
||||||
t.date = 0;
|
t.date = 0;
|
||||||
t.insulin = 1d;
|
t.insulin = 1d;
|
||||||
|
|
||||||
|
|
|
@ -189,11 +189,11 @@ public class NSClientInternalPlugin implements PluginBase {
|
||||||
|
|
||||||
private void updateLog() {
|
private void updateLog() {
|
||||||
try {
|
try {
|
||||||
String newTextLog = "";
|
StringBuilder newTextLog = new StringBuilder();
|
||||||
for (EventNSClientNewLog log : listLog) {
|
for (EventNSClientNewLog log : listLog) {
|
||||||
newTextLog = newTextLog + log.toPreparedHtml();
|
newTextLog.append(log.toPreparedHtml());
|
||||||
}
|
}
|
||||||
textLog = Html.fromHtml(newTextLog);
|
textLog = Html.fromHtml(newTextLog.toString());
|
||||||
MainApp.bus().post(new EventNSClientUpdateGUI());
|
MainApp.bus().post(new EventNSClientUpdateGUI());
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "Out of memory!\nStop using this phone !!!", R.raw.error);
|
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "Out of memory!\nStop using this phone !!!", R.raw.error);
|
||||||
|
|
|
@ -22,14 +22,15 @@ public class EventNSClientNewLog {
|
||||||
this.logText = logText;
|
this.logText = logText;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Spanned toHtml() {
|
public StringBuilder toPreparedHtml() {
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
|
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
|
||||||
Spanned line = Html.fromHtml(timeFormat.format(date) + " <b>" + action + "</b> " + logText + "<br>");
|
stringBuilder.append(timeFormat.format(date));
|
||||||
return line;
|
stringBuilder.append(" <b>");
|
||||||
}
|
stringBuilder.append(action);
|
||||||
|
stringBuilder.append("</b> ");
|
||||||
public String toPreparedHtml() {
|
stringBuilder.append(logText);
|
||||||
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
|
stringBuilder.append("<br>");
|
||||||
return timeFormat.format(date) + " <b>" + action + "</b> " + logText + "<br>";
|
return stringBuilder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue