optimize dia

This commit is contained in:
Milos Kozak 2017-06-13 15:06:41 +02:00
parent 2a1fe205ee
commit d3de51cc88
7 changed files with 69 additions and 22 deletions

View file

@ -1,5 +1,7 @@
package info.nightscout.androidaps.data;
import android.support.v4.util.LongSparseArray;
import com.crashlytics.android.Crashlytics;
import org.json.JSONArray;
@ -30,8 +32,11 @@ public class Profile {
double dia = Constants.defaultDIA;
TimeZone timeZone = TimeZone.getDefault();
JSONArray isf;
private LongSparseArray<Double> isf_v = null; // oldest at index 0
JSONArray ic;
private LongSparseArray<Double> ic_v = null; // oldest at index 0
JSONArray basal;
private LongSparseArray<Double> basal_v = null; // oldest at index 0
JSONArray targetLow;
JSONArray targetHigh;
@ -136,6 +141,21 @@ public class Profile {
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) {
Double lastValue = null;
@ -156,6 +176,21 @@ public class Profile {
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) {
String retValue = "";
@ -188,7 +223,9 @@ public class Profile {
}
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() {
@ -204,7 +241,9 @@ public class Profile {
}
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() {
@ -220,7 +259,9 @@ public class Profile {
}
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() {

View file

@ -180,7 +180,7 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
int realDuration = getDurationToTime(time);
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);
double spacing = realDuration / aboutFiveMinIntervals;
@ -191,11 +191,11 @@ public class ExtendedBolus implements Interval, DataPointWithLabelInterface {
if (calcdate > dia_ago && calcdate <= time) {
double tempBolusSize = absoluteRate() * spacing / 60d;
Treatment tempBolusPart = new Treatment(insulinInterface);
Treatment tempBolusPart = new Treatment(insulinInterface, dia);
tempBolusPart.insulin = tempBolusSize;
tempBolusPart.date = calcdate;
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, profile.getDia());
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, dia);
result.iob += aIOB.iobContrib;
result.activity += aIOB.activityContrib;
result.extendedBolusInsulin += tempBolusPart.insulin;

View file

@ -179,8 +179,8 @@ public class TemporaryBasal implements Interval {
if (realDuration > 0) {
Double netBasalRate = 0d;
Double dia_ago = time - profile.getDia() * 60 * 60 * 1000;
double dia = profile.getDia();
Double dia_ago = time - dia * 60 * 60 * 1000;
int aboutFiveMinIntervals = (int) Math.ceil(realDuration / 5d);
double tempBolusSpacing = realDuration / aboutFiveMinIntervals;
@ -202,11 +202,11 @@ public class TemporaryBasal implements Interval {
double tempBolusSize = netBasalRate * tempBolusSpacing / 60d;
netBasalAmount += tempBolusSize;
Treatment tempBolusPart = new Treatment(insulinInterface);
Treatment tempBolusPart = new Treatment(insulinInterface, dia);
tempBolusPart.insulin = tempBolusSize;
tempBolusPart.date = calcdate;
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, profile.getDia());
Iob aIOB = insulinInterface.iobCalcForTreatment(tempBolusPart, time, dia);
result.basaliob += aIOB.iobContrib;
result.activity += aIOB.activityContrib;
result.netbasalinsulin += tempBolusPart.insulin;

View file

@ -65,6 +65,11 @@ public class Treatment implements DataPointWithLabelInterface {
dia = insulin.getDia();
}
public Treatment(InsulinInterface insulin, double dia) {
insulinInterfaceID = insulin.getId();
this.dia = dia;
}
public long getMillisecondsFromStart() {
return new Date().getTime() - date;
}

View file

@ -38,7 +38,7 @@ public class ActivityGraph extends GraphView {
double dia = insulin.getDia();
int hours = (int) Math.floor(dia + 1);
Treatment t = new Treatment(insulin);
Treatment t = new Treatment(insulin, dia);
t.date = 0;
t.insulin = 1d;

View file

@ -189,11 +189,11 @@ public class NSClientInternalPlugin implements PluginBase {
private void updateLog() {
try {
String newTextLog = "";
StringBuilder newTextLog = new StringBuilder();
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());
} catch (OutOfMemoryError e) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "Out of memory!\nStop using this phone !!!", R.raw.error);

View file

@ -22,14 +22,15 @@ public class EventNSClientNewLog {
this.logText = logText;
}
public Spanned toHtml() {
public StringBuilder toPreparedHtml() {
StringBuilder stringBuilder = new StringBuilder();
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Spanned line = Html.fromHtml(timeFormat.format(date) + " <b>" + action + "</b> " + logText + "<br>");
return line;
}
public String toPreparedHtml() {
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
return timeFormat.format(date) + " <b>" + action + "</b> " + logText + "<br>";
stringBuilder.append(timeFormat.format(date));
stringBuilder.append(" <b>");
stringBuilder.append(action);
stringBuilder.append("</b> ");
stringBuilder.append(logText);
stringBuilder.append("<br>");
return stringBuilder;
}
}