Search in sources :

Example 6 with LibreBlock

use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip by NightscoutFoundation.

the class LibreTrendGraph method setupCharts.

public void setupCharts() {
    final TextView trendView = (TextView) findViewById(R.id.textLibreHeader);
    chart = (LineChartView) findViewById(R.id.libre_chart);
    List<Line> lines = new ArrayList<Line>();
    List<PointValue> lineValues = new ArrayList<PointValue>();
    final float conversion_factor_mmol = (float) (doMgdl ? 1 : Constants.MGDL_TO_MMOLL);
    LibreBlock libreBlock = LibreBlock.getLatestForTrend();
    if (libreBlock == null) {
        trendView.setText("No libre data to display");
        setupEmptyCharts();
        return;
    }
    String time = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date((long) libreBlock.timestamp));
    ArrayList<Float> bg_data = getLatestBgForXMinutes(MINUTES_TO_DISPLAY);
    if (bg_data == null) {
        trendView.setText("Error displaying data for " + time);
        setupEmptyCharts();
        return;
    }
    trendView.setText("Scan from " + time);
    float min = 1000;
    float max = 0;
    int i = 0;
    for (float bg : bg_data) {
        if (bg <= 0) {
            i++;
            continue;
        }
        if (min > bg) {
            min = bg;
        }
        if (max < bg) {
            max = bg;
        }
        lineValues.add(new PointValue(-i, bg * conversion_factor_mmol));
        i++;
    }
    Line trendLine = new Line(lineValues);
    trendLine.setColor(ChartUtils.COLOR_RED);
    trendLine.setHasLines(false);
    trendLine.setHasPoints(true);
    trendLine.setPointRadius(3);
    lines.add(trendLine);
    final int MIN_GRAPH = 20;
    if (max - min < MIN_GRAPH) {
        // On relative flat trend the graph can look very noise althouth with the right resolution it is not that way.
        // I will add two dummy invisible points that will cause the graph to look with bigger Y range.
        float average = (max + min) / 2;
        List<PointValue> dummyPointValues = new ArrayList<PointValue>();
        Line dummyPointLine = new Line(dummyPointValues);
        dummyPointValues.add(new PointValue(0, (average - MIN_GRAPH / 2) * conversion_factor_mmol));
        dummyPointValues.add(new PointValue(0, (average + MIN_GRAPH / 2) * conversion_factor_mmol));
        dummyPointLine.setColor(ChartUtils.COLOR_RED);
        dummyPointLine.setHasLines(false);
        dummyPointLine.setHasPoints(false);
        lines.add(dummyPointLine);
    }
    Axis axisX = new Axis();
    Axis axisY = new Axis().setHasLines(true);
    axisX.setTextSize(16);
    axisY.setTextSize(16);
    axisX.setName("Time from last scan");
    axisY.setName("Glucose " + (doMgdl ? "mg/dl" : "mmol/l"));
    data = new LineChartData(lines);
    data.setAxisXBottom(axisX);
    data.setAxisYLeft(axisY);
    chart.setLineChartData(data);
}
Also used : PointValue(lecho.lib.hellocharts.model.PointValue) ArrayList(java.util.ArrayList) Date(java.util.Date) Line(lecho.lib.hellocharts.model.Line) LibreBlock(com.eveningoutpost.dexdrip.Models.LibreBlock) LineChartData(lecho.lib.hellocharts.model.LineChartData) TextView(android.widget.TextView) Axis(lecho.lib.hellocharts.model.Axis)

Example 7 with LibreBlock

use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip by NightscoutFoundation.

the class LibreTrendUtil method CalculateFactor.

