From a37b94305ccc25d11f1a577a8d69a55d9b94dbfa Mon Sep 17 00:00:00 2001 From: Navid200 <51497406+Navid200@users.noreply.github.com> Date: Tue, 24 Dec 2024 23:30:52 -0500 Subject: [PATCH] Rapid Reconnect --- .../dexdrip/g5model/DexSyncKeeper.java | 15 ++++++++++++--- .../dexdrip/g5model/Ob1G5StateMachine.java | 9 +++++++++ .../dexdrip/services/Ob1G5CollectionService.java | 13 +++++++++++++ .../dexdrip/utilitymodels/BgGraphBuilder.java | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/DexSyncKeeper.java b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/DexSyncKeeper.java index 31e0c28b75..ebf77d647e 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/DexSyncKeeper.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/DexSyncKeeper.java @@ -4,10 +4,12 @@ import com.eveningoutpost.dexdrip.models.JoH; import com.eveningoutpost.dexdrip.models.UserError; +import com.eveningoutpost.dexdrip.services.Ob1G5CollectionService; import com.eveningoutpost.dexdrip.utilitymodels.Constants; import com.eveningoutpost.dexdrip.utilitymodels.PersistentStore; import static com.eveningoutpost.dexdrip.utilitymodels.BgGraphBuilder.DEXCOM_PERIOD; +import static com.eveningoutpost.dexdrip.utilitymodels.BgGraphBuilder.DEX_RAPID_RECONNECT_PERIOD; public class DexSyncKeeper { @@ -67,14 +69,21 @@ public static long anticipate(final String transmitterId) { // -1 means we don't know anything static long anticipate(final String transmitterId, final long now) { final long last = PersistentStore.getLong(DEX_SYNC_STORE + transmitterId); - if (last < OLDEST_POSSIBLE) { - return -1; - } if (last > now) { UserError.Log.e(TAG, "Anticipation time in the future! cannot use: " + JoH.dateTimeText(last)); return -1; // can't be in the future } + if (Ob1G5CollectionService.rapid_reconnect) { // Once a minute + final long modulo = (now - last) % DEX_RAPID_RECONNECT_PERIOD; + if ((modulo < GRACE_TIME) && ((now - last) > GRACE_TIME)) return now; + final long next = now + (DEX_RAPID_RECONNECT_PERIOD - modulo); + return next; + } + if (last < OLDEST_POSSIBLE) { + return -1; + } + if (now - last > VALIDITY_PERIOD) { UserError.Log.e(TAG, "Anticipation time too old to use: " + JoH.dateTimeText(last)); return -1; 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..4a4110d03f 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/g5model/Ob1G5StateMachine.java @@ -888,6 +888,15 @@ public static boolean doGetData(Ob1G5CollectionService parent, RxBleConnection c } else { parent.msg("Invalid Glucose"); } + UserError.Log.d(TAG, "Rapid Reconnect is active: " + Ob1G5CollectionService.rapid_reconnect); + if (Ob1G5CollectionService.rapid_reconnect) { // Manage wake frequency after pairing for G7 + Ob1G5CollectionService.rapid_reconnect_count++; + UserError.Log.e(TAG, "# of Rapid Reconnect handshakes: " + Ob1G5CollectionService.rapid_reconnect_count); + if (Ob1G5CollectionService.rapid_reconnect_count > 2) { + UserError.Log.e(TAG, "Back to waking once every 5 minutes "); + Ob1G5CollectionService.rapid_reconnect = false; + } + } break; diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/services/Ob1G5CollectionService.java b/app/src/main/java/com/eveningoutpost/dexdrip/services/Ob1G5CollectionService.java index 30e9bca565..6050412337 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/services/Ob1G5CollectionService.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/services/Ob1G5CollectionService.java @@ -248,6 +248,8 @@ public class Ob1G5CollectionService extends G5BaseService { private static boolean do_discovery = true; private static final boolean do_auth = true; //private static boolean initiate_bonding = false; + public static boolean rapid_reconnect = false; + public static int rapid_reconnect_count = 0; private static final Set alwaysScanModels = Sets.newHashSet("SM-N910V", "G Watch"); private static final List alwaysScanModelFamilies = Arrays.asList("SM-N910"); @@ -948,6 +950,10 @@ private void handleWakeup() { always_scan = true; UserError.Log.e(TAG, "Switching to scan always mode due to connect failures metric: " + connectFailures); changeState(SCAN); + } else if (rapid_reconnect) { + always_scan = true; + UserError.Log.e(TAG, "Scan always mode to help with Rapid Reconnect "); + changeState(SCAN); } else if (use_auto_connect && (connectNowFailures > 1) && (connectFailures < 0)) { UserError.Log.d(TAG, "Avoiding power connect due to failure metric: " + connectNowFailures + " " + connectFailures); changeState(CONNECT); @@ -1734,6 +1740,13 @@ public void onReceive(Context context, Intent intent) { UserError.Log.e(TAG, "onReceive UPDATE Name " + parcel_device.getName() + " Value " + parcel_device.getAddress() + " Bond state " + parcel_device.getBondState() + bondState(parcel_device.getBondState()) + " " + "bs: " + bondState(bond_state_extra) + " was " + bondState(previous_bond_state_extra)); + if (DexCollectionType.getBestCollectorHardwareName().equals("G7") && parcel_device.getBondState() == BluetoothDevice.BOND_BONDED) { // G7 just paired + rapid_reconnect = true; // There is only 20% chance we are on the correct time grid. Let's wake once a minute to find the right grid. + rapid_reconnect_count = 0; + always_scan = true; + UserError.Log.e(TAG, "Scan always mode and wake every minute to gracefully exit Rapid Reconnect "); + changeState(SCAN); + } try { if (parcel_device.getAddress().equals(transmitterMAC)) { msg(bondState(bond_state_extra).replace(" ", "")); diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/BgGraphBuilder.java b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/BgGraphBuilder.java index 221ba90404..a54bc60f0a 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/BgGraphBuilder.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/BgGraphBuilder.java @@ -89,6 +89,7 @@ public class BgGraphBuilder { public static final int FUZZER = (int) (30 * Constants.SECOND_IN_MS); public final static long DEXCOM_PERIOD = 300_000; // 5 minutes + public final static long DEX_RAPID_RECONNECT_PERIOD = 60_000; // 1 minute public final static double NOISE_TRIGGER = 10; public final static double NOISE_TRIGGER_ULTRASENSITIVE = 1; public final static double NOISE_TOO_HIGH_FOR_PREDICT = 60;