Search in sources :

Example 1 with PenData

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

the class ProcessPenData method process.

public static synchronized void process() {
    // get data sets
    final List<List<PenData>> classifyResults = PrimeDetection.classify();
    final List<PenData> doses = classifyResults.get(1);
    final List<PenData> rewinds = classifyResults.get(2);
    final List<Treatments> treatments = Treatments.latestForGraph(50000, JoH.tsl() - Constants.WEEK_IN_MS, JoH.tsl());
    UserError.Log.d(TAG, "Existing treatments size: " + treatments.size());
    boolean newData = false;
    if (doses.size() > 0 || rewinds.size() > 0) {
        for (final PenData pd : rewinds) {
            if (!Treatments.matchUUID(treatments, pd.uuid)) {
                UserError.Log.d(TAG, "New rewind: " + pd.brief());
                // create rewind treatment entry thing
                // TODO format string
                Treatments.create_note("Pen cartridge change: rewound: " + pd.units + "U on " + pd.penName(), pd.timestamp, 0, pd.uuid);
                newData = true;
            }
        }
        for (final PenData pd : doses) {
            if (!Treatments.matchUUID(treatments, pd.uuid)) {
                UserError.Log.d(TAG, "New Dose: " + pd.brief());
                Treatments.create(0, pd.units, pd.timestamp, pd.uuid);
                newData = true;
            }
        }
        if (newData) {
            Home.staticRefreshBGChartsOnIdle();
        }
    } else {
        UserError.Log.d(TAG, "No results to process");
    }
// TODO show temporal prime a button
// TODO adjust bitfields etc on pen data to indicate processed?
}
Also used : PenData(com.eveningoutpost.dexdrip.Models.PenData) List(java.util.List) Treatments(com.eveningoutpost.dexdrip.Models.Treatments)

Example 2 with PenData

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

the class PrimeDetection method classify.

public static synchronized List<List<PenData>> classify() {
    UserError.Log.d(TAG, "Classify called");
    final long now = JoH.tsl();
    final List<PenData> list = PenData.getAllRecordsBetween(now - Constants.DAY_IN_MS * 3, now);
    final List<PenData> primes = new ArrayList<>(list.size());
    final List<PenData> doses = new ArrayList<>(list.size());
    final List<PenData> rewinds = new ArrayList<>();
    String penType = "n/a";
    String penMac = "n/a";
    boolean detect_primes = false;
    double prime_units = INVALID_PRIME;
    long prime_gap_ms = -1;
    boolean orphan_prime_is_prime = true;
    PenData primeCandidate = null;
    for (final PenData pd : list) {
        if (!penType.equalsIgnoreCase(pd.type)) {
            switch(pd.type) {
                case ID_INPEN:
                    detect_primes = Pref.getBooleanDefaultFalse("inpen_detect_priming");
                    prime_units = Pref.getStringToDouble("inpen_prime_units", INVALID_PRIME);
                    prime_gap_ms = (long) (Pref.getStringToDouble("inpen_prime_minutes", -1) * Constants.MINUTE_IN_MS);
                    orphan_prime_is_prime = Pref.getBoolean("inpen_orphan_primes_ignored", true);
                    break;
                default:
                    UserError.Log.e(TAG, "Unknown pen type: " + pd.type);
                    prime_units = INVALID_PRIME;
                    // do we need to do this?
                    prime_gap_ms = -1;
                    orphan_prime_is_prime = true;
                    break;
            }
            UserError.Log.d(TAG, "Starting processing of pen type: " + pd.type + " with prime units of: " + prime_units);
            penType = pd.type;
        }
        if (!penMac.equalsIgnoreCase(pd.mac)) {
            UserError.Log.d(TAG, "Starting processing of pen mac: " + pd.mac);
            penMac = pd.mac;
            primeCandidate = null;
        }
        if (pd.units < 0) {
            UserError.Log.d(TAG, "Pen rewind: " + pd.brief());
            rewinds.add(pd);
            continue;
        }
        if (primeCandidate != null) {
            final long timeDifference = pd.timestamp - primeCandidate.timestamp;
            if (timeDifference > 0 && timeDifference <= prime_gap_ms) {
                UserError.Log.d(TAG, "primeCandidate is suitable prime: " + JoH.niceTimeScalar(timeDifference));
                primes.add(primeCandidate);
                primeCandidate = null;
                // store current value as cannot also be prime
                doses.add(pd);
                UserError.Log.d(TAG, "Regular dose following prime: " + pd.brief());
                continue;
            } else {
                if (orphan_prime_is_prime) {
                    primes.add(primeCandidate);
                    UserError.Log.d(TAG, "Orphan prime candidate treated as prime: " + primeCandidate.brief());
                } else {
                    doses.add(primeCandidate);
                    UserError.Log.d(TAG, "Orphan prime candidate treated as dose: " + primeCandidate.brief());
                }
            }
            primeCandidate = null;
        }
        if (detect_primes && roundDouble(pd.units, ACCURACY_PLACES) == roundDouble(prime_units, ACCURACY_PLACES)) {
            UserError.Log.d(TAG, "Possible Prime dose: " + pd.brief());
            primeCandidate = pd;
        } else {
            if (D)
                UserError.Log.d(TAG, "Regular dose: " + pd.brief());
            doses.add(pd);
        }
    }
    // final one if last state was to mark a prime candidate
    if (primeCandidate != null) {
        if (JoH.msSince(primeCandidate.timestamp) > prime_gap_ms) {
            if (orphan_prime_is_prime) {
                primes.add(primeCandidate);
                UserError.Log.d(TAG, "Trailing Orphan prime candidate treated as prime: " + primeCandidate.brief());
            } else {
                doses.add(primeCandidate);
                UserError.Log.d(TAG, "Trailing Orphan prime candidate treated as dose: " + primeCandidate.brief());
            }
        } else {
            // treat as temporal
            UserError.Log.d(TAG, "Temporal prime candidate: " + primeCandidate.brief());
        }
    }
    final List<List<PenData>> results = new ArrayList<>();
    results.add(primes);
    results.add(doses);
    results.add(rewinds);
    return results;
}
Also used : ArrayList(java.util.ArrayList) PenData(com.eveningoutpost.dexdrip.Models.PenData) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with PenData

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

