From b83d1aa24eb9f07ae99dbd688f167205dfbed02a Mon Sep 17 00:00:00 2001 From: Navid200 <51497406+Navid200@users.noreply.github.com> Date: Sun, 26 Jan 2025 11:48:53 -0500 Subject: [PATCH] Backfill overlap avoidance --- .../java/com/eveningoutpost/dexdrip/Home.java | 6 +++-- .../dexdrip/g5model/Ob1G5StateMachine.java | 27 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/Home.java b/app/src/main/java/com/eveningoutpost/dexdrip/Home.java index 85193b0887..27017fba2b 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/Home.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/Home.java @@ -2624,9 +2624,11 @@ private void updateCurrentBgInfoCommon(DexCollectionType collector, TextView not boolean isSensorActive = Sensor.isActive(); // automagically start an xDrip sensor session if G5 transmitter already has active sensor - if (!isSensorActive && Ob1G5CollectionService.isG5SensorStarted() && !Sensor.stoppedRecently()) { + if (!isSensorActive && Ob1G5CollectionService.isG5SensorStarted() && (!Sensor.stoppedRecently() || shortTxId())) { JoH.static_toast_long(getString(R.string.auto_starting_sensor)); - Sensor.create(tsl() - HOUR_IN_MS * 3); + Sensor.create(tsl() - Ob1G5StateMachine.getLatestReadingsTime()); // Create an internal session after last existing readings + UserError.Log.ueh(TAG, "Started internal session (native session in progress H)"); + Treatments.sensorStartIfNeeded(); isSensorActive = Sensor.isActive(); } diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java index d32ceacd80..d5510fcb64 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java @@ -134,7 +134,8 @@ public class Ob1G5StateMachine { private static volatile AuthRequestTxMessage lastAuthPacket; private static volatile boolean backup_loaded = false; private static final int OLDEST_RAW = 300 * 24 * 60 * 60; // 300 days - private static long relAutoSessionStartTime = HOUR_IN_MS * 3; + private static long noOverlapBackfillRelTime = HOUR_IN_MS * 3; + private static boolean IGNORE_SENSOR = true; public static long maxBackfillPeriod_MS = 0; @@ -1555,17 +1556,23 @@ private static void checkAndActivateSensor() { // automagically start an xDrip sensor session if transmitter already has active sensor if (!Sensor.isActive() && Ob1G5CollectionService.isG5SensorStarted() && (!Sensor.stoppedRecently() || shortTxId())) { JoH.static_toast_long(xdrip.gs(R.string.auto_starting_sensor)); - if (shortTxId()) relAutoSessionStartTime = HOUR_IN_MS * 24; // If we are using a G7 - final List last = BgReading.latest(1); // Last reading - if ((last != null) && (last.size() > 0)) { // Have we had a reading? - final long now = JoH.tsl(); - final long since = now - last.get(0).timestamp; // Time since last reading - if (since < relAutoSessionStartTime) { // If the last reading was less than 3 hours ago, or if we are using G7 and the last reading was less than 24 hours ago - relAutoSessionStartTime = since; // We will start the new session starting from the last reading. - } + Sensor.create(tsl() - getLatestReadingsTime()); // Create an internal session after last existing readings + UserError.Log.ueh(TAG, "Started internal session (native session in progress OB)"); + Treatments.sensorStartIfNeeded(); + } + } + + public static long getLatestReadingsTime() { // This is the backfill period, or how long ago we have had our latest readings if more recent than the backfill period. + if (shortTxId()) noOverlapBackfillRelTime = HOUR_IN_MS * 24; // If we are using a G7 + final List last = BgReading.latest(1, IGNORE_SENSOR); // Last reading + if ((last != null) && (last.size() > 0)) { // If we have had a reading + final long now = JoH.tsl(); + final long since = now - last.get(0).timestamp; // Time since last reading + if (since < noOverlapBackfillRelTime) { // If the last reading was less than 3 hours ago, or if we are using G7 and the last reading was less than 24 hours ago + noOverlapBackfillRelTime = since; // We will start the new session starting from the last reading. } - Sensor.create(tsl() - relAutoSessionStartTime); } + return noOverlapBackfillRelTime; } private static void processGlucoseRxMessage(Ob1G5CollectionService parent, final BaseGlucoseRxMessage glucose) {