Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Mar 27, 2022
2 parents 90d1e1a + 715927a commit 524456a
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 176 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/eveningoutpost/dexdrip/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,7 @@ private void updateCurrentBgInfoCommon(DexCollectionType collector, TextView not
showUncalibratedSlope();
return;
}
if (DexCollectionType.isLibreOOPAlgorithm(collector)) {
if (DexCollectionType.isLibreOOPNonCalibratebleAlgorithm(collector)) {
// Rest of this function deals with initial calibration. Since we currently don't have a way to calibrate,
// And even once we will have, there is probably no need to force a calibration at start of sensor use.
displayCurrentInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Collections;
import java.util.List;

import static com.eveningoutpost.dexdrip.Models.JoH.tsl;
import static com.eveningoutpost.dexdrip.UtilityModels.Constants.LIBRE_MULTIPLIER;
import static com.eveningoutpost.dexdrip.xdrip.gs;

Expand Down Expand Up @@ -146,21 +147,22 @@ public void run() {

Log.d(TAG, "Receiving LIBRE_ALARM broadcast");

oldest_cmp = oldest;
newest_cmp = newest;
Log.d(TAG, "At Start: Oldest : " + JoH.dateTimeText(oldest_cmp) + " Newest : " + JoH.dateTimeText(newest_cmp));

final String data = bundle.getString("data");
final int bridge_battery = bundle.getInt("bridge_battery");
if (bridge_battery > 0) {
Pref.setInt("bridge_battery", bridge_battery);
CheckBridgeBattery.checkBridgeBattery();
}
try {

final ReadingData.TransferObject object =
new Gson().fromJson(data, ReadingData.TransferObject.class);
processReadingDataTransferObject(object.data, JoH.tsl(), "LibreAlarm", false, null, null);
Log.d(TAG, "At End: Oldest : " + JoH.dateTimeText(oldest_cmp) + " Newest : " + JoH.dateTimeText(newest_cmp));
if (object.data.raw_data == null) {
Log.e(TAG, "Please update LibreAlarm to use OOP algorithm");
JoH.static_toast_long(gs(R.string.please_update_librealarm_to_use_oop_algorithm));
break;
}
NFCReaderX.HandleGoodReading("LibreAlarm", object.data.raw_data, JoH.tsl(), false, null, null);
} catch (Exception e) {
Log.wtf(TAG, "Could not process data structure from LibreAlarm: " + e.toString());
JoH.static_toast_long(gs(R.string.librealarm_data_format_appears_incompatible_protocol_changed_or_no_data));
Expand All @@ -180,31 +182,29 @@ public void run() {
}.start();
}

public static void processReadingDataTransferObject(ReadingData readingData, long CaptureDateTime, String tagid, boolean allowUpload, byte[] patchUid, byte[] patchInfo) {
public static void processReadingDataTransferObject(ReadingData readingData, long CaptureDateTime, String tagid, boolean allowUpload, byte[] patchUid, byte[] patchInfo, boolean bg_val_exists) {
Log.d(TAG, "Data that was recieved from librealarm is " + HexDump.dumpHexString(readingData.raw_data));
// Save raw block record (we start from block 0)
LibreBlock libreBlock = LibreBlock.createAndSave(tagid, CaptureDateTime, readingData.raw_data, 0, allowUpload, patchUid, patchInfo);

if (Pref.getBooleanDefaultFalse("external_blukon_algorithm")) {
if (readingData.raw_data == null) {
Log.e(TAG, "Please update LibreAlarm to use OOP algorithm");
JoH.static_toast_long(gs(R.string.please_update_librealarm_to_use_oop_algorithm));
return;
}
LibreOOPAlgorithm.sendData(readingData.raw_data, CaptureDateTime, tagid);
return;
}

LibreTrendUtil libreTrendUtil = LibreTrendUtil.getInstance();
// Get the data for the last 24 hours, as this affects the cache.
List<LibreTrendPoint> libreTrendPoints = libreTrendUtil.getData(JoH.tsl() - Constants.DAY_IN_MS, JoH.tsl(), true);

readingData.ClearErrors(libreTrendPoints);
// This is not a perfect solution, but it should work well in almost all casses except restart.
// (after restart we will only have data of one reading).
readingData.copyBgVals(libreTrendPoints);

boolean use_smoothed_data = Pref.getBooleanDefaultFalse("libre_use_smoothed_data");
if (use_smoothed_data) {
readingData.calculateSmoothDataImproved(libreTrendPoints);
readingData.calculateSmoothDataImproved(libreTrendPoints, bg_val_exists);
}
CalculateFromDataTransferObject(readingData, use_smoothed_data, true);
if (Pref.getBooleanDefaultFalse("external_blukon_algorithm") != false) {
Log.wtf(TAG, "Error external_blukon_algorithm should be false here");
}
boolean use_raw = Pref.getBoolean("calibrate_external_libre_2_algorithm", true);
CalculateFromDataTransferObject(readingData, use_smoothed_data, use_raw);
}

public static void CalculateFromDataTransferObject(ReadingData readingData, boolean use_smoothed_data, boolean use_raw) {
Expand Down Expand Up @@ -240,24 +240,22 @@ public static void CalculateFromDataTransferObject(ReadingData readingData, bool
}
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);
//applyTimeShift(mTrend, shiftx);
// This function changes timeShiftNearest which is the latest value we have, so we are far enough from it.
getTimeShift(mTrend);

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));
Log.d(TAG, "Skipping record due to closeness to the most recent value: " + JoH.dateTimeText(gd.realDate));
continue;
}
if (use_raw) {
createBGfromGD(gd, use_smoothed_data, false); // not quick for recent
} else {
BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, true);
BgReading.bgReadingInsertFromInt(use_smoothed_data ? gd.glucoseLevelSmoothed : gd.glucoseLevel, gd.realDate, segmentation_timeslice, true);
}
}
} else {
Expand All @@ -282,7 +280,7 @@ public static void CalculateFromDataTransferObject(ReadingData readingData, bool
} else {
polyyList.add((double) gd.glucoseLevel);
// add in the actual value
BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, false);
BgReading.bgReadingInsertFromInt(gd.glucoseLevel, gd.realDate, segmentation_timeslice, false);
}
}