the class PrimeDetection method classify.

public static synchronized List<List<PenData>> classify() {
    UserError.Log.d(TAG, "Classify called");
    final long now = JoH.tsl();
    final List<PenData> list = PenData.getAllRecordsBetween(now - Constants.DAY_IN_MS * 3, now);
    final List<PenData> primes = new ArrayList<>(list.size());
    final List<PenData> doses = new ArrayList<>(list.size());
    final List<PenData> rewinds = new ArrayList<>();
    String penType = "n/a";
    String penMac = "n/a";
    boolean detect_primes = false;
    double prime_units = INVALID_PRIME;
    long prime_gap_ms = -1;
    boolean orphan_prime_is_prime = true;
    PenData primeCandidate = null;
    for (final PenData pd : list) {
        if (!penType.equalsIgnoreCase(pd.type)) {
            switch(pd.type) {
                case ID_INPEN:
                    detect_primes = Pref.getBooleanDefaultFalse("inpen_detect_priming");
                    prime_units = Pref.getStringToDouble("inpen_prime_units", INVALID_PRIME);
                    prime_gap_ms = (long) (Pref.getStringToDouble("inpen_prime_minutes", -1) * Constants.MINUTE_IN_MS);
                    orphan_prime_is_prime = Pref.getBoolean("inpen_orphan_primes_ignored", true);
                    break;
                default:
                    UserError.Log.e(TAG, "Unknown pen type: " + pd.type);
                    prime_units = INVALID_PRIME;
                    // do we need to do this?
                    prime_gap_ms = -1;
                    orphan_prime_is_prime = true;
                    break;
            }
            UserError.Log.d(TAG, "Starting processing of pen type: " + pd.type + " with prime units of: " + prime_units);
            penType = pd.type;
        }
        if (!penMac.equalsIgnoreCase(pd.mac)) {
            UserError.Log.d(TAG, "Starting processing of pen mac: " + pd.mac);
            penMac = pd.mac;
            primeCandidate = null;
        }
        if (pd.units < 0) {
            UserError.Log.d(TAG, "Pen rewind: " + pd.brief());
            rewinds.add(pd);
            continue;
        }
        if (primeCandidate != null) {
            final long timeDifference = pd.timestamp - primeCandidate.timestamp;
            if (timeDifference > 0 && timeDifference <= prime_gap_ms) {
                UserError.Log.d(TAG, "primeCandidate is suitable prime: " + JoH.niceTimeScalar(timeDifference));
                primes.add(primeCandidate);
                primeCandidate = null;
                // store current value as cannot also be prime
                doses.add(pd);
                UserError.Log.d(TAG, "Regular dose following prime: " + pd.brief());
                continue;
            } else {
                if (orphan_prime_is_prime) {
                    primes.add(primeCandidate);
                    UserError.Log.d(TAG, "Orphan prime candidate treated as prime: " + primeCandidate.brief());
                } else {
                    doses.add(primeCandidate);
                    UserError.Log.d(TAG, "Orphan prime candidate treated as dose: " + primeCandidate.brief());
                }
            }
            primeCandidate = null;
        }
        if (detect_primes && roundDouble(pd.units, ACCURACY_PLACES) == roundDouble(prime_units, ACCURACY_PLACES)) {
            UserError.Log.d(TAG, "Possible Prime dose: " + pd.brief());
            primeCandidate = pd;
        } else {
            if (D)
                UserError.Log.d(TAG, "Regular dose: " + pd.brief());
            doses.add(pd);
        }
    }
    // final one if last state was to mark a prime candidate
    if (primeCandidate != null) {
        if (JoH.msSince(primeCandidate.timestamp) > prime_gap_ms) {
            if (orphan_prime_is_prime) {
                primes.add(primeCandidate);
                UserError.Log.d(TAG, "Trailing Orphan prime candidate treated as prime: " + primeCandidate.brief());
            } else {
                doses.add(primeCandidate);
                UserError.Log.d(TAG, "Trailing Orphan prime candidate treated as dose: " + primeCandidate.brief());
            }
        } else {
            // treat as temporal
            UserError.Log.d(TAG, "Temporal prime candidate: " + primeCandidate.brief());
        }
    }
    final List<List<PenData>> results = new ArrayList<>();
    results.add(primes);
    results.add(doses);
    results.add(rewinds);
    return results;
}
Also used : ArrayList(java.util.ArrayList) PenData(com.eveningoutpost.dexdrip.Models.PenData) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with PenData

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

