Search in sources :

Example 1 with DownloadResults

use of com.nightscout.core.model.DownloadResults in project android-uploader by nightscout.

the class AbstractDevice method download.

public final DownloadResults download() {
    DownloadResults download = doDownload();
    onDownload();
    return download;
}
Also used : DownloadResults(com.nightscout.core.model.DownloadResults)

Example 2 with DownloadResults

use of com.nightscout.core.model.DownloadResults 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 3 with DownloadResults

use of com.nightscout.core.model.DownloadResults in project android-uploader by nightscout.

the class SyncingService method handleActionSync.

/**
     * Handle action Sync in the provided background thread with the provided
     * parameters.
     */
protected void handleActionSync(int numOfPages, Context context, DeviceTransport serialDriver) {
    boolean broadcastSent = false;
    AndroidPreferences preferences = new AndroidPreferences(context);
    Tracker tracker = ((Nightscout) context).getTracker();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NSDownload");
    wl.acquire();
    if (serialDriver != null) {
        AbstractUploaderDevice uploaderDevice = AndroidUploaderDevice.getUploaderDevice(context);
        AbstractDevice device = new DexcomG4(serialDriver, preferences, uploaderDevice);
        ((DexcomG4) device).setNumOfPages(numOfPages);
        ((CdcAcmSerialDriver) serialDriver).setPowerManagementEnabled(preferences.isRootEnabled());
        try {
            DownloadResults results = device.download();
            G4Download download = results.getDownload();
            Uploader uploader = new Uploader(context, preferences);
            boolean uploadStatus;
            if (numOfPages < 20) {
                uploadStatus = uploader.upload(results, 1);
            } else {
                uploadStatus = uploader.upload(results);
            }
            EGVRecord recentEGV;
            if (download.download_status == DownloadStatus.SUCCESS) {
                recentEGV = new EGVRecord(download.sgv.get(download.sgv.size() - 1));
            } else {
                recentEGV = new EGVRecord(-1, TrendArrow.NONE, new Date(), new Date(), G4Noise.NOISE_NONE);
            }
            broadcastSGVToUI(recentEGV, uploadStatus, results.getNextUploadTime() + TIME_SYNC_OFFSET, results.getDisplayTime(), results.getResultArray(), download.receiver_battery);
            broadcastSent = true;
        } catch (ArrayIndexOutOfBoundsException e) {
            Log.wtf("Unable to read from the dexcom, maybe it will work next time", e);
            tracker.send(new HitBuilders.ExceptionBuilder().setDescription("Array Index out of bounds").setFatal(false).build());
        } catch (NegativeArraySizeException e) {
            Log.wtf("Negative array exception from receiver", e);
            tracker.send(new HitBuilders.ExceptionBuilder().setDescription("Negative Array size").setFatal(false).build());
        } catch (IndexOutOfBoundsException e) {
            Log.wtf("IndexOutOfBounds exception from receiver", e);
            tracker.send(new HitBuilders.ExceptionBuilder().setDescription("IndexOutOfBoundsException").setFatal(false).build());
        } catch (CRCFailError 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);
            tracker.send(new HitBuilders.ExceptionBuilder().setDescription("CRC Failed").setFatal(false).build());
        } catch (Exception e) {
            Log.wtf("Unhandled exception caught", e);
            tracker.send(new HitBuilders.ExceptionBuilder().setDescription("Catch all exception in handleActionSync").setFatal(false).build());
        }
    }
    if (!broadcastSent)
        broadcastSGVToUI();
    wl.release();
}
Also used : AndroidPreferences(com.nightscout.android.preferences.AndroidPreferences) Tracker(com.google.android.gms.analytics.Tracker) HitBuilders(com.google.android.gms.analytics.HitBuilders) DownloadResults(com.nightscout.core.model.DownloadResults) DexcomG4(com.nightscout.core.drivers.DexcomG4) CdcAcmSerialDriver(com.nightscout.android.drivers.USB.CdcAcmSerialDriver) AbstractUploaderDevice(com.nightscout.core.drivers.AbstractUploaderDevice) CRCFailError(com.nightscout.core.dexcom.CRCFailError) EGVRecord(com.nightscout.core.dexcom.records.EGVRecord) Date(java.util.Date) G4Download(com.nightscout.core.model.G4Download) PowerManager(android.os.PowerManager) AbstractDevice(com.nightscout.core.drivers.AbstractDevice) Uploader(com.nightscout.android.upload.Uploader)

Aggregations

DownloadResults (com.nightscout.core.model.DownloadResults)3 EGVRecord (com.nightscout.core.dexcom.records.EGVRecord)2 G4Download (com.nightscout.core.model.G4Download)2 Date (java.util.Date)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 CalRecord (com.nightscout.core.dexcom.records.CalRecord)1 MeterRecord (com.nightscout.core.dexcom.records.MeterRecord)1 SensorRecord (com.nightscout.core.dexcom.records.SensorRecord)1 AbstractDevice (com.nightscout.core.drivers.AbstractDevice)1 AbstractUploaderDevice (com.nightscout.core.drivers.AbstractUploaderDevice)1 DexcomG4 (com.nightscout.core.drivers.DexcomG4)1 CalibrationEntry (com.nightscout.core.model.CalibrationEntry)1 DownloadStatus (com.nightscout.core.model.DownloadStatus)1