Search in sources :

Example 1 with ReadingData

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

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)

Example 2 with ReadingData

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

the class NFCReaderX method HandleGoodReading.

// returns true if checksum passed.
public static boolean HandleGoodReading(String tagId, byte[] data1) {
    final boolean checksum_ok = JoH.LibreCrc(data1);
    if (!checksum_ok) {
        return false;
    }
    if (Pref.getBooleanDefaultFalse("external_blukon_algorithm")) {
        long now = JoH.tsl();
        // Save raw block record (we start from block 0)
        LibreBlock.createAndSave(tagId, now, data1, 0);
        LibreOOPAlgorithm.SendData(data1, now);
    } else {
        final ReadingData mResult = parseData(0, tagId, data1);
        new Thread() {

            @Override
            public void run() {
                final PowerManager.WakeLock wl = JoH.getWakeLock("processTransferObject", 60000);
                try {
                    LibreAlarmReceiver.processReadingDataTransferObject(new ReadingData.TransferObject(1, mResult));
                    Home.staticRefreshBGCharts();
                } finally {
                    JoH.releaseWakeLock(wl);
                }
            }
        }.start();
    }
    // Checksum tests have passed.
    return true;
}
Also used : ReadingData(com.eveningoutpost.dexdrip.Models.ReadingData)

Example 3 with ReadingData

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

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 4 with ReadingData

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

the class NFCReaderX method HandleGoodReading.

// returns true if checksum passed.
public static boolean HandleGoodReading(String tagId, byte[] data1) {
    final boolean checksum_ok = JoH.LibreCrc(data1);
    if (!checksum_ok) {
        return false;
    }
    if (Pref.getBooleanDefaultFalse("external_blukon_algorithm")) {
        long now = JoH.tsl();
        // Save raw block record (we start from block 0)
        LibreBlock.createAndSave(tagId, now, data1, 0);
        LibreOOPAlgorithm.SendData(data1, now);
    } else {
        final ReadingData mResult = parseData(0, tagId, data1);
        new Thread() {

            @Override
            public void run() {
                final PowerManager.WakeLock wl = JoH.getWakeLock("processTransferObject", 60000);
                try {
                    LibreAlarmReceiver.processReadingDataTransferObject(new ReadingData.TransferObject(1, mResult));
                    Home.staticRefreshBGCharts();
                } finally {
                    JoH.releaseWakeLock(wl);
                }
            }
        }.start();
    }
    // Checksum tests have passed.
    return true;
}
Also used : ReadingData(com.eveningoutpost.dexdrip.Models.ReadingData)

Example 5 with ReadingData

use of com.eveningoutpost.dexdrip.Models.ReadingData 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)

Aggregations

ReadingData (com.eveningoutpost.dexdrip.Models.ReadingData)6 GlucoseData (com.eveningoutpost.dexdrip.Models.GlucoseData)4 ArrayList (java.util.ArrayList)4 SuppressLint (android.annotation.SuppressLint)2 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2