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

Dex Share Follow Delay #3663

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -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 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) {
Expand Down Expand Up @@ -126,11 +127,12 @@ static void updateTreatmentDownloaded() {
}

static void scheduleWakeUp() {
NightscoutFollowService nightscoutFollowService = new NightscoutFollowService();
final BgReading lastBg = BgReading.lastNoSenssor();
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, nightscoutFollowService.lag) + grace;
wakeup_time = next;
UserError.Log.d(TAG, "Anticipate next: " + JoH.dateTimeText(next) + " last: " + JoH.dateTimeText(last));

Expand Down
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this from inside Anticipate is probably a good idea

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)) {
Expand All @@ -47,4 +46,4 @@ public static long next(long now, final long lastTimeStamp, final long period, f

return nextMin;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class ShareFollowService extends ForegroundService {
private static volatile long lastBgTime;

private static ShareFollowDownload downloader;
private 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() {
Expand Down Expand Up @@ -116,11 +117,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}

static void scheduleWakeUp() {
ShareFollowService shareFollowService = new ShareFollowService();
final BgReading lastBg = BgReading.lastNoSenssor();
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, shareFollowService.lag) + grace;
wakeup_time = next;
UserError.Log.d(TAG, "Anticipate next: " + JoH.dateTimeText(next) + " last: " + JoH.dateTimeText(last));

Expand Down Expand Up @@ -173,9 +175,10 @@ private static String getBestStatusMessage() {
}

/**
* MegaStatus for Nightscout Follower
* MegaStatus for Dex Share Follower
*/
public static List<StatusItem> megaStatus() {
ShareFollowService shareFollowService = new ShareFollowService();
final BgReading lastBg = BgReading.lastNoSenssor();

long hightlightGrace = Constants.SECOND_IN_MS * 30; // 30 seconds
Expand All @@ -185,10 +188,10 @@ public static List<StatusItem> megaStatus() {
StatusItem.Highlight ageOfLastBgPollHighlight = StatusItem.Highlight.NORMAL;
if (bgReceiveDelay > 0) {
ageOfBgLastPoll = JoH.niceTimeScalar(bgReceiveDelay);
if (bgReceiveDelay > SAMPLE_PERIOD / 2) {
if (bgReceiveDelay - shareFollowService.lag > SAMPLE_PERIOD / 2) {
ageOfLastBgPollHighlight = StatusItem.Highlight.BAD;
}
if (bgReceiveDelay > SAMPLE_PERIOD * 2) {
if (bgReceiveDelay - shareFollowService.lag > SAMPLE_PERIOD * 2) {
ageOfLastBgPollHighlight = StatusItem.Highlight.CRITICAL;
}
}
Expand All @@ -199,7 +202,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 + shareFollowService.lag) {
bgAgeHighlight = StatusItem.Highlight.BAD;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
//
}
Expand All @@ -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) {
//
}
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 @@ -546,4 +546,23 @@
<item>180</item>
</string-array>

<!-- Dex Share follow wake delay, with respect to the source timestamp
The default is no delay, and is marked with the single symbol > -->
<string-array name="dexsharefollowlag_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="dexsharefollowlag_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 @@ -1347,6 +1347,7 @@
<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>
<string name="title_dexsharefollow_lag">Dex Share 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
8 changes: 8 additions & 0 deletions app/src/main/res/xml/pref_data_source.xml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@
android:key="dex_share_us_acct"
android:summary="Your account and follower app are from the USA"
android:title="US Servers" />
<ListPreference
android:defaultValue="0"
android:key="dex_share_follow_lag"
android:summary=""
android:title="@string/title_dexsharefollow_lag"
android:entries="@array/dexsharefollowlag_entries"
android:entryValues="@array/dexsharefollowlag_values"
/>

<ListPreference
android:defaultValue="gb"
Expand Down
Loading