Search in sources :

Example 6 with CalRecord

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

the class SyncingService method performCalibrationCheckin.

/**
 * Handle action Sync in the provided background thread with the provided
 * parameters.
 */
private void performCalibrationCheckin() {
    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NSDownload");
    wl.acquire();
    try {
        Log.i("CALIBRATION-CHECK-IN: ", "Wake Lock Acquired");
        if (acquireSerialDevice()) {
            try {
                ReadData readData = new ReadData(mSerialDevice, mConnection, dexcom);
                // ReadData readData = new ReadData(mSerialDevice);
                CalRecord[] calRecords = readData.getRecentCalRecords();
                Log.i("CALIBRATION-CHECK-IN: ", "Found " + calRecords.length + " Records!");
                save_most_recent_cal_record(calRecords);
            } catch (Exception e) {
                Log.wtf("Unhandled exception caught", e);
            } finally {
                // Close serial
                try {
                    mSerialDevice.getPorts().get(0).close();
                } catch (IOException e) {
                    Log.e(TAG, "Unable to close", e);
                }
            }
        } else {
            Log.w("CALIBRATION-CHECK-IN: ", "Failed to acquire serial device");
        }
    } finally {
        wl.release();
    }
}
Also used : PowerManager(android.os.PowerManager) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord) IOException(java.io.IOException) IOException(java.io.IOException)

Example 7 with CalRecord

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

the class SlopeParameters method create.

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

Example 8 with CalRecord

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

the class SlopeParameters method create.

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

Example 9 with CalRecord

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

the class ShareTest method attemptRead.

public void attemptRead() {
    final ReadDataShare readData = new ReadDataShare(this);
    final Action1<Long> systemTimeListener = new Action1<Long>() {

        @Override
        public void call(Long s) {
            Log.d(TAG, "Made the full round trip, got " + s + " as the system time");
            Log.d("SYSTTIME", "Made the full round trip, got " + s + " as the system time");
            final long addativeSystemTimeOffset = new Date().getTime() - s;
            Log.d(TAG, "Made the full round trip, got " + addativeSystemTimeOffset + " offset");
            Log.d("SYSTTIME", "Made the full round trip, got " + addativeSystemTimeOffset + " offset");
            final Action1<CalRecord[]> calRecordListener = new Action1<CalRecord[]>() {

                @Override
                public void call(CalRecord[] calRecords) {
                    Log.d(TAG, "Made the full round trip, got " + calRecords.length + " Cal Records");
                    Calibration.create(calRecords, addativeSystemTimeOffset, getApplicationContext());
                    final Action1<SensorRecord[]> sensorRecordListener = new Action1<SensorRecord[]>() {

                        @Override
                        public void call(SensorRecord[] sensorRecords) {
                            Log.d(TAG, "Made the full round trip, got " + sensorRecords.length + " Sensor Records");
                            BgReading.create(sensorRecords, addativeSystemTimeOffset, getApplicationContext());
                            final Action1<EGVRecord[]> evgRecordListener = new Action1<EGVRecord[]>() {

                                @Override
                                public void call(EGVRecord[] egvRecords) {
                                    Log.d(TAG, "Made the full round trip, got " + egvRecords.length + " EVG Records");
                                    BgReading.create(egvRecords, addativeSystemTimeOffset, getApplicationContext());
                                }
                            };
                            readData.getRecentEGVs(evgRecordListener);
                        }
                    };
                    readData.getRecentSensorRecords(sensorRecordListener);
                }
            };
            readData.getRecentCalRecords(calRecordListener);
        }
    };
    readData.readSystemTime(systemTimeListener);
}
Also used : Action1(rx.functions.Action1) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord) SensorRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.SensorRecord) EGVRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.EGVRecord) ReadDataShare(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.ReadDataShare) Date(java.util.Date)

Example 10 with CalRecord

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

the class SyncingService method sync.

private void sync(int numOfPages) {
    boolean broadcastSent;
    if (acquireSerialDevice()) {
        try {
            ReadData readData = new ReadData(mSerialDevice);
            // TODO: need to check if numOfPages if valid on ReadData side
            EGVRecord[] recentRecords = readData.getRecentEGVsPages(numOfPages);
            MeterRecord[] meterRecords = readData.getRecentMeterRecords();
            // TODO: need to check if numOfPages if valid on ReadData side
            SensorRecord[] sensorRecords = readData.getRecentSensorRecords(numOfPages);
            GlucoseDataSet[] glucoseDataSets = Utils.mergeGlucoseDataRecords(recentRecords, sensorRecords);
            // FIXME: This is a workaround for the new Dexcom AP which seems to have a new format
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
            CalRecord[] calRecords = new CalRecord[1];
            if (prefs.getBoolean("cloud_cal_data", false)) {
                calRecords = readData.getRecentCalRecords();
            }
            long timeSinceLastRecord = readData.getTimeSinceEGVRecord(recentRecords[recentRecords.length - 1]);
            // TODO: determine if the logic here is correct. I suspect it assumes the last record was less than 5
            // minutes ago. If a reading is skipped and the device is plugged in then nextUploadTime will be
            // set to a negative number. This situation will eventually correct itself.
            long nextUploadTime = (1000 * 60 * 5) - (timeSinceLastRecord * (1000));
            long displayTime = readData.readDisplayTime().getTime();
            // FIXME: Device seems to flake out on battery level reads. Removing for now.
            // int batLevel = readData.readBatteryLevel();
            int batLevel = 100;
            // convert into json for d3 plot
            JSONArray array = new JSONArray();
            for (int i = 0; i < recentRecords.length; i++) array.put(recentRecords[i].toJSON());
            EGVRecord recentEGV = recentRecords[recentRecords.length - 1];
            // broadcastSGVToUI(recentEGV, uploadStatus, nextUploadTime + TIME_SYNC_OFFSET,
            // displayTime, array ,batLevel);
            broadcastSent = true;
        } catch (ArrayIndexOutOfBoundsException e) {
            Log.wtf("Unable to read from the dexcom, maybe it will work next time", e);
        } catch (NegativeArraySizeException e) {
            Log.wtf("Negative array exception from receiver", e);
        } catch (IndexOutOfBoundsException e) {
            Log.wtf("IndexOutOfBounds exception from receiver", e);
        } catch (CRCFailRuntimeException e) {
            // FIXME: may consider localizing this catch at a lower level (like ReadData) so that
            // if the CRC check fails on one type of record we can capture the values if it
            // doesn't fail on other types of records. This means we'd need to broadcast back
            // partial results to the UI. Adding it to a lower level could make the ReadData class
            // more difficult to maintain - needs discussion.
            Log.wtf("CRC failed", e);
        } catch (Exception e) {
            Log.wtf("Unhandled exception caught", e);
        } finally {
            // Close serial
            try {
                mSerialDevice.getPorts().get(0).close();
            } catch (IOException e) {
                Log.e(TAG, "Unable to close", e);
            }
        }
    }
// if (!broadcastSent) broadcastSGVToUI();
}
Also used : SharedPreferences(android.content.SharedPreferences) JSONArray(org.json.JSONArray) IOException(java.io.IOException) EGVRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.EGVRecord) IOException(java.io.IOException) MeterRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.MeterRecord) SensorRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.SensorRecord) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord) GlucoseDataSet(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GlucoseDataSet)

Aggregations

CalRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord)14 EGVRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.EGVRecord)10 SensorRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.SensorRecord)10 MeterRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.MeterRecord)6 Date (java.util.Date)6 PowerManager (android.os.PowerManager)4 GenericXMLRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GenericXMLRecord)4 PageHeader (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.PageHeader)4 IOException (java.io.IOException)4 Action1 (rx.functions.Action1)4 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 ReadDataShare (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.ReadDataShare)2 CalSubrecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalSubrecord)2 GlucoseDataSet (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GlucoseDataSet)2 Notifications (com.eveningoutpost.dexdrip.UtilityModels.Notifications)2 JSONArray (org.json.JSONArray)2