Skip to content

Commit

Permalink
Dex Share Follow Delay
Browse files Browse the repository at this point in the history
  • Loading branch information
Navid200 committed Sep 14, 2024
1 parent 2b9f3a8 commit 2b73c2e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 17 deletions.
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 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) {
Expand Down Expand Up @@ -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));

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.
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 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() {
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -173,7 +174,7 @@ private static String getBestStatusMessage() {
}

/**
* MegaStatus for Nightscout Follower
* MegaStatus for Dex Share Follower
*/
public static List<StatusItem> megaStatus() {
final BgReading lastBg = BgReading.lastNoSenssor();
Expand All @@ -185,10 +186,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 - lag > SAMPLE_PERIOD / 2) {
ageOfLastBgPollHighlight = StatusItem.Highlight.BAD;
}
if (bgReceiveDelay > SAMPLE_PERIOD * 2) {
if (bgReceiveDelay - lag > SAMPLE_PERIOD * 2) {
ageOfLastBgPollHighlight = StatusItem.Highlight.CRITICAL;
}
}
Expand All @@ -199,7 +200,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 = 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

0 comments on commit 2b73c2e

Please sign in to comment.