Expand All @@ -306,7 +304,7 @@ public static void CalculateFromDataTransferObject(ReadingData readingData, bool
// Here we do not use smoothed data, since data is already smoothed for the history
createBGfromGD(new GlucoseData((int) polySplineF.value(ptime), ptime), false, true);
} else {
BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, false);
BgReading.bgReadingInsertFromInt((int) polySplineF.value(ptime), ptime, segmentation_timeslice, false);
}
}
} catch (org.apache.commons.math3.exception.NonMonotonicSequenceException e) {
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/com/eveningoutpost/dexdrip/Models/BgReading.java
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,12 @@ public static List<BgReading> latestForGraphAsc(int number, long startTime, long
.execute();
}

public static BgReading readingNearTimeStamp(double startTime) {
final double margin = (4 * 60 * 1000);
public static BgReading readingNearTimeStamp(long startTime) {
long margin = (4 * 60 * 1000);
return readingNearTimeStamp(startTime, margin);
}

public static BgReading readingNearTimeStamp(long startTime, final long margin) {
final DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(1);
return new Select()
Expand Down Expand Up @@ -1289,7 +1293,7 @@ public static BgReading bgReadingInsertFromJson(String json, boolean do_notifica
}

// TODO this method shares some code with above.. merge
public static void bgReadingInsertFromInt(int value, long timestamp, boolean do_notification) {
public static void bgReadingInsertFromInt(int value, long timestamp, long margin, boolean do_notification) {
// TODO sanity check data!

if ((value <= 0) || (timestamp <= 0)) {
Expand Down Expand Up @@ -1319,11 +1323,11 @@ public static void bgReadingInsertFromInt(int value, long timestamp, boolean do_
}

try {
if (readingNearTimeStamp(bgr.timestamp) == null) {
if (readingNearTimeStamp(bgr.timestamp, margin) == null) {
bgr.save();
bgr.find_slope();
if (do_notification) {
// xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), Notifications.class)); // alerts et al
// xdrip.getAppContext().startService(new Intent(xdrip.getAppContext(), Notifications.class)); // alerts et al
Notifications.start(); // this may not be needed as it is duplicated in handleNewBgReading
}
BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), false, !do_notification); // pebble and widget
Expand All @@ -1334,7 +1338,7 @@ public static void bgReadingInsertFromInt(int value, long timestamp, boolean do_
Log.e(TAG, "Could not save BGR: ", e);
}
} else {
Log.e(TAG,"Got null bgr from create");
Log.e(TAG, "Got null bgr from create");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public enum DataSource {NOT_SET, FRAM, BLE}

;

public long realDate; // The time of this reading in ms
public int sensorTime; // The counter in minutes from start of sensor.
public int glucoseLevel = -1;
public long realDate; // The time of this reading in ms
public int sensorTime; // The counter in minutes from start of sensor.
public int glucoseLevel = -1; // The bg value that was calculated by the oop algorithm.
public int glucoseLevelSmoothed = -1; // The smoothed bg value that was calculated by the oop algorithm.
public int glucoseLevelRaw = -1;
public int glucoseLevelRawSmoothed;
public int flags;
Expand All @@ -33,7 +34,7 @@ public GlucoseData(int glucoseLevelRaw, long timestamp) {
public String toString() {
return "{ sensorTime = " + sensorTime + " glucoseLevel = " + glucoseLevel + " glucoseLevelRaw = " + glucoseLevelRaw +
" glucoseLevelRawSmoothed = " + glucoseLevelRawSmoothed + " flags = " + flags +
" source = " + source + "}";
" source = " + source + " glucoseLevel " + glucoseLevel + " glucoseLevelSmoothed " + glucoseLevelSmoothed + "}";
}

public String glucose(boolean mmol) {
Expand Down
Loading

0 comments on commit 524456a

Please sign in to comment.