Skip to content

Commit

Permalink
Rapid Reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Navid200 committed Dec 25, 2024
1 parent d829e21 commit a37b943
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> alwaysScanModels = Sets.newHashSet("SM-N910V", "G Watch");
private static final List<String> alwaysScanModelFamilies = Arrays.asList("SM-N910");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(" ", ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a37b943

Please sign in to comment.