Search in sources :

Example 6 with GlucoseData

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

the class LibreAlarmReceiver method CalculateFromDataTransferObject.

public static void CalculateFromDataTransferObject(ReadingData.TransferObject object, boolean use_raw) {
    // insert any recent data we can
    final List<GlucoseData> mTrend = object.data.trend;
    if (mTrend != null) {
        Collections.sort(mTrend);
        final long thisSensorAge = mTrend.get(mTrend.size() - 1).sensorTime;
        sensorAge = Pref.getInt("nfc_sensor_age", 0);
        if (thisSensorAge > sensorAge) {
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", false);
            Log.d(TAG, "Sensor age advanced to: " + thisSensorAge);
        } else if (thisSensorAge == sensorAge) {
            Log.wtf(TAG, "Sensor age has not advanced: " + sensorAge);
            JoH.static_toast_long("Sensor clock has not advanced!");
            Pref.setBoolean("nfc_age_problem", true);
            // do not try to insert again
            return;
        } else {
            Log.wtf(TAG, "Sensor age has gone backwards!!! " + sensorAge);
            JoH.static_toast_long("Sensor age has gone backwards!!");
            sensorAge = thisSensorAge;
            Pref.setInt("nfc_sensor_age", (int) sensorAge);
            Pref.setBoolean("nfc_age_problem", true);
        }
        if (d)
            Log.d(TAG, "Oldest cmp: " + JoH.dateTimeText(oldest_cmp) + " Newest cmp: " + JoH.dateTimeText(newest_cmp));
        long shiftx = 0;
        if (mTrend.size() > 0) {
            shiftx = getTimeShift(mTrend);
            if (shiftx != 0)
                Log.d(TAG, "Lag Timeshift: " + shiftx);
            for (GlucoseData gd : mTrend) {
                if (d)
                    Log.d(TAG, "DEBUG: sensor time: " + gd.sensorTime);
                if ((timeShiftNearest > 0) && ((timeShiftNearest - gd.realDate) < segmentation_timeslice) && (timeShiftNearest - gd.realDate != 0)) {
                    if (d)
                        Log.d(TAG, "Skipping record due to closeness: " + JoH.dateTimeText(gd.realDate));
                    continue;
                }
                if (use_raw) {
                    // not quick for recent
                    createBGfromGD(gd, false);
                } else {
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, true);
                }
            }
        } else {
            Log.e(TAG, "Trend data was empty!");
        }
        // munge and insert the history data if any is missing
        final List<GlucoseData> mHistory = object.data.history;
        if ((mHistory != null) && (mHistory.size() > 1)) {
            Collections.sort(mHistory);
            // applyTimeShift(mTrend, shiftx);
            final List<Double> polyxList = new ArrayList<Double>();
            final List<Double> polyyList = new ArrayList<Double>();
            for (GlucoseData gd : mHistory) {
                if (d)
                    Log.d(TAG, "history : " + JoH.dateTimeText(gd.realDate) + " " + gd.glucose(false));
                polyxList.add((double) gd.realDate);
                if (use_raw) {
                    polyyList.add((double) gd.glucoseLevelRaw);
                    createBGfromGD(gd, true);
                } else {
                    polyyList.add((double) gd.glucoseLevel);
                    // add in the actual value
                    BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, false);
                }
            }
            // ConstrainedSplineInterpolator splineInterp = new ConstrainedSplineInterpolator();
            final SplineInterpolator splineInterp = new SplineInterpolator();
            try {
                PolynomialSplineFunction polySplineF = splineInterp.interpolate(Forecast.PolyTrendLine.toPrimitiveFromList(polyxList), Forecast.PolyTrendLine.toPrimitiveFromList(polyyList));
                final long startTime = mHistory.get(0).realDate;
                final long endTime = mHistory.get(mHistory.size() - 1).realDate;
                for (long ptime = startTime; ptime <= endTime; ptime += 300000) {
                    if (d)
                        Log.d(TAG, "Spline: " + JoH.dateTimeText((long) ptime) + " value: " + (int) polySplineF.value(ptime));
                    if (use_raw) {
                        createBGfromGD(new GlucoseData((int) polySplineF.value(ptime), ptime), true);
                    } else {
                        BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, false);
                    }
                }
            } catch (org.apache.commons.math3.exception.NonMonotonicSequenceException e) {
                Log.e(TAG, "NonMonotonicSequenceException: " + e);
            }
        } else {
            Log.e(TAG, "no librealarm history data");
        }
    } else {
        Log.d(TAG, "Trend data is null!");
    }
}
Also used : ArrayList(java.util.ArrayList) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData)

Example 7 with GlucoseData

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

the class NFCReaderX method parseData.

