com.eveningoutpost.dexdrip.UtilityModels.CalibrationSendQueue Java Examples

The following examples show how to use com.eveningoutpost.dexdrip.UtilityModels.CalibrationSendQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void clear_byuuid(String uuid, boolean from_interactive) {
    if (uuid == null) return;
    Calibration calibration = byuuid(uuid);
    if (calibration != null) {
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        newFingerStickData();
        if (from_interactive) {
            GcmActivity.clearLastCalibration(uuid);
        }
    }
}
 
Example #2
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void clearLastCalibration() {
    CalibrationRequest.clearAll();
    Log.d(TAG, "Trying to clear last calibration");
    Calibration calibration = Calibration.last();
    if (calibration != null) {
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        newFingerStickData();
    }
}
 
Example #3
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void clearCalibrationByUUID(String uuid) {
    final Calibration calibration = Calibration.byuuid(uuid);
    if (calibration != null) {
        CalibrationRequest.clearAll();
        Log.d(TAG, "Trying to clear last calibration: " + uuid);
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        newFingerStickData();
    } else {
        Log.d(TAG,"Could not find calibration to clear: "+uuid);
    }
}
 
Example #4
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void clear_byuuid(String uuid, boolean from_interactive) {
    if (uuid == null) return;
    Calibration calibration = byuuid(uuid);
    if (calibration != null) {
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        newFingerStickData();
        if (from_interactive) {
            GcmActivity.clearLastCalibration(uuid);
        }
    }
}
 
Example #5
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void clear_byuuid(String uuid, boolean from_interactive) {
    if (uuid == null) return;
    Calibration calibration = byuuid(uuid);
    if (calibration != null) {
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        //KS newFingerStickData();
        //KS if (from_interactive) {
        //KS     GcmActivity.clearLastCalibration();
        //KS }
    }
}
 
Example #6
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void clearLastCalibration() {
    CalibrationRequest.clearAll();
    Log.d(TAG, "Trying to clear last calibration");
    Calibration calibration = Calibration.last();
    if (calibration != null) {
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        //KS newFingerStickData();
    }
}
 
Example #7
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void clearCalibrationByUUID(String uuid) {
    final Calibration calibration = Calibration.byuuid(uuid);
    if (calibration != null) {
        CalibrationRequest.clearAll();
        Log.d(TAG, "Trying to clear last calibration: " + uuid);
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        newFingerStickData();
    } else {
        Log.d(TAG,"Could not find calibration to clear: "+uuid);
    }
}
 
Example #8
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void clearLastCalibration() {
    CalibrationRequest.clearAll();
    Log.d(TAG, "Trying to clear last calibration");
    Calibration calibration = Calibration.last();
    if (calibration != null) {
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        newFingerStickData();
    }
}
 
Example #9
Source File: Calibration.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
public static void clearLastCalibration(Context context) {

    Calibration last_calibration = Calibration.last();
    if(last_calibration == null) {
        return;
    }
    last_calibration.sensor_confidence = 0;
    last_calibration.slope_confidence = 0;
    last_calibration.save();
    CalibrationSendQueue.addToQueue(last_calibration, context);
}
 
Example #10
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void clear_byuuid(String uuid, boolean from_interactive) {
    if (uuid == null) return;
    Calibration calibration = byuuid(uuid);
    if (calibration != null) {
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        //KS newFingerStickData();
        //KS if (from_interactive) {
        //KS     GcmActivity.clearLastCalibration();
        //KS }
    }
}
 
Example #11
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void clearLastCalibration() {
    CalibrationRequest.clearAll();
    Log.d(TAG, "Trying to clear last calibration");
    Calibration calibration = Calibration.last();
    if (calibration != null) {
        calibration.invalidate();
        CalibrationSendQueue.addToQueue(calibration, xdrip.getAppContext());
        //KS newFingerStickData();
    }
}
 
Example #12
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public void rawValueOverride(double rawValue, Context context) {
    estimate_raw_at_time_of_calibration = rawValue;
    save();
    calculate_w_l_s();
    CalibrationSendQueue.addToQueue(this, context);
}
 
Example #13
Source File: Calibration.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public void rawValueOverride(double rawValue, Context context) {
    estimate_raw_at_time_of_calibration = rawValue;
    save();
    calculate_w_l_s(context);
    CalibrationSendQueue.addToQueue(this, context);
}
 
Example #14
Source File: Calibration.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public static Calibration create(double bg, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String unit = prefs.getString("units", "mgdl");
    boolean adjustPast = prefs.getBoolean("adjust_past", false);

    if(unit.compareTo("mgdl") != 0 ) {
        bg = bg * Constants.MMOLL_TO_MGDL;
    }

    CalibrationRequest.clearAll();
    Calibration calibration = new Calibration();
    Sensor sensor = Sensor.currentSensor();

    if (sensor != null) {
        BgReading bgReading = BgReading.last();
        if (bgReading != null) {
            calibration.sensor = sensor;
            calibration.bg = bg;
            calibration.check_in = false;
            calibration.timestamp = new Date().getTime();
            calibration.raw_value = bgReading.raw_data;
            calibration.adjusted_raw_value = bgReading.age_adjusted_raw_value;
            calibration.sensor_uuid = sensor.uuid;
            calibration.slope_confidence = Math.min(Math.max(((4 - Math.abs((bgReading.calculated_value_slope) * 60000))/4), 0), 1);

            double estimated_raw_bg = BgReading.estimated_raw_bg(new Date().getTime());
            calibration.raw_timestamp = bgReading.timestamp;
            if (Math.abs(estimated_raw_bg - bgReading.age_adjusted_raw_value) > 20) {
                calibration.estimate_raw_at_time_of_calibration = bgReading.age_adjusted_raw_value;
            } else {
                calibration.estimate_raw_at_time_of_calibration = estimated_raw_bg;
            }
            calibration.distance_from_estimate = Math.abs(calibration.bg - bgReading.calculated_value);
            calibration.sensor_confidence = Math.max(((-0.0018 * bg * bg) + (0.6657 * bg) + 36.7505) / 100, 0);
            calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sensor.started_at;
            calibration.uuid = UUID.randomUUID().toString();
            calibration.save();

            bgReading.calibration = calibration;
            bgReading.calibration_flag = true;
            bgReading.save();
            BgSendQueue.handleNewBgReading(bgReading, "update", context);

            calculate_w_l_s(context);

            adjustRecentBgReadings(adjustPast?30:1);
            CalibrationSendQueue.addToQueue(calibration, context);
            context.startService(new Intent(context, Notifications.class));
            Calibration.requestCalibrationIfRangeTooNarrow();
        }
    } else {
        Log.d("CALIBRATION", "No sensor, cant save!");
    }
    return Calibration.last();
}
 
Example #15
Source File: Calibration.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
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;
                    }
                    calibration.slope_confidence = 0.8; //TODO: query backwards to find this value near the timestamp
                    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();
                }
            }
            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));
        }
    }
 
Example #16
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
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.w("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;
                    if (calibration.timestamp > new Date().getTime()) {
                        Log.e(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;
                    }
                    calibration.slope_confidence = 0.8; //TODO: query backwards to find this value near the timestamp
                    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();
                }
            }
            if(firstCalRecord.getCalSubrecords()[0] != null && firstCalRecord.getCalSubrecords()[2] == null) {
                if(Calibration.latest(2).size() == 1) {
                    Calibration.create(calRecords, context, true, 0);
                }
            }
            Notifications.notificationSetter(context);
        }
    }
 
Example #17
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static Calibration create(double bg, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String unit = prefs.getString("units", "mgdl");

    if(unit.compareTo("mgdl") != 0 ) {
        bg = bg * Constants.MMOLL_TO_MGDL;
    }

    CalibrationRequest.clearAll();
    Calibration calibration = new Calibration();
    Sensor sensor = Sensor.currentSensor();

    if (sensor != null) {
        BgReading bgReading = BgReading.last();
        if (bgReading != null) {
            calibration.sensor = sensor;
            calibration.bg = bg;
            calibration.check_in = false;
            calibration.timestamp = new Date().getTime();
            calibration.raw_value = bgReading.raw_data;
            calibration.adjusted_raw_value = bgReading.age_adjusted_raw_value;
            calibration.sensor_uuid = sensor.uuid;
            calibration.slope_confidence = Math.min(Math.max(((4 - Math.abs((bgReading.calculated_value_slope) * 60000))/4), 0), 1);

            double estimated_raw_bg = BgReading.estimated_raw_bg(new Date().getTime());
            calibration.raw_timestamp = bgReading.timestamp;
            if (Math.abs(estimated_raw_bg - bgReading.age_adjusted_raw_value) > 20) {
                calibration.estimate_raw_at_time_of_calibration = bgReading.age_adjusted_raw_value;
            } else {
                calibration.estimate_raw_at_time_of_calibration = estimated_raw_bg;
            }
            calibration.distance_from_estimate = Math.abs(calibration.bg - bgReading.calculated_value);
            calibration.sensor_confidence = Math.max(((-0.0018 * bg * bg) + (0.6657 * bg) + 36.7505) / 100, 0);
            calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sensor.started_at;
            calibration.uuid = UUID.randomUUID().toString();
            calibration.save();

            bgReading.calibration = calibration;
            bgReading.calibration_flag = true;
            bgReading.save();
            BgSendQueue.addToQueue(bgReading, "update", context);

            calculate_w_l_s();
            adjustRecentBgReadings();
            CalibrationSendQueue.addToQueue(calibration, context);
            Notifications.notificationSetter(context);
            Calibration.requestCalibrationIfRangeTooNarrow();
        }
    } else {
        Log.w("CALIBRATION", "No sensor, cant save!");
    }
    return Calibration.last();
}
 
Example #18
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
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;
                    }
                    calibration.slope_confidence = 0.8; //TODO: query backwards to find this value near the timestamp
                    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));
        }
    }
 
Example #19
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public void rawValueOverride(double rawValue, Context context) {
    estimate_raw_at_time_of_calibration = rawValue;
    save();
    calculate_w_l_s();
    CalibrationSendQueue.addToQueue(this, context);
}
 
Example #20
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
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;
                    }
                    calibration.slope_confidence = 0.8; //TODO: query backwards to find this value near the timestamp
                    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();
                    newFingerStickData();
                }
            }
            if (firstCalRecord.getCalSubrecords()[0] != null && firstCalRecord.getCalSubrecords()[2] == null) {
                if (Calibration.latest(2).size() == 1) {
                    Calibration.create(calRecords, context, true, 0);
                }
            }
            Notifications.start();
        }
    }
 
Example #21
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public void rawValueOverride(double rawValue, Context context) {
    estimate_raw_at_time_of_calibration = rawValue;
    save();
    calculate_w_l_s();
    CalibrationSendQueue.addToQueue(this, context);
}
 