private void CalculateFactor(List<LibreBlock> latestBlocks) {
    // Go for the last libreBlock and get calculated bg and timestamp.
    ListIterator<LibreBlock> li = latestBlocks.listIterator(latestBlocks.size());
    long lastBlockTime = 0;
    boolean isLast = true;
    while (li.hasPrevious()) {
        LibreBlock libreBlock = li.previous();
        // Get of the loop if no valid data in the last 15 minutes.
        if (lastBlockTime == 0) {
            lastBlockTime = libreBlock.timestamp;
        } else {
            if (JoH.msSince(lastBlockTime, libreBlock.timestamp) > 16 * 60 * 1000) {
                // We have readings for the last 16 minutes, but none of them has a BG value.
                // This should not happen, but if it does, data is too old to be used.
                Log.w(TAG, "getData was not able to find a valid time - quiting");
                break;
            }
        }
        if (isLast) {
            List<GlucoseData> trend = NFCReaderX.getLibreTrend(libreBlock);
            if (trend == null || trend.size() == 0) {
                Log.w(TAG, "Error: NFCReaderX.getTrend returned null or empty for latest block");
                continue;
            }
            // The last object is used to calculate the timestamp and id.
            isLast = false;
            m_libreTrendLatest.id = (int) trend.get(0).sensorTime;
            m_libreTrendLatest.timestamp = libreBlock.timestamp;
        }
        // Now trying to get a valid object with BG and a raw value.
        if (libreBlock.calculated_bg == 0) {
            continue;
        }
        List<GlucoseData> trend = NFCReaderX.getLibreTrend(libreBlock);
        if (trend == null || trend.size() == 0) {
            Log.e(TAG, "Error: NFCReaderX.getTrend returned null or empty for latest block");
            return;
        }
        if (trend.get(0).glucoseLevelRaw == 0) {
            continue;
        }
        m_libreTrendLatest.setFactorData(trend.get(0).glucoseLevelRaw, libreBlock.calculated_bg);
        String time = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date((long) m_libreTrendLatest.timestamp));
        Log.i(TAG, "Latest values with valid bg " + time + " m_latestId = " + m_libreTrendLatest.id + " m_libreTrendLatest.m_GlucoseLevelRaw = " + trend.get(0).glucoseLevelRaw + " bg = " + libreBlock.calculated_bg);
        // We have finished the calculations, so getting out.
        break;
    }
}
Also used : LibreBlock(com.eveningoutpost.dexdrip.Models.LibreBlock) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData) Date(java.util.Date)

Example 8 with LibreBlock

use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip-plus by jamorham.

the class NFCReaderX method SendLibrereading.

public static void SendLibrereading(final String tagId, byte[] data1, final long CaptureDateTime, byte[] patchUid, byte[] patchInfo) {
    if (!Home.get_master()) {
        return;
    }
    LibreBlock libreBlock = LibreBlock.getForTimestamp(CaptureDateTime);
    if (libreBlock != null) {
        // We already have this one, so we have already sent it, so let's not crate storms.
        return;
    }
    // Create the object to send
    libreBlock = LibreBlock.create(tagId, CaptureDateTime, data1, 0, patchUid, patchInfo);
    if (libreBlock == null) {
        Log.e(TAG, "Error could not create libreBlock for libre-allhouse");
        return;
    }
    final String json = libreBlock.toExtendedJson();
    GcmActivity.pushLibreBlock(json);
}
Also used : LibreBlock(com.eveningoutpost.dexdrip.Models.LibreBlock)

Example 9 with LibreBlock

use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip-plus by jamorham.

the class LibreTrendUtil method CalculateFactor.

private void CalculateFactor(List<LibreBlock> latestBlocks) {
    // Go for the last libreBlock and get calculated bg and timestamp.
    ListIterator<LibreBlock> li = latestBlocks.listIterator(latestBlocks.size());
    long lastBlockTime = 0;
    boolean isLast = true;
    while (li.hasPrevious()) {
        LibreBlock libreBlock = li.previous();
        // Get of the loop if no valid data in the last 15 minutes.
        if (lastBlockTime == 0) {
            lastBlockTime = libreBlock.timestamp;
        } else {
            if (JoH.msSince(lastBlockTime, libreBlock.timestamp) > 16 * 60 * 1000) {
                // We have readings for the last 16 minutes, but none of them has a BG value.
                // This should not happen, but if it does, data is too old to be used.
                Log.w(TAG, "getData was not able to find a valid time - quiting");
                break;
            }
        }
        if (isLast) {
            List<GlucoseData> trend = NFCReaderX.getLibreTrend(libreBlock);
            if (trend == null || trend.size() == 0) {
                Log.w(TAG, "Error: NFCReaderX.getTrend returned null or empty for latest block");
                continue;
            }
            // The last object is used to calculate the timestamp and id.
            isLast = false;
            m_libreTrendLatest.id = (int) trend.get(0).sensorTime;
            m_libreTrendLatest.timestamp = libreBlock.timestamp;
        }
        // Now trying to get a valid object with BG and a raw value.
        if (libreBlock.calculated_bg == 0) {
            continue;
        }
        List<GlucoseData> trend = NFCReaderX.getLibreTrend(libreBlock);
        if (trend == null || trend.size() == 0) {
            Log.e(TAG, "Error: NFCReaderX.getTrend returned null or empty for latest block");
            return;
        }
        if (trend.get(0).glucoseLevelRaw == 0) {
            continue;
        }
        m_libreTrendLatest.setFactorData(trend.get(0).glucoseLevelRaw, libreBlock.calculated_bg);
        String time = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date((long) m_libreTrendLatest.timestamp));
        Log.i(TAG, "Latest values with valid bg " + time + " m_latestId = " + m_libreTrendLatest.id + " m_libreTrendLatest.m_GlucoseLevelRaw = " + trend.get(0).glucoseLevelRaw + " bg = " + libreBlock.calculated_bg);
        // We have finished the calculations, so getting out.
        break;
    }
}
Also used : LibreBlock(com.eveningoutpost.dexdrip.Models.LibreBlock) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData) Date(java.util.Date)