public static ReadingData parseData(int attempt, String tagId, byte[] data) {
    long ourTime = System.currentTimeMillis();
    int indexTrend = data[26] & 0xFF;
    // double check this bitmask? should be lower?
    int indexHistory = data[27] & 0xFF;
    final int sensorTime = 256 * (data[317] & 0xFF) + (data[316] & 0xFF);
    long sensorStartTime = ourTime - sensorTime * MINUTE;
    // option to use 13 bit mask
    // final boolean thirteen_bit_mask = Pref.getBooleanDefaultFalse("testing_use_thirteen_bit_mask");
    final boolean thirteen_bit_mask = true;
    ArrayList<GlucoseData> historyList = new ArrayList<>();
    // loads history values (ring buffer, starting at index_trent. byte 124-315)
    for (int index = 0; index < 32; index++) {
        int i = indexHistory - index - 1;
        if (i < 0)
            i += 32;
        GlucoseData glucoseData = new GlucoseData();
        // glucoseData.glucoseLevel =
        // getGlucose(new byte[]{data[(i * 6 + 125)], data[(i * 6 + 124)]});
        glucoseData.glucoseLevelRaw = getGlucoseRaw(new byte[] { data[(i * 6 + 125)], data[(i * 6 + 124)] }, thirteen_bit_mask);
        int time = Math.max(0, Math.abs((sensorTime - 3) / 15) * 15 - index * 15);
        glucoseData.realDate = sensorStartTime + time * MINUTE;
        glucoseData.sensorId = tagId;
        glucoseData.sensorTime = time;
        historyList.add(glucoseData);
    }
    ArrayList<GlucoseData> trendList = new ArrayList<>();
    // loads trend values (ring buffer, starting at index_trent. byte 28-123)
    for (int index = 0; index < 16; index++) {
        int i = indexTrend - index - 1;
        if (i < 0)
            i += 16;
        GlucoseData glucoseData = new GlucoseData();
        // glucoseData.glucoseLevel =
        // getGlucose(new byte[]{data[(i * 6 + 29)], data[(i * 6 + 28)]});
        glucoseData.glucoseLevelRaw = getGlucoseRaw(new byte[] { data[(i * 6 + 29)], data[(i * 6 + 28)] }, thirteen_bit_mask);
        int time = Math.max(0, sensorTime - index);
        glucoseData.realDate = sensorStartTime + time * MINUTE;
        glucoseData.sensorId = tagId;
        glucoseData.sensorTime = time;
        trendList.add(glucoseData);
    }
    final ReadingData readingData = new ReadingData(null, trendList, historyList);
    readingData.raw_data = data;
    return readingData;
}
Also used : ArrayList(java.util.ArrayList) ReadingData(com.eveningoutpost.dexdrip.Models.ReadingData) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData) SuppressLint(android.annotation.SuppressLint)

Example 8 with GlucoseData

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

the class LibreTrendGraph method getLatestBg.

private static ArrayList<Float> getLatestBg(LibreBlock libreBlock) {
    ReadingData readingData = NFCReaderX.getTrend(libreBlock);
    if (readingData == null) {
        Log.e(TAG, "NFCReaderX.getTrend returned null");
        return null;
    }
    ArrayList<Float> ret = new ArrayList<Float>();
    if (readingData.trend.size() == 0 || readingData.trend.get(0).glucoseLevelRaw == 0) {
        Log.e(TAG, "libreBlock exists but no trend data exists, or first value is zero ");
        return null;
    }
    double factor = libreBlock.calculated_bg / readingData.trend.get(0).glucoseLevelRaw;
    if (factor == 0) {
        // We don't have the calculated value, but we do have the raw value. (No calibration exists)
        // I want to show raw data.
        Log.w(TAG, "Bg data was not calculated, working on raw data");
        List<BgReading> latestReading = BgReading.latestForGraph(1, libreBlock.timestamp - 1000, libreBlock.timestamp + 1000);
        if (latestReading == null || latestReading.size() == 0) {
            Log.e(TAG, "libreBlock exists but no matching bg record exists");
            return null;
        }
        factor = latestReading.get(0).raw_data / readingData.trend.get(0).glucoseLevelRaw;
    }
    for (GlucoseData data : readingData.trend) {
        ret.add(new Float(factor * data.glucoseLevelRaw));
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) ReadingData(com.eveningoutpost.dexdrip.Models.ReadingData) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData)

Aggregations

GlucoseData (com.eveningoutpost.dexdrip.Models.GlucoseData)8 ArrayList (java.util.ArrayList)6 ReadingData (com.eveningoutpost.dexdrip.Models.ReadingData)4 SuppressLint (android.annotation.SuppressLint)2 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2 SplineInterpolator (org.apache.commons.math3.analysis.interpolation.SplineInterpolator)2 PolynomialSplineFunction (org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction)2