reworked to use DBHelper
This commit is contained in:
parent
62b2d83a18
commit
6e63d340cd
|
@ -7,6 +7,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import info.nightscout.androidaps.Constants;
|
import info.nightscout.androidaps.Constants;
|
||||||
|
@ -18,8 +19,8 @@ import info.nightscout.androidaps.plugins.general.nsclient.data.NSSgv;
|
||||||
import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin;
|
import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin;
|
||||||
import info.nightscout.androidaps.plugins.general.overview.graphExtensions.DataPointWithLabelInterface;
|
import info.nightscout.androidaps.plugins.general.overview.graphExtensions.DataPointWithLabelInterface;
|
||||||
import info.nightscout.androidaps.plugins.general.overview.graphExtensions.PointsWithLabelGraphSeries;
|
import info.nightscout.androidaps.plugins.general.overview.graphExtensions.PointsWithLabelGraphSeries;
|
||||||
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
|
|
||||||
import info.nightscout.androidaps.utils.DecimalFormatter;
|
import info.nightscout.androidaps.utils.DecimalFormatter;
|
||||||
|
import info.nightscout.androidaps.utils.T;
|
||||||
|
|
||||||
@DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS)
|
@DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS)
|
||||||
public class BgReading implements DataPointWithLabelInterface {
|
public class BgReading implements DataPointWithLabelInterface {
|
||||||
|
@ -249,19 +250,29 @@ public class BgReading implements DataPointWithLabelInterface {
|
||||||
|
|
||||||
// Copied from xDrip+
|
// Copied from xDrip+
|
||||||
String calculateDirection() {
|
String calculateDirection() {
|
||||||
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
|
// Rework to get bgreaings from internal DB and calculate on that base
|
||||||
double slope;
|
|
||||||
if (glucoseStatus == null || glucoseStatus.prev_glucose == 0)
|
List<BgReading> bgReadingsList = MainApp.getDbHelper().getAllBgreadingsDataFromTime(this.date - T.mins(10).msecs(), false);
|
||||||
|
if (bgReadingsList == null || bgReadingsList.size() < 2)
|
||||||
return "NONE";
|
return "NONE";
|
||||||
|
BgReading current = bgReadingsList.get(1);
|
||||||
|
BgReading previous = bgReadingsList.get(0);
|
||||||
|
|
||||||
|
if (bgReadingsList.get(1).date < bgReadingsList.get(0).date) {
|
||||||
|
current = bgReadingsList.get(0);
|
||||||
|
previous = bgReadingsList.get(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
double slope;
|
||||||
|
|
||||||
// Avoid division by 0
|
// Avoid division by 0
|
||||||
if (glucoseStatus.date == glucoseStatus.previous_date)
|
if (current.date == previous.date)
|
||||||
slope = 0;
|
slope = 0;
|
||||||
else
|
else
|
||||||
slope = (glucoseStatus.prev_glucose - glucoseStatus.glucose) / (glucoseStatus.previous_date - glucoseStatus.date);
|
slope = (previous.value - current.value) / (previous.date - current.date);
|
||||||
|
|
||||||
if (L.isEnabled(L.GLUCOSE))
|
if (L.isEnabled(L.GLUCOSE))
|
||||||
log.debug("Slope is :" + slope + " delta " + glucoseStatus.delta + " date difference " + (glucoseStatus.date - glucoseStatus.previous_date));
|
log.debug("Slope is :" + slope + " delta " + (previous.value - current.value) + " date difference " + (current.date - previous.date));
|
||||||
|
|
||||||
double slope_by_minute = slope * 60000;
|
double slope_by_minute = slope * 60000;
|
||||||
String arrow = "NONE";
|
String arrow = "NONE";
|
||||||
|
|
|
@ -26,8 +26,6 @@ public class GlucoseStatus {
|
||||||
public double short_avgdelta = 0d;
|
public double short_avgdelta = 0d;
|
||||||
public double long_avgdelta = 0d;
|
public double long_avgdelta = 0d;
|
||||||
public long date = 0L;
|
public long date = 0L;
|
||||||
public long previous_date = 0L;
|
|
||||||
public double prev_glucose = 0d;
|
|
||||||
|
|
||||||
|
|
||||||
public String log() {
|
public String log() {
|
||||||
|
@ -60,8 +58,6 @@ public class GlucoseStatus {
|
||||||
// load 45min
|
// load 45min
|
||||||
//long fromtime = DateUtil.now() - 60 * 1000L * 45;
|
//long fromtime = DateUtil.now() - 60 * 1000L * 45;
|
||||||
//List<BgReading> data = MainApp.getDbHelper().getBgreadingsDataFromTime(fromtime, false);
|
//List<BgReading> data = MainApp.getDbHelper().getBgreadingsDataFromTime(fromtime, false);
|
||||||
long prevDate = 0;
|
|
||||||
double prevValue = 0;
|
|
||||||
|
|
||||||
synchronized (IobCobCalculatorPlugin.getPlugin().getDataLock()) {
|
synchronized (IobCobCalculatorPlugin.getPlugin().getDataLock()) {
|
||||||
|
|
||||||
|
@ -98,9 +94,6 @@ public class GlucoseStatus {
|
||||||
status.long_avgdelta = 0d;
|
status.long_avgdelta = 0d;
|
||||||
status.avgdelta = 0d; // for OpenAPS MA
|
status.avgdelta = 0d; // for OpenAPS MA
|
||||||
status.date = now_date;
|
status.date = now_date;
|
||||||
status.previous_date = 0; // setting the previous value date for slope calculation
|
|
||||||
status.prev_glucose = 0;
|
|
||||||
|
|
||||||
if (L.isEnabled(L.GLUCOSE))
|
if (L.isEnabled(L.GLUCOSE))
|
||||||
log.debug("sizeRecords==1");
|
log.debug("sizeRecords==1");
|
||||||
return status.round();
|
return status.round();
|
||||||
|
@ -125,11 +118,6 @@ public class GlucoseStatus {
|
||||||
// multiply by 5 to get the same units as delta, i.e. mg/dL/5m
|
// multiply by 5 to get the same units as delta, i.e. mg/dL/5m
|
||||||
change = now.value - then.value;
|
change = now.value - then.value;
|
||||||
avgdelta = change / minutesago * 5;
|
avgdelta = change / minutesago * 5;
|
||||||
// save the value of date if it was 5 min ago or less than 10 min
|
|
||||||
if( minutesago >= 5 && minutesago < 10 ) {
|
|
||||||
prevDate = then_date;
|
|
||||||
prevValue = then.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (L.isEnabled(L.GLUCOSE))
|
if (L.isEnabled(L.GLUCOSE))
|
||||||
log.debug(then.toString() + " minutesago=" + minutesago + " avgdelta=" + avgdelta);
|
log.debug(then.toString() + " minutesago=" + minutesago + " avgdelta=" + avgdelta);
|
||||||
|
@ -171,8 +159,6 @@ public class GlucoseStatus {
|
||||||
|
|
||||||
status.long_avgdelta = average(long_deltas);
|
status.long_avgdelta = average(long_deltas);
|
||||||
status.avgdelta = status.short_avgdelta; // for OpenAPS MA
|
status.avgdelta = status.short_avgdelta; // for OpenAPS MA
|
||||||
status.previous_date = prevDate; // setting the previous value date for slope calculation
|
|
||||||
status.prev_glucose = prevValue;
|
|
||||||
|
|
||||||
if (L.isEnabled(L.GLUCOSE))
|
if (L.isEnabled(L.GLUCOSE))
|
||||||
log.debug(status.log());
|
log.debug(status.log());
|
||||||
|
|
|
@ -3,15 +3,16 @@ package info.nightscout.androidaps.db;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.powermock.api.mockito.PowerMockito;
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
import org.powermock.modules.junit4.PowerMockRunner;
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import edu.emory.mathcs.backport.java.util.Arrays;
|
||||||
import info.AAPSMocker;
|
import info.AAPSMocker;
|
||||||
import info.nightscout.androidaps.Constants;
|
import info.nightscout.androidaps.Constants;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
|
@ -22,6 +23,8 @@ import info.nightscout.androidaps.utils.SP;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(PowerMockRunner.class)
|
||||||
|
@ -110,41 +113,32 @@ public class BgReadingTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void calculateDirection() {
|
public void calculateDirection() {
|
||||||
|
List<BgReading> bgReadingsList = null;
|
||||||
|
AAPSMocker.mockDatabaseHelper();
|
||||||
|
|
||||||
|
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
|
||||||
assertEquals("NONE", bgReading.calculateDirection());
|
assertEquals("NONE", bgReading.calculateDirection());
|
||||||
|
bgReadingsList = setReadings(72,0);
|
||||||
glucoseStatus = new GlucoseStatus();
|
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
|
||||||
glucoseStatus.glucose = 0;
|
assertEquals("DoubleUp", bgReading.calculateDirection());
|
||||||
glucoseStatus.prev_glucose = 0;
|
bgReadingsList = setReadings(76,60);
|
||||||
glucoseStatus.date = 1000L * 60 * 12;
|
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
|
||||||
glucoseStatus.previous_date = 1000L * 60 * 6;
|
assertEquals("SingleUp", bgReading.calculateDirection());
|
||||||
|
bgReadingsList = setReadings(74,65);
|
||||||
BgReading newReading = new BgReading();
|
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
|
||||||
|
assertEquals("FortyFiveUp", bgReading.calculateDirection());
|
||||||
PowerMockito.mockStatic(GlucoseStatus.class);
|
bgReadingsList = setReadings(72,72);
|
||||||
when(GlucoseStatus.getGlucoseStatusData()).thenReturn(glucoseStatus);
|
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
|
||||||
|
assertEquals("Flat", bgReading.calculateDirection());
|
||||||
assertEquals("NONE", newReading.calculateDirection());
|
bgReadingsList = setReadings(0,72);
|
||||||
glucoseStatus.glucose = 72;
|
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
|
||||||
glucoseStatus.prev_glucose = 10;
|
assertEquals("DoubleDown", bgReading.calculateDirection());
|
||||||
assertEquals("DoubleUp", newReading.calculateDirection());
|
bgReadingsList = setReadings(60,76);
|
||||||
glucoseStatus.glucose = 72;
|
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
|
||||||
glucoseStatus.prev_glucose = 55;
|
assertEquals("SingleDown", bgReading.calculateDirection());
|
||||||
assertEquals("SingleUp", newReading.calculateDirection());
|
bgReadingsList = setReadings(65,74);
|
||||||
glucoseStatus.glucose = 72;
|
when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList);
|
||||||
glucoseStatus.prev_glucose = 65;
|
assertEquals("FortyFiveDown", bgReading.calculateDirection());
|
||||||
assertEquals("FortyFiveUp", newReading.calculateDirection());
|
|
||||||
glucoseStatus.glucose = 72;
|
|
||||||
glucoseStatus.prev_glucose = 70;
|
|
||||||
assertEquals("Flat", newReading.calculateDirection());
|
|
||||||
glucoseStatus.glucose = 10;
|
|
||||||
glucoseStatus.prev_glucose = 72;
|
|
||||||
assertEquals("DoubleDown", newReading.calculateDirection());
|
|
||||||
glucoseStatus.glucose = 55;
|
|
||||||
glucoseStatus.prev_glucose = 72;
|
|
||||||
assertEquals("SingleDown", newReading.calculateDirection());
|
|
||||||
glucoseStatus.glucose = 65;
|
|
||||||
glucoseStatus.prev_glucose = 72;
|
|
||||||
assertEquals("FortyFiveDown", newReading.calculateDirection());
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -155,5 +149,20 @@ public class BgReadingTest {
|
||||||
AAPSMocker.mockApplicationContext();
|
AAPSMocker.mockApplicationContext();
|
||||||
AAPSMocker.mockSP();
|
AAPSMocker.mockSP();
|
||||||
AAPSMocker.mockL();
|
AAPSMocker.mockL();
|
||||||
|
AAPSMocker.mockDatabaseHelper();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BgReading> setReadings(int current_value, int previous_value){
|
||||||
|
BgReading now = new BgReading();
|
||||||
|
now.value = current_value;
|
||||||
|
now.date = System.currentTimeMillis();
|
||||||
|
BgReading previous = new BgReading();
|
||||||
|
previous.value = previous_value;
|
||||||
|
previous.date = System.currentTimeMillis() - ( 6 * 60 * 1000L);
|
||||||
|
List<BgReading> bgReadings = new ArrayList() {{
|
||||||
|
add(now);
|
||||||
|
add(previous);
|
||||||
|
}};
|
||||||
|
return bgReadings;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue