Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nightscout follower delay option #3179

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2417,6 +2420,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
Loading