InMemoryGlucoseValue

This commit is contained in:
Milos Kozak 2020-06-02 21:59:51 +02:00
parent 484f3a2a98
commit 15378fb2b8
4 changed files with 46 additions and 38 deletions

View file

@ -0,0 +1,8 @@
package info.nightscout.androidaps.plugins.iob.iobCobCalculator
import info.nightscout.androidaps.db.BgReading
class InMemoryGlucoseValue constructor(var timestamp: Long = 0L, var value: Double = 0.0) {
constructor(gv: BgReading) : this(gv.date, gv.value)
}

View file

@ -80,7 +80,7 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
private LongSparseArray<BasalData> basalDataTable = new LongSparseArray<>(); // oldest at index 0
private volatile List<BgReading> bgReadings = null; // newest at index 0
private volatile List<BgReading> bucketed_data = null;
private volatile List<InMemoryGlucoseValue> bucketed_data = null;
private final Object dataLock = new Object();
@ -232,7 +232,7 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
this.bgReadings = bgReadings;
}
public List<BgReading> getBucketedData() {
public List<InMemoryGlucoseValue> getBucketedData() {
return bucketed_data;
}
@ -342,15 +342,15 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
break;
if (older.date == newer.date) { // direct hit
bucketed_data.add(newer);
bucketed_data.add(new InMemoryGlucoseValue(newer));
} else {
double bgDelta = newer.value - older.value;
long timeDiffToNew = newer.date - currentTime;
double currentBg = newer.value - (double) timeDiffToNew / (newer.date - older.date) * bgDelta;
BgReading newBgreading = new BgReading(injector);
newBgreading.date = currentTime;
newBgreading.value = Math.round(currentBg);
InMemoryGlucoseValue newBgreading = new InMemoryGlucoseValue();
newBgreading.setTimestamp(currentTime);
newBgreading.setValue(Math.round(currentBg));
bucketed_data.add(newBgreading);
//log.debug("BG: " + newBgreading.value + " (" + new Date(newBgreading.date).toLocaleString() + ") Prev: " + older.value + " (" + new Date(older.date).toLocaleString() + ") Newer: " + newer.value + " (" + new Date(newer.date).toLocaleString() + ")");
}
@ -367,7 +367,7 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
}
bucketed_data = new ArrayList<>();
bucketed_data.add(bgReadings.get(0));
bucketed_data.add(new InMemoryGlucoseValue(bgReadings.get(0)));
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgReadings.get(0).date) + " lastbgTime: " + "none-first-value" + " " + bgReadings.get(0).toString());
int j = 0;
for (int i = 1; i < bgReadings.size(); ++i) {
@ -388,12 +388,12 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
while (elapsed_minutes > 5) {
nextbgTime = lastbgTime - 5 * 60 * 1000;
j++;
BgReading newBgreading = new BgReading(injector);
newBgreading.date = nextbgTime;
InMemoryGlucoseValue newBgreading = new InMemoryGlucoseValue();
newBgreading.setTimestamp(nextbgTime);
double gapDelta = bgReadings.get(i).value - lastbg;
//console.error(gapDelta, lastbg, elapsed_minutes);
double nextbg = lastbg + (5d / elapsed_minutes * gapDelta);
newBgreading.value = Math.round(nextbg);
newBgreading.setValue(Math.round(nextbg));
//console.error("Interpolated", bucketed_data[j]);
bucketed_data.add(newBgreading);
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgTime) + " lastbgTime: " + DateUtil.toISOString(lastbgTime) + " " + newBgreading.toString());
@ -403,38 +403,38 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
lastbgTime = nextbgTime;
}
j++;
BgReading newBgreading = new BgReading(injector);
newBgreading.value = bgReadings.get(i).value;
newBgreading.date = bgTime;
InMemoryGlucoseValue newBgreading = new InMemoryGlucoseValue();
newBgreading.setValue(bgReadings.get(i).value);
newBgreading.setTimestamp(bgTime);
bucketed_data.add(newBgreading);
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgTime) + " lastbgTime: " + DateUtil.toISOString(lastbgTime) + " " + newBgreading.toString());
} else if (Math.abs(elapsed_minutes) > 2) {
j++;
BgReading newBgreading = new BgReading(injector);
newBgreading.value = bgReadings.get(i).value;
newBgreading.date = bgTime;
InMemoryGlucoseValue newBgreading = new InMemoryGlucoseValue();
newBgreading.setValue(bgReadings.get(i).value);
newBgreading.setTimestamp(bgTime);
bucketed_data.add(newBgreading);
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgTime) + " lastbgTime: " + DateUtil.toISOString(lastbgTime) + " " + newBgreading.toString());
} else {
bucketed_data.get(j).value = (bucketed_data.get(j).value + bgReadings.get(i).value) / 2;
bucketed_data.get(j).setValue((bucketed_data.get(j).getValue() + bgReadings.get(i).value) / 2);
//log.error("***** Average");
}
}
// Normalize bucketed data
for (int i = bucketed_data.size() - 2; i >= 0; i--) {
BgReading current = bucketed_data.get(i);
BgReading previous = bucketed_data.get(i + 1);
long msecDiff = current.date - previous.date;
InMemoryGlucoseValue current = bucketed_data.get(i);
InMemoryGlucoseValue previous = bucketed_data.get(i + 1);
long msecDiff = current.getTimestamp() - previous.getTimestamp();
long adjusted = (msecDiff - T.mins(5).msecs()) / 1000;
getAapsLogger().debug(LTag.AUTOSENS, "Adjusting bucketed data time. Current: " + DateUtil.toISOString(current.date) + " to: " + DateUtil.toISOString(previous.date + T.mins(5).msecs()) + " by " + adjusted + " sec");
getAapsLogger().debug(LTag.AUTOSENS, "Adjusting bucketed data time. Current: " + DateUtil.toISOString(current.getTimestamp()) + " to: " + DateUtil.toISOString(previous.getTimestamp() + T.mins(5).msecs()) + " by " + adjusted + " sec");
if (Math.abs(adjusted) > 90) {
// too big adjustment, fallback to non 5 min data
getAapsLogger().debug(LTag.AUTOSENS, "Fallback to non 5 min data");
createBucketedDataRecalculated();
return;
}
current.date = previous.date + T.mins(5).msecs();
current.setTimestamp(previous.getTimestamp() + T.mins(5).msecs());
}
getAapsLogger().debug(LTag.AUTOSENS, "Bucketed data created. Size: " + bucketed_data.size());
@ -551,8 +551,8 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
if (bucketed_data == null)
return null;
for (int index = 0; index < bucketed_data.size(); index++) {
if (bucketed_data.get(index).date <= time)
return bucketed_data.get(index).date;
if (bucketed_data.get(index).getTimestamp() <= time)
return bucketed_data.get(index).getTimestamp();
}
return null;
}