Example 10 with LibreBlock

use of com.eveningoutpost.dexdrip.Models.LibreBlock in project xDrip-plus by jamorham.

the class UploaderTask method doInBackground.

public Void doInBackground(String... urls) {
    try {
        Log.d(TAG, "UploaderTask doInBackground called");
        final List<Long> circuits = new ArrayList<>();
        final List<String> types = new ArrayList<>();
        types.add(BgReading.class.getSimpleName());
        types.add(Calibration.class.getSimpleName());
        types.add(BloodTest.class.getSimpleName());
        types.add(Treatments.class.getSimpleName());
        types.add(TransmitterData.class.getSimpleName());
        types.add(LibreBlock.class.getSimpleName());
        if (Pref.getBooleanDefaultFalse("wear_sync")) {
            circuits.add(UploaderQueue.WATCH_WEARAPI);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_mongodb_enable")) {
            circuits.add(UploaderQueue.MONGO_DIRECT);
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_api_enable")) {
            if ((Pref.getBoolean("cloud_storage_api_use_mobile", true) || (JoH.isLANConnected()))) {
                circuits.add(UploaderQueue.NIGHTSCOUT_RESTAPI);
            } else {
                Log.e(TAG, "Skipping Nightscout upload due to mobile data only");
            }
        }
        if (Pref.getBooleanDefaultFalse("cloud_storage_influxdb_enable")) {
            circuits.add(UploaderQueue.INFLUXDB_RESTAPI);
        }
        for (long THIS_QUEUE : circuits) {
            final List<BgReading> bgReadings = new ArrayList<>();
            final List<Calibration> calibrations = new ArrayList<>();
            final List<BloodTest> bloodtests = new ArrayList<>();
            final List<Treatments> treatmentsAdd = new ArrayList<>();
            final List<String> treatmentsDel = new ArrayList<>();
            final List<TransmitterData> transmittersData = new ArrayList<>();
            final List<LibreBlock> libreBlock = new ArrayList<>();
            final List<UploaderQueue> items = new ArrayList<>();
            for (String type : types) {
                final List<UploaderQueue> bgups = UploaderQueue.getPendingbyType(type, THIS_QUEUE);
                if (bgups != null) {
                    for (UploaderQueue up : bgups) {
                        switch(up.action) {
                            case "insert":
                            case "update":
                            case "create":
                                items.add(up);
                                if (type.equals(BgReading.class.getSimpleName())) {
                                    final BgReading this_bg = BgReading.byid(up.reference_id);
                                    if (this_bg != null) {
                                        bgReadings.add(this_bg);
                                    } else {
                                        Log.wtf(TAG, "BgReading with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Calibration.class.getSimpleName())) {
                                    final Calibration this_cal = Calibration.byid(up.reference_id);
                                    if (this_cal != null) {
                                        if (this_cal.isValid()) {
                                            calibrations.add(this_cal);
                                        } else {
                                            Log.d(TAG, "Calibration with ID: " + up.reference_id + " is marked invalid");
                                        }
                                    } else {
                                        Log.wtf(TAG, "Calibration with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(BloodTest.class.getSimpleName())) {
                                    final BloodTest this_bt = BloodTest.byid(up.reference_id);
                                    if (this_bt != null) {
                                        bloodtests.add(this_bt);
                                    } else {
                                        Log.wtf(TAG, "Bloodtest with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(Treatments.class.getSimpleName())) {
                                    final Treatments this_treat = Treatments.byid(up.reference_id);
                                    if (this_treat != null) {
                                        treatmentsAdd.add(this_treat);
                                    } else {
                                        Log.wtf(TAG, "Treatments with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(TransmitterData.class.getSimpleName())) {
                                    final TransmitterData this_transmitterData = TransmitterData.byid(up.reference_id);
                                    if (this_transmitterData != null) {
                                        transmittersData.add(this_transmitterData);
                                    } else {
                                        Log.wtf(TAG, "TransmitterData with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                } else if (type.equals(LibreBlock.class.getSimpleName())) {
                                    final LibreBlock this_LibreBlock = LibreBlock.byid(up.reference_id);
                                    if (this_LibreBlock != null) {
                                        libreBlock.add(this_LibreBlock);
                                    } else {
                                        Log.wtf(TAG, "LibreBlock with ID: " + up.reference_id + " appears to have been deleted");
                                    }
                                }
                                break;
                            case "delete":
                                if ((THIS_QUEUE == UploaderQueue.WATCH_WEARAPI || THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) && type.equals(Treatments.class.getSimpleName())) {
                                    items.add(up);
                                    Log.wtf(TAG, "Delete Treatments with ID: " + up.reference_uuid);
                                    treatmentsDel.add(up.reference_uuid);
                                } else if (up.reference_uuid != null) {
                                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " delete not yet implemented: " + up.reference_uuid);
                                    // mark as completed so as not to tie up the queue for now
                                    up.completed(THIS_QUEUE);
                                }
                                break;
                            default:
                                Log.e(TAG, "Unsupported operation type for " + type + " " + up.action);
                                break;
                        }
                    }
                }
            }
            if ((bgReadings.size() > 0) || (calibrations.size() > 0) || (bloodtests.size() > 0) || (treatmentsAdd.size() > 0 || treatmentsDel.size() > 0) || (transmittersData.size() > 0) || (libreBlock.size() > 0) || (UploaderQueue.getPendingbyType(Treatments.class.getSimpleName(), THIS_QUEUE, 1).size() > 0)) {
                Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Processing: " + bgReadings.size() + " BgReadings and " + calibrations.size() + " Calibrations " + bloodtests.size() + " bloodtests " + treatmentsAdd.size() + " treatmentsAdd " + treatmentsDel.size() + " treatmentsDel");
                boolean uploadStatus = false;
                if (THIS_QUEUE == UploaderQueue.MONGO_DIRECT) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadMongo(bgReadings, calibrations, calibrations, transmittersData, libreBlock);
                } else if (THIS_QUEUE == UploaderQueue.NIGHTSCOUT_RESTAPI) {
                    final NightscoutUploader uploader = new NightscoutUploader(xdrip.getAppContext());
                    uploadStatus = uploader.uploadRest(bgReadings, bloodtests, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.INFLUXDB_RESTAPI) {
                    final InfluxDBUploader influxDBUploader = new InfluxDBUploader(xdrip.getAppContext());
                    uploadStatus = influxDBUploader.upload(bgReadings, calibrations, calibrations);
                } else if (THIS_QUEUE == UploaderQueue.WATCH_WEARAPI) {
                    uploadStatus = WatchUpdaterService.sendWearUpload(bgReadings, calibrations, bloodtests, treatmentsAdd, treatmentsDel);
                }
                if (retry_timer) {
                    // standard retry timer
                    SyncService.startSyncService(Constants.MINUTE_IN_MS * 6);
                }
                // TODO some kind of fail counter?
                if (uploadStatus) {
                    for (UploaderQueue up : items) {
                        // approve all types for this queue
                        up.completed(THIS_QUEUE);
                    }
                    Log.d(TAG, UploaderQueue.getCircuitName(THIS_QUEUE) + " Marking: " + items.size() + " Items as successful");
                    if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                        Log.d(TAG, "Scheduling boosted repeat query");
                        SyncService.startSyncService(2000);
                    }
                }
            } else {
                Log.d(TAG, "Nothing to upload for: " + UploaderQueue.getCircuitName(THIS_QUEUE));
                if (PersistentStore.getBoolean(BACKFILLING_BOOSTER)) {
                    PersistentStore.setBoolean(BACKFILLING_BOOSTER, false);
                    Log.d(TAG, "Switched off backfilling booster");
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "caught exception", e);
        exception = e;
        return null;
    }
    return null;
}
Also used : BloodTest(com.eveningoutpost.dexdrip.Models.BloodTest) ArrayList(java.util.ArrayList) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) TransmitterData(com.eveningoutpost.dexdrip.Models.TransmitterData) LibreBlock(com.eveningoutpost.dexdrip.Models.LibreBlock) InfluxDBUploader(com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Treatments(com.eveningoutpost.dexdrip.Models.Treatments)

Aggregations

LibreBlock (com.eveningoutpost.dexdrip.Models.LibreBlock)20 Date (java.util.Date)10 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)4 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)4 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)4 LibreTrendPoint (com.eveningoutpost.dexdrip.utils.LibreTrendPoint)4 ArrayList (java.util.ArrayList)4 TextView (android.widget.TextView)2 InfluxDBUploader (com.eveningoutpost.dexdrip.InfluxDB.InfluxDBUploader)2 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)2 GlucoseData (com.eveningoutpost.dexdrip.Models.GlucoseData)2 TransmitterData (com.eveningoutpost.dexdrip.Models.TransmitterData)2 LibreTrendUtil (com.eveningoutpost.dexdrip.utils.LibreTrendUtil)2 BasicDBObject (com.mongodb.BasicDBObject)2 DB (com.mongodb.DB)2 DBCollection (com.mongodb.DBCollection)2 MongoClient (com.mongodb.MongoClient)2 MongoClientURI (com.mongodb.MongoClientURI)2 WriteResult (com.mongodb.WriteResult)2 IOException (java.io.IOException)2