Example #22
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public static Calibration create(double bg, long timeoffset, Context context, boolean note_only, long estimatedInterstitialLagSeconds) {
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String unit = prefs.getString("units", "mgdl");
    final boolean adjustPast = prefs.getBoolean("rewrite_history", true);

    if (unit.compareTo("mgdl") != 0) {
        bg = bg * Constants.MMOLL_TO_MGDL;
    }

    if ((bg < 40) || (bg > 400)) {
        Log.wtf(TAG, "Invalid out of range calibration glucose mg/dl value of: " + bg);
        JoH.static_toast_long("Calibration out of range: " + bg + " mg/dl");
        return null;
    }

    if (!note_only) CalibrationRequest.clearAll();
    final Calibration calibration = new Calibration();
    Sensor sensor = Sensor.currentSensor();

    boolean is_follower = prefs.getString("dex_collection_method", "").equals("Follower");
    if ((sensor == null)
            && (is_follower)) {
        Sensor.create(Math.round(JoH.ts())); // no sensor? no problem, create virtual one for follower
        sensor = Sensor.currentSensor();
    }

    if (sensor != null) {
        BgReading bgReading = null;
        if (timeoffset == 0) {
            bgReading = BgReading.last(is_follower);
        } else {
            // get closest bg reading we can find with a cut off at 15 minutes max time
            bgReading = BgReading.getForPreciseTimestamp(new Date().getTime() - ((timeoffset - estimatedInterstitialLagSeconds) * 1000 ), (15 * 60 * 1000));
        }
        if (bgReading != null) {
            calibration.sensor = sensor;
            calibration.bg = bg;
            calibration.check_in = false;
            calibration.timestamp = new Date().getTime() - (timeoffset * 1000); //  potential historical bg readings
            calibration.raw_value = bgReading.raw_data;
            calibration.adjusted_raw_value = bgReading.age_adjusted_raw_value;
            calibration.sensor_uuid = sensor.uuid;
            calibration.slope_confidence = Math.min(Math.max(((4 - Math.abs((bgReading.calculated_value_slope) * 60000)) / 4), 0), 1);

            double estimated_raw_bg = BgReading.estimated_raw_bg(new Date().getTime());
            calibration.raw_timestamp = bgReading.timestamp;
            if (Math.abs(estimated_raw_bg - bgReading.age_adjusted_raw_value) > 20) {
                calibration.estimate_raw_at_time_of_calibration = bgReading.age_adjusted_raw_value;
            } else {
                calibration.estimate_raw_at_time_of_calibration = estimated_raw_bg;
            }
            calibration.distance_from_estimate = Math.abs(calibration.bg - bgReading.calculated_value);
            if (!note_only) {
                calibration.sensor_confidence = Math.max(((-0.0018 * bg * bg) + (0.6657 * bg) + 36.7505) / 100, 0);
            } else {
                calibration.sensor_confidence = 0; // exclude from calibrations but show on graph
                calibration.slope = 0;
                calibration.intercept = 0;
            }
            calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sensor.started_at;
            calibration.uuid = UUID.randomUUID().toString();
            calibration.save();

            if (!note_only) {
                bgReading.calibration = calibration;
                bgReading.calibration_flag = true;
                bgReading.save();
            }

            if ((!is_follower) && (!note_only)) {
                BgSendQueue.handleNewBgReading(bgReading, "update", context);
                // TODO probably should add a more fine grained prefs option in future
                calculate_w_l_s(prefs.getBoolean("infrequent_calibration", false));
                adjustRecentBgReadings(adjustPast ? 30 : 2);
                CalibrationSendQueue.addToQueue(calibration, context);
                context.startService(new Intent(context, Notifications.class));
                Calibration.requestCalibrationIfRangeTooNarrow();
                //KS newFingerStickData();
            } else {
                Log.d(TAG, "Follower mode or note so not processing calibration deeply");
            }
        } else {
            // we couldn't get a reading close enough to the calibration timestamp
            if (!is_follower) {
                JoH.static_toast(context, "No close enough reading for Calib (15 min)", Toast.LENGTH_LONG);
            }
        }
    } else {
        Log.d("CALIBRATION", "No sensor, cant save!");
    }
    return Calibration.last();
}
 
Example #23
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
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;
                    }
                    calibration.slope_confidence = 0.8; //TODO: query backwards to find this value near the timestamp
                    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));
        }
    }
 
Example #24
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public void rawValueOverride(double rawValue, Context context) {
    estimate_raw_at_time_of_calibration = rawValue;
    save();
    calculate_w_l_s();
    CalibrationSendQueue.addToQueue(this, context);
}
 
Example #25
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
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;
                    }
                    calibration.slope_confidence = 0.8; //TODO: query backwards to find this value near the timestamp
                    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();
                    newFingerStickData();
                }
            }
            if (firstCalRecord.getCalSubrecords()[0] != null && firstCalRecord.getCalSubrecords()[2] == null) {
                if (Calibration.latest(2).size() == 1) {
                    Calibration.create(calRecords, context, true, 0);
                }
            }
            Notifications.start();
        }
    }
 
Example #26
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public void rawValueOverride(double rawValue, Context context) {
    estimate_raw_at_time_of_calibration = rawValue;
    save();
    calculate_w_l_s();
    CalibrationSendQueue.addToQueue(this, context);
}
 
