Search in sources :

Example 11 with Calibration

use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.

the class CalibrationGraph method getCalibrationsLine.

@NonNull
public List<Line> getCalibrationsLine(List<Calibration> calibrations, int color) {
    if (calibrations == null)
        return new ArrayList<>();
    List<PointValue> values = new ArrayList<PointValue>();
    List<PointValue> valuesb = new ArrayList<PointValue>();
    List<PointValue> valuesc = new ArrayList<PointValue>();
    for (Calibration calibration : calibrations) {
        if (calibration.estimate_raw_at_time_of_calibration > end_x) {
            end_x = calibration.estimate_raw_at_time_of_calibration;
        }
        PointValue point = new PointValue((float) calibration.estimate_raw_at_time_of_calibration, doMgdl ? (float) calibration.bg : ((float) calibration.bg) * (float) Constants.MGDL_TO_MMOLL);
        PointValue pointb = new PointValue((float) calibration.raw_value, doMgdl ? (float) calibration.bg : ((float) calibration.bg) * (float) Constants.MGDL_TO_MMOLL);
        PointValue pointc = new PointValue((float) calibration.adjusted_raw_value, doMgdl ? (float) calibration.bg : ((float) calibration.bg) * (float) Constants.MGDL_TO_MMOLL);
        String time;
        if (show_days_since) {
            final int days_ago = daysAgo(calibration.raw_timestamp);
            time = (days_ago > 0) ? Integer.toString(days_ago) + "d  " : "";
            time = time + (JoH.hourMinuteString(calibration.raw_timestamp));
        } else {
            time = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date((long) calibration.raw_timestamp));
        }
        point.setLabel(time);
        values.add(point);
        // extra points showing real raw and age_adjusted raw for each calbration point
        valuesb.add(pointb);
        valuesc.add(pointc);
    }
    Line line = new Line(values);
    line.setColor(color);
    line.setHasLines(false);
    line.setPointRadius(4);
    line.setHasPoints(true);
    line.setHasLabels(true);
    List<Line> lines = new ArrayList<>();
    lines.add(line);
    if (Pref.getBooleanDefaultFalse("engineering_mode")) {
        // actual raw
        Line lineb = new Line(valuesb);
        lineb.setColor(Color.RED);
        lineb.setHasLines(false);
        lineb.setPointRadius(1);
        lineb.setHasPoints(true);
        lineb.setHasLabels(false);
        // age adjusted raw
        Line linec = new Line(valuesc);
        linec.setColor(Color.YELLOW);
        linec.setHasLines(false);
        linec.setPointRadius(1);
        linec.setHasPoints(true);
        linec.setHasLabels(false);
        lines.add(lineb);
        lines.add(linec);
    }
    return lines;
}
Also used : Line(lecho.lib.hellocharts.model.Line) PointValue(lecho.lib.hellocharts.model.PointValue) ArrayList(java.util.ArrayList) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) Date(java.util.Date) NonNull(android.support.annotation.NonNull)

Example 12 with Calibration

use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.

the class CalibrationGraph method overWriteSlope.

private void overWriteSlope() {
    final EditText editText = new EditText(this);
    editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    new AlertDialog.Builder(this).setTitle("Ovewrite Slope").setMessage("Overwrite Slope").setView(editText).setPositiveButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            String text = editText.getText().toString();
            if (!TextUtils.isEmpty(text)) {
                double doubleValue = JoH.tolerantParseDouble(text);
                Calibration calibration = Calibration.lastValid();
                calibration.slope = doubleValue;
                calibration.save();
                CalibrationSendQueue.addToQueue(calibration, getApplicationContext());
                recreate();
            } else {
                JoH.static_toast_long("Input not found! Cancelled!");
            }
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            JoH.static_toast_long("Cancelled!");
        }
    }).show();
}
Also used : EditText(android.widget.EditText) DialogInterface(android.content.DialogInterface) Calibration(com.eveningoutpost.dexdrip.Models.Calibration)

Example 13 with Calibration

use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.

the class InfluxDBUploader method upload.

public boolean upload(List<BgReading> glucoseDataSets, List<Calibration> meterRecords, List<Calibration> calRecords) {
    try {
        BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy("autogen").consistency(InfluxDB.ConsistencyLevel.ALL).build();
        for (BgReading record : glucoseDataSets) {
            if (record == null) {
                Log.e(TAG, "InfluxDB glucose record is null");
                continue;
            }
            batchPoints.point(createGlucosePoint(record));
        }
        for (Calibration record : meterRecords) {
            if (record == null) {
                Log.e(TAG, "InfluxDB meter record is null");
                continue;
            }
            batchPoints.point(createMeterPoint(record));
        }
        for (Calibration record : calRecords) {
            if (record == null) {
                Log.e(TAG, "InfluxDB calibration record is null");
                continue;
            }
            if (record.slope == 0d)
                continue;
            batchPoints.point(createCalibrationPoint(record));
        }
        try {
            Log.d(TAG, "Influx url: " + dbUri);
            InfluxDBFactory.connect(dbUri, dbUser, dbPassword, client).enableGzip().write(batchPoints);
            last_error = null;
            return true;
        } catch (java.lang.ExceptionInInitializerError e) {
            Log.e(TAG, "InfluxDB failed: " + e.getCause());
            return false;
        } catch (java.lang.NoClassDefFoundError e) {
            Log.e(TAG, "InfluxDB failed more: " + e);
            return false;
        } catch (IllegalArgumentException e) {
            Log.wtf(TAG, "InfluxDB problem: " + e);
            return false;
        } catch (Exception e) {
            Log.e(TAG, "Write to InfluxDB failed: " + e);
            last_error = e.getMessage();
            return false;
        }
    } catch (Exception e) {
        Log.wtf(TAG, "Exception during initialization: ", e);
        return false;
    }
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) IOException(java.io.IOException)

Example 14 with Calibration

use of com.eveningoutpost.dexdrip.Models.Calibration in project xDrip by NightscoutFoundation.

the class UploaderTask method doInBackground.

public Void doInBackground(String... urls) {
    try {
        final List<Long> circuits = new ArrayList<>();
        final List<String> types = new ArrayList<>();
        types.add(BgReading.class.getSimpleName());
        types.add(Calibration.class.getSimpleName());
        types.add(BloodTest.class.getSimpleName());
        types.add(Treatments.class.getSimpleName());
        if (Pref.getBooleanDefaultFalse("wear_sync")) {
            circuits.add(UploaderQueue.WATCH_WEARAPI);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_mongodb_enable")) {
            circuits.add(UploaderQueue.MONGO_DIRECT);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_api_enable")) {
            if ((Pref.getBoolean("cloud_storage_api_use_mobile", true) || (JoH.isLANConnected()))) {
                circuits.add(UploaderQueue.NIGHTSCOUT_RESTAPI);
            } else {
                Log.e(TAG, "Skipping Nightscout upload due to mobile data only");
            }
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_influxdb_enable")) {
            circuits.add(UploaderQueue.INFLUXDB_RESTAPI);
        }
        for (long THIS_QUEUE : circuits) {
            final List<BgReading> bgReadings = new ArrayList<>();
            final List<Calibration> calibrations = new ArrayList<>();
            final List<BloodTest> bloodtests = new ArrayList<>();
            final List<Treatments> treatmentsAdd = new ArrayList<>();
            final List<String> treatmentsDel = new ArrayList<>();
            final List<UploaderQueue> items = new ArrayList<>();
            for (String type : types) {
                final List<UploaderQueue> bgups = UploaderQueue.getPendingbyType(type, THIS_QUEUE);
                if (bgups != null) {
                    for (UploaderQueue up : bgups) {
                        switch(up.action) {
                            case "insert":
                            case "update":
                            case "create":
                                items.add(up);
                                if (type.equals(BgReading.class.getSimpleName())) {
                                    final BgReading this_bg = BgReading.byid(up.reference_id);
                                    if (this_bg != null) {
                                        bgReadings.add(this_bg);
                                    } else {
                                        Log.wtf(TAG, "BgReading with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Calibration.class.getSimpleName())) {
                                    final Calibration this_cal = Calibration.byid(up.reference_id);
                                    if ((this_cal != null) && (this_cal.isValid())) {
                                        calibrations.add(this_cal);
                                    } else {
                                        Log.wtf(TAG, "Calibration with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(BloodTest.class.getSimpleName())) {
                                    final BloodTest this_bt = BloodTest.byid(up.reference_id);
                                    if (this_bt != null) {
                                        bloodtests.add(this_bt);
                                    } else {
                                        Log.wtf(TAG, "Bloodtest with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Treatments.class.getSimpleName())) {
                                    final Treatments this_treat = Treatments.byid(up.reference_id);
                                    if (this_treat != null) {
                                        treatmentsAdd.add(this_treat);
                                    } else {
                                        Log.wtf(TAG, "Treatments with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                }
                                break;
                            case "delete":
                                if ((THIS_QUEUE == UploaderQueue.WATCH_WEARAPI || THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) && type.equals(Treatments.class.getSimpleName())) {
                                    items.add(up);
                                    Log.wtf(TAG, "Delete Treatments with ID: " + up.reference_uuid);
                                    treatmentsDel.add(up.reference_uuid);
                                } else if (up.reference_uuid != null) {
                                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " delete not yet implemented: " + up.reference_uuid);
                                    // mark as completed so as not to tie up the queue for now
                                    up.completed(THIS_QUEUE);
                                }
                                break;
                            default:
                                Log.e(TAG, "Unsupported operation type for " + type + " " + up.action);
                                break;
                        }
                    }
                }
            }
            if ((bgReadings.size() > 0) || (calibrations.size() > 0) || (bloodtests.size() > 0) || (treatmentsAdd.size() > 0 || treatmentsDel.size() > 0) || (UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), THIS_QUEUE, 1).size() > 0)) {
                Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Processing: " + bgReadings.size() + " BgReadings and " + calibrations.size() + " Calibrations " + bloodtests.size() + " bloodtests " + treatmentsAdd.size() + " treatmentsAdd " + treatmentsDel.size() + " treatmentsDel");
                boolean uploadStatus = false;
                if (THIS_QUEUE == UploaderQueue.MONGO_DIRECT) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadMongo(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadRest(bgReadings, bloodtests, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.INFLUXDB_RESTAPI) {
                    final InfluxDBUploader influxDBUploader = new InfluxDBUploader(xdrip.getAppContext());
                    uploadStatus = influxDBUploader.upload(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.WATCH_WEARAPI) {
                    uploadStatus = WatchUpdaterService.sendWearUpload(bgReadings, calibrations, bloodtests, treatmentsAdd, treatmentsDel);
                }
                // TODO some kind of fail counter?
                if (uploadStatus) {
                    for (UploaderQueue up : items) {
                        // approve all types for this queue
                        up.completed(THIS_QUEUE);
                    }
                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Marking: " + items.size() + " Items as successful");
                    if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                        Log.d(TAG, "Scheduling boosted repeat query");
                        SyncService.startSyncService(2000);
                    }
                }
            } else {
                Log.d(TAG, "Nothing to upload for: " + UploaderQueue.getCircuitName(THIS_QUEUE));
                if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                    PersistentStore.setBoolean(BACKFILLING_BOOSTER, false);
                    Log.d(TAG, "Switched off backfilling booster");
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "caught exception", e);
        exception = e;
        return null;
    }
    return null;
}
Also used : BloodTest(com.eveningoutpost.dexdrip.Models.BloodTest) ArrayList(java.util.ArrayList) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) InfluxDBUploader(com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Treatments(com.eveningoutpost.dexdrip.Models.Treatments)

Example 15 with Calibration

use of com.eveningoutpost.dexdrip.Models.Calibration 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)

Aggregations

Calibration (com.eveningoutpost.dexdrip.Models.Calibration)54 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)20 ArrayList (java.util.ArrayList)16 Date (java.util.Date)12 EditText (android.widget.EditText)8 Sensor (com.eveningoutpost.dexdrip.Models.Sensor)8 DataMap (com.google.android.gms.wearable.DataMap)8 DialogInterface (android.content.DialogInterface)6 Intent (android.content.Intent)6 NonNull (android.support.annotation.NonNull)6 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)6 PolyTrendLine (com.eveningoutpost.dexdrip.Models.Forecast.PolyTrendLine)6 TrendLine (com.eveningoutpost.dexdrip.Models.Forecast.TrendLine)6 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)6 CalibrationAbstract (com.eveningoutpost.dexdrip.calibrations.CalibrationAbstract)6 PluggableCalibration (com.eveningoutpost.dexdrip.calibrations.PluggableCalibration)6 IOException (java.io.IOException)6 PointValue (lecho.lib.hellocharts.model.PointValue)6 JSONException (org.json.JSONException)6 View (android.view.View)4