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 3a794fed28..f4534e2d21 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 @@ -13,6 +13,7 @@ import com.eveningoutpost.dexdrip.R; import com.eveningoutpost.dexdrip.utilitymodels.Constants; import com.eveningoutpost.dexdrip.utilitymodels.Inevitable; +import com.eveningoutpost.dexdrip.utilitymodels.Pref; import com.eveningoutpost.dexdrip.utilitymodels.StatusItem; import com.eveningoutpost.dexdrip.utilitymodels.StatusItem.Highlight; import com.eveningoutpost.dexdrip.cgm.nsfollow.utils.Anticipate; @@ -146,6 +147,7 @@ private static boolean shouldServiceRun() { */ public static List megaStatus() { final BgReading lastBg = BgReading.lastNoSenssor(); + final long lag = Constants.SECOND_IN_MS * Pref.getStringToInt("nsfollow_lag", 0); // Wake delay selected by user String lastPollText = "n/a"; if (lastPoll > 0) { @@ -159,10 +161,10 @@ public static List megaStatus() { Highlight ageOfLastBgPollHighlight = Highlight.NORMAL; if (bgReceiveDelay > 0) { ageOfBgLastPoll = JoH.niceTimeScalar(bgReceiveDelay); - if (bgReceiveDelay > SAMPLE_PERIOD / 2) { + if (bgReceiveDelay - lag > SAMPLE_PERIOD / 2) { ageOfLastBgPollHighlight = Highlight.BAD; } - if (bgReceiveDelay > SAMPLE_PERIOD * 2) { + if (bgReceiveDelay - lag > SAMPLE_PERIOD * 2) { ageOfLastBgPollHighlight = Highlight.CRITICAL; } } @@ -173,7 +175,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 = Highlight.BAD; } } 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 7a94d455b5..426bf0c629 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,5 +1,8 @@ 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 @@ -16,7 +19,10 @@ public class Anticipate { * If last + period and a bit < now, ask again after grace * If last + period and a bit >= now, ask again after last + period and grace */ - public static long next(long now, final long last, final long period, final long 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. final long since = now - last; if (since <= (grace * 2)) { 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 493ecf412d..3c0c67f7df 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java @@ -1257,6 +1257,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { final Preference nsFollowDownload = findPreference("nsfollow_download_treatments"); final Preference nsFollowUrl = findPreference("nsfollow_url"); + final Preference nsFollowLag = findPreference("nsfollow_lag"); // Show the Nightscout follow wake delay setting only when NS follow is the data source + bindPreferenceSummaryToValue(findPreference("nsfollow_lag")); // Show the selected value as summary try { nsFollowUrl.setOnPreferenceChangeListener((preference, newValue) -> { NightscoutFollow.resetInstance(); @@ -1721,6 +1723,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { try { collectionCategory.removePreference(nsFollowUrl); collectionCategory.removePreference(nsFollowDownload); + collectionCategory.removePreference(nsFollowLag); } catch (Exception e) { // } @@ -2417,6 +2420,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { if (collectionType == DexCollectionType.NSFollow) { collectionCategory.addPreference(nsFollowUrl); collectionCategory.addPreference(nsFollowDownload); + collectionCategory.addPreference(nsFollowLag); } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index eebf70386d..2b2c7a6dc6 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -527,4 +527,23 @@ 400 + + + 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 5235fc2950..b2cc3093c3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1337,6 +1337,7 @@ Nightscout Follow URL Also download treatments from Nightscout as follower Download Treatments + Nightscout Follow delay