the class InPenService method processRecordsQueue.

private synchronized void processRecordsQueue() {
    boolean newRecord = false;
    while (!records.isEmpty()) {
        final byte[] record = records.poll();
        if (record != null) {
            final RecordRx recordRx = new RecordRx(currentPenTime).fromBytes(record);
            if (recordRx != null) {
                UserError.Log.d(TAG, "RECORD RECORD: " + recordRx.toS());
                final PenData penData = PenData.create(I.address, ID_INPEN, recordRx.index, recordRx.units, recordRx.getRealTimeStamp(), recordRx.temperature, record);
                if (penData == null) {
                    UserError.Log.wtf(TAG, "Error creating PenData record from " + HexDump.dumpHexString(record));
                } else {
                    penData.battery = recordRx.battery;
                    penData.flags = recordRx.flags;
                    UserError.Log.d(TAG, "Saving Record index: " + penData.index);
                    penData.save();
                    newRecord = true;
                    gotIndex = (int) penData.index;
                    gotAll = lastIndex == gotIndex;
                    if (InPen.soundsEnabled() && JoH.ratelimit("pen_data_in", 1)) {
                        JoH.playResourceAudio(R.raw.bt_meter_data_in);
                    }
                    lastPenData = penData;
                }
            } else {
                UserError.Log.e(TAG, "Error creating record from: " + HexDump.dumpHexString(record));
            }
        }
    }
    if (newRecord) {
        Inevitable.task("process-inpen-data", 1000, ProcessPenData::process);
    }
}
Also used : RecordRx(com.eveningoutpost.dexdrip.insulin.inpen.messages.RecordRx) ProcessPenData(com.eveningoutpost.dexdrip.insulin.shared.ProcessPenData) PenData(com.eveningoutpost.dexdrip.Models.PenData) ProcessPenData(com.eveningoutpost.dexdrip.insulin.shared.ProcessPenData)

Example 5 with PenData

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

the class InPenService method processRecordsQueue.

private synchronized void processRecordsQueue() {
    boolean newRecord = false;
    while (!records.isEmpty()) {
        final byte[] record = records.poll();
        if (record != null) {
            final RecordRx recordRx = new RecordRx(currentPenTime).fromBytes(record);
            if (recordRx != null) {
                UserError.Log.d(TAG, "RECORD RECORD: " + recordRx.toS());
                final PenData penData = PenData.create(I.address, ID_INPEN, recordRx.index, recordRx.units, recordRx.getRealTimeStamp(), recordRx.temperature, record);
                if (penData == null) {
                    UserError.Log.wtf(TAG, "Error creating PenData record from " + HexDump.dumpHexString(record));
                } else {
                    penData.battery = recordRx.battery;
                    penData.flags = recordRx.flags;
                    UserError.Log.d(TAG, "Saving Record index: " + penData.index);
                    penData.save();
                    newRecord = true;
                    gotIndex = (int) penData.index;
                    gotAll = lastIndex == gotIndex;
                    if (InPen.soundsEnabled() && JoH.ratelimit("pen_data_in", 1)) {
                        JoH.playResourceAudio(R.raw.bt_meter_data_in);
                    }
                    lastPenData = penData;
                }
            } else {
                UserError.Log.e(TAG, "Error creating record from: " + HexDump.dumpHexString(record));
            }
        }
    }
    if (newRecord) {
        Inevitable.task("process-inpen-data", 1000, ProcessPenData::process);
    }
}
Also used : RecordRx(com.eveningoutpost.dexdrip.insulin.inpen.messages.RecordRx) ProcessPenData(com.eveningoutpost.dexdrip.insulin.shared.ProcessPenData) PenData(com.eveningoutpost.dexdrip.Models.PenData) ProcessPenData(com.eveningoutpost.dexdrip.insulin.shared.ProcessPenData)

Aggregations

PenData (com.eveningoutpost.dexdrip.Models.PenData)6 List (java.util.List)4 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)2 RecordRx (com.eveningoutpost.dexdrip.insulin.inpen.messages.RecordRx)2 ProcessPenData (com.eveningoutpost.dexdrip.insulin.shared.ProcessPenData)2 ArrayList (java.util.ArrayList)2