diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/nsfollow/NightscoutFollowService.java b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/nsfollow/NightscoutFollowService.java index 720182618b..67a1e44c47 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/nsfollow/NightscoutFollowService.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/nsfollow/NightscoutFollowService.java @@ -58,6 +58,7 @@ public class NightscoutFollowService extends ForegroundService { private static volatile Treatments lastTreatment; private static volatile long lastTreatmentTime = 0; private static volatile long treatmentReceivedDelay = 0; + private static long lag = Constants.SECOND_IN_MS * Pref.getStringToInt("nsfollow_lag", 0); // User can choose a wake delay with a 0 default. private void buggySamsungCheck() { if (buggySamsung == null) { @@ -130,7 +131,7 @@ static void scheduleWakeUp() { final long last = lastBg != null ? lastBg.timestamp : 0; final long grace = Constants.SECOND_IN_MS * 10; - final long next = Anticipate.next(JoH.tsl(), last, SAMPLE_PERIOD, grace) + grace; + final long next = Anticipate.next(JoH.tsl(), last, SAMPLE_PERIOD, grace, lag) + grace; wakeup_time = next; UserError.Log.d(TAG, "Anticipate next: " + JoH.dateTimeText(next) + " last: " + JoH.dateTimeText(last)); diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/nsfollow/utils/Anticipate.java b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/nsfollow/utils/Anticipate.java index 426bf0c629..b3f1a82b16 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/nsfollow/utils/Anticipate.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/nsfollow/utils/Anticipate.java @@ -1,15 +1,12 @@ package com.eveningoutpost.dexdrip.cgm.nsfollow.utils; -import com.eveningoutpost.dexdrip.utilitymodels.Constants; -import com.eveningoutpost.dexdrip.utilitymodels.Pref; - /** * Choose optimum anticipation times for re-attempting data collection to minimize - * number of requests to nightscout but at the same time reduce latency from new - * value is available till it is shown in xdrip. + * number of requests from the source but at the same time reduce latency for new + * value shown in xdrip. * - * We're trying to give the user the lowest latency on the data we can, but avoiding constantly - * polling for data to conserve battery life and mobile data costs. + * We're trying to provide the lowest latency we can, but avoid constantly + * polling the source to conserve battery life and mobile data costs. * * @author Original author jamorham */ @@ -20,9 +17,11 @@ public class Anticipate { * If last + period and a bit >= now, ask again after last + period and grace */ - public static long next(long now, final long lastTimeStamp, final long period, final long grace) { - final long lag = Constants.SECOND_IN_MS * Pref.getStringToInt("nsfollow_lag", 0); // User can choose a wake delay with a 0 default. - final long last = lastTimeStamp + lag; // We delay the source timestamp and use it as the time we received the reading to account for any source delay. + public static long next(long now, final long lastTimeStamp, final long period, final long grace) { // Calling the anticipate method without a lag parameter will use a default lag of 0 + return next(now, lastTimeStamp, period, grace, 0); + } + public static long next(long now, final long lastTimeStamp, final long period, final long grace, final long lag) { + final long last = lastTimeStamp + lag; // The calling method can include a non-zero lag parameter to delay the anticipation time to account for source delay. final long since = now - last; if (since <= (grace * 2)) { @@ -47,4 +46,4 @@ public static long next(long now, final long lastTimeStamp, final long period, f return nextMin; } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/sharefollow/ShareFollowService.java b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/sharefollow/ShareFollowService.java index 53bc57a08b..1a2d6a09bf 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/cgm/sharefollow/ShareFollowService.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/cgm/sharefollow/ShareFollowService.java @@ -57,6 +57,7 @@ public class ShareFollowService extends ForegroundService { private static volatile long lastBgTime; private static ShareFollowDownload downloader; + private static long lag = Constants.SECOND_IN_MS * Pref.getStringToInt("dex_share_follow_lag", 0); // User can choose a wake delay with a 0 default. @Override public void onCreate() { @@ -120,7 +121,7 @@ static void scheduleWakeUp() { final long last = lastBg != null ? lastBg.timestamp : 0; final long grace = Constants.SECOND_IN_MS * 10; - final long next = Anticipate.next(JoH.tsl(), last, SAMPLE_PERIOD, grace) + grace; + final long next = Anticipate.next(JoH.tsl(), last, SAMPLE_PERIOD, grace, lag) + grace; wakeup_time = next; UserError.Log.d(TAG, "Anticipate next: " + JoH.dateTimeText(next) + " last: " + JoH.dateTimeText(last)); @@ -173,7 +174,7 @@ private static String getBestStatusMessage() { } /** - * MegaStatus for Nightscout Follower + * MegaStatus for Dex Share Follower */ public static List megaStatus() { final BgReading lastBg = BgReading.lastNoSenssor(); @@ -185,10 +186,10 @@ public static List megaStatus() { StatusItem.Highlight ageOfLastBgPollHighlight = StatusItem.Highlight.NORMAL; if (bgReceiveDelay > 0) { ageOfBgLastPoll = JoH.niceTimeScalar(bgReceiveDelay); - if (bgReceiveDelay > SAMPLE_PERIOD / 2) { + if (bgReceiveDelay - lag > SAMPLE_PERIOD / 2) { ageOfLastBgPollHighlight = StatusItem.Highlight.BAD; } - if (bgReceiveDelay > SAMPLE_PERIOD * 2) { + if (bgReceiveDelay - lag > SAMPLE_PERIOD * 2) { ageOfLastBgPollHighlight = StatusItem.Highlight.CRITICAL; } } @@ -199,7 +200,7 @@ public static List megaStatus() { if (lastBg != null) { long age = JoH.msSince(lastBg.timestamp); ageLastBg = JoH.niceTimeScalar(age); - if (age > SAMPLE_PERIOD + hightlightGrace) { + if (age > SAMPLE_PERIOD + hightlightGrace + lag) { bgAgeHighlight = StatusItem.Highlight.BAD; } } diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java index d86da4ce00..42fa3b6059 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java @@ -1335,6 +1335,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { final Preference shFollowUser = findPreference("shfollow_user"); final Preference shFollowPass = findPreference("shfollow_pass"); final Preference shFollowServerUS = findPreference("dex_share_us_acct"); + final Preference dexShareFollowLag = findPreference("dex_share_follow_lag"); // Show the Dex share follow wake delay setting only when Dex share follow is the data source + bindPreferenceSummaryToValue(findPreference("dex_share_follow_lag")); // Show the selected value as summary if (collectionType == DexCollectionType.SHFollow) { final Preference.OnPreferenceChangeListener shFollowListener = new Preference.OnPreferenceChangeListener() { @@ -1350,6 +1352,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { shFollowUser.setOnPreferenceChangeListener(shFollowListener); shFollowPass.setOnPreferenceChangeListener(shFollowListener); shFollowServerUS.setOnPreferenceChangeListener(shFollowListener); + dexShareFollowLag.setOnPreferenceChangeListener(shFollowListener); } catch (Exception e) { // } @@ -1359,6 +1362,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { collectionCategory.removePreference(shFollowUser); collectionCategory.removePreference(shFollowPass); collectionCategory.removePreference(shFollowServerUS); + collectionCategory.removePreference(dexShareFollowLag); } catch (Exception e) { // } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 339e50f0be..980dfadd81 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -546,4 +546,23 @@ 180 + + + 0 > + 40 s + 1 min + 1.5 min + 2 min + 3 min + + + 0 + 40 + 60 + 90 + 120 + 180 + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9d16251821..784bc47cd2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1347,6 +1347,7 @@ Also download treatments from Nightscout as follower Download Treatments Nightscout Follow delay + Dex Share Follow delay