Search in sources :

Example 1 with EGVRecord

use of com.nightscout.core.dexcom.records.EGVRecord in project android-uploader by nightscout.

the class DexcomG4 method doDownload.

@Override
protected DownloadResults doDownload() {
    DownloadStatus status = DownloadStatus.SUCCESS;
    try {
        transport.open();
    } catch (IOException e) {
        //TODO record this in the event log later
        status = DownloadStatus.IO_ERROR;
    }
    ReadData readData = new ReadData(transport);
    List<EGVRecord> recentRecords = new ArrayList<>();
    List<MeterRecord> meterRecords = new ArrayList<>();
    List<SensorRecord> sensorRecords = new ArrayList<>();
    List<CalRecord> calRecords = new ArrayList<>();
    long displayTime = 0;
    long timeSinceLastRecord = 0;
    int batLevel = 100;
    long systemTime = 0;
    if (status == DownloadStatus.SUCCESS) {
        try {
            recentRecords = readData.getRecentEGVsPages(numOfPages);
            meterRecords = readData.getRecentMeterRecords();
            if (preferences.isSensorUploadEnabled()) {
                sensorRecords = readData.getRecentSensorRecords(numOfPages);
            }
            if (preferences.isCalibrationUploadEnabled()) {
                calRecords = readData.getRecentCalRecords();
            }
            if (recentRecords.size() == 0) {
                status = DownloadStatus.NO_DATA;
            }
            displayTime = readData.readDisplayTime().getTime();
            if (status == DownloadStatus.SUCCESS && recentRecords.size() > 0) {
                timeSinceLastRecord = readData.getTimeSinceEGVRecord(recentRecords.get(recentRecords.size() - 1));
            }
            systemTime = readData.readSystemTime();
            // FIXME: readData.readBatteryLevel() seems to flake out on battery level reads.
            // Removing for now.
            batLevel = 100;
        // TODO pull in other exceptions once we have the analytics/acra reporters
        } catch (IOException e) {
            //TODO record this in the event log later
            status = DownloadStatus.IO_ERROR;
        } catch (InvalidRecordLengthException e) {
            status = DownloadStatus.APPLICATION_ERROR;
        } finally {
            try {
                transport.close();
            } catch (IOException e) {
                //TODO record this in the event log later
                status = DownloadStatus.IO_ERROR;
            }
        }
    }
    List<SensorGlucoseValueEntry> cookieMonsterG4SGVs = EGVRecord.toProtobufList(recentRecords);
    List<CalibrationEntry> cookieMonsterG4Cals = CalRecord.toProtobufList(calRecords);
    List<MeterEntry> cookieMonsterG4Meters = MeterRecord.toProtobufList(meterRecords);
    List<SensorEntry> cookieMonsterG4Sensors = SensorRecord.toProtobufList(sensorRecords);
    G4Download.Builder downloadBuilder = new G4Download.Builder();
    downloadBuilder.sgv(cookieMonsterG4SGVs).cal(cookieMonsterG4Cals).sensor(cookieMonsterG4Sensors).meter(cookieMonsterG4Meters).receiver_system_time_sec(systemTime).download_timestamp(new Date().toString()).download_status(status).uploader_battery(uploaderDevice.getBatteryLevel()).receiver_battery(batLevel).units(GlucoseUnit.MGDL);
    // 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 = standardMinutes(5).minus(standardSeconds(timeSinceLastRecord)).getMillis();
    // convert into json for d3 plot
    JSONArray array = new JSONArray();
    for (EGVRecord recentRecord : recentRecords) {
        try {
            array.put(recentRecord.toJSON());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return new DownloadResults(downloadBuilder.build(), nextUploadTime, array, displayTime);
}
Also used : SensorGlucoseValueEntry(com.nightscout.core.model.SensorGlucoseValueEntry) InvalidRecordLengthException(com.nightscout.core.dexcom.InvalidRecordLengthException) ArrayList(java.util.ArrayList) EGVRecord(com.nightscout.core.dexcom.records.EGVRecord) G4Download(com.nightscout.core.model.G4Download) MeterRecord(com.nightscout.core.dexcom.records.MeterRecord) DownloadStatus(com.nightscout.core.model.DownloadStatus) CalRecord(com.nightscout.core.dexcom.records.CalRecord) DownloadResults(com.nightscout.core.model.DownloadResults) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) Date(java.util.Date) SensorEntry(com.nightscout.core.model.SensorEntry) MeterEntry(com.nightscout.core.model.MeterEntry) SensorRecord(com.nightscout.core.dexcom.records.SensorRecord) CalibrationEntry(com.nightscout.core.model.CalibrationEntry)

Example 2 with EGVRecord

use of com.nightscout.core.dexcom.records.EGVRecord in project android-uploader by nightscout.

the class ReadData method parsePage.

private <T extends GenericTimestampRecord> List<T> parsePage(byte[] data, Class<T> clazz) {
    PageHeader pageHeader = new PageHeader(data);
    List<T> records = new ArrayList<>();
    try {
        for (int i = 0; i < pageHeader.getNumOfRecords(); i++) {
            int startIdx;
            switch(pageHeader.getRecordType()) {
                case EGV_DATA:
                    startIdx = PageHeader.HEADER_SIZE + (EGVRecord.RECORD_SIZE + 1) * i;
                    records.add(clazz.cast(new EGVRecord(Arrays.copyOfRange(data, startIdx, startIdx + EGVRecord.RECORD_SIZE))));
                    break;
                case CAL_SET:
                    int recordLength = (pageHeader.getRevision() <= 2) ? CalRecord.RECORD_SIZE : CalRecord.RECORD_V2_SIZE;
                    startIdx = PageHeader.HEADER_SIZE + (recordLength + 1) * i;
                    records.add(clazz.cast(new CalRecord(Arrays.copyOfRange(data, startIdx, startIdx + recordLength))));
                    break;
                case METER_DATA:
                    startIdx = PageHeader.HEADER_SIZE + (MeterRecord.RECORD_SIZE + 1) * i;
                    records.add(clazz.cast(new MeterRecord(Arrays.copyOfRange(data, startIdx, startIdx + MeterRecord.RECORD_SIZE))));
                    break;
                case SENSOR_DATA:
                    startIdx = PageHeader.HEADER_SIZE + (SensorRecord.RECORD_SIZE + 1) * i;
                    records.add(clazz.cast(new SensorRecord(Arrays.copyOfRange(data, startIdx, startIdx + SensorRecord.RECORD_SIZE))));
                    break;
                default:
                    throw new IllegalArgumentException(String.format("Unknown record type: %s", pageHeader.getRecordType().name()));
            }
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    return records;
}
Also used : MeterRecord(com.nightscout.core.dexcom.records.MeterRecord) PageHeader(com.nightscout.core.dexcom.records.PageHeader) ArrayList(java.util.ArrayList) CalRecord(com.nightscout.core.dexcom.records.CalRecord) SensorRecord(com.nightscout.core.dexcom.records.SensorRecord) EGVRecord(com.nightscout.core.dexcom.records.EGVRecord)

Example 3 with EGVRecord

use of com.nightscout.core.dexcom.records.EGVRecord in project android-uploader by nightscout.

the class MockFactory method mockGlucoseDataSet.

public static GlucoseDataSet mockGlucoseDataSet() {
    EGVRecord egvRecord = new EGVRecord(1, TrendArrow.DOUBLE_DOWN, new DateTime(0).toDate(), new DateTime(5).toDate(), G4Noise.CLEAN);
    SensorRecord sensorRecord = new SensorRecord(new byte[SensorRecord.RECORD_SIZE]);
    return new GlucoseDataSet(egvRecord, sensorRecord);
}
Also used : SensorRecord(com.nightscout.core.dexcom.records.SensorRecord) EGVRecord(com.nightscout.core.dexcom.records.EGVRecord) DateTime(org.joda.time.DateTime) GlucoseDataSet(com.nightscout.core.dexcom.records.GlucoseDataSet)

Example 4 with EGVRecord

use of com.nightscout.core.dexcom.records.EGVRecord in project android-uploader by nightscout.

the class EgvRecordTest method shouldNotParseLargeEgvRecord.

@Test(expected = InvalidRecordLengthException.class)
public void shouldNotParseLargeEgvRecord() throws Exception {
    byte[] record = new byte[] { (byte) 0xC4, (byte) 0x88, (byte) 0x1A, (byte) 0x0B, (byte) 0x61, (byte) 0x34, (byte) 0x1A, (byte) 0x0B, (byte) 0x05, (byte) 0x00, (byte) 0x58, (byte) 0x3E, (byte) 0x00, (byte) 0x00 };
    EGVRecord egvRecord = new EGVRecord(record);
}
Also used : EGVRecord(com.nightscout.core.dexcom.records.EGVRecord) Test(org.junit.Test)

Example 5 with EGVRecord

use of com.nightscout.core.dexcom.records.EGVRecord in project android-uploader by nightscout.

the class EgvRecordTest method shouldNotParseSmallEgvRecord.

@Test(expected = InvalidRecordLengthException.class)
public void shouldNotParseSmallEgvRecord() throws Exception {
    byte[] record = new byte[] { (byte) 0xC4, (byte) 0x88, (byte) 0x1A, (byte) 0x0B, (byte) 0x61, (byte) 0x34, (byte) 0x1A, (byte) 0x0B, (byte) 0x05, (byte) 0x00, (byte) 0x58 };
    EGVRecord egvRecord = new EGVRecord(record);
}
Also used : EGVRecord(com.nightscout.core.dexcom.records.EGVRecord) Test(org.junit.Test)

Aggregations

EGVRecord (com.nightscout.core.dexcom.records.EGVRecord)11 Test (org.junit.Test)5 SensorRecord (com.nightscout.core.dexcom.records.SensorRecord)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 CalRecord (com.nightscout.core.dexcom.records.CalRecord)2 MeterRecord (com.nightscout.core.dexcom.records.MeterRecord)2 DownloadResults (com.nightscout.core.model.DownloadResults)2 G4Download (com.nightscout.core.model.G4Download)2 PowerManager (android.os.PowerManager)1 HitBuilders (com.google.android.gms.analytics.HitBuilders)1 Tracker (com.google.android.gms.analytics.Tracker)1 CdcAcmSerialDriver (com.nightscout.android.drivers.USB.CdcAcmSerialDriver)1 AndroidPreferences (com.nightscout.android.preferences.AndroidPreferences)1 Uploader (com.nightscout.android.upload.Uploader)1 CRCFailError (com.nightscout.core.dexcom.CRCFailError)1 InvalidRecordLengthException (com.nightscout.core.dexcom.InvalidRecordLengthException)1 GlucoseDataSet (com.nightscout.core.dexcom.records.GlucoseDataSet)1 PageHeader (com.nightscout.core.dexcom.records.PageHeader)1 AbstractDevice (com.nightscout.core.drivers.AbstractDevice)1