Search in sources :

Example 1 with CalRecord

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

the class DexShareCollectionService method attemptRead.

public void attemptRead() {
    PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock wakeLock1 = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ReadingShareData");
    wakeLock1.acquire(60000);
    requestHighPriority();
    Log.d(TAG, "Attempting to read data");
    final Action1<Long> systemTimeListener = new Action1<Long>() {

        @Override
        public void call(Long s) {
            if (s != null) {
                Log.d(TAG, "Made the full round trip, got " + s + " as the system time");
                final long additiveSystemTimeOffset = new Date().getTime() - s;
                final Action1<Long> dislpayTimeListener = new Action1<Long>() {

                    @Override
                    public void call(Long s) {
                        if (s != null) {
                            Log.d(TAG, "Made the full round trip, got " + s + " as the display time offset");
                            final long addativeDisplayTimeOffset = additiveSystemTimeOffset - (s * 1000);
                            Log.d(TAG, "Making " + addativeDisplayTimeOffset + " the the total time offset");
                            final Action1<EGVRecord[]> evgRecordListener = new Action1<EGVRecord[]>() {

                                @Override
                                public void call(EGVRecord[] egvRecords) {
                                    if (egvRecords != null) {
                                        Log.d(TAG, "Made the full round trip, got " + egvRecords.length + " EVG Records");
                                        BgReading.create(egvRecords, additiveSystemTimeOffset, getApplicationContext());
                                        statusErrors = 0;
                                        {
                                            Log.d(TAG, "Releasing wl in egv");
                                            requestLowPriority();
                                            if (wakeLock1 != null && wakeLock1.isHeld())
                                                wakeLock1.release();
                                            Log.d(TAG, "released");
                                        }
                                        if (shouldDisconnect) {
                                            stopSelf();
                                        } else {
                                            setRetryTimer();
                                        }
                                    }
                                }
                            };
                            final Action1<SensorRecord[]> sensorRecordListener = new Action1<SensorRecord[]>() {

                                @Override
                                public void call(SensorRecord[] sensorRecords) {
                                    if (sensorRecords != null) {
                                        Log.d(TAG, "Made the full round trip, got " + sensorRecords.length + " Sensor Records");
                                        BgReading.create(sensorRecords, additiveSystemTimeOffset, getApplicationContext());
                                        statusErrors = 0;
                                        readData.getRecentEGVs(evgRecordListener);
                                    }
                                }
                            };
                            final Action1<CalRecord[]> calRecordListener = new Action1<CalRecord[]>() {

                                @Override
                                public void call(CalRecord[] calRecords) {
                                    if (calRecords != null) {
                                        Log.d(TAG, "Made the full round trip, got " + calRecords.length + " Cal Records");
                                        Calibration.create(calRecords, addativeDisplayTimeOffset, getApplicationContext());
                                        statusErrors = 0;
                                        readData.getRecentSensorRecords(sensorRecordListener);
                                    }
                                }
                            };
                            readData.getRecentCalRecords(calRecordListener);
                        } else if (wakeLock1 != null && wakeLock1.isHeld())
                            wakeLock1.release();
                    }
                };
                readData.readDisplayTimeOffset(dislpayTimeListener);
            } else if (wakeLock1 != null && wakeLock1.isHeld())
                wakeLock1.release();
        }
    };
    readData.readSystemTime(systemTimeListener);
}
Also used : Action1(rx.functions.Action1) EGVRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.EGVRecord) Date(java.util.Date) PowerManager(android.os.PowerManager) SensorRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.SensorRecord) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord)

Example 2 with CalRecord

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

the class ReadDataShare method ParsePage.

