Search in sources :

Example 1 with Home

use of com.eveningoutpost.dexdrip.Home in project xDrip by NightscoutFoundation.

the class MedtrumCollectionService method processBackFillPacket.

private void processBackFillPacket(final byte[] packet) {
    final BackFillRx backFillRx = new BackFillRx(packet);
    UserError.Log.d(TAG, backFillRx.toS());
    if (backFillRx.isOk()) {
        boolean changed = false;
        final List<Integer> backsies = backFillRx.getRawList();
        if (backsies != null) {
            for (int index = 0; index < backsies.size(); index++) {
                final long timestamp = timeStampFromTickCounter(serial, backFillRx.sequenceStart + index);
                UserError.Log.d(TAG, "Backsie:  id:" + (backFillRx.sequenceStart + index) + " raw:" + backsies.get(index) + " @ " + JoH.dateTimeText(timestamp));
                final long since = msSince(timestamp);
                if ((since > HOUR_IN_MS * 6) || (since < 0)) {
                    UserError.Log.wtf(TAG, "Backfill timestamp unrealistic: " + JoH.dateTimeText(timestamp) + " (ignored)");
                } else {
                    final double glucose = backFillRx.getGlucose(backsies.get(index));
                    final int scaled_raw_data = backFillRx.getSensorRawEmulateDex(backsies.get(index));
                    if (BgReading.getForPreciseTimestamp(timestamp, (long) (Constants.MINUTE_IN_MS * 2.5)) == null) {
                        if (isNative()) {
                            // Native version
                            if (glucose > 0) {
                                BgReading.bgReadingInsertMedtrum(glucose, timestamp, "Backfill", scaled_raw_data);
                                UserError.Log.d(TAG, "Adding native backfilled reading: " + JoH.dateTimeText(timestamp) + " " + BgGraphBuilder.unitized_string_static(glucose));
                            }
                            final BgReading bgReadingTemp = BgReading.createFromRawNoSave(null, null, scaled_raw_data, scaled_raw_data, timestamp);
                            if (bgReadingTemp.calculated_value > 0) {
                                Prediction.create(bgReadingTemp.timestamp, (int) bgReadingTemp.calculated_value, "Medtrum2nd").save();
                            }
                        } else {
                            if (glucose > 0) {
                                Prediction.create(timestamp, (int) glucose, "Medtrum2nd").save();
                            }
                            // xDrip as primary
                            final BgReading bgreading = BgReading.create(scaled_raw_data, scaled_raw_data, xdrip.getAppContext(), timestamp);
                            if (bgreading != null) {
                                UserError.Log.d(TAG, "Backfilled BgReading created: " + bgreading.uuid + " " + JoH.dateTimeText(bgreading.timestamp));
                            } else {
                                UserError.Log.d(TAG, "BgReading null!");
                            }
                        }
                        Inevitable.task("backfill-ui-update", 3000, Home::staticRefreshBGChartsOnIdle);
                        changed = true;
                    }
                }
            }
            if (!changed && backsies.size() < requestedBackfillSize) {
                if (JoH.ratelimit("mt-backfill-repeat", 60)) {
                    UserError.Log.d(TAG, "Requesting additional backfill with offset: " + backsies.size());
                    backFillIfNeeded(lastAnnex, backsies.size());
                }
            }
        }
    } else {
        UserError.Log.e(TAG, "Backfill data reports not ok");
    }
}
Also used : BackFillRx(com.eveningoutpost.dexdrip.cgm.medtrum.messages.BackFillRx) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Home(com.eveningoutpost.dexdrip.Home) SuppressLint(android.annotation.SuppressLint)

Example 2 with Home

use of com.eveningoutpost.dexdrip.Home 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);
                    JoH.runOnUiThreadDelayed(Home::staticRefreshBGCharts, 300);
                    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));
                                    JoH.clearCache();
                                    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) Home(com.eveningoutpost.dexdrip.Home)

Example 3 with Home

use of com.eveningoutpost.dexdrip.Home 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);
                    JoH.runOnUiThreadDelayed(Home::staticRefreshBGCharts, 300);
                    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));
                                    JoH.clearCache();
                                    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) Home(com.eveningoutpost.dexdrip.Home)

Example 4 with Home

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

the class MedtrumCollectionService method processBackFillPacket.

private void processBackFillPacket(final byte[] packet) {
    final BackFillRx backFillRx = new BackFillRx(packet);
    UserError.Log.d(TAG, backFillRx.toS());
    if (backFillRx.isOk()) {
        boolean changed = false;
        final List<Integer> backsies = backFillRx.getRawList();
        if (backsies != null) {
            for (int index = 0; index < backsies.size(); index++) {
                final long timestamp = timeStampFromTickCounter(serial, backFillRx.sequenceStart + index);
                UserError.Log.d(TAG, "Backsie:  id:" + (backFillRx.sequenceStart + index) + " raw:" + backsies.get(index) + " @ " + JoH.dateTimeText(timestamp));
                final long since = msSince(timestamp);
                if ((since > HOUR_IN_MS * 6) || (since < 0)) {
                    UserError.Log.wtf(TAG, "Backfill timestamp unrealistic: " + JoH.dateTimeText(timestamp) + " (ignored)");
                } else {
                    final double glucose = backFillRx.getGlucose(backsies.get(index));
                    final int scaled_raw_data = backFillRx.getSensorRawEmulateDex(backsies.get(index));
                    if (BgReading.getForPreciseTimestamp(timestamp, (long) (Constants.MINUTE_IN_MS * 2.5)) == null) {
                        if (isNative()) {
                            // Native version
                            if (glucose > 0) {
                                BgReading.bgReadingInsertMedtrum(glucose, timestamp, "Backfill", scaled_raw_data);
                                UserError.Log.d(TAG, "Adding native backfilled reading: " + JoH.dateTimeText(timestamp) + " " + BgGraphBuilder.unitized_string_static(glucose));
                            }
                            final BgReading bgReadingTemp = BgReading.createFromRawNoSave(null, null, scaled_raw_data, scaled_raw_data, timestamp);
                            if (bgReadingTemp.calculated_value > 0) {
                                Prediction.create(bgReadingTemp.timestamp, (int) bgReadingTemp.calculated_value, "Medtrum2nd").save();
                            }
                        } else {
                            if (glucose > 0) {
                                Prediction.create(timestamp, (int) glucose, "Medtrum2nd").save();
                            }
                            // xDrip as primary
                            final BgReading bgreading = BgReading.create(scaled_raw_data, scaled_raw_data, xdrip.getAppContext(), timestamp);
                            if (bgreading != null) {
                                UserError.Log.d(TAG, "Backfilled BgReading created: " + bgreading.uuid + " " + JoH.dateTimeText(bgreading.timestamp));
                            } else {
                                UserError.Log.d(TAG, "BgReading null!");
                            }
                        }
                        Inevitable.task("backfill-ui-update", 3000, Home::staticRefreshBGChartsOnIdle);
                        changed = true;
                    }
                }
            }
            if (!changed && backsies.size() < requestedBackfillSize) {
                if (JoH.ratelimit("mt-backfill-repeat", 60)) {
                    UserError.Log.d(TAG, "Requesting additional backfill with offset: " + backsies.size());
                    backFillIfNeeded(lastAnnex, backsies.size());
                }
            }
        }
    } else {
        UserError.Log.e(TAG, "Backfill data reports not ok");
    }
}
Also used : BackFillRx(com.eveningoutpost.dexdrip.cgm.medtrum.messages.BackFillRx) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Home(com.eveningoutpost.dexdrip.Home) SuppressLint(android.annotation.SuppressLint)

Aggregations

Home (com.eveningoutpost.dexdrip.Home)4 SuppressLint (android.annotation.SuppressLint)2 GlucoseReadingRx (com.eveningoutpost.dexdrip.GlucoseMeter.GlucoseReadingRx)2 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)2 BackFillRx (com.eveningoutpost.dexdrip.cgm.medtrum.messages.BackFillRx)2