This commit is contained in:
Milos Kozak 2019-05-10 08:47:32 +02:00 committed by Roumen Georgiev
parent 85bf1fd6bd
commit 62b2d83a18
3 changed files with 52 additions and 50 deletions

View file

@ -23,7 +23,7 @@ import info.nightscout.androidaps.utils.DecimalFormatter;
@DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS) @DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS)
public class BgReading implements DataPointWithLabelInterface { public class BgReading implements DataPointWithLabelInterface {
private static Logger log = LoggerFactory.getLogger(L.DATABASE); private static Logger log = LoggerFactory.getLogger(L.GLUCOSE);
@DatabaseField(id = true) @DatabaseField(id = true)
public long date; public long date;
@ -74,10 +74,10 @@ public class BgReading implements DataPointWithLabelInterface {
public String directionToSymbol() { public String directionToSymbol() {
String symbol = ""; String symbol = "";
if (direction == null) { if (direction == null)
direction = calculateDirection(); direction = calculateDirection();
this.directionToSymbol(); // possible endless loop ?!?
} else if (direction.compareTo("DoubleDown") == 0) { if (direction.compareTo("DoubleDown") == 0) {
symbol = "\u21ca"; symbol = "\u21ca";
} else if (direction.compareTo("SingleDown") == 0) { } else if (direction.compareTo("SingleDown") == 0) {
symbol = "\u2193"; symbol = "\u2193";
@ -97,18 +97,13 @@ public class BgReading implements DataPointWithLabelInterface {
return symbol; return symbol;
} }
public static boolean isSlopeNameInvalid(String direction) { private static boolean isSlopeNameInvalid(String direction) {
if (direction.compareTo("NOT_COMPUTABLE") == 0 || return direction.compareTo("NOT_COMPUTABLE") == 0 ||
direction.compareTo("NOT COMPUTABLE") == 0 || direction.compareTo("NOT COMPUTABLE") == 0 ||
direction.compareTo("OUT_OF_RANGE") == 0 || direction.compareTo("OUT_OF_RANGE") == 0 ||
direction.compareTo("OUT OF RANGE") == 0 || direction.compareTo("OUT OF RANGE") == 0 ||
direction.compareTo("NONE") == 0 || direction.compareTo("NONE") == 0 ||
direction.compareTo("NotComputable") == 0 direction.compareTo("NotComputable") == 0;
) {
return true;
} else {
return false;
}
} }
@ -125,6 +120,7 @@ public class BgReading implements DataPointWithLabelInterface {
public boolean isDataChanging(BgReading other) { public boolean isDataChanging(BgReading other) {
if (date != other.date) { if (date != other.date) {
if (L.isEnabled(L.GLUCOSE))
log.error("Comparing different"); log.error("Comparing different");
return false; return false;
} }
@ -135,6 +131,7 @@ public class BgReading implements DataPointWithLabelInterface {
public boolean isEqual(BgReading other) { public boolean isEqual(BgReading other) {
if (date != other.date) { if (date != other.date) {
if (L.isEnabled(L.GLUCOSE))
log.error("Comparing different"); log.error("Comparing different");
return false; return false;
} }
@ -151,6 +148,7 @@ public class BgReading implements DataPointWithLabelInterface {
public void copyFrom(BgReading other) { public void copyFrom(BgReading other) {
if (date != other.date) { if (date != other.date) {
if (L.isEnabled(L.GLUCOSE))
log.error("Copying different"); log.error("Copying different");
return; return;
} }
@ -250,18 +248,21 @@ public class BgReading implements DataPointWithLabelInterface {
// Copied from xDrip+ // Copied from xDrip+
public String calculateDirection(){ String calculateDirection() {
GlucoseStatus glucoseStatus = getGlucoseStatus(); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
double slope = 0; double slope;
if (glucoseStatus == null || glucoseStatus.prev_glucose == 0) if (glucoseStatus == null || glucoseStatus.prev_glucose == 0)
return "??"; return "NONE";
// Avoid division by 0 // Avoid division by 0
if (glucoseStatus.date == glucoseStatus.previous_date) if (glucoseStatus.date == glucoseStatus.previous_date)
slope = 0; slope = 0;
else else
slope = (glucoseStatus.prev_glucose - glucoseStatus.glucose) / (glucoseStatus.previous_date - glucoseStatus.date); slope = (glucoseStatus.prev_glucose - glucoseStatus.glucose) / (glucoseStatus.previous_date - glucoseStatus.date);
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 " + glucoseStatus.delta + " date difference " + (glucoseStatus.date - glucoseStatus.previous_date));
double slope_by_minute = slope * 60000; double slope_by_minute = slope * 60000;
String arrow = "NONE"; String arrow = "NONE";
@ -280,14 +281,8 @@ public class BgReading implements DataPointWithLabelInterface {
} else if (slope_by_minute <= (40)) { } else if (slope_by_minute <= (40)) {
arrow = "DoubleUp"; arrow = "DoubleUp";
} }
if (L.isEnabled(L.GLUCOSE))
log.debug("Direction set to: " + arrow); log.debug("Direction set to: " + arrow);
return arrow; return arrow;
} }
// Used for testing purpose
protected GlucoseStatus getGlucoseStatus() {
return GlucoseStatus.getGlucoseStatusData();
}
} }

View file

@ -98,8 +98,8 @@ 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 = prevDate; // setting the previous value date for slope calculation status.previous_date = 0; // setting the previous value date for slope calculation
status.prev_glucose = prevValue; status.prev_glucose = 0;
if (L.isEnabled(L.GLUCOSE)) if (L.isEnabled(L.GLUCOSE))
log.debug("sizeRecords==1"); log.debug("sizeRecords==1");
@ -171,6 +171,8 @@ 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());

View file

@ -5,6 +5,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
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;
@ -18,14 +19,16 @@ import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SP;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.doReturn; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Logger.class, L.class, SP.class}) @PrepareForTest({MainApp.class, Logger.class, L.class, SP.class, GlucoseStatus.class})
public class BgReadingTest { public class BgReadingTest {
private BgReading bgReading = new BgReading(); private BgReading bgReading = new BgReading();
@Mock
GlucoseStatus glucoseStatus; GlucoseStatus glucoseStatus;
@Test @Test
@ -101,23 +104,26 @@ public class BgReadingTest {
copy.copyFrom(bgReading); copy.copyFrom(bgReading);
assertEquals(true, copy.isEqual(bgReading)); assertTrue(copy.isEqual(bgReading));
assertEquals(false, copy.isEqual(new BgReading())); assertFalse(copy.isEqual(new BgReading()));
} }
@Test @Test
public void calculateDirection() throws Exception { public void calculateDirection() {
assertEquals("??", bgReading.calculateDirection()); assertEquals("NONE", bgReading.calculateDirection());
bgReading = new BgReading();
glucoseStatus = new GlucoseStatus(); glucoseStatus = new GlucoseStatus();
glucoseStatus.glucose = 0; glucoseStatus.glucose = 0;
glucoseStatus.prev_glucose = 0; glucoseStatus.prev_glucose = 0;
glucoseStatus.date = 1000L * 60 * 12;; glucoseStatus.date = 1000L * 60 * 12;
glucoseStatus.previous_date = 1000L * 60 * 6; glucoseStatus.previous_date = 1000L * 60 * 6;
BgReading newReading = Mockito.spy(new BgReading());
doReturn(glucoseStatus).when(newReading).getGlucoseStatus(); BgReading newReading = new BgReading();
assertEquals("??", newReading.calculateDirection());
PowerMockito.mockStatic(GlucoseStatus.class);
when(GlucoseStatus.getGlucoseStatusData()).thenReturn(glucoseStatus);
assertEquals("NONE", newReading.calculateDirection());
glucoseStatus.glucose = 72; glucoseStatus.glucose = 72;
glucoseStatus.prev_glucose = 10; glucoseStatus.prev_glucose = 10;
assertEquals("DoubleUp", newReading.calculateDirection()); assertEquals("DoubleUp", newReading.calculateDirection());
@ -141,7 +147,6 @@ public class BgReadingTest {
assertEquals("FortyFiveDown", newReading.calculateDirection()); assertEquals("FortyFiveDown", newReading.calculateDirection());
} }
@Before @Before