private <T> T ParsePage(byte[] data, int recordType, Action1<T> parsedPageReceiver) {
    int HEADER_LEN = 28;
    PageHeader pageHeader = new PageHeader(data);
    int NUM_REC_OFFSET = 4;
    int numRec = data[NUM_REC_OFFSET];
    int rec_len;
    switch(Constants.RECORD_TYPES.values()[recordType]) {
        case MANUFACTURING_DATA:
            GenericXMLRecord xmlRecord = new GenericXMLRecord(Arrays.copyOfRange(data, HEADER_LEN, data.length - 1));
            if (parsedPageReceiver != null) {
                Observable.just((T) xmlRecord).subscribe(parsedPageReceiver);
            } else {
                return (T) xmlRecord;
            }
            break;
        case SENSOR_DATA:
            rec_len = 20;
            SensorRecord[] sensorRecords = new SensorRecord[numRec];
            for (int i = 0; i < numRec; i++) {
                int startIdx = HEADER_LEN + rec_len * i;
                sensorRecords[i] = new SensorRecord(Arrays.copyOfRange(data, startIdx, startIdx + rec_len - 1));
            }
            if (parsedPageReceiver != null) {
                Observable.just((T) sensorRecords).subscribe(parsedPageReceiver);
            } else {
                return (T) sensorRecords;
            }
            break;
        case EGV_DATA:
            rec_len = 13;
            EGVRecord[] egvRecords = new EGVRecord[numRec];
            for (int i = 0; i < numRec; i++) {
                int startIdx = HEADER_LEN + rec_len * i;
                egvRecords[i] = new EGVRecord(Arrays.copyOfRange(data, startIdx, startIdx + rec_len - 1));
            }
            if (parsedPageReceiver != null) {
                Observable.just((T) egvRecords).subscribe(parsedPageReceiver);
            } else {
                return (T) egvRecords;
            }
            break;
        case METER_DATA:
            rec_len = 16;
            MeterRecord[] meterRecords = new MeterRecord[numRec];
            for (int i = 0; i < numRec; i++) {
                int startIdx = HEADER_LEN + rec_len * i;
                meterRecords[i] = new MeterRecord(Arrays.copyOfRange(data, startIdx, startIdx + rec_len - 1));
            }
            if (parsedPageReceiver != null) {
                Observable.just((T) meterRecords).subscribe(parsedPageReceiver);
            } else {
                return (T) meterRecords;
            }
            break;
        case CAL_SET:
            rec_len = 249;
            if (pageHeader.getRevision() <= 2) {
                rec_len = 148;
            }
            CalRecord[] calRecords = new CalRecord[numRec];
            for (int i = 0; i < numRec; i++) {
                int startIdx = HEADER_LEN + rec_len * i;
                calRecords[i] = new CalRecord(Arrays.copyOfRange(data, startIdx, startIdx + rec_len - 1));
            }
            if (parsedPageReceiver != null) {
                Observable.just((T) calRecords).subscribe(parsedPageReceiver);
            } else {
                return (T) calRecords;
            }
            break;
        default:
            break;
    }
    Observable.just((T) null).subscribe(parsedPageReceiver);
    return (T) null;
}
Also used : MeterRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.MeterRecord) PageHeader(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.PageHeader) SensorRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.SensorRecord) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord) GenericXMLRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GenericXMLRecord) EGVRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.EGVRecord)

Example 3 with CalRecord

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

the class ReadData method ParsePage.

private <T> T ParsePage(byte[] data, int recordType) {
    int HEADER_LEN = 28;
    PageHeader pageHeader = new PageHeader(data);
    int NUM_REC_OFFSET = 4;
    int numRec = data[NUM_REC_OFFSET];
    int rec_len;
    switch(Dex_Constants.RECORD_TYPES.values()[recordType]) {
        case MANUFACTURING_DATA:
            GenericXMLRecord xmlRecord = new GenericXMLRecord(Arrays.copyOfRange(data, HEADER_LEN, data.length - 1));
            return (T) xmlRecord;
        case SENSOR_DATA:
            rec_len = 20;
            SensorRecord[] sensorRecords = new SensorRecord[numRec];
            for (int i = 0; i < numRec; i++) {
                int startIdx = HEADER_LEN + rec_len * i;
                sensorRecords[i] = new SensorRecord(Arrays.copyOfRange(data, startIdx, startIdx + rec_len - 1));
            }
            return (T) sensorRecords;
        case EGV_DATA:
            rec_len = 13;
            EGVRecord[] egvRecords = new EGVRecord[numRec];
            for (int i = 0; i < numRec; i++) {
                int startIdx = HEADER_LEN + rec_len * i;
                egvRecords[i] = new EGVRecord(Arrays.copyOfRange(data, startIdx, startIdx + rec_len - 1));
            }
            return (T) egvRecords;
        case METER_DATA:
            rec_len = 16;
            MeterRecord[] meterRecords = new MeterRecord[numRec];
            for (int i = 0; i < numRec; i++) {
                int startIdx = HEADER_LEN + rec_len * i;
                meterRecords[i] = new MeterRecord(Arrays.copyOfRange(data, startIdx, startIdx + rec_len - 1));
            }
            return (T) meterRecords;
        case CAL_SET:
            rec_len = 249;
            if (pageHeader.getRevision() <= 2) {
                rec_len = 148;
            }
            CalRecord[] calRecords = new CalRecord[numRec];
            for (int i = 0; i < numRec; i++) {
                int startIdx = HEADER_LEN + rec_len * i;
                calRecords[i] = new CalRecord(Arrays.copyOfRange(data, startIdx, startIdx + rec_len - 1));
            }
            return (T) calRecords;
        default:
            // Throw error "Database record not supported"
            break;
    }
    return (T) null;
}
Also used : MeterRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.MeterRecord) PageHeader(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.PageHeader) SensorRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.SensorRecord) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord) GenericXMLRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.GenericXMLRecord) EGVRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.EGVRecord)

Example 4 with CalRecord

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

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 5 with CalRecord

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

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