Search in sources :

Example 1 with MeterRecord

use of com.nightscout.core.dexcom.records.MeterRecord 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 MeterRecord

use of com.nightscout.core.dexcom.records.MeterRecord 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 MeterRecord

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

the class MeterRecordTest method shouldNotParseSmallMeterRecord.

@Test(expected = InvalidRecordLengthException.class)
public void shouldNotParseSmallMeterRecord() throws Exception {
    byte[] record = new byte[] { (byte) 0x28, (byte) 0x80, (byte) 0x18, (byte) 0x0B, (byte) 0xC5, (byte) 0x2B, (byte) 0x18, (byte) 0x0B, (byte) 0x71, (byte) 0x00, (byte) 0x0A, (byte) 0x80, (byte) 0x18, (byte) 0x0B };
    MeterRecord meterRecord = new MeterRecord(record);
}
Also used : MeterRecord(com.nightscout.core.dexcom.records.MeterRecord) Test(org.junit.Test)

Example 4 with MeterRecord

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

the class ReadDataTest method testGetMeterPage.

@Test
public void testGetMeterPage() throws IOException, ParseException {
    ReadData readData = new ReadData(usbDevice);
    byte[] response1 = new byte[] { (byte) 0x01, (byte) 0x0e, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x98, (byte) 0xcd };
    byte[] response2 = new byte[] { (byte) 0x01, (byte) 0x16, (byte) 0x02, (byte) 0x01, (byte) 0x1f, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x4a, (byte) 0x1d, (byte) 0x28, (byte) 0x80, (byte) 0x18, (byte) 0x0b, (byte) 0xc5, (byte) 0x2b, (byte) 0x18, (byte) 0x0b, (byte) 0x71, (byte) 0x00, (byte) 0x0a, (byte) 0x80, (byte) 0x18, (byte) 0x0b, (byte) 0xac, (byte) 0xc2, (byte) 0x4c, (byte) 0xb5, (byte) 0x1a, (byte) 0x0b, (byte) 0xe9, (byte) 0x60, (byte) 0x1a, (byte) 0x0b, (byte) 0x46, (byte) 0x00, (byte) 0x2e, (byte) 0xb5, (byte) 0x1a, (byte) 0x0b, (byte) 0x73, (byte) 0xac, (byte) 0x63, (byte) 0xb5, (byte) 0x1a, (byte) 0x0b, (byte) 0x00, (byte) 0x61, (byte) 0x1a, (byte) 0x0b, (byte) 0x48, (byte) 0x00, (byte) 0x45, (byte) 0xb5, (byte) 0x1a, (byte) 0x0b, (byte) 0xb1, (byte) 0x0c, (byte) 0x7c, (byte) 0xd0, (byte) 0x1a, (byte) 0x0b, (byte) 0x1a, (byte) 0x7c, (byte) 0x1a, (byte) 0x0b, (byte) 0x06, (byte) 0x01, (byte) 0x5e, (byte) 0xd0, (byte) 0x1a, (byte) 0x0b, (byte) 0x06, (byte) 0x5e, (byte) 0x04, (byte) 0x02, (byte) 0x1c, (byte) 0x0b, (byte) 0xa2, (byte) 0xad, (byte) 0x1b, (byte) 0x0b, (byte) 0x55, (byte) 0x00, (byte) 0xe6, (byte) 0x01, (byte) 0x1c, (byte) 0x0b, (byte) 0x62, (byte) 0x39, (byte) 0x49, (byte) 0xd7, (byte) 0x1d, (byte) 0x0b, (byte) 0xe7, (byte) 0x82, (byte) 0x1d, (byte) 0x0b, (byte) 0x52, (byte) 0x00, (byte) 0x2b, (byte) 0xd7, (byte) 0x1d, (byte) 0x0b, (byte) 0x62, (byte) 0x6c, (byte) 0x31, (byte) 0x52, (byte) 0x1f, (byte) 0x0b, (byte) 0xce, (byte) 0xfd, (byte) 0x1e, (byte) 0x0b, (byte) 0x28, (byte) 0x01, (byte) 0x13, (byte) 0x52, (byte) 0x1f, (byte) 0x0b, (byte) 0xde, (byte) 0x95, (byte) 0x2c, (byte) 0xe5, (byte) 0x20, (byte) 0x0b, (byte) 0xca, (byte) 0x90, (byte) 0x20, (byte) 0x0b, (byte) 0x75, (byte) 0x00, (byte) 0x0e, (byte) 0xe5, (byte) 0x20, (byte) 0x0b, (byte) 0x6c, (byte) 0x3b, (byte) 0x3a, (byte) 0xe5, (byte) 0x20, (byte) 0x0b, (byte) 0xd7, (byte) 0x90, (byte) 0x20, (byte) 0x0b, (byte) 0x79, (byte) 0x00, (byte) 0x1c, (byte) 0xe5, (byte) 0x20, (byte) 0x0b, (byte) 0x7d, (byte) 0x17, (byte) 0xe7, (byte) 0x11, (byte) 0x22, (byte) 0x0b, (byte) 0x85, (byte) 0xbd, (byte) 0x21, (byte) 0x0b, (byte) 0x53, (byte) 0x00, (byte) 0xc9, (byte) 0x11, (byte) 0x22, (byte) 0x0b, (byte) 0xa9, (byte) 0x6f, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x2b, (byte) 0x88 };
    when(usbDevice.write((byte[]) anyObject(), anyInt())).thenReturn(7).thenReturn(12);
    when(usbDevice.read(anyInt(), anyInt())).thenReturn(response1).thenReturn(response2);
    List<MeterRecord> records = readData.getRecentMeterRecords();
    assertThat(records, is(MockFactory.mockMeterPage()));
}
Also used : MeterRecord(com.nightscout.core.dexcom.records.MeterRecord) ReadData(com.nightscout.core.drivers.ReadData) Test(org.junit.Test)

