InMemoryGlucoseValue
This commit is contained in:
parent
484f3a2a98
commit
15378fb2b8
4 changed files with 46 additions and 38 deletions
|
@ -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)
|
||||||
|
}
|
|
@ -80,7 +80,7 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
|
||||||
private LongSparseArray<BasalData> basalDataTable = new LongSparseArray<>(); // oldest at index 0
|
private LongSparseArray<BasalData> basalDataTable = new LongSparseArray<>(); // oldest at index 0
|
||||||
|
|
||||||
private volatile List<BgReading> bgReadings = null; // newest 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();
|
private final Object dataLock = new Object();
|
||||||
|
|
||||||
|
@ -232,7 +232,7 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
|
||||||
this.bgReadings = bgReadings;
|
this.bgReadings = bgReadings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<BgReading> getBucketedData() {
|
public List<InMemoryGlucoseValue> getBucketedData() {
|
||||||
return bucketed_data;
|
return bucketed_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,15 +342,15 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (older.date == newer.date) { // direct hit
|
if (older.date == newer.date) { // direct hit
|
||||||
bucketed_data.add(newer);
|
bucketed_data.add(new InMemoryGlucoseValue(newer));
|
||||||
} else {
|
} else {
|
||||||
double bgDelta = newer.value - older.value;
|
double bgDelta = newer.value - older.value;
|
||||||
long timeDiffToNew = newer.date - currentTime;
|
long timeDiffToNew = newer.date - currentTime;
|
||||||
|
|
||||||
double currentBg = newer.value - (double) timeDiffToNew / (newer.date - older.date) * bgDelta;
|
double currentBg = newer.value - (double) timeDiffToNew / (newer.date - older.date) * bgDelta;
|
||||||
BgReading newBgreading = new BgReading(injector);
|
InMemoryGlucoseValue newBgreading = new InMemoryGlucoseValue();
|
||||||
newBgreading.date = currentTime;
|
newBgreading.setTimestamp(currentTime);
|
||||||
newBgreading.value = Math.round(currentBg);
|
newBgreading.setValue(Math.round(currentBg));
|
||||||
bucketed_data.add(newBgreading);
|
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() + ")");
|
//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 = 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());
|
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgReadings.get(0).date) + " lastbgTime: " + "none-first-value" + " " + bgReadings.get(0).toString());
|
||||||
int j = 0;
|
int j = 0;
|
||||||
for (int i = 1; i < bgReadings.size(); ++i) {
|
for (int i = 1; i < bgReadings.size(); ++i) {
|
||||||
|
@ -388,12 +388,12 @@ public class IobCobCalculatorPlugin extends PluginBase implements IobCobCalculat
|
||||||
while (elapsed_minutes > 5) {
|
while (elapsed_minutes > 5) {
|
||||||
nextbgTime = lastbgTime - 5 * 60 * 1000;
|
nextbgTime = lastbgTime - 5 * 60 * 1000;
|
||||||
j++;
|
j++;
|
||||||
BgReading newBgreading = new BgReading(injector);
|
InMemoryGlucoseValue newBgreading = new InMemoryGlucoseValue();
|
||||||
newBgreading.date = nextbgTime;
|
newBgreading.setTimestamp(nextbgTime);
|
||||||
double gapDelta = bgReadings.get(i).value - lastbg;
|
double gapDelta = bgReadings.get(i).value - lastbg;
|
||||||
//console.error(gapDelta, lastbg, elapsed_minutes);
|
//console.error(gapDelta, lastbg, elapsed_minutes);
|
||||||
double nextbg = lastbg + (5d / elapsed_minutes * gapDelta);
|
double nextbg = lastbg + (5d / elapsed_minutes * gapDelta);
|
||||||
newBgreading.value = Math.round(nextbg);
|
newBgreading.setValue(Math.round(nextbg));
|
||||||
//console.error("Interpolated", bucketed_data[j]);
|
//console.error("Interpolated", bucketed_data[j]);
|
||||||
bucketed_data.add(newBgreading);
|
bucketed_data.add(newBgreading);
|
||||||
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgTime) + " lastbgTime: " + DateUtil.toISOString(lastbgTime) + " " + newBgreading.toString());
|
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;
|
lastbgTime = nextbgTime;
|
||||||
}
|
}
|
||||||
j++;
|
j++;
|
||||||
BgReading newBgreading = new BgReading(injector);
|
InMemoryGlucoseValue newBgreading = new InMemoryGlucoseValue();
|
||||||
newBgreading.value = bgReadings.get(i).value;
|
newBgreading.setValue(bgReadings.get(i).value);
|
||||||
newBgreading.date = bgTime;
|
newBgreading.setTimestamp(bgTime);
|
||||||
bucketed_data.add(newBgreading);
|
bucketed_data.add(newBgreading);
|
||||||
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgTime) + " lastbgTime: " + DateUtil.toISOString(lastbgTime) + " " + newBgreading.toString());
|
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgTime) + " lastbgTime: " + DateUtil.toISOString(lastbgTime) + " " + newBgreading.toString());
|
||||||
} else if (Math.abs(elapsed_minutes) > 2) {
|
} else if (Math.abs(elapsed_minutes) > 2) {
|
||||||
j++;
|
j++;
|
||||||
BgReading newBgreading = new BgReading(injector);
|
InMemoryGlucoseValue newBgreading = new InMemoryGlucoseValue();
|
||||||
newBgreading.value = bgReadings.get(i).value;
|
newBgreading.setValue(bgReadings.get(i).value);
|
||||||
newBgreading.date = bgTime;
|
newBgreading.setTimestamp(bgTime);
|
||||||
bucketed_data.add(newBgreading);
|
bucketed_data.add(newBgreading);
|
||||||
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgTime) + " lastbgTime: " + DateUtil.toISOString(lastbgTime) + " " + newBgreading.toString());
|
getAapsLogger().debug(LTag.AUTOSENS, "Adding. bgTime: " + DateUtil.toISOString(bgTime) + " lastbgTime: " + DateUtil.toISOString(lastbgTime) + " " + newBgreading.toString());
|
||||||
} else {
|
} 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");
|
//log.error("***** Average");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize bucketed data
|
// Normalize bucketed data
|
||||||
for (int i = bucketed_data.size() - 2; i >= 0; i--) {
|
for (int i = bucketed_data.size() - 2; i >= 0; i--) {
|
||||||
BgReading current = bucketed_data.get(i);
|
InMemoryGlucoseValue current = bucketed_data.get(i);
|
||||||
BgReading previous = bucketed_data.get(i + 1);
|
InMemoryGlucoseValue previous = bucketed_data.get(i + 1);
|
||||||
long msecDiff = current.date - previous.date;
|
long msecDiff = current.getTimestamp() - previous.getTimestamp();
|
||||||
long adjusted = (msecDiff - T.mins(5).msecs()) / 1000;
|
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) {
|
if (Math.abs(adjusted) > 90) {
|
||||||
// too big adjustment, fallback to non 5 min data
|
// too big adjustment, fallback to non 5 min data
|
||||||
getAapsLogger().debug(LTag.AUTOSENS, "Fallback to non 5 min data");
|
getAapsLogger().debug(LTag.AUTOSENS, "Fallback to non 5 min data");
|
||||||
createBucketedDataRecalculated();
|
createBucketedDataRecalculated();
|
||||||
return;
|
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());
|
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)
|
if (bucketed_data == null)
|
||||||
return null;
|
return null;
|
||||||
for (int index = 0; index < bucketed_data.size(); index++) {
|
for (int index = 0; index < bucketed_data.size(); index++) {
|
||||||
if (bucketed_data.get(index).date <= time)
|
if (bucketed_data.get(index).getTimestamp() <= time)
|
||||||
return bucketed_data.get(index).date;
|
return bucketed_data.get(index).getTimestamp();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,7 @@ public class IobCobOref1Thread extends Thread {
|
||||||
iobCobCalculatorPlugin.createBucketedData();
|
iobCobCalculatorPlugin.createBucketedData();
|
||||||
rxBus.send(new EventAutosensBgLoaded(cause));
|
rxBus.send(new EventAutosensBgLoaded(cause));
|
||||||
}
|
}
|
||||||
List<BgReading> bucketed_data = iobCobCalculatorPlugin.getBucketedData();
|
List<InMemoryGlucoseValue> bucketed_data = iobCobCalculatorPlugin.getBucketedData();
|
||||||
LongSparseArray<AutosensData> autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable();
|
LongSparseArray<AutosensData> autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable();
|
||||||
|
|
||||||
if (bucketed_data == null || bucketed_data.size() < 3) {
|
if (bucketed_data == null || bucketed_data.size() < 3) {
|
||||||
|
@ -127,7 +127,7 @@ public class IobCobOref1Thread extends Thread {
|
||||||
return;
|
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));
|
aapsLogger.debug(LTag.AUTOSENS, "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime));
|
||||||
AutosensData previous = autosensDataTable.get(prevDataTime);
|
AutosensData previous = autosensDataTable.get(prevDataTime);
|
||||||
// start from oldest to be able sub cob
|
// start from oldest to be able sub cob
|
||||||
|
@ -141,7 +141,7 @@ public class IobCobOref1Thread extends Thread {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// check if data already exists
|
// check if data already exists
|
||||||
long bgTime = bucketed_data.get(i).date;
|
long bgTime = bucketed_data.get(i).getTimestamp();
|
||||||
bgTime = IobCobCalculatorPlugin.roundUpTime(bgTime);
|
bgTime = IobCobCalculatorPlugin.roundUpTime(bgTime);
|
||||||
if (bgTime > IobCobCalculatorPlugin.roundUpTime(now()))
|
if (bgTime > IobCobCalculatorPlugin.roundUpTime(now()))
|
||||||
continue;
|
continue;
|
||||||
|
@ -173,14 +173,14 @@ public class IobCobOref1Thread extends Thread {
|
||||||
double bg;
|
double bg;
|
||||||
double avgDelta;
|
double avgDelta;
|
||||||
double delta;
|
double delta;
|
||||||
bg = bucketed_data.get(i).value;
|
bg = bucketed_data.get(i).getValue();
|
||||||
if (bg < 39 || bucketed_data.get(i + 3).value < 39) {
|
if (bg < 39 || bucketed_data.get(i + 3).getValue() < 39) {
|
||||||
aapsLogger.error("! value < 39");
|
aapsLogger.error("! value < 39");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
autosensData.bg = bg;
|
autosensData.bg = bg;
|
||||||
delta = (bg - bucketed_data.get(i + 1).value);
|
delta = (bg - bucketed_data.get(i + 1).getValue());
|
||||||
avgDelta = (bg - bucketed_data.get(i + 3).value) / 3;
|
avgDelta = (bg - bucketed_data.get(i + 3).getValue()) / 3;
|
||||||
|
|
||||||
IobTotal iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile);
|
IobTotal iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile);
|
||||||
|
|
||||||
|
|
|
@ -116,7 +116,7 @@ public class IobCobThread extends Thread {
|
||||||
iobCobCalculatorPlugin.createBucketedData();
|
iobCobCalculatorPlugin.createBucketedData();
|
||||||
rxBus.send(new EventAutosensBgLoaded(cause));
|
rxBus.send(new EventAutosensBgLoaded(cause));
|
||||||
}
|
}
|
||||||
List<BgReading> bucketed_data = iobCobCalculatorPlugin.getBucketedData();
|
List<InMemoryGlucoseValue> bucketed_data = iobCobCalculatorPlugin.getBucketedData();
|
||||||
LongSparseArray<AutosensData> autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable();
|
LongSparseArray<AutosensData> autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable();
|
||||||
|
|
||||||
if (bucketed_data == null || bucketed_data.size() < 3) {
|
if (bucketed_data == null || bucketed_data.size() < 3) {
|
||||||
|
@ -124,7 +124,7 @@ public class IobCobThread extends Thread {
|
||||||
return;
|
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));
|
aapsLogger.debug(LTag.AUTOSENS, "Prev data time: " + dateUtil.dateAndTimeString(prevDataTime));
|
||||||
AutosensData previous = autosensDataTable.get(prevDataTime);
|
AutosensData previous = autosensDataTable.get(prevDataTime);
|
||||||
// start from oldest to be able sub cob
|
// start from oldest to be able sub cob
|
||||||
|
@ -138,7 +138,7 @@ public class IobCobThread extends Thread {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// check if data already exists
|
// check if data already exists
|
||||||
long bgTime = bucketed_data.get(i).date;
|
long bgTime = bucketed_data.get(i).getTimestamp();
|
||||||
bgTime = IobCobCalculatorPlugin.roundUpTime(bgTime);
|
bgTime = IobCobCalculatorPlugin.roundUpTime(bgTime);
|
||||||
if (bgTime > IobCobCalculatorPlugin.roundUpTime(now()))
|
if (bgTime > IobCobCalculatorPlugin.roundUpTime(now()))
|
||||||
continue;
|
continue;
|
||||||
|
@ -170,14 +170,14 @@ public class IobCobThread extends Thread {
|
||||||
double bg;
|
double bg;
|
||||||
double avgDelta;
|
double avgDelta;
|
||||||
double delta;
|
double delta;
|
||||||
bg = bucketed_data.get(i).value;
|
bg = bucketed_data.get(i).getValue();
|
||||||
if (bg < 39 || bucketed_data.get(i + 3).value < 39) {
|
if (bg < 39 || bucketed_data.get(i + 3).getValue() < 39) {
|
||||||
aapsLogger.error("! value < 39");
|
aapsLogger.error("! value < 39");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
autosensData.bg = bg;
|
autosensData.bg = bg;
|
||||||
delta = (bg - bucketed_data.get(i + 1).value);
|
delta = (bg - bucketed_data.get(i + 1).getValue());
|
||||||
avgDelta = (bg - bucketed_data.get(i + 3).value) / 3;
|
avgDelta = (bg - bucketed_data.get(i + 3).getValue()) / 3;
|
||||||
|
|
||||||
IobTotal iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile);
|
IobTotal iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue