BgReading implements DataPointWithLabelInterface { String calculateDirection() { List<BgReading> bgReadingsList = MainApp.getDbHelper().getAllBgreadingsDataFromTime(this.date - T.mins(10).msecs(), false); if (bgReadingsList == null || bgReadingsList.size() < 2) 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; if (current.date == previous.date) slope = 0; else slope = (previous.value - current.value) / (previous.date - current.date); if (L.isEnabled(L.GLUCOSE)) log.debug("Slope is :" + slope + " delta " + (previous.value - current.value) + " date difference " + (current.date - previous.date)); double slope_by_minute = slope * 60000; String arrow = "NONE"; if (slope_by_minute <= (-3.5)) { arrow = "DoubleDown"; } else if (slope_by_minute <= (-2)) { arrow = "SingleDown"; } else if (slope_by_minute <= (-1)) { arrow = "FortyFiveDown"; } else if (slope_by_minute <= (1)) { arrow = "Flat"; } else if (slope_by_minute <= (2)) { arrow = "FortyFiveUp"; } else if (slope_by_minute <= (3.5)) { arrow = "SingleUp"; } else if (slope_by_minute <= (40)) { arrow = "DoubleUp"; } if (L.isEnabled(L.GLUCOSE)) log.debug("Direction set to: " + arrow); return arrow; } BgReading();  BgReading(NSSgv sgv); Double valueToUnits(String units); String valueToUnitsToString(String units); String directionToSymbol(); @Override String toString(); boolean isDataChanging(BgReading other); boolean isEqual(BgReading other); void copyFrom(BgReading other); BgReading date(long date); BgReading date(Date date); BgReading value(double value); @Override double getX(); @Override double getY(); @Override void setY(double y); @Override String getLabel(); @Override long getDuration(); @Override PointsWithLabelGraphSeries.Shape getShape(); @Override float getSize(); @Override int getColor(); int getPredectionColor(); @DatabaseField(id = true)
    public long date; @DatabaseField
    public boolean isValid; @DatabaseField
    public double value; @DatabaseField
    public String direction; @DatabaseField
    public double raw; @DatabaseField
    public int source; @DatabaseField
    public String _id; public boolean isCOBPrediction; public boolean isaCOBPrediction; public boolean isIOBPrediction; public boolean isUAMPrediction; public boolean isZTPrediction; }
@Test public void calculateDirection() { List<BgReading> bgReadingsList = null; AAPSMocker.mockDatabaseHelper(); when(MainApp.getDbHelper().getAllBgreadingsDataFromTime(anyLong(),anyBoolean())).thenReturn(bgReadingsList); assertEquals("NONE", bgReading.calculateDirection()); setReadings(72,0); assertEquals("DoubleUp", bgReading.calculateDirection()); setReadings(76,60); assertEquals("SingleUp", bgReading.calculateDirection()); setReadings(74,65); assertEquals("FortyFiveUp", bgReading.calculateDirection()); setReadings(72,72); assertEquals("Flat", bgReading.calculateDirection()); setReadings(0,72); assertEquals("DoubleDown", bgReading.calculateDirection()); setReadings(60,76); assertEquals("SingleDown", bgReading.calculateDirection()); setReadings(65,74); assertEquals("FortyFiveDown", bgReading.calculateDirection()); }