View file

@ -119,7 +119,7 @@ public class IobCobOref1Thread extends Thread {
iobCobCalculatorPlugin.createBucketedData();
rxBus.send(new EventAutosensBgLoaded(cause));
}
List<BgReading> bucketed_data = iobCobCalculatorPlugin.getBucketedData();
List<InMemoryGlucoseValue> bucketed_data = iobCobCalculatorPlugin.getBucketedData();
LongSparseArray<AutosensData> autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable();
if (bucketed_data == null || bucketed_data.size() < 3) {
@ -127,7 +127,7 @@ public class IobCobOref1Thread extends Thread {
return;
}
long prevDataTime = IobCobCalculatorPlugin.roundUpTime(bucketed_data.get(bucketed_data.size() - 3).date);
long prevDataTime = IobCobCalculatorPlugin.roundUpTime(bucketed_data.get(bucketed_data.size() - 3).getTimestamp());
aapsLogger.debug(LTag.AUTOSENS, "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime));
AutosensData previous = autosensDataTable.get(prevDataTime);
// start from oldest to be able sub cob
@ -141,7 +141,7 @@ public class IobCobOref1Thread extends Thread {
return;
}
// check if data already exists
long bgTime = bucketed_data.get(i).date;
long bgTime = bucketed_data.get(i).getTimestamp();
bgTime = IobCobCalculatorPlugin.roundUpTime(bgTime);
if (bgTime > IobCobCalculatorPlugin.roundUpTime(now()))
continue;
@ -173,14 +173,14 @@ public class IobCobOref1Thread extends Thread {
double bg;
double avgDelta;
double delta;
bg = bucketed_data.get(i).value;
if (bg < 39 || bucketed_data.get(i + 3).value < 39) {
bg = bucketed_data.get(i).getValue();
if (bg < 39 || bucketed_data.get(i + 3).getValue() < 39) {
aapsLogger.error("! value < 39");
continue;
}
autosensData.bg = bg;
delta = (bg - bucketed_data.get(i + 1).value);
avgDelta = (bg - bucketed_data.get(i + 3).value) / 3;
delta = (bg - bucketed_data.get(i + 1).getValue());
avgDelta = (bg - bucketed_data.get(i + 3).getValue()) / 3;
IobTotal iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile);

View file

@ -116,7 +116,7 @@ public class IobCobThread extends Thread {
iobCobCalculatorPlugin.createBucketedData();
rxBus.send(new EventAutosensBgLoaded(cause));
}
List<BgReading> bucketed_data = iobCobCalculatorPlugin.getBucketedData();
List<InMemoryGlucoseValue> bucketed_data = iobCobCalculatorPlugin.getBucketedData();
LongSparseArray<AutosensData> autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable();
if (bucketed_data == null || bucketed_data.size() < 3) {
@ -124,7 +124,7 @@ public class IobCobThread extends Thread {
return;
}
long prevDataTime = IobCobCalculatorPlugin.roundUpTime(bucketed_data.get(bucketed_data.size() - 3).date);
long prevDataTime = IobCobCalculatorPlugin.roundUpTime(bucketed_data.get(bucketed_data.size() - 3).getTimestamp());
aapsLogger.debug(LTag.AUTOSENS, "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime));
AutosensData previous = autosensDataTable.get(prevDataTime);
// start from oldest to be able sub cob
@ -138,7 +138,7 @@ public class IobCobThread extends Thread {
return;
}
// check if data already exists
long bgTime = bucketed_data.get(i).date;
long bgTime = bucketed_data.get(i).getTimestamp();
bgTime = IobCobCalculatorPlugin.roundUpTime(bgTime);
if (bgTime > IobCobCalculatorPlugin.roundUpTime(now()))
continue;
@ -170,14 +170,14 @@ public class IobCobThread extends Thread {
double bg;
double avgDelta;
double delta;
bg = bucketed_data.get(i).value;
if (bg < 39 || bucketed_data.get(i + 3).value < 39) {
bg = bucketed_data.get(i).getValue();
if (bg < 39 || bucketed_data.get(i + 3).getValue() < 39) {
aapsLogger.error("! value < 39");
continue;
}
autosensData.bg = bg;
delta = (bg - bucketed_data.get(i + 1).value);
avgDelta = (bg - bucketed_data.get(i + 3).value) / 3;
delta = (bg - bucketed_data.get(i + 1).getValue());
avgDelta = (bg - bucketed_data.get(i + 3).getValue()) / 3;
IobTotal iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile);