Search in sources :

Example 1 with GlucoseDataSet

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

the class Utils method mergeGlucoseDataRecords.

public static GlucoseDataSet[] mergeGlucoseDataRecords(EGVRecord[] egvRecords, SensorRecord[] sensorRecords) {
    int egvLength = egvRecords.length;
    int sensorLength = sensorRecords.length;
    int smallerLength = egvLength < sensorLength ? egvLength : sensorLength;
    GlucoseDataSet[] glucoseDataSets = new GlucoseDataSet[smallerLength];
    for (int i = 1; i <= smallerLength; i++) {
        glucoseDataSets[smallerLength - i] = new GlucoseDataSet(egvRecords[egvLength - i], sensorRecords[sensorLength - i]);
    }
    return glucoseDataSets;
}
Also used : GlucoseDataSet(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GlucoseDataSet)

Example 2 with GlucoseDataSet

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

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)

Example 3 with GlucoseDataSet

use of com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GlucoseDataSet 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)

Example 4 with GlucoseDataSet

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

the class Utils method mergeGlucoseDataRecords.

public static GlucoseDataSet[] mergeGlucoseDataRecords(EGVRecord[] egvRecords, SensorRecord[] sensorRecords) {
    int egvLength = egvRecords.length;
    int sensorLength = sensorRecords.length;
    int smallerLength = egvLength < sensorLength ? egvLength : sensorLength;
    GlucoseDataSet[] glucoseDataSets = new GlucoseDataSet[smallerLength];
    for (int i = 1; i <= smallerLength; i++) {
        glucoseDataSets[smallerLength - i] = new GlucoseDataSet(egvRecords[egvLength - i], sensorRecords[sensorLength - i]);
    }
    return glucoseDataSets;
}
Also used : GlucoseDataSet(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GlucoseDataSet)

Aggregations

GlucoseDataSet (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GlucoseDataSet)4 SharedPreferences (android.content.SharedPreferences)2 CalRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord)2 EGVRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.EGVRecord)2 MeterRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.MeterRecord)2 SensorRecord (com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.SensorRecord)2 IOException (java.io.IOException)2 JSONArray (org.json.JSONArray)2