use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip-plus by jamorham.
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;
}
}
use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip-plus by jamorham.
the class WatchUpdaterService method sendWearCalibrationData.
private static boolean sendWearCalibrationData(Integer count, long startTime, List<Calibration> list) {
try {
if (googleApiClient != null && !googleApiClient.isConnected() && !googleApiClient.isConnecting()) {
googleApiClient.connect();
}
// if ((googleApiClient != null) && (googleApiClient.isConnected())) {
if (googleApiClient != null) {
Log.d(TAG, "sendWearCalibrationData");
final Sensor sensor = Sensor.currentSensor();
final Calibration last = list != null && list.size() > 0 ? list.get(0) : Calibration.last();
List<Calibration> latest;
BgReading lastBgReading = BgReading.last();
// From BgReading: lastBgReading.calibration.rawValueOverride()
if (list != null)
latest = list;
else if (startTime != 0)
latest = Calibration.latestForGraphSensor(count, startTime, Long.MAX_VALUE);
else if (lastBgReading != null && lastBgReading.calibration != null && lastBgReading.calibration_flag == true) {
Log.d(TAG, "sendWearCalibrationData lastBgReading.calibration_flag=" + lastBgReading.calibration_flag + " lastBgReading.timestamp: " + lastBgReading.timestamp + " lastBgReading.calibration.timestamp: " + lastBgReading.calibration.timestamp);
latest = Calibration.allForSensor();
} else {
latest = Calibration.latest(count);
}
if ((sensor != null) && (last != null) && (latest != null && !latest.isEmpty())) {
Log.d(TAG, "sendWearCalibrationData latest count = " + latest.size());
final DataMap entries = dataMap(last);
final ArrayList<DataMap> dataMaps = new ArrayList<>(latest.size());
if (sensor.uuid != null) {
for (Calibration bg : latest) {
if ((bg != null) && (bg.sensor_uuid != null) && (bg.sensor_uuid.equals(sensor.uuid))) {
dataMaps.add(dataMap(bg));
}
}
}
// MOST IMPORTANT LINE FOR TIMESTAMP
entries.putLong("time", new Date().getTime());
entries.putDataMapArrayList("entries", dataMaps);
new SendToDataLayerThread(WEARABLE_CALIBRATION_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, entries);
} else
Log.d(TAG, "sendWearCalibrationData latest count = 0");
} else {
Log.e(TAG, "sendWearCalibrationData No connection to wearable available for send treatment!");
return false;
}
} catch (NullPointerException e) {
Log.e(TAG, "Nullpointer exception in sendWearCalibrationData: " + e);
return false;
}
return true;
}
use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip-plus by jamorham.
the class WebServicePebble method request.
// process the request and produce a response object
public WebResponse request(String query) {
final BestGlucose.DisplayGlucose dg = BestGlucose.getDisplayGlucose();
// TODO better error handling?
if (dg == null)
return null;
final BgReading bgReading = BgReading.last();
// TODO better error handling?
if (bgReading == null)
return null;
// this can be null
final Calibration calibration = Calibration.lastValid();
// prepare json objects
final JSONObject reply = new JSONObject();
final JSONObject status = new JSONObject();
final JSONObject bgs = new JSONObject();
final JSONObject cals = new JSONObject();
final JSONArray status_array = new JSONArray();
final JSONArray bgs_array = new JSONArray();
final JSONArray cals_array = new JSONArray();
// populate json structures
try {
status.put("now", JoH.tsl());
bgs.put("sgv", dg.unitized);
// beware not coming from Display Glucose
bgs.put("trend", bgReading.getSlopeOrdinal());
bgs.put("direction", dg.delta_name);
bgs.put("datetime", dg.timestamp);
bgs.put("filtered", (long) (bgReading.filtered_data * 1000));
bgs.put("unfiltered", (long) (bgReading.raw_data * 1000));
bgs.put("noise", bgReading.noiseValue());
// apparently curious way to differentiate between mgdl/mmol on the watch face
if (dg.doMgDl) {
bgs.put("bgdelta", (long) dg.delta_mgdl);
} else {
bgs.put("bgdelta", dg.unitized_delta_no_units.replaceFirst("\\+", ""));
}
bgs.put("battery", microStatus.gs("bestBridgeBattery"));
// TODO get iob
bgs.put("iob", 0);
// TODO output bwp and bwpo
status_array.put(status);
bgs_array.put(bgs);
reply.put("status", status_array);
reply.put("bgs", bgs_array);
// optional calibration
if (calibration != null) {
cals.put("scale", 1);
cals.put("slope", calibration.slope * 1000);
// negated??
cals.put("intercept", calibration.intercept * 1000);
cals_array.put(cals);
reply.put("cals", cals_array);
}
Log.d(TAG, "Output: " + reply.toString());
} catch (JSONException e) {
UserError.Log.wtf(TAG, "Got json exception: " + e);
}
return new WebResponse(reply.toString());
}
use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip-plus by jamorham.
the class Datricsae method getCalibrationData.
@Override
public synchronized CalibrationData getCalibrationData(long until) {
// first is most recent
final List<Calibration> calibrations = Calibration.latestValid(CALIBRATIONS_TO_USE, until);
if ((calibrations == null) || (calibrations.size() == 0))
return null;
// Log.d(TAG,"graph: DATRICSAE: got: "+calibrations.size()+" until: "+JoH.dateTimeText(until)+" last: "+JoH.dateTimeText(calibrations.get(0).timestamp));
// have we got enough data to have a go
final long highest_calibration_timestamp = calibrations.get(0).timestamp;
CalibrationData cd = loadDataFromCache(TAG, highest_calibration_timestamp);
if (d) {
if (cd == null) {
Log.d(TAG, "GETCALIB No cache match for: " + JoH.dateTimeText(highest_calibration_timestamp));
} else {
Log.d(TAG, "GETCALIB Cache hit " + cd.slope + " " + cd.intercept + " " + JoH.dateTimeText(highest_calibration_timestamp) + " " + JoH.dateTimeText(until));
}
}
if (cd == null) {
if (calibrations.size() < FALLBACK_TO_ORIGINAL_CALIBRATIONS_MINIMUM) {
// just use whatever xDrip original would have come up with at this point
cd = new CalibrationData(calibrations.get(0).slope, calibrations.get(0).intercept);
} else {
// TODO sanity checks
final TrendLine bg_to_raw = new Forecast.PolyTrendLine(1);
final List<Double> raws = new ArrayList<>();
final List<Double> bgs = new ArrayList<>();
final boolean adjust_raw = !DexCollectionType.hasLibre();
for (int i = 1; i < 3; i++) {
final List<Calibration> cweight = Calibration.latestValid(i, until);
if (cweight != null)
// additional weight to most recent
calibrations.addAll(cweight);
}
final int ccount = calibrations.size();
for (Calibration calibration : calibrations) {
// sanity check?
// weighting!
final double raw = adjust_raw ? calibration.adjusted_raw_value : calibration.raw_value;
Log.d(TAG, "Calibration: " + JoH.qs(raw, 4) + " -> " + JoH.qs(calibration.bg, 4) + " @ " + JoH.dateTimeText(calibration.raw_timestamp));
raws.add(raw);
bgs.add(calibration.bg);
}
try {
bg_to_raw.setValues(PolyTrendLine.toPrimitiveFromList(bgs), PolyTrendLine.toPrimitiveFromList(raws));
final double all_varience = bg_to_raw.errorVarience();
Log.d(TAG, "Error Variance All: " + all_varience);
// TODO CHECK SLOPE IN RANGE HERE
final List<Double> all_bgs_set = new ArrayList<>();
final List<Double> all_raw_set = new ArrayList<>();
all_bgs_set.addAll(bgs);
all_raw_set.addAll(raws);
List<Double> lowest_bgs_set = new ArrayList<>();
List<Double> lowest_raws_set = new ArrayList<>();
lowest_bgs_set.addAll(all_bgs_set);
lowest_raws_set.addAll(all_raw_set);
double lowest_variance = all_varience;
// TODO single pass at the mo must be carefully handled
if (ccount >= OPTIMIZE_OUTLIERS_CALIBRATION_MINIMUM) {
for (int i = 0; i < ccount; i++) {
// reset
bgs.clear();
bgs.addAll(all_bgs_set);
raws.clear();
raws.addAll(all_raw_set);
bgs.remove(i);
raws.remove(i);
bg_to_raw.setValues(PolyTrendLine.toPrimitiveFromList(bgs), PolyTrendLine.toPrimitiveFromList(raws));
final double this_variance = bg_to_raw.errorVarience();
Log.d(TAG, "Error Variance drop: " + i + " = " + JoH.qs(this_variance, 3));
if (this_variance < lowest_variance) {
final double intercept = bg_to_raw.predict(0);
final double one = bg_to_raw.predict(1);
final double slope = one - intercept;
Log.d(TAG, "Removing outlier: " + i + " Reduces varience to: " + JoH.qs(this_variance, 3) + " Slope: " + JoH.qs(slope, 3) + " " + slope_in_range(slope));
if (slope_in_range(slope)) {
lowest_variance = this_variance;
lowest_bgs_set.clear();
lowest_bgs_set.addAll(bgs);
lowest_raws_set.clear();
lowest_raws_set.addAll(raws);
}
}
}
}
bg_to_raw.setValues(PolyTrendLine.toPrimitiveFromList(lowest_bgs_set), PolyTrendLine.toPrimitiveFromList(lowest_raws_set));
final double intercept = bg_to_raw.predict(0);
Log.d(TAG, "Intercept: " + intercept);
final double one = bg_to_raw.predict(1);
Log.d(TAG, "One: " + one);
final double slope = one - intercept;
Log.d(TAG, "Slope: " + slope);
// last sanity check
if (slope_in_range(slope)) {
cd = new CalibrationData(slope, intercept);
} else {
cd = new CalibrationData(calibrations.get(0).slope, calibrations.get(0).intercept);
Log.wtf(TAG, "ERROR: Slope outside range: " + slope + " REVERTING TO FALLBACK! " + calibrations.get(0).slope);
}
} catch (org.apache.commons.math3.linear.SingularMatrixException e) {
cd = new CalibrationData(calibrations.get(0).slope, calibrations.get(0).intercept);
Log.wtf(TAG, "ERROR: Math Error REVERTING TO FALLBACK! " + e + " / slope: " + calibrations.get(0).slope);
}
// Save cached data
saveDataToCache(TAG, cd, until, highest_calibration_timestamp);
}
} else {
if (d)
Log.d(TAG, "Returning cached calibration data object");
}
// null if invalid
return cd;
}
use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip-plus by jamorham.
the class LastSevenUnweightedA method getCalibrationData.
@Override
public CalibrationData getCalibrationData(long until) {
// TODO cache must understand until
// CalibrationData cd = loadDataFromCache(TAG);
CalibrationData cd = null;
if (cd == null) {
// first is most recent
final List<Calibration> calibrations = Calibration.latestValid(7, until);
if ((calibrations == null) || (calibrations.size() == 0))
return null;
// have we got enough data to have a go
if (calibrations.size() < 4) {
// just use whatever xDrip original would have come up with at this point
Log.d(TAG, "Falling back to xDrip-original values");
cd = new CalibrationData(calibrations.get(0).slope, calibrations.get(0).intercept);
} else {
// TODO sanity checks
final TrendLine bg_to_raw = new PolyTrendLine(1);
final List<Double> raws = new ArrayList<>();
final List<Double> bgs = new ArrayList<>();
final boolean adjust_raw = !DexCollectionType.hasLibre();
for (Calibration calibration : calibrations) {
// sanity check?
// weighting!
final double raw = adjust_raw ? calibration.adjusted_raw_value : calibration.raw_value;
Log.d(TAG, "Calibration: " + raw + " -> " + calibration.bg);
raws.add(raw);
bgs.add(calibration.bg);
}
bg_to_raw.setValues(PolyTrendLine.toPrimitiveFromList(bgs), PolyTrendLine.toPrimitiveFromList(raws));
Log.d(TAG, "Error Variance: " + bg_to_raw.errorVarience());
final double intercept = bg_to_raw.predict(0);
Log.d(TAG, "Intercept: " + intercept);
final double one = bg_to_raw.predict(1);
Log.d(TAG, "One: " + one);
final double slope = one - intercept;
Log.d(TAG, "Slope: " + slope);
cd = new CalibrationData(slope, intercept);
}
}
// null if invalid
return cd;
}
Aggregations