Search in sources :

Example 1 with GlucoseReadingRx

use of com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx in project xDrip-plus by jamorham.

the class BluetoothGlucoseMeter method evaluateLastRecords.

// decide what to do with newest data
private synchronized void evaluateLastRecords() {
    if (lastBloodTest != null) {
        GcmActivity.syncBloodTests();
        final boolean delay_calibration = true;
        final GlucoseReadingRx lastGlucoseRecord = lastBloodTest.glucoseReadingRx;
        if ((lastGlucoseRecord != null) && (lastGlucoseRecord.device != null) && (ct != null)) {
            final String sequence_id = "last-btm-sequence-" + lastGlucoseRecord.device;
            final String timestamp_id = "last-btm-timestamp" + lastGlucoseRecord.device;
            // sequence numbers start from 0 so we add 1
            if ((lastGlucoseRecord.sequence + 1) > PersistentStore.getLong(sequence_id)) {
                PersistentStore.setLong(sequence_id, lastGlucoseRecord.sequence + 1);
                // get adjusted timestamp
                if (lastBloodTest.timestamp > PersistentStore.getLong(timestamp_id)) {
                    PersistentStore.setLong(timestamp_id, lastBloodTest.timestamp);
                    Log.d(TAG, "evaluateLastRecords: appears to be a new record: sequence:" + lastGlucoseRecord.sequence);
                    if (Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations") || Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations_auto")) {
                        final long time_since = JoH.msSince(lastBloodTest.timestamp);
                        if (time_since >= 0) {
                            if (time_since < (12 * Constants.HOUR_IN_MS)) {
                                final Calibration calibration = Calibration.lastValid();
                                // check must also be younger than most recent calibration
                                if ((calibration == null) || (lastBloodTest.timestamp > calibration.timestamp)) {
                                    UserError.Log.ueh(TAG, "Prompting for calibration for: " + BgGraphBuilder.unitized_string_with_units_static(lastBloodTest.mgdl) + " from: " + JoH.dateTimeText(lastBloodTest.timestamp));
                                    Home.startHomeWithExtra(getApplicationContext(), Home.HOME_FULL_WAKEUP, "1");
                                    JoH.runOnUiThreadDelayed(new Runnable() {

                                        @Override
                                        public void run() {
                                            Home.staticRefreshBGCharts();
                                            if ((Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations_auto") && isSlopeFlatEnough())) {
                                                Log.d(TAG, "Slope flat enough for auto calibration");
                                                if (!delay_calibration) {
                                                    Home.startHomeWithExtra(xdrip.getAppContext(), Home.BLUETOOTH_METER_CALIBRATION, BgGraphBuilder.unitized_string_static(lastBloodTest.mgdl), Long.toString(time_since), "auto");
                                                } else {
                                                    Log.d(TAG, "Delaying calibration for later");
                                                    JoH.static_toast_long("Waiting for 15 minutes more sensor data for calibration");
                                                }
                                            } else {
                                                if (Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations")) {
                                                    // manual calibration
                                                    Home.startHomeWithExtra(xdrip.getAppContext(), Home.BLUETOOTH_METER_CALIBRATION, BgGraphBuilder.unitized_string_static(lastBloodTest.mgdl), Long.toString(time_since), "manual");
                                                } else {
                                                    Log.d(TAG, "Not flat enough slope for auto calibration and manual calibration not enabled");
                                                }
                                            }
                                        }
                                    }, 500);
                                } else {
                                    UserError.Log.e(TAG, "evaluateLastRecords: meter reading is at least as old as last calibration - ignoring");
                                }
                            } else {
                                UserError.Log.e(TAG, "evaluateLastRecords: meter reading is too far in the past: " + JoH.dateTimeText(lastBloodTest.timestamp));
                            }
                        } else {
                            UserError.Log.e(TAG, "evaluateLastRecords: time is in the future - ignoring");
                        }
                    }
                }
            } else {
                UserError.Log.d(TAG, "evaluateLastRecords: sequence isn't newer");
            }
        } else {
            UserError.Log.e(TAG, "evaluateLastRecords: Data missing for evaluation");
        }
    } else {
        UserError.Log.e(TAG, "evaluateLastRecords: lastBloodTest is Null!!");
    }
}
Also used : Calibration(com.eveningoutpost.dexdrip.Models.Calibration) GlucoseReadingRx(com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx)

Example 2 with GlucoseReadingRx

use of com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx in project xDrip-plus by jamorham.

the class BluetoothGlucoseMeter method processCharacteristicChange.

private synchronized void processCharacteristicChange(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
    // extra debug
    if (d) {
        UserError.Log.d(TAG, "charactersiticChanged: " + characteristic.getUuid().toString() + " " + JoH.bytesToHex(characteristic.getValue()));
    }
    if (GLUCOSE_CHARACTERISTIC.equals(characteristic.getUuid())) {
        final GlucoseReadingRx gtb = new GlucoseReadingRx(characteristic.getValue(), gatt.getDevice().getAddress());
        UserError.Log.d(TAG, "Result: " + gtb.toString());
        if (ct == null) {
            statusUpdate("Cannot process glucose record as we do not know device time!");
        } else {
            markDeviceAsSuccessful(gatt);
            statusUpdate("Glucose Record: " + JoH.dateTimeText((gtb.time - ct.timediff) + gtb.offsetMs()) + "\n" + unitized_string_with_units_static(gtb.mgdl));
            if (playSounds() && JoH.ratelimit("bt_meter_data_in", 1))
                JoH.playResourceAudio(R.raw.bt_meter_data_in);
            if ((!ignore_control_solution_tests) || (gtb.sampleType != 10)) {
                final BloodTest bt = BloodTest.create((gtb.time - ct.timediff) + gtb.offsetMs(), gtb.mgdl, BLUETOOTH_GLUCOSE_METER_TAG + ":\n" + mLastManufacturer + "   " + mLastConnectedDeviceAddress);
                if (bt != null) {
                    UserError.Log.d(TAG, "Successfully created new BloodTest: " + bt.toS());
                    // add reference
                    bt.glucoseReadingRx = gtb;
                    lastBloodTest = bt;
                    final long record_time = lastBloodTest.timestamp;
                    JoH.runOnUiThreadDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (lastBloodTest.timestamp == record_time) {
                                evaluateLastRecords();
                            }
                        }
                    }, 1000);
                } else {
                    if (d)
                        UserError.Log.d(TAG, "Failed to create BloodTest record");
                }
            } else {
                UserError.Log.d(TAG, "Ignoring control solution test");
            }
        }
    } else if (RECORDS_CHARACTERISTIC.equals(characteristic.getUuid())) {
        UserError.Log.d(TAG, "Change notification for RECORDS: " + JoH.bytesToHex(characteristic.getValue()));
    } else if (CONTEXT_CHARACTERISTIC.equals(characteristic.getUuid())) {
        UserError.Log.d(TAG, "Change notification for CONTEXT: " + JoH.bytesToHex(characteristic.getValue()));
    } else if (VERIO_F7A3_NOTIFICATION.equals(characteristic.getUuid())) {
        UserError.Log.d(TAG, "Change notification for VERIO: " + JoH.bytesToHex(characteristic.getValue()));
        try {
            final GlucoseReadingRx gtb = VerioHelper.parseMessage(characteristic.getValue());
            if (gtb != null) {
                // if this was a BG reading we could process (offset already pre-calculated in time) - not robust against meter clock changes
                markDeviceAsSuccessful(gatt);
                statusUpdate("Glucose Record: " + JoH.dateTimeText((gtb.time + gtb.offsetMs())) + "\n" + unitized_string_with_units_static(gtb.mgdl));
                if (playSounds() && JoH.ratelimit("bt_meter_data_in", 1))
                    JoH.playResourceAudio(R.raw.bt_meter_data_in);
                final BloodTest bt = BloodTest.create((gtb.time) + gtb.offsetMs(), gtb.mgdl, BLUETOOTH_GLUCOSE_METER_TAG + ":\n" + mLastManufacturer + "   " + mLastConnectedDeviceAddress);
                if (bt != null) {
                    UserError.Log.d(TAG, "Successfully created new BloodTest: " + bt.toS());
                    // add reference
                    bt.glucoseReadingRx = gtb;
                    lastBloodTest = bt;
                    final long record_time = lastBloodTest.timestamp;
                    JoH.runOnUiThreadDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (lastBloodTest.timestamp == record_time) {
                                // zero hack
                                ct = new CurrentTimeRx();
                                evaluateLastRecords();
                            }
                        }
                    }, 1000);
                } else {
                    if (d)
                        UserError.Log.d(TAG, "Failed to create BloodTest record");
                }
            }
        } catch (Exception e) {
            UserError.Log.wtf(TAG, "Got exception processing Verio data " + e);
        }
    } else {
        UserError.Log.e(TAG, "Unknown characteristic change: " + characteristic.getUuid().toString() + " " + JoH.bytesToHex(characteristic.getValue()));
    }
}
Also used : CurrentTimeRx(com.eveningoutpost.dexdrip.GlucoseMeter.CurrentTimeRx) BloodTest(com.eveningoutpost.dexdrip.Models.BloodTest) GlucoseReadingRx(com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx)

Example 3 with GlucoseReadingRx

use of com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx in project xDrip by NightscoutFoundation.

the class BluetoothGlucoseMeter method evaluateLastRecords.

// decide what to do with newest data
private synchronized void evaluateLastRecords() {
    if (lastBloodTest != null) {
        GcmActivity.syncBloodTests();
        final boolean delay_calibration = true;
        final GlucoseReadingRx lastGlucoseRecord = lastBloodTest.glucoseReadingRx;
        if ((lastGlucoseRecord != null) && (lastGlucoseRecord.device != null) && (ct != null)) {
            final String sequence_id = "last-btm-sequence-" + lastGlucoseRecord.device;
            final String timestamp_id = "last-btm-timestamp" + lastGlucoseRecord.device;
            // sequence numbers start from 0 so we add 1
            if ((lastGlucoseRecord.sequence + 1) > PersistentStore.getLong(sequence_id)) {
                PersistentStore.setLong(sequence_id, lastGlucoseRecord.sequence + 1);
                // get adjusted timestamp
                if (lastBloodTest.timestamp > PersistentStore.getLong(timestamp_id)) {
                    PersistentStore.setLong(timestamp_id, lastBloodTest.timestamp);
                    Log.d(TAG, "evaluateLastRecords: appears to be a new record: sequence:" + lastGlucoseRecord.sequence);
                    if (Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations") || Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations_auto")) {
                        final long time_since = JoH.msSince(lastBloodTest.timestamp);
                        if (time_since >= 0) {
                            if (time_since < (12 * Constants.HOUR_IN_MS)) {
                                final Calibration calibration = Calibration.lastValid();
                                // check must also be younger than most recent calibration
                                if ((calibration == null) || (lastBloodTest.timestamp > calibration.timestamp)) {
                                    UserError.Log.ueh(TAG, "Prompting for calibration for: " + BgGraphBuilder.unitized_string_with_units_static(lastBloodTest.mgdl) + " from: " + JoH.dateTimeText(lastBloodTest.timestamp));
                                    Home.startHomeWithExtra(getApplicationContext(), Home.HOME_FULL_WAKEUP, "1");
                                    JoH.runOnUiThreadDelayed(new Runnable() {

                                        @Override
                                        public void run() {
                                            Home.staticRefreshBGCharts();
                                            if ((Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations_auto") && isSlopeFlatEnough())) {
                                                Log.d(TAG, "Slope flat enough for auto calibration");
                                                if (!delay_calibration) {
                                                    Home.startHomeWithExtra(xdrip.getAppContext(), Home.BLUETOOTH_METER_CALIBRATION, BgGraphBuilder.unitized_string_static(lastBloodTest.mgdl), Long.toString(time_since), "auto");
                                                } else {
                                                    Log.d(TAG, "Delaying calibration for later");
                                                    JoH.static_toast_long("Waiting for 15 minutes more sensor data for calibration");
                                                }
                                            } else {
                                                if (Pref.getBooleanDefaultFalse("bluetooth_meter_for_calibrations")) {
                                                    // manual calibration
                                                    Home.startHomeWithExtra(xdrip.getAppContext(), Home.BLUETOOTH_METER_CALIBRATION, BgGraphBuilder.unitized_string_static(lastBloodTest.mgdl), Long.toString(time_since), "manual");
                                                } else {
                                                    Log.d(TAG, "Not flat enough slope for auto calibration and manual calibration not enabled");
                                                }
                                            }
                                        }
                                    }, 500);
                                } else {
                                    UserError.Log.e(TAG, "evaluateLastRecords: meter reading is at least as old as last calibration - ignoring");
                                }
                            } else {
                                UserError.Log.e(TAG, "evaluateLastRecords: meter reading is too far in the past: " + JoH.dateTimeText(lastBloodTest.timestamp));
                            }
                        } else {
                            UserError.Log.e(TAG, "evaluateLastRecords: time is in the future - ignoring");
                        }
                    }
                }
            } else {
                UserError.Log.d(TAG, "evaluateLastRecords: sequence isn't newer");
            }
        } else {
            UserError.Log.e(TAG, "evaluateLastRecords: Data missing for evaluation");
        }
    } else {
        UserError.Log.e(TAG, "evaluateLastRecords: lastBloodTest is Null!!");
    }
}
Also used : Calibration(com.eveningoutpost.dexdrip.Models.Calibration) GlucoseReadingRx(com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx)

Example 4 with GlucoseReadingRx

use of com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx in project xDrip by NightscoutFoundation.

the class BluetoothGlucoseMeter method processCharacteristicChange.

private synchronized void processCharacteristicChange(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
    // extra debug
    if (d) {
        UserError.Log.d(TAG, "charactersiticChanged: " + characteristic.getUuid().toString() + " " + JoH.bytesToHex(characteristic.getValue()));
    }
    if (GLUCOSE_CHARACTERISTIC.equals(characteristic.getUuid())) {
        final GlucoseReadingRx gtb = new GlucoseReadingRx(characteristic.getValue(), gatt.getDevice().getAddress());
        UserError.Log.d(TAG, "Result: " + gtb.toString());
        if (ct == null) {
            statusUpdate("Cannot process glucose record as we do not know device time!");
        } else {
            markDeviceAsSuccessful(gatt);
            statusUpdate("Glucose Record: " + JoH.dateTimeText((gtb.time - ct.timediff) + gtb.offsetMs()) + "\n" + unitized_string_with_units_static(gtb.mgdl));
            if (playSounds() && JoH.ratelimit("bt_meter_data_in", 1))
                JoH.playResourceAudio(R.raw.bt_meter_data_in);
            if ((!ignore_control_solution_tests) || (gtb.sampleType != 10)) {
                final BloodTest bt = BloodTest.create((gtb.time - ct.timediff) + gtb.offsetMs(), gtb.mgdl, BLUETOOTH_GLUCOSE_METER_TAG + ":\n" + mLastManufacturer + "   " + mLastConnectedDeviceAddress);
                if (bt != null) {
                    UserError.Log.d(TAG, "Successfully created new BloodTest: " + bt.toS());
                    // add reference
                    bt.glucoseReadingRx = gtb;
                    lastBloodTest = bt;
                    final long record_time = lastBloodTest.timestamp;
                    JoH.runOnUiThreadDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (lastBloodTest.timestamp == record_time) {
                                evaluateLastRecords();
                            }
                        }
                    }, 1000);
                } else {
                    if (d)
                        UserError.Log.d(TAG, "Failed to create BloodTest record");
                }
            } else {
                UserError.Log.d(TAG, "Ignoring control solution test");
            }
        }
    } else if (RECORDS_CHARACTERISTIC.equals(characteristic.getUuid())) {
        UserError.Log.d(TAG, "Change notification for RECORDS: " + JoH.bytesToHex(characteristic.getValue()));
    } else if (CONTEXT_CHARACTERISTIC.equals(characteristic.getUuid())) {
        UserError.Log.d(TAG, "Change notification for CONTEXT: " + JoH.bytesToHex(characteristic.getValue()));
    } else if (VERIO_F7A3_NOTIFICATION.equals(characteristic.getUuid())) {
        UserError.Log.d(TAG, "Change notification for VERIO: " + JoH.bytesToHex(characteristic.getValue()));
        try {
            final GlucoseReadingRx gtb = VerioHelper.parseMessage(characteristic.getValue());
            if (gtb != null) {
                // if this was a BG reading we could process (offset already pre-calculated in time) - not robust against meter clock changes
                markDeviceAsSuccessful(gatt);
                statusUpdate("Glucose Record: " + JoH.dateTimeText((gtb.time + gtb.offsetMs())) + "\n" + unitized_string_with_units_static(gtb.mgdl));
                if (playSounds() && JoH.ratelimit("bt_meter_data_in", 1))
                    JoH.playResourceAudio(R.raw.bt_meter_data_in);
                final BloodTest bt = BloodTest.create((gtb.time) + gtb.offsetMs(), gtb.mgdl, BLUETOOTH_GLUCOSE_METER_TAG + ":\n" + mLastManufacturer + "   " + mLastConnectedDeviceAddress);
                if (bt != null) {
                    UserError.Log.d(TAG, "Successfully created new BloodTest: " + bt.toS());
                    // add reference
                    bt.glucoseReadingRx = gtb;
                    lastBloodTest = bt;
                    final long record_time = lastBloodTest.timestamp;
                    JoH.runOnUiThreadDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (lastBloodTest.timestamp == record_time) {
                                // zero hack
                                ct = new CurrentTimeRx();
                                evaluateLastRecords();
                            }
                        }
                    }, 1000);
                } else {
                    if (d)
                        UserError.Log.d(TAG, "Failed to create BloodTest record");
                }
            }
        } catch (Exception e) {
            UserError.Log.wtf(TAG, "Got exception processing Verio data " + e);
        }
    } else {
        UserError.Log.e(TAG, "Unknown characteristic change: " + characteristic.getUuid().toString() + " " + JoH.bytesToHex(characteristic.getValue()));
    }
}
Also used : CurrentTimeRx(com.eveningoutpost.dexdrip.GlucoseMeter.CurrentTimeRx) BloodTest(com.eveningoutpost.dexdrip.Models.BloodTest) GlucoseReadingRx(com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx)

Aggregations

GlucoseReadingRx (com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx)4 CurrentTimeRx (com.eveningoutpost.dexdrip.GlucoseMeter.CurrentTimeRx)2 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)2 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)2