Example #27
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static Calibration create(double bg, long timeoffset, Context context, boolean note_only, long estimatedInterstitialLagSeconds) {
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String unit = prefs.getString("units", "mgdl");
    final boolean adjustPast = prefs.getBoolean("rewrite_history", true);

    if (unit.compareTo("mgdl") != 0) {
        bg = bg * Constants.MMOLL_TO_MGDL;
    }

    if ((bg < 40) || (bg > 400)) {
        Log.wtf(TAG, "Invalid out of range calibration glucose mg/dl value of: " + bg);
        JoH.static_toast_long("Calibration out of range: " + bg + " mg/dl");
        return null;
    }

    if (!note_only) CalibrationRequest.clearAll();
    final Calibration calibration = new Calibration();
    Sensor sensor = Sensor.currentSensor();

    boolean is_follower = prefs.getString("dex_collection_method", "").equals("Follower");
    if ((sensor == null)
            && (is_follower)) {
        Sensor.create(Math.round(JoH.ts())); // no sensor? no problem, create virtual one for follower
        sensor = Sensor.currentSensor();
    }

    if (sensor != null) {
        BgReading bgReading = null;
        if (timeoffset == 0) {
            bgReading = BgReading.last(is_follower);
        } else {
            // get closest bg reading we can find with a cut off at 15 minutes max time
            bgReading = BgReading.getForPreciseTimestamp(new Date().getTime() - ((timeoffset - estimatedInterstitialLagSeconds) * 1000 ), (15 * 60 * 1000));
        }
        if (bgReading != null) {
            calibration.sensor = sensor;
            calibration.bg = bg;
            calibration.check_in = false;
            calibration.timestamp = new Date().getTime() - (timeoffset * 1000); //  potential historical bg readings
            calibration.raw_value = bgReading.raw_data;
            calibration.adjusted_raw_value = bgReading.age_adjusted_raw_value;
            calibration.sensor_uuid = sensor.uuid;
            calibration.slope_confidence = Math.min(Math.max(((4 - Math.abs((bgReading.calculated_value_slope) * 60000)) / 4), 0), 1);

            double estimated_raw_bg = BgReading.estimated_raw_bg(new Date().getTime());
            calibration.raw_timestamp = bgReading.timestamp;
            if (Math.abs(estimated_raw_bg - bgReading.age_adjusted_raw_value) > 20) {
                calibration.estimate_raw_at_time_of_calibration = bgReading.age_adjusted_raw_value;
            } else {
                calibration.estimate_raw_at_time_of_calibration = estimated_raw_bg;
            }
            calibration.distance_from_estimate = Math.abs(calibration.bg - bgReading.calculated_value);
            if (!note_only) {
                calibration.sensor_confidence = Math.max(((-0.0018 * bg * bg) + (0.6657 * bg) + 36.7505) / 100, 0);
            } else {
                calibration.sensor_confidence = 0; // exclude from calibrations but show on graph
                calibration.slope = 0;
                calibration.intercept = 0;
            }
            calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sensor.started_at;
            calibration.uuid = UUID.randomUUID().toString();
            calibration.save();

            if (!note_only) {
                bgReading.calibration = calibration;
                bgReading.calibration_flag = true;
                bgReading.save();
            }

            if ((!is_follower) && (!note_only)) {
                BgSendQueue.handleNewBgReading(bgReading, "update", context);
                // TODO probably should add a more fine grained prefs option in future
                calculate_w_l_s(prefs.getBoolean("infrequent_calibration", false));
                adjustRecentBgReadings(adjustPast ? 30 : 2);
                CalibrationSendQueue.addToQueue(calibration, context);
                context.startService(new Intent(context, Notifications.class));
                Calibration.requestCalibrationIfRangeTooNarrow();
                //KS newFingerStickData();
            } else {
                Log.d(TAG, "Follower mode or note so not processing calibration deeply");
            }
        } else {
            // we couldn't get a reading close enough to the calibration timestamp
            if (!is_follower) {
                JoH.static_toast(context, "No close enough reading for Calib (15 min)", Toast.LENGTH_LONG);
            }
        }
    } else {
        Log.d("CALIBRATION", "No sensor, cant save!");
    }
    return Calibration.last();
}