Example 5 with MeterRecord

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

the class MockFactory method mockMeterPage.

public static List<MeterRecord> mockMeterPage() throws ParseException {
    DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
    format.setTimeZone(TimeZone.getDefault());
    List<MeterRecord> record = new ArrayList<>();
    record.add(new MeterRecord(113, 186155018, 186133445, 186155048));
    record.add(new MeterRecord(70, 186299694, 186278121, 186299724));
    record.add(new MeterRecord(72, 186299717, 186278144, 186299747));
    record.add(new MeterRecord(262, 186306654, 186285082, 186306684));
    record.add(new MeterRecord(85, 186384870, 186363298, 186384900));
    record.add(new MeterRecord(82, 186505003, 186483431, 186505033));
    record.add(new MeterRecord(296, 186602003, 186580430, 186602033));
    record.add(new MeterRecord(117, 186705166, 186683594, 186705196));
    record.add(new MeterRecord(121, 186705180, 186683607, 186705210));
    record.add(new MeterRecord(83, 186782153, 186760581, 186782183));
    return record;
}
Also used : MeterRecord(com.nightscout.core.dexcom.records.MeterRecord) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

MeterRecord (com.nightscout.core.dexcom.records.MeterRecord)7 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 CalRecord (com.nightscout.core.dexcom.records.CalRecord)2 EGVRecord (com.nightscout.core.dexcom.records.EGVRecord)2 SensorRecord (com.nightscout.core.dexcom.records.SensorRecord)2 InvalidRecordLengthException (com.nightscout.core.dexcom.InvalidRecordLengthException)1 PageHeader (com.nightscout.core.dexcom.records.PageHeader)1 ReadData (com.nightscout.core.drivers.ReadData)1 CalibrationEntry (com.nightscout.core.model.CalibrationEntry)1 DownloadResults (com.nightscout.core.model.DownloadResults)1 DownloadStatus (com.nightscout.core.model.DownloadStatus)1 G4Download (com.nightscout.core.model.G4Download)1 MeterEntry (com.nightscout.core.model.MeterEntry)1 SensorEntry (com.nightscout.core.model.SensorEntry)1 SensorGlucoseValueEntry (com.nightscout.core.model.SensorGlucoseValueEntry)1 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1