use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.
the class CalibrationOverride method addListenerOnButton.
public void addListenerOnButton() {
button = (Button) findViewById(R.id.save_calibration_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Sensor.isActive()) {
EditText value = (EditText) findViewById(R.id.bg_value);
String string_value = value.getText().toString();
if (!TextUtils.isEmpty(string_value)) {
try {
final double calValue = JoH.tolerantParseDouble(string_value);
final Calibration last_calibration = Calibration.lastValid();
if (last_calibration == null) {
Log.wtf(TAG, "Last valid calibration is null when trying to cancel it in override!");
} else {
last_calibration.sensor_confidence = 0;
last_calibration.slope_confidence = 0;
last_calibration.save();
CalibrationSendQueue.addToQueue(last_calibration, getApplicationContext());
// TODO we need to push the nixing of this last calibration
}
final Calibration calibration = Calibration.create(calValue, getApplicationContext());
if (calibration != null) {
UndoRedo.addUndoCalibration(calibration.uuid);
GcmActivity.pushCalibration(string_value, "0");
// startWatchUpdaterService(v.getContext(), WatchUpdaterService.ACTION_SYNC_CALIBRATION, TAG);
} else {
Log.e(TAG, "Calibration creation resulted in null");
JoH.static_toast_long("Could not create calibration!");
}
Intent tableIntent = new Intent(v.getContext(), Home.class);
startActivity(tableIntent);
finish();
} catch (NumberFormatException e) {
value.setError("Number error: " + e);
}
} else {
value.setError("Calibration Can Not be blank");
}
} else {
Log.w("Calibration", "ERROR, no active sensor");
}
}
});
}
use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.
the class NewCalibration method upsertSensorCalibratonsFromJson.
static void upsertSensorCalibratonsFromJson(String json) {
Log.i(TAG, "upsertSensorCalibratonsFromJson called");
SensorCalibrations[] sensorCalibrations = getSensorCalibrations(json);
for (SensorCalibrations SensorCalibration : sensorCalibrations) {
Sensor.upsertFromMaster(SensorCalibration.sensor);
for (Calibration calibration : SensorCalibration.calibrations) {
Log.d(TAG, "upsertSensorCalibratonsFromJson updating calibration " + calibration.uuid);
Calibration.upsertFromMaster(calibration);
}
}
}
use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.
the class AddCalibration method automatedCalibration.
// jamorham - receive automated calibration via broadcast intent / tasker receiver
public synchronized void automatedCalibration() {
final PowerManager.WakeLock wl = JoH.getWakeLock("xdrip-autocalib", 60000);
Log.d(TAG, "Auto calibration...");
final Bundle extras = getIntent().getExtras();
if (extras != null) {
final String string_value = extras.getString("bg_string");
final String bg_age = extras.getString("bg_age");
final String from_external = extras.getString("from_external", "false");
final String from_interactive = extras.getString("from_interactive", "false");
final String note_only = extras.getString("note_only", "false");
final String allow_undo = extras.getString("allow_undo", "false");
if ((Sensor.isActive() || Home.get_follower())) {
if (!TextUtils.isEmpty(string_value)) {
if (!TextUtils.isEmpty(bg_age)) {
final double calValue = Double.parseDouble(string_value);
new Thread() {
@Override
public void run() {
final PowerManager.WakeLock wlt = JoH.getWakeLock("xdrip-autocalibt", 60000);
long bgAgeNumber = Long.parseLong(bg_age);
if ((bgAgeNumber >= 0) && (bgAgeNumber < 86400)) {
long localEstimatedInterstitialLagSeconds = 0;
// adjust timestamp for this if we can
if (bgAgeNumber > estimatedInterstitialLagSeconds) {
localEstimatedInterstitialLagSeconds = estimatedInterstitialLagSeconds;
}
if (calValue > 0) {
if (lastExternalCalibrationValue == 0) {
lastExternalCalibrationValue = PersistentStore.getDouble(LAST_EXTERNAL_CALIBRATION);
}
if (calValue != lastExternalCalibrationValue) {
if (!Home.get_follower()) {
lastExternalCalibrationValue = calValue;
PersistentStore.setDouble(LAST_EXTERNAL_CALIBRATION, calValue);
final Calibration calibration = Calibration.create(calValue, bgAgeNumber, getApplicationContext(), (note_only.equals("true")), localEstimatedInterstitialLagSeconds);
if ((calibration != null) && allow_undo.equals("true")) {
UndoRedo.addUndoCalibration(calibration.uuid);
}
// startWatchUpdaterService(getApplicationContext(), WatchUpdaterService.ACTION_SYNC_CALIBRATION, TAG);
} else {
// follower sends the calibration data onwards only if sourced from interactive request
if (from_interactive.equals("true")) {
Log.d(TAG, "Interactive calibration and we are follower so sending to master");
sendFollowerCalibration(calValue, bgAgeNumber);
} else {
Log.d(TAG, "Not an interactive calibration so not sending to master");
}
}
} else {
Log.w(TAG, "Ignoring Remote calibration value as identical to last one: " + calValue);
}
if (from_external.equals("true")) {
Log.d("jamorham calib", "Relaying tasker pushed calibration");
GcmActivity.pushCalibration(string_value, bg_age);
}
}
} else {
Log.wtf("CALERROR", "bg age either in future or older than 1 day: " + bgAgeNumber);
}
JoH.releaseWakeLock(wlt);
}
}.start();
} else {
Log.w("CALERROR", "ERROR during automated calibration - no valid bg age");
}
} else {
Log.w("CALERROR", "ERROR during automated calibration - no valid value");
}
} else {
Log.w("CALERROR", "ERROR during automated calibration - no active sensor");
}
JoH.releaseWakeLock(wl);
finish();
}
}
use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.
the class AddCalibration method addListenerOnButton.
public void addListenerOnButton() {
button = (Button) findViewById(R.id.save_calibration_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(final View v) {
if ((Sensor.isActive() || Home.get_follower())) {
final EditText value = (EditText) findViewById(R.id.bg_value);
final String string_value = value.getText().toString();
if (!TextUtils.isEmpty(string_value)) {
try {
final double calValue = JoH.tolerantParseDouble(string_value);
if (!Home.get_follower()) {
Calibration calibration = Calibration.create(calValue, getApplicationContext());
if (calibration != null) {
UndoRedo.addUndoCalibration(calibration.uuid);
// startWatchUpdaterService(v.getContext(), WatchUpdaterService.ACTION_SYNC_CALIBRATION, TAG);
} else {
Log.e(TAG, "Calibration creation resulted in null");
JoH.static_toast_long("Could not create calibration!");
// TODO probably follower must ensure it has a valid sensor regardless..
}
} else if (Home.get_follower()) {
// Sending the data for the master to update the main tables.
// default offset is 0
sendFollowerCalibration(calValue, 0);
}
Intent tableIntent = new Intent(v.getContext(), Home.class);
startActivity(tableIntent);
} catch (NumberFormatException e) {
Log.e(TAG, "Number format exception ", e);
Home.toaststatic("Got error parsing number in calibration");
}
// }
// }.start();
finish();
} else {
value.setError("Calibration Can Not be blank");
}
} else {
Log.w("CALERROR", "Sensor is not active, cannot calibrate");
}
}
});
}
use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.
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;
}
Aggregations