use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip by NightscoutFoundation.
the class Home method extraStatusLine.
@NonNull
public static String extraStatusLine() {
final StringBuilder extraline = new StringBuilder();
final Calibration lastCalibration = Calibration.lastValid();
if (Pref.getBoolean("status_line_calibration_long", false) && lastCalibration != null) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append("slope = ");
extraline.append(String.format("%.2f", lastCalibration.slope));
extraline.append(' ');
extraline.append("inter = ");
extraline.append(String.format("%.2f", lastCalibration.intercept));
}
if (Pref.getBoolean("status_line_calibration_short", false) && lastCalibration != null) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append("s:");
extraline.append(String.format("%.2f", lastCalibration.slope));
extraline.append(' ');
extraline.append("i:");
extraline.append(String.format("%.2f", lastCalibration.intercept));
}
if (Pref.getBoolean("status_line_avg", false) || Pref.getBoolean("status_line_a1c_dcct", false) || Pref.getBoolean("status_line_a1c_ifcc", false) || Pref.getBoolean("status_line_in", false) || Pref.getBoolean("status_line_high", false) || Pref.getBoolean("status_line_low", false) || Pref.getBoolean("status_line_stdev", false) || Pref.getBoolean("status_line_carbs", false) || Pref.getBoolean("status_line_insulin", false) || Pref.getBoolean("status_line_royce_ratio", false) || Pref.getBoolean("status_line_accuracy", false) || Pref.getBoolean("status_line_capture_percentage", false) || Pref.getBoolean("status_line_pump_reservoir", false)) {
final StatsResult statsResult = new StatsResult(Pref.getInstance(), Pref.getBooleanDefaultFalse("extra_status_stats_24h"));
if (Pref.getBoolean("status_line_avg", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(statsResult.getAverageUnitised());
}
if (Pref.getBoolean("status_line_a1c_dcct", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(statsResult.getA1cDCCT());
}
if (Pref.getBoolean("status_line_a1c_ifcc", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(statsResult.getA1cIFCC());
}
if (Pref.getBoolean("status_line_in", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(statsResult.getInPercentage());
}
if (Pref.getBoolean("status_line_high", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(statsResult.getHighPercentage());
}
if (Pref.getBoolean("status_line_low", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(statsResult.getLowPercentage());
}
if (Pref.getBoolean("status_line_stdev", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(statsResult.getStdevUnitised());
}
if (Pref.getBoolean("status_line_carbs", false)) {
if (extraline.length() != 0)
extraline.append(' ');
// extraline.append("Carbs: " + statsResult.getTotal_carbs());
extraline.append("Carbs:" + Math.round(statsResult.getTotal_carbs()));
}
if (Pref.getBoolean("status_line_insulin", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append("U:" + JoH.qs(statsResult.getTotal_insulin(), 2));
}
if (Pref.getBoolean("status_line_royce_ratio", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append("C/I:" + JoH.qs(statsResult.getRatio(), 2));
}
if (Pref.getBoolean("status_line_capture_percentage", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(statsResult.getCapturePercentage(false));
}
if (Pref.getBoolean("status_line_accuracy", false)) {
final long accuracy_period = DAY_IN_MS * 3;
if (extraline.length() != 0)
extraline.append(' ');
final String accuracy_report = Accuracy.evaluateAccuracy(accuracy_period);
if ((accuracy_report != null) && (accuracy_report.length() > 0)) {
extraline.append(accuracy_report);
} else {
final String accuracy = BloodTest.evaluateAccuracy(accuracy_period);
extraline.append(((accuracy != null) ? " " + accuracy : ""));
}
}
if (Pref.getBoolean("status_line_pump_reservoir", false)) {
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(PumpStatus.getBolusIoBString());
extraline.append(PumpStatus.getReservoirString());
extraline.append(PumpStatus.getBatteryString());
}
}
if (Pref.getBoolean("extra_status_calibration_plugin", false)) {
// make sure do this only once
final CalibrationAbstract plugin = getCalibrationPluginFromPreferences();
if (plugin != null) {
final CalibrationAbstract.CalibrationData pcalibration = plugin.getCalibrationData();
// not tested on the widget yet
if (extraline.length() > 0)
extraline.append("\n");
if (pcalibration != null)
extraline.append("(" + plugin.getAlgorithmName() + ") s:" + JoH.qs(pcalibration.slope, 2) + " i:" + JoH.qs(pcalibration.intercept, 2));
BgReading bgReading = BgReading.last();
if (bgReading != null) {
final boolean doMgdl = Pref.getString("units", "mgdl").equals("mgdl");
extraline.append(" \u21D2 " + BgGraphBuilder.unitized_string(plugin.getGlucoseFromSensorValue(bgReading.age_adjusted_raw_value), doMgdl) + " " + BgGraphBuilder.unit(doMgdl));
}
}
// If we are using the plugin as the primary then show xdrip original as well
if (Pref.getBooleanDefaultFalse("display_glucose_from_plugin") || Pref.getBooleanDefaultFalse("use_pluggable_alg_as_primary")) {
// make sure do this only once
final CalibrationAbstract plugin_xdrip = getCalibrationPlugin(PluggableCalibration.Type.xDripOriginal);
if (plugin_xdrip != null) {
final CalibrationAbstract.CalibrationData pcalibration = plugin_xdrip.getCalibrationData();
if (extraline.length() > 0)
// not tested on the widget yet
extraline.append("\n");
if (pcalibration != null)
extraline.append("(" + plugin_xdrip.getAlgorithmName() + ") s:" + JoH.qs(pcalibration.slope, 2) + " i:" + JoH.qs(pcalibration.intercept, 2));
BgReading bgReading = BgReading.last();
if (bgReading != null) {
final boolean doMgdl = Pref.getString("units", "mgdl").equals("mgdl");
extraline.append(" \u21D2 " + BgGraphBuilder.unitized_string(plugin_xdrip.getGlucoseFromSensorValue(bgReading.age_adjusted_raw_value), doMgdl) + " " + BgGraphBuilder.unit(doMgdl));
}
}
}
}
if (Pref.getBoolean("status_line_time", false)) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
if (extraline.length() != 0)
extraline.append(' ');
extraline.append(sdf.format(new Date()));
}
return extraline.toString();
}
use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip by NightscoutFoundation.
the class Home method displayCurrentInfo.
private void displayCurrentInfo() {
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
final boolean isDexbridge = CollectionServiceStarter.isDexBridgeOrWifiandDexBridge();
// final boolean hasBtWixel = DexCollectionType.hasBtWixel();
final boolean isLimitter = CollectionServiceStarter.isLimitter();
// if (isDexbridge||isLimitter||hasBtWixel||is_follower) {
if (DexCollectionType.hasBattery()) {
final int bridgeBattery = Pref.getInt("bridge_battery", 0);
if (bridgeBattery < 1) {
// dexbridgeBattery.setText(R.string.waiting_for_packet);
dexbridgeBattery.setVisibility(View.INVISIBLE);
} else {
if (isDexbridge) {
dexbridgeBattery.setText(getString(R.string.xbridge_battery) + ": " + bridgeBattery + "%");
} else if (isLimitter) {
final String limitterName = DexCollectionService.getBestLimitterHardwareName();
if (limitterName.equals(DexCollectionService.LIMITTER_NAME)) {
dexbridgeBattery.setText(getString(R.string.limitter_battery) + ": " + bridgeBattery + "%");
} else {
dexbridgeBattery.setText(limitterName + " " + getString(R.string.battery) + ": " + bridgeBattery + "%");
}
} else {
dexbridgeBattery.setText("Bridge battery" + ": " + bridgeBattery + ((bridgeBattery < 200) ? "%" : "mV"));
}
}
if (bridgeBattery < 50)
dexbridgeBattery.setTextColor(Color.YELLOW);
if (bridgeBattery < 25)
dexbridgeBattery.setTextColor(Color.RED);
else
dexbridgeBattery.setTextColor(Color.GREEN);
dexbridgeBattery.setVisibility(View.VISIBLE);
} else {
dexbridgeBattery.setVisibility(View.INVISIBLE);
}
if (DexCollectionType.hasWifi()) {
final int bridgeBattery = Pref.getInt("parakeet_battery", 0);
if (bridgeBattery > 0) {
if (bridgeBattery < 50) {
parakeetBattery.setText(getString(R.string.parakeet_battery) + ": " + bridgeBattery + "%");
if (bridgeBattery < 40) {
parakeetBattery.setTextColor(Color.RED);
} else {
parakeetBattery.setTextColor(Color.YELLOW);
}
parakeetBattery.setVisibility(View.VISIBLE);
} else {
parakeetBattery.setVisibility(View.INVISIBLE);
}
}
} else {
parakeetBattery.setVisibility(View.INVISIBLE);
}
if (!Pref.getBoolean("display_bridge_battery", true)) {
dexbridgeBattery.setVisibility(View.INVISIBLE);
parakeetBattery.setVisibility(View.INVISIBLE);
}
final int sensor_age = Pref.getInt("nfc_sensor_age", 0);
if ((sensor_age > 0) && (DexCollectionType.hasLibre())) {
final String age_problem = (Pref.getBooleanDefaultFalse("nfc_age_problem") ? " \u26A0\u26A0\u26A0" : "");
if (Pref.getBoolean("nfc_show_age", true)) {
sensorAge.setText("Age: " + JoH.qs(((double) sensor_age) / 1440, 1) + "d" + age_problem);
} else {
try {
final double expires = JoH.tolerantParseDouble(Pref.getString("nfc_expiry_days", "14.5")) - ((double) sensor_age) / 1440;
sensorAge.setText(((expires >= 0) ? ("Expires: " + JoH.qs(expires, 1) + "d") : "EXPIRED! ") + age_problem);
} catch (Exception e) {
Log.e(TAG, "expiry calculation: " + e);
sensorAge.setText("Expires: " + "???");
}
}
sensorAge.setVisibility(View.VISIBLE);
if (sensor_age < 1440) {
sensorAge.setTextColor(Color.YELLOW);
} else if (sensor_age < (1440 * 12)) {
sensorAge.setTextColor(Color.GREEN);
} else {
sensorAge.setTextColor(Color.RED);
}
} else {
sensorAge.setVisibility(View.GONE);
}
if (blockTouches) {
sensorAge.setText("SCANNING.. DISPLAY LOCKED!");
sensorAge.setVisibility(View.VISIBLE);
sensorAge.setTextColor(Color.GREEN);
}
if ((currentBgValueText.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) > 0) {
currentBgValueText.setPaintFlags(currentBgValueText.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
dexbridgeBattery.setPaintFlags(dexbridgeBattery.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
if (get_follower()) {
GcmActivity.requestPing();
}
}
final BgReading lastBgReading = BgReading.lastNoSenssor();
boolean predictive = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("predictive_bg", false);
if (isBTShare) {
predictive = false;
}
if (lastBgReading != null) {
// detect broken data from G5 or other sources
if ((lastBgReading.raw_data != 0) && (lastBgReading.raw_data * 2 == lastBgReading.filtered_data)) {
if (JoH.ratelimit("g5-corrupt-data-warning", 1200)) {
final String msg = "filtered data is exactly double raw sensor data which looks wrong! (Transmitter maybe dead)" + lastBgReading.raw_data;
toaststaticnext(msg);
}
}
displayCurrentInfoFromReading(lastBgReading, predictive);
} else {
display_delta = "";
}
if (Pref.getBoolean("extra_status_line", false)) {
extraStatusLineText.setText(extraStatusLine());
extraStatusLineText.setVisibility(View.VISIBLE);
} else {
extraStatusLineText.setText("");
extraStatusLineText.setVisibility(View.GONE);
}
}
use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip by NightscoutFoundation.
the class BestGlucose method getDisplayGlucose.
// note we don't support the original depreciated "predictive" mode
// TODO stale data
// TODO time ago
// TODO check BGgraph data is current
// TODO internalize delta handling to handle irregular periods and missing data plugins etc
// TODO see getSlopeArrowSymbolBeforeCalibration for calculation method for arbitary slope
// TODO option to process noise or not
// TODO check what happens if there is only a single entry, especially regarding delta
// TODO select by time
public static DisplayGlucose getDisplayGlucose() {
if (prefs == null)
prefs = PreferenceManager.getDefaultSharedPreferences(xdrip.getAppContext());
// return value
final DisplayGlucose dg = new DisplayGlucose();
final boolean doMgdl = (prefs.getString("units", "mgdl").equals("mgdl"));
final boolean is_follower = Home.get_follower();
dg.doMgDl = doMgdl;
List<BgReading> last_2 = BgReading.latest(2);
final BgReading lastBgReading = BgReading.last(is_follower);
if (lastBgReading == null)
return null;
final CalibrationAbstract.CalibrationData pcalibration;
final CalibrationAbstract plugin = getCalibrationPluginFromPreferences();
double estimate = -1;
double filtered = -1;
long timestamp = -1;
double previous_estimate = -1;
double previous_filtered = -1;
long previous_timestamp = -1;
// normal first
estimate = lastBgReading.calculated_value;
filtered = lastBgReading.filtered_calculated_value;
timestamp = lastBgReading.timestamp;
if (last_2.size() == 2) {
previous_estimate = last_2.get(1).calculated_value;
previous_filtered = last_2.get(1).filtered_calculated_value;
previous_timestamp = last_2.get(1).timestamp;
}
dg.mssince = JoH.msSince(lastBgReading.timestamp);
dg.timestamp = lastBgReading.timestamp;
// if we are actively using a plugin, get the glucose calculation from there
if ((plugin != null) && ((pcalibration = plugin.getCalibrationData()) != null) && (Pref.getBoolean("display_glucose_from_plugin", false))) {
dg.plugin_name = plugin.getAlgorithmName();
Log.d(TAG, "Using plugin: " + dg.plugin_name);
dg.from_plugin = true;
estimate = plugin.getGlucoseFromBgReading(lastBgReading, pcalibration);
filtered = plugin.getGlucoseFromFilteredBgReading(lastBgReading, pcalibration);
// also try to update the previous values in the same way
if (last_2.size() == 2) {
previous_estimate = plugin.getGlucoseFromBgReading(last_2.get(1), pcalibration);
previous_filtered = plugin.getGlucoseFromFilteredBgReading(last_2.get(1), pcalibration);
}
}
int warning_level = 0;
String slope_arrow = "";
String slope_name = "";
String extrastring = "";
double estimated_delta = 0;
// TODO refresh bggraph if needed based on cache - observe
// should this be conditional on whether bg_compensate_noise is set?
BgGraphBuilder.refreshNoiseIfOlderThan(dg.timestamp);
dg.noise = BgGraphBuilder.last_noise;
boolean bg_from_filtered = prefs.getBoolean("bg_from_filtered", false);
// if noise has settled down then switch off filtered mode
if ((bg_from_filtered) && (BgGraphBuilder.last_noise < BgGraphBuilder.NOISE_FORGIVE) && (prefs.getBoolean("bg_compensate_noise", false))) {
bg_from_filtered = false;
prefs.edit().putBoolean("bg_from_filtered", false).apply();
}
// TODO Noise uses plugin in bggraphbuilder
if (compensateNoise()) {
// this maybe needs scaling based on noise intensity
estimate = BgGraphBuilder.best_bg_estimate;
estimated_delta = BgGraphBuilder.best_bg_estimate - BgGraphBuilder.last_bg_estimate;
// TODO handle ratio when period is not dexcom period?
double estimated_delta_by_minute = estimated_delta / (BgGraphBuilder.DEXCOM_PERIOD / 60000);
dg.slope = estimated_delta_by_minute / 60000;
dg.unitized_delta_no_units = BgGraphBuilder.unitizedDeltaStringRaw(false, true, estimated_delta, doMgdl);
// TODO optimize adding units
dg.unitized_delta = BgGraphBuilder.unitizedDeltaStringRaw(true, true, estimated_delta, doMgdl);
// delta by minute
slope_arrow = BgReading.slopeToArrowSymbol(estimated_delta_by_minute);
slope_name = BgReading.slopeName(estimated_delta_by_minute);
// warning symbol !
extrastring = "\u26A0";
warning_level = 1;
if ((BgGraphBuilder.last_noise > BgGraphBuilder.NOISE_HIGH) && (DexCollectionType.hasFiltered())) {
// force filtered mode
bg_from_filtered = true;
}
} else {
// TODO ignores plugin
// dg.unitized_delta = BgGraphBuilder.unitizedDeltaString(true, true, is_follower , doMgdl);
dg.unitized_delta_no_units = unitizedDeltaString(false, true, doMgdl, estimate, timestamp, previous_estimate, previous_timestamp);
// TODO time stretch adjustment?
estimated_delta = estimate - previous_estimate;
// TODO optimize adding units
dg.unitized_delta = unitizedDeltaString(true, true, doMgdl, estimate, timestamp, previous_estimate, previous_timestamp);
long time_delta = timestamp - previous_timestamp;
if (time_delta < 0)
Log.wtf(TAG, "Time delta is negative! : " + time_delta);
// slope_arrow = lastBgReading.slopeArrow(); // internalize this for plugins
double slope = calculateSlope(estimate, timestamp, previous_estimate, previous_timestamp);
dg.slope = slope;
// slope by minute
slope_arrow = BgReading.slopeToArrowSymbol(slope * 60000);
slope_name = BgReading.slopeName(slope * 60000);
Log.d(TAG, "No noise option slope by minute: " + JoH.qs(slope * 60000, 5));
}
// TODO bit more work on deltas etc needed here
if (bg_from_filtered) {
estimate = filtered;
warning_level = 2;
}
dg.unitized_value = BgGraphBuilder.unitized(estimate, doMgdl);
final String stringEstimate = BgGraphBuilder.unitized_string(estimate, doMgdl);
if ((lastBgReading.hide_slope) || (bg_from_filtered)) {
slope_arrow = "";
slope_name = "NOT COMPUTABLE";
}
dg.mgdl = estimate;
dg.delta_mgdl = estimated_delta;
dg.warning = warning_level;
dg.unitized = stringEstimate;
dg.delta_arrow = slope_arrow;
dg.extra_string = extrastring;
dg.delta_name = slope_name;
// to be moved one day
if (!SensorSanity.isRawValueSane(lastBgReading.raw_data)) {
dg.delta_arrow = "!";
dg.unitized = ">!?";
dg.mgdl = 0;
dg.delta_mgdl = 0;
dg.unitized_value = 0;
dg.unitized_delta = "";
dg.slope = 0;
if (JoH.ratelimit("exceeding_max_raw", 120)) {
UserError.Log.wtf(TAG, "Failing raw bounds validation: " + lastBgReading.raw_data);
}
}
if (d)
Log.d(TAG, "dg result: " + dg.unitized + " previous: " + BgGraphBuilder.unitized_string(previous_estimate, doMgdl));
return dg;
}
use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip by NightscoutFoundation.
the class FakeNumbers method addListenerOnButton.
public void addListenerOnButton() {
button = (Button) findViewById(R.id.log);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText value = (EditText) findViewById(R.id.bg_value);
int intValue = Integer.parseInt(value.getText().toString());
int filterdValue = intValue;
if (intValue > 200) {
filterdValue = (int) (filterdValue * 1.2);
}
BgReading bgReading = BgReading.create(intValue * 1000, filterdValue * 1000, getApplicationContext(), new Date().getTime());
Intent intent = new Intent(getApplicationContext(), Home.class);
startActivity(intent);
finish();
}
});
button = (Button) findViewById(R.id.StartTest);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ActiveBgAlert aba = ActiveBgAlert.getOnly();
ActiveBgAlert.ClearData();
ActiveBgAlert.Create("some string", true, new Date().getTime());
}
});
button = (Button) findViewById(R.id.StartTestAlerts);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertType.testAll(getApplicationContext());
BgReading.TestgetUnclearTimes();
}
});
}
use of com.eveningoutpost.dexdrip.Models.BgReading in project xDrip by NightscoutFoundation.
the class InfluxDBUploader method upload.
public boolean upload(List<BgReading> glucoseDataSets, List<Calibration> meterRecords, List<Calibration> calRecords) {
try {
BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy("autogen").consistency(InfluxDB.ConsistencyLevel.ALL).build();
for (BgReading record : glucoseDataSets) {
if (record == null) {
Log.e(TAG, "InfluxDB glucose record is null");
continue;
}
batchPoints.point(createGlucosePoint(record));
}
for (Calibration record : meterRecords) {
if (record == null) {
Log.e(TAG, "InfluxDB meter record is null");
continue;
}
batchPoints.point(createMeterPoint(record));
}
for (Calibration record : calRecords) {
if (record == null) {
Log.e(TAG, "InfluxDB calibration record is null");
continue;
}
if (record.slope == 0d)
continue;
batchPoints.point(createCalibrationPoint(record));
}
try {
Log.d(TAG, "Influx url: " + dbUri);
InfluxDBFactory.connect(dbUri, dbUser, dbPassword, client).enableGzip().write(batchPoints);
last_error = null;
return true;
} catch (java.lang.ExceptionInInitializerError e) {
Log.e(TAG, "InfluxDB failed: " + e.getCause());
return false;
} catch (java.lang.NoClassDefFoundError e) {
Log.e(TAG, "InfluxDB failed more: " + e);
return false;
} catch (IllegalArgumentException e) {
Log.wtf(TAG, "InfluxDB problem: " + e);
return false;
} catch (Exception e) {
Log.e(TAG, "Write to InfluxDB failed: " + e);
last_error = e.getMessage();
return false;
}
} catch (Exception e) {
Log.wtf(TAG, "Exception during initialization: ", e);
return false;
}
}
Aggregations