Skip to content

Commit

Permalink
Merge pull request #3179 from Navid200/Navid_2023_10_19
Browse files Browse the repository at this point in the history
Nightscout follower delay option
  • Loading branch information
jamorham authored Nov 21, 2023
2 parents 3e86af7 + 5439078 commit 4b66c92
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,6 +147,7 @@ private static boolean shouldServiceRun() {
*/
public static List<StatusItem> 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) {
Expand All @@ -159,10 +161,10 @@ public static List<StatusItem> 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;
}
}
Expand All @@ -173,7 +175,7 @@ public static List<StatusItem> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1721,6 +1723,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
try {
collectionCategory.removePreference(nsFollowUrl);
collectionCategory.removePreference(nsFollowDownload);
collectionCategory.removePreference(nsFollowLag);
} catch (Exception e) {
//
}
Expand Down Expand Up @@ -2426,6 +2429,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
if (collectionType == DexCollectionType.NSFollow) {
collectionCategory.addPreference(nsFollowUrl);
collectionCategory.addPreference(nsFollowDownload);
collectionCategory.addPreference(nsFollowLag);
}


Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,23 @@
<item>400</item>
</string-array>

<!-- Nightscout follow wake delay, with respect to the source timestamp
The default is no delay, and is marked with the single symbol > -->
<string-array name="nsfollowlag_entries">
<item>0 ></item>
<item>40 s</item>
<item>1 min</item>
<item>1.5 min</item>
<item>2 min</item>
<item>3 min</item>
</string-array>
<string-array name="nsfollowlag_values">
<item>0</item>
<item>40</item>
<item>60</item>
<item>90</item>
<item>120</item>
<item>180</item>
</string-array>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@
<string name="title_nsfollow_url">Nightscout Follow URL</string>
<string name="summary_nsfollow_download_treatments">Also download treatments from Nightscout as follower</string>
<string name="title_nsfollow_download_treatments">Download Treatments</string>
<string name="title_nsfollow_lag">Nightscout Follow delay</string>
<!-- Maybe we can use them later?
<string name="title_clfollow_user">CareLink Username</string>
<string name="summary_clfollow_user">CareLink login username</string>
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/xml/pref_data_source.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,14 @@
android:defaultValue="false"
android:key="nsfollow_download_treatments"
android:summary="@string/summary_nsfollow_download_treatments"
android:title="@string/title_nsfollow_download_treatments"
android:title="@string/title_nsfollow_download_treatments" />
<ListPreference
android:defaultValue="0"
android:key="nsfollow_lag"
android:summary=""
android:title="@string/title_nsfollow_lag"
android:entries="@array/nsfollowlag_entries"
android:entryValues="@array/nsfollowlag_values"
/>

<EditTextPreference
Expand Down

0 comments on commit 4b66c92

Please sign in to comment.