Search in sources :

Example 1 with CalSubrecord

use of com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalSubrecord in project xDrip-plus by jamorham.

the class SlopeParameters method create.

public static void create(CalRecord[] calRecords, Context context, boolean override, long addativeOffset) {
    // TODO: Change calibration.last and other queries to order calibrations by timestamp rather than ID
    Log.i("CALIBRATION-CHECK-IN: ", "Creating Calibration Record");
    Sensor sensor = Sensor.currentSensor();
    CalRecord firstCalRecord = calRecords[0];
    CalRecord secondCalRecord = calRecords[0];
    // CalRecord secondCalRecord = calRecords[calRecords.length - 1];
    // TODO: Figgure out how the ratio between the two is determined
    double calSlope = ((secondCalRecord.getScale() / secondCalRecord.getSlope()) + (3 * firstCalRecord.getScale() / firstCalRecord.getSlope())) * 250;
    double calIntercept = (((secondCalRecord.getScale() * secondCalRecord.getIntercept()) / secondCalRecord.getSlope()) + ((3 * firstCalRecord.getScale() * firstCalRecord.getIntercept()) / firstCalRecord.getSlope())) / -4;
    if (sensor != null) {
        for (int i = 0; i < firstCalRecord.getCalSubrecords().length - 1; i++) {
            if (((firstCalRecord.getCalSubrecords()[i] != null && Calibration.is_new(firstCalRecord.getCalSubrecords()[i], addativeOffset))) || (i == 0 && override)) {
                CalSubrecord calSubrecord = firstCalRecord.getCalSubrecords()[i];
                Calibration calibration = new Calibration();
                calibration.bg = calSubrecord.getCalBGL();
                calibration.timestamp = calSubrecord.getDateEntered().getTime() + addativeOffset;
                calibration.raw_timestamp = calibration.timestamp;
                if (calibration.timestamp > new Date().getTime()) {
                    Log.d(TAG, "ERROR - Calibration timestamp is from the future, wont save!");
                    return;
                }
                calibration.raw_value = calSubrecord.getCalRaw() / 1000;
                calibration.slope = calSlope;
                calibration.intercept = calIntercept;
                calibration.sensor_confidence = ((-0.0018 * calibration.bg * calibration.bg) + (0.6657 * calibration.bg) + 36.7505) / 100;
                if (calibration.sensor_confidence <= 0) {
                    calibration.sensor_confidence = 0;
                }
                // TODO: query backwards to find this value near the timestamp
                calibration.slope_confidence = 0.8;
                calibration.estimate_raw_at_time_of_calibration = calSubrecord.getCalRaw() / 1000;
                calibration.sensor = sensor;
                calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sensor.started_at;
                calibration.uuid = UUID.randomUUID().toString();
                calibration.sensor_uuid = sensor.uuid;
                calibration.check_in = true;
                calibration.first_decay = firstCalRecord.getDecay();
                calibration.second_decay = secondCalRecord.getDecay();
                calibration.first_slope = firstCalRecord.getSlope();
                calibration.second_slope = secondCalRecord.getSlope();
                calibration.first_scale = firstCalRecord.getScale();
                calibration.second_scale = secondCalRecord.getScale();
                calibration.first_intercept = firstCalRecord.getIntercept();
                calibration.second_intercept = secondCalRecord.getIntercept();
                calibration.save();
                CalibrationSendQueue.addToQueue(calibration, context);
                Calibration.requestCalibrationIfRangeTooNarrow();
            // KS newFingerStickData();
            }
        }
        if (firstCalRecord.getCalSubrecords()[0] != null && firstCalRecord.getCalSubrecords()[2] == null) {
            if (Calibration.latest(2).size() == 1) {
                Calibration.create(calRecords, context, true, 0);
            }
        }
        context.startService(new Intent(context, Notifications.class));
    }
}
Also used : CalSubrecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalSubrecord) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord) Intent(android.content.Intent) Notifications(com.eveningoutpost.dexdrip.UtilityModels.Notifications) Date(java.util.Date)

Example 2 with CalSubrecord

use of com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalSubrecord in project xDrip by NightscoutFoundation.

the class SlopeParameters method create.

public static void create(CalRecord[] calRecords, Context context, boolean override, long addativeOffset) {
    // TODO: Change calibration.last and other queries to order calibrations by timestamp rather than ID
    Log.i("CALIBRATION-CHECK-IN: ", "Creating Calibration Record");
    Sensor sensor = Sensor.currentSensor();
    CalRecord firstCalRecord = calRecords[0];
    CalRecord secondCalRecord = calRecords[0];
    // CalRecord secondCalRecord = calRecords[calRecords.length - 1];
    // TODO: Figgure out how the ratio between the two is determined
    double calSlope = ((secondCalRecord.getScale() / secondCalRecord.getSlope()) + (3 * firstCalRecord.getScale() / firstCalRecord.getSlope())) * 250;
    double calIntercept = (((secondCalRecord.getScale() * secondCalRecord.getIntercept()) / secondCalRecord.getSlope()) + ((3 * firstCalRecord.getScale() * firstCalRecord.getIntercept()) / firstCalRecord.getSlope())) / -4;
    if (sensor != null) {
        for (int i = 0; i < firstCalRecord.getCalSubrecords().length - 1; i++) {
            if (((firstCalRecord.getCalSubrecords()[i] != null && Calibration.is_new(firstCalRecord.getCalSubrecords()[i], addativeOffset))) || (i == 0 && override)) {
                CalSubrecord calSubrecord = firstCalRecord.getCalSubrecords()[i];
                Calibration calibration = new Calibration();
                calibration.bg = calSubrecord.getCalBGL();
                calibration.timestamp = calSubrecord.getDateEntered().getTime() + addativeOffset;
                calibration.raw_timestamp = calibration.timestamp;
                if (calibration.timestamp > new Date().getTime()) {
                    Log.d(TAG, "ERROR - Calibration timestamp is from the future, wont save!");
                    return;
                }
                calibration.raw_value = calSubrecord.getCalRaw() / 1000;
                calibration.slope = calSlope;
                calibration.intercept = calIntercept;
                calibration.sensor_confidence = ((-0.0018 * calibration.bg * calibration.bg) + (0.6657 * calibration.bg) + 36.7505) / 100;
                if (calibration.sensor_confidence <= 0) {
                    calibration.sensor_confidence = 0;
                }
                // TODO: query backwards to find this value near the timestamp
                calibration.slope_confidence = 0.8;
                calibration.estimate_raw_at_time_of_calibration = calSubrecord.getCalRaw() / 1000;
                calibration.sensor = sensor;
                calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sensor.started_at;
                calibration.uuid = UUID.randomUUID().toString();
                calibration.sensor_uuid = sensor.uuid;
                calibration.check_in = true;
                calibration.first_decay = firstCalRecord.getDecay();
                calibration.second_decay = secondCalRecord.getDecay();
                calibration.first_slope = firstCalRecord.getSlope();
                calibration.second_slope = secondCalRecord.getSlope();
                calibration.first_scale = firstCalRecord.getScale();
                calibration.second_scale = secondCalRecord.getScale();
                calibration.first_intercept = firstCalRecord.getIntercept();
                calibration.second_intercept = secondCalRecord.getIntercept();
                calibration.save();
                CalibrationSendQueue.addToQueue(calibration, context);
                Calibration.requestCalibrationIfRangeTooNarrow();
            // KS newFingerStickData();
            }
        }
        if (firstCalRecord.getCalSubrecords()[0] != null && firstCalRecord.getCalSubrecords()[2] == null) {
            if (Calibration.latest(2).size() == 1) {
                Calibration.create(calRecords, context, true, 0);
            }
        }
        context.startService(new Intent(context, Notifications.class));
    }
}
Also used : CalSubrecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalSubrecord) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord) Intent(android.content.Intent) Notifications(com.eveningoutpost.dexdrip.UtilityModels.Notifications) Date(java.util.Date)

Aggregations

Intent (android.content.Intent)2 CalRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord)2 CalSubrecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalSubrecord)2 Notifications (com.eveningoutpost.dexdrip.UtilityModels.Notifications)2 Date (java.util.Date)2