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

Persistent High alert threshold #3554

Closed
Closed
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 @@ -8,6 +8,14 @@
* out of sequence with two end points that are both above threshold.
*/

/**
* Navid
*
* The same setting, highValue, has been used both for statistics high as well as persistent high alert.
* This imposed a limitation on user.
* We are changing this by adding a new setting, persistent_high_threshold, to be used only for the alert.
*/

import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.models.BgReading;
import com.eveningoutpost.dexdrip.models.JoH;
Expand Down Expand Up @@ -40,19 +48,20 @@ public static boolean checkForPersistentHigh() {
final List<BgReading> last = BgReading.latest(1);
if ((last != null) && (last.size() > 0)) {

final double highMarkMgDl = Home.convertToMgDlIfMmol(
JoH.tolerantParseDouble(Pref.getString("highValue", "170"), 170d));
final double vThreshold = Home.convertToMgDlIfMmol(
JoH.tolerantParseDouble(Pref.getString("persistent_high_threshold", "170"), 170d));

final long now = JoH.tsl();
final long since = now - last.get(0).timestamp;
// only process if last reading <10 mins
if (since < MINUTE_IN_MS * 10) {
// check if exceeding high
if (last.get(0).getDg_mgdl() > highMarkMgDl) {
// check if exceeding persistent high threshold
if (last.get(0).getDg_mgdl() > vThreshold) {

final double this_slope = last.get(0).getDg_slope() * MINUTE_IN_MS;
Log.d(TAG, "CheckForPersistentHigh: Slope: " + JoH.qs(this_slope)+ " "+JoH.dateTimeText(last.get(0).timestamp));

// TODO What if it is dropping at an extremely low slope? What if it is dropping at 1mg/dL per day?!
// if not falling
if (this_slope > 0 && !last.get(0).hide_slope) {
final long high_since = Pref.getLong(PERSISTENT_HIGH_SINCE, 0);
Expand All @@ -78,7 +87,7 @@ public static boolean checkForPersistentHigh() {
return false;
}

if (!dataQualityCheck(high_since, highMarkMgDl)) {
if (!dataQualityCheck(high_since, vThreshold)) {
Log.d(TAG, "Insufficient data quality to raise persistent high alert");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1834,10 +1834,10 @@ public static boolean checkForPersistentHigh() {
final long since = now - last.get(0).timestamp;
// only process if last reading <10 mins
if (since < 600000) {
// check if exceeding high
// check if exceeding persistent high threshold
if (last.get(0).calculated_value >
Home.convertToMgDlIfMmol(
JoH.tolerantParseDouble(Pref.getString("highValue", "170")))) {
JoH.tolerantParseDouble(Pref.getString("persistent_high_threshold", "170")))) {

final double this_slope = last.get(0).calculated_value_slope * 60000;
//Log.d(TAG, "CheckForPersistentHigh: Slope: " + JoH.qs(this_slope));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.eveningoutpost.dexdrip.models.LibreData;
import com.eveningoutpost.dexdrip.models.PenData;
import com.eveningoutpost.dexdrip.models.Prediction;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.models.UserNotification;
import com.eveningoutpost.dexdrip.R;
import com.eveningoutpost.dexdrip.SnoozeActivity;
Expand Down Expand Up @@ -132,6 +133,33 @@ private void migrateToNewStyleRestUris() {
}
}

// When adding a new setting to an existing function, this method lets us control what happens to an existing
// xDrip install after update. The new added setting should offer more flexibility to the user rather than modifying
// the xDrip behavior without the user's knowledge.
public static void startup() {
if (!Pref.isPreferenceSet("warning_agreed_to")) { // This is the very first time xDrip is running after a fresh install
try { // Everything here runs only once after a fresh install

Pref.setBoolean("migrate_persistent_high_alert_threshold", false); // Never migrate; we will use the default

} catch (Exception e) {
UserError.Log.wtf(TAG, "Initial startup settings failed! Please report.");
}
}
try { // Everything here runs after startup, fresh install or not

if (Pref.getBoolean("migrate_persistent_high_alert_threshold", true)) { // Only if not migrated yet
final String vThreshold = Pref.getString("highValue", "170");
UserError.Log.e(TAG, "You can now set a persistent high alert threshold. Until you do, we set it equal to your current High Value setting: " + vThreshold);
Pref.setString("persistent_high_threshold", vThreshold); // Set the persistent high alert threshold equal to the high value
Pref.setBoolean("migrate_persistent_high_alert_threshold", false); // Done - never migrate again

}
} catch (Exception e) {
UserError.Log.wtf(TAG, "Startup settings failed! Please report");
}
}

// This function moves us from calibrate_external_libre_2_algorithm which is a boolean to a
// multi value list option
public static void migrateOOP2CalibrationPreferences() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ public void onCreate(Bundle savedInstanceState) {
bindPreferenceSummaryToValue(findPreference("rising_bg_val"));
bindPreferenceSummaryToValue(findPreference("other_alerts_sound"));
bindPreferenceSummaryToValue(findPreference("bridge_battery_alert_level"));
bindPreferenceSummaryToValueAndEnsureNumeric(findPreference("persistent_high_threshold"));

addPreferencesFromResource(R.xml.pref_data_source);

Expand Down Expand Up @@ -2972,6 +2973,7 @@ public static void handleUnitsChange(Preference preference, Object newValue, All
final Double lowVal = Double.parseDouble(preferences.getString("lowValue", "0"));
final Double default_insulin_sensitivity = Double.parseDouble(preferences.getString("profile_insulin_sensitivity_default", "54"));
final Double default_target_glucose = Double.parseDouble(preferences.getString("plus_target_range", "100"));
final Double persistent_high_Val = Double.parseDouble(preferences.getString("persistent_high_threshold", "0"));


static_units = newValue.toString();
Expand All @@ -2990,6 +2992,11 @@ public static void handleUnitsChange(Preference preference, Object newValue, All
preferences.edit().putString("plus_target_range", Long.toString(Math.round(default_target_glucose * Constants.MMOLL_TO_MGDL))).apply();
Profile.invalidateProfile();
}
if (persistent_high_Val < 36) {
ProfileEditor.convertData(Constants.MMOLL_TO_MGDL);
preferences.edit().putString("persistent_high_threshold", Long.toString(Math.round(persistent_high_Val * Constants.MMOLL_TO_MGDL))).apply();
Profile.invalidateProfile();
}

} else {
if (highVal > 35) {
Expand All @@ -3006,11 +3013,17 @@ public static void handleUnitsChange(Preference preference, Object newValue, All
preferences.edit().putString("plus_target_range", JoH.qs(default_target_glucose * Constants.MGDL_TO_MMOLL,1)).apply();
Profile.invalidateProfile();
}
if (persistent_high_Val > 35) {
ProfileEditor.convertData(Constants.MGDL_TO_MMOLL);
preferences.edit().putString("persistent_high_threshold", JoH.qs(persistent_high_Val * Constants.MGDL_TO_MMOLL, 1)).apply();
Profile.invalidateProfile();
}
}
if (preference != null) preference.setSummary(newValue.toString());
if (allPrefsFragment != null) {
allPrefsFragment.setSummary("highValue");
allPrefsFragment.setSummary("lowValue");
allPrefsFragment.setSummary("persistent_high_threshold");
}
if (profile_insulin_sensitivity_default != null) {
Log.d(TAG, "refreshing profile insulin sensitivity default display");
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/xdrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void onCreate() {
executor = new PlusAsyncExecutor();

IdempotentMigrations.migrateOOP2CalibrationPreferences(); // needs to run before preferences get defaults
IdempotentMigrations.startup(); // Must run before defaults are loaded

PreferenceManager.setDefaultValues(this, R.xml.pref_general, true);
PreferenceManager.setDefaultValues(this, R.xml.pref_data_sync, true);
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
<string name="other_alerts">Other Alerts</string>
<string name="extra_alerts_xdrip_plus">Extra Alerts (xDrip+)</string>
<string name="persistent_high_alert">Persistent High Alert</string>
<string name="summary_persistent_high_alert">Notify if above threshold, and not falling, for long</string>
<string name="forecasted_low_alert">Forecasted Low Alert</string>
<string name="extrapolate_data_to_try_to_predict_lows">Extrapolate data to try to predict lows</string>
<string name="alarm_at_forecasted_low_mins">Alarm at Forecasted Low mins</string>
Expand Down Expand Up @@ -1501,7 +1502,6 @@
<string name="category_noisy_readings">Noisy Readings</string>
<string name="category_falling_rising_bg">Falling/Rising BG</string>
<string name="category_alert_prefs">Alert Preferences (for these alerts)</string>
<string name="summary_persistent_high_alert">When above High level for too long and not heading downwards</string>
<string name="note_search_button">SEARCH</string>
<string name="all_note_button">ALL</string>
<string name="title_full_screen_mode">Full Screen mode</string>
Expand Down Expand Up @@ -1786,6 +1786,8 @@
<string name="select_tone_for_alerts">Select tone for Alerts</string>
<string name="show_unsmoothed">Show unsmoothed</string>
<string name="show_unsmoothed_summary">Show unsmoothed data also if Graph Smoothing is enabled. If calibration plugin is used, will show unsmoothed data instead of plugin data.</string>
<string name="title_persistent_high_threshold">Persistent high threshold</string>
<string name="summary_persistent_high_threshold">Persistent high alert threshold</string>
<string name="you_can_use_google_drive_for_backup">You can use Google Drive for backup</string>
<string name="select_backup_location">Select Backup Location</string>
<string name="do_backup_now">Do Backup Now</string>
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/xml/pref_notifications.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,15 @@
<SwitchPreference
android:defaultValue="false"
android:key="persistent_high_alert_enabled"
android:summary="@string/alarm_if_above_high_value"
android:summary="@string/summary_persistent_high_alert"
android:title="@string/persistent_high_alert" />
<EditTextPreference
android:defaultValue="170"
android:dependency="persistent_high_alert_enabled"
android:inputType="numberDecimal"
android:key="persistent_high_threshold"
android:title="@string/title_persistent_high_threshold"
android:summary="@string/summary_persistent_high_threshold"/>
<EditTextPreference
android:defaultValue="60"
android:dependency="persistent_high_alert_enabled"
Expand Down
Loading