2016-06-07 21:48:17 +02:00
|
|
|
package info.nightscout.androidaps.db;
|
|
|
|
|
|
|
|
import com.j256.ormlite.field.DatabaseField;
|
|
|
|
import com.j256.ormlite.table.DatabaseTable;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2017-06-06 17:14:17 +02:00
|
|
|
import java.util.Date;
|
2017-06-07 00:11:33 +02:00
|
|
|
import java.util.Objects;
|
2017-06-06 17:14:17 +02:00
|
|
|
|
2016-06-07 21:48:17 +02:00
|
|
|
import info.nightscout.androidaps.Constants;
|
2017-06-05 00:50:31 +02:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
|
|
|
import info.nightscout.androidaps.R;
|
|
|
|
import info.nightscout.androidaps.data.Profile;
|
2017-02-17 13:18:36 +01:00
|
|
|
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv;
|
2017-06-05 00:50:31 +02:00
|
|
|
import info.nightscout.androidaps.plugins.Overview.OverviewPlugin;
|
|
|
|
import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface;
|
|
|
|
import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries;
|
2016-07-11 18:07:54 +02:00
|
|
|
import info.nightscout.utils.DecimalFormatter;
|
2017-06-05 00:50:31 +02:00
|
|
|
import info.nightscout.utils.SP;
|
2016-06-07 21:48:17 +02:00
|
|
|
|
2016-07-21 15:10:42 +02:00
|
|
|
@DatabaseTable(tableName = DatabaseHelper.DATABASE_BGREADINGS)
|
2017-06-05 00:50:31 +02:00
|
|
|
public class BgReading implements DataPointWithLabelInterface {
|
2016-06-07 21:48:17 +02:00
|
|
|
private static Logger log = LoggerFactory.getLogger(BgReading.class);
|
|
|
|
|
2017-05-21 22:05:03 +02:00
|
|
|
@DatabaseField(id = true)
|
|
|
|
public long date;
|
2016-06-07 21:48:17 +02:00
|
|
|
|
2017-05-21 22:05:03 +02:00
|
|
|
@DatabaseField
|
|
|
|
public boolean isValid = true;
|
2016-06-07 21:48:17 +02:00
|
|
|
|
|
|
|
@DatabaseField
|
|
|
|
public double value;
|
|
|
|
@DatabaseField
|
2016-12-27 20:41:28 +01:00
|
|
|
public String direction;
|
2016-06-07 21:48:17 +02:00
|
|
|
@DatabaseField
|
|
|
|
public double raw;
|
|
|
|
|
|
|
|
@DatabaseField
|
2017-05-21 22:05:03 +02:00
|
|
|
public int source = Source.NONE;
|
|
|
|
@DatabaseField
|
|
|
|
public String _id = null; // NS _id
|
|
|
|
|
2017-08-21 15:36:52 +02:00
|
|
|
public boolean isCOBPrediction = false; // true when drawing predictions as bg points (COB)
|
|
|
|
public boolean isaCOBPrediction = false; // true when drawing predictions as bg points (aCOB)
|
|
|
|
public boolean isIOBPrediction = false; // true when drawing predictions as bg points (IOB)
|
|
|
|
public boolean isUAMPrediction = false; // true when drawing predictions as bg points (UAM)
|
2017-06-05 00:50:31 +02:00
|
|
|
|
2017-06-06 17:14:17 +02:00
|
|
|
public BgReading() {
|
|
|
|
}
|
2016-06-07 21:48:17 +02:00
|
|
|
|
|
|
|
public BgReading(NSSgv sgv) {
|
2017-05-21 22:05:03 +02:00
|
|
|
date = sgv.getMills();
|
2016-06-07 21:48:17 +02:00
|
|
|
value = sgv.getMgdl();
|
2017-03-18 23:35:43 +01:00
|
|
|
raw = sgv.getFiltered() != null ? sgv.getFiltered() : value;
|
2016-12-27 20:41:28 +01:00
|
|
|
direction = sgv.getDirection();
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Double valueToUnits(String units) {
|
|
|
|
if (units.equals(Constants.MGDL))
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
return value * Constants.MGDL_TO_MMOLL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String valueToUnitsToString(String units) {
|
2016-07-11 18:07:54 +02:00
|
|
|
if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(value);
|
2016-07-11 18:42:14 +02:00
|
|
|
else return DecimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL);
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 17:14:17 +02:00
|
|
|
public String directionToSymbol() {
|
2016-12-27 20:41:28 +01:00
|
|
|
String symbol = "";
|
2017-06-28 09:09:19 +02:00
|
|
|
if (direction == null) {
|
|
|
|
symbol = "??";
|
|
|
|
} else if (direction.compareTo("DoubleDown") == 0) {
|
2016-12-27 20:41:28 +01:00
|
|
|
symbol = "\u21ca";
|
|
|
|
} else if (direction.compareTo("SingleDown") == 0) {
|
|
|
|
symbol = "\u2193";
|
|
|
|
} else if (direction.compareTo("FortyFiveDown") == 0) {
|
|
|
|
symbol = "\u2198";
|
|
|
|
} else if (direction.compareTo("Flat") == 0) {
|
|
|
|
symbol = "\u2192";
|
|
|
|
} else if (direction.compareTo("FortyFiveUp") == 0) {
|
|
|
|
symbol = "\u2197";
|
|
|
|
} else if (direction.compareTo("SingleUp") == 0) {
|
|
|
|
symbol = "\u2191";
|
|
|
|
} else if (direction.compareTo("DoubleUp") == 0) {
|
|
|
|
symbol = "\u21c8";
|
|
|
|
} else if (isSlopeNameInvalid(direction)) {
|
|
|
|
symbol = "??";
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isSlopeNameInvalid(String direction) {
|
|
|
|
if (direction.compareTo("NOT_COMPUTABLE") == 0 ||
|
|
|
|
direction.compareTo("NOT COMPUTABLE") == 0 ||
|
|
|
|
direction.compareTo("OUT_OF_RANGE") == 0 ||
|
|
|
|
direction.compareTo("OUT OF RANGE") == 0 ||
|
2017-11-29 16:07:26 +01:00
|
|
|
direction.compareTo("NONE") == 0 ||
|
|
|
|
direction.compareTo("NotComputable") == 0
|
|
|
|
) {
|
2016-12-27 20:41:28 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-07 21:48:17 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2016-06-07 23:36:22 +02:00
|
|
|
return "BgReading{" +
|
2017-05-21 22:05:03 +02:00
|
|
|
"date=" + date +
|
2017-06-06 17:14:17 +02:00
|
|
|
", date=" + new Date(date).toLocaleString() +
|
2016-06-07 21:48:17 +02:00
|
|
|
", value=" + value +
|
2016-12-27 20:41:28 +01:00
|
|
|
", direction=" + direction +
|
2016-06-07 21:48:17 +02:00
|
|
|
", raw=" + raw +
|
|
|
|
'}';
|
|
|
|
}
|
2016-06-09 00:01:28 +02:00
|
|
|
|
2017-06-06 17:14:17 +02:00
|
|
|
public boolean isDataChanging(BgReading other) {
|
|
|
|
if (date != other.date) {
|
|
|
|
log.error("Comparing different");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (value != other.value)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEqual(BgReading other) {
|
|
|
|
if (date != other.date) {
|
|
|
|
log.error("Comparing different");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (value != other.value)
|
|
|
|
return false;
|
|
|
|
if (raw != other.raw)
|
|
|
|
return false;
|
|
|
|
if (!direction.equals(other.direction))
|
|
|
|
return false;
|
2017-06-07 00:11:33 +02:00
|
|
|
if (!Objects.equals(_id, other._id))
|
2017-06-06 17:14:17 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copyFrom(BgReading other) {
|
|
|
|
if (date != other.date) {
|
|
|
|
log.error("Copying different");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
value = other.value;
|
|
|
|
raw = other.raw;
|
|
|
|
direction = other.direction;
|
|
|
|
_id = other._id;
|
|
|
|
}
|
|
|
|
|
2017-06-05 00:50:31 +02:00
|
|
|
// ------------------ DataPointWithLabelInterface ------------------
|
2016-06-09 00:01:28 +02:00
|
|
|
@Override
|
|
|
|
public double getX() {
|
2017-05-21 22:05:03 +02:00
|
|
|
return date;
|
2016-06-09 00:01:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double getY() {
|
2017-06-13 12:08:30 +02:00
|
|
|
String units = MainApp.getConfigBuilder().getProfileUnits();
|
2016-06-09 00:01:28 +02:00
|
|
|
return valueToUnits(units);
|
|
|
|
}
|
2016-07-17 15:04:33 +02:00
|
|
|
|
2017-06-05 00:50:31 +02:00
|
|
|
@Override
|
|
|
|
public void setY(double y) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getLabel() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getDuration() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public PointsWithLabelGraphSeries.Shape getShape() {
|
2017-08-21 15:36:52 +02:00
|
|
|
if (isPrediction())
|
|
|
|
return PointsWithLabelGraphSeries.Shape.PREDICTION;
|
|
|
|
else
|
|
|
|
return PointsWithLabelGraphSeries.Shape.BG;
|
2017-06-05 00:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public float getSize() {
|
|
|
|
boolean isTablet = MainApp.sResources.getBoolean(R.bool.isTablet);
|
|
|
|
return isTablet ? 8 : 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getColor() {
|
2017-06-13 12:08:30 +02:00
|
|
|
String units = MainApp.getConfigBuilder().getProfileUnits();
|
2017-06-05 00:50:31 +02:00
|
|
|
Double lowLine = SP.getDouble("low_mark", 0d);
|
|
|
|
Double highLine = SP.getDouble("high_mark", 0d);
|
|
|
|
if (lowLine < 1) {
|
|
|
|
lowLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetLow, units);
|
|
|
|
}
|
|
|
|
if (highLine < 1) {
|
|
|
|
highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units);
|
|
|
|
}
|
|
|
|
int color = MainApp.sResources.getColor(R.color.inrange);
|
2017-08-21 15:36:52 +02:00
|
|
|
if (isPrediction())
|
2017-06-05 00:50:31 +02:00
|
|
|
color = MainApp.sResources.getColor(R.color.prediction);
|
|
|
|
else if (valueToUnits(units) < lowLine)
|
|
|
|
color = MainApp.sResources.getColor(R.color.low);
|
|
|
|
else if (valueToUnits(units) > highLine)
|
|
|
|
color = MainApp.sResources.getColor(R.color.high);
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2017-08-21 15:36:52 +02:00
|
|
|
@Override
|
|
|
|
public int getSecondColor() {
|
|
|
|
if (isIOBPrediction)
|
|
|
|
return MainApp.sResources.getColor(R.color.iob);
|
|
|
|
if (isCOBPrediction)
|
|
|
|
return MainApp.sResources.getColor(R.color.cob);
|
|
|
|
if (isaCOBPrediction)
|
|
|
|
return 0x80FFFFFF & MainApp.sResources.getColor(R.color.cob);
|
|
|
|
if (isUAMPrediction)
|
|
|
|
return MainApp.sResources.getColor(R.color.uam);
|
|
|
|
return R.color.mdtp_white;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isPrediction() {
|
|
|
|
return isaCOBPrediction || isCOBPrediction || isIOBPrediction || isUAMPrediction;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|