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 #3704

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -30,28 +30,30 @@ public class PersistentHigh {

private static final String TAG = PersistentHigh.class.getSimpleName();
private static final String PERSISTENT_HIGH_SINCE = "persistent_high_since";
public static double persistentHighThreshold = Home.convertToMgDlIfMmol(JoH.tolerantParseDouble(Pref.getString("highValue", "170"))); // By default, the persistent High threshold is equal to the High Value

public static boolean checkForPersistentHigh() {

if (!Pref.getBoolean("high_value_is_persistent_high_threshold", true)) { // If the user has chosen not to use the High Value as the threshold
persistentHighThreshold = Home.convertToMgDlIfMmol(JoH.tolerantParseDouble(Pref.getString("persistent_high_threshold", "170"))); // Set the threshold to the value chosen by the user
}

// skip if not enabled
if (!Pref.getBooleanDefaultFalse("persistent_high_alert_enabled")) return false;


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 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() > persistentHighThreshold) {

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));
Log.d(TAG, "CheckForPersistentHigh: Slope: " + JoH.qs(this_slope) + " " + JoH.dateTimeText(last.get(0).timestamp));

// if not falling
if (this_slope > 0 && !last.get(0).hide_slope) {
Expand All @@ -78,7 +80,7 @@ public static boolean checkForPersistentHigh() {
return false;
}

if (!dataQualityCheck(high_since, highMarkMgDl)) {
if (!dataQualityCheck(high_since, persistentHighThreshold)) {
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
@@ -1,5 +1,6 @@
package com.eveningoutpost.dexdrip.models;

import static com.eveningoutpost.dexdrip.evaluators.PersistentHigh.persistentHighThreshold;
import static com.eveningoutpost.dexdrip.g5model.Ob1G5StateMachine.shortTxId;
import static com.eveningoutpost.dexdrip.importedlibraries.dexcom.Dex_Constants.TREND_ARROW_VALUES.NOT_COMPUTABLE;
import static com.eveningoutpost.dexdrip.importedlibraries.dexcom.Dex_Constants.TREND_ARROW_VALUES.getTrend;
Expand Down Expand Up @@ -1834,10 +1835,8 @@ 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
if (last.get(0).calculated_value >
Home.convertToMgDlIfMmol(
JoH.tolerantParseDouble(Pref.getString("highValue", "170")))) {
// check if exceeding persistent high threshold
if (last.get(0).calculated_value > persistentHighThreshold) {

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 @@ -748,6 +748,19 @@ public boolean onPreferenceChange(Preference preference, Object value) {
return false;
}
};

private static Preference.OnPreferenceChangeListener sBindNumericUnitizedPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { // This listener adds glucose unit in addition to the value to the summary
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (isNumeric(stringValue)) {
final boolean domgdl = Pref.getString("units", "mgdl").equals("mgdl"); // Identify which unit is chosen
preference.setSummary(stringValue + " " + (domgdl ? "mg/dl" : "mmol/l")); // Set the summary to show the value followed by the chosen unit
return true;
}
return false;
}
};
private static Preference.OnPreferenceChangeListener sBindPreferenceTitleAppendToValueListenerUpdateChannel = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
Expand Down Expand Up @@ -996,6 +1009,13 @@ private static void bindPreferenceSummaryToValueAndEnsureNumeric(Preference pref
.getString(preference.getKey(), ""));
}

private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Preference preference) { // Use this to show the value as well as the corresponding glucose unit as the summary
preference.setOnPreferenceChangeListener(sBindNumericUnitizedPreferenceSummaryToValueListener);
sBindNumericUnitizedPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}

@RequiredArgsConstructor
public static class AllPrefsFragment extends PreferenceFragment {
Expand Down Expand Up @@ -1074,6 +1094,7 @@ public void onCreate(Bundle savedInstanceState) {
bindPreferenceSummaryToValue(findPreference("rising_bg_val"));
bindPreferenceSummaryToValue(findPreference("other_alerts_sound"));
bindPreferenceSummaryToValue(findPreference("bridge_battery_alert_level"));
bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(findPreference("persistent_high_threshold"));

addPreferencesFromResource(R.xml.pref_data_source);

Expand Down Expand Up @@ -3057,6 +3078,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 @@ -3068,6 +3090,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();
}
if (lowVal < 36) {
ProfileEditor.convertData(Constants.MMOLL_TO_MGDL);
preferences.edit().putString("lowValue", Long.toString(Math.round(lowVal * Constants.MMOLL_TO_MGDL))).apply();
Expand All @@ -3084,6 +3111,11 @@ 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 (lowVal > 35) {
ProfileEditor.convertData(Constants.MGDL_TO_MMOLL);
preferences.edit().putString("lowValue", JoH.qs(lowVal * Constants.MGDL_TO_MMOLL, 1)).apply();
Expand All @@ -3096,6 +3128,7 @@ public static void handleUnitsChange(Preference preference, Object newValue, All
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
19 changes: 12 additions & 7 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@
<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="persistent_high_alert_enable">Enable</string>
<string name="title_persistent_high_threshold">Threshold</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 @@ -436,15 +438,15 @@
<string name="new_version_date_colon">"New Version date: "</string>
<string name="old_version_date_colon">"Old Version date: "</string>
<string name="speak_your_note_text">Speak your note text</string>
<string name="alarm_if_above_high_value">Alarm if above high value</string>
<string name="alarm_if_above_high_value">Notify if above threshold and not falling for long</string>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this english is clear what this means

Copy link
Collaborator Author

@Navid200 Navid200 Oct 9, 2024

Choose a reason for hiding this comment

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

How about this instead?

Notify if above threshold for longer than the time specified below

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have changed the string to something that I hope is more clear.

Thanks for the review

<string name="for_longer_than_minutes">for longer than (minutes)</string>
<string name="choose_sound_used_for_persistent_high_alarm">Choose sound used for persistent high alarm.</string>
<string name="persistent_high_sound">Persistent High Sound</string>
<string name="choose_sound_used_for_persistent_high_alarm">Choose the sound used for the alarm.</string>
<string name="persistent_high_sound">Alert Sound</string>
<string name="forecast_lows">Forecast Lows</string>
<string name="raise_alarm_on_forecast_low">Raise alarm on Forecast Low</string>
<string name="notify_when_predicted_low_time_reaches_threshold">Notify when predicted low time reaches threshold</string>
<string name="predicted_low_sound">Predicted Low Sound</string>
<string name="choose_sound_used_for_predicted_low_alarm">Choose sound used for predicted low alarm.</string>
<string name="predicted_low_sound">Alert Sound</string>
<string name="choose_sound_used_for_predicted_low_alarm">Choose the sound used for the alarm.</string>
<string name="notify_when_parakeet_device_stops_checking_in">Notify when Parakeet device stops checking in</string>
<string name="parakeet_related_alerts">Parakeet related alerts</string>
<string name="raise_parakeet_notification_silently_when_charging">Raise Parakeet notification silently when charging</string>
Expand Down Expand Up @@ -1509,7 +1511,9 @@
<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="summary_persistent_high_alert">When above threshold and not falling for long</string>
<string name="summary_persistent_high_threshold_link">When enabled, High Value is the threshold.\nWhen disabled, the threshold is the value specified below.</string>
<string name="title_persistent_high_threshold_link">Threshold: High Value</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 @@ -1861,7 +1865,8 @@
<string name="summary_ymax">Choose yMax for when there are no greater readings.</string>
<string name="wait_to_connect">Verify settings and wait for connectivity.</string>
<string name="summary_sens_expiry_notify">Raise notifications when the sensor gets close to expiry.</string>
<string name="title_sens_expiry_notify">Sensor expiry</string>
<string name="title_sens_expiry">Sensor Expiry Notifications</string>
<string name="title_sens_expiry_notify">Enable</string>
<string name="close">Close</string>
<string name="title_advanced_settings_4_Lib2">Advanced settings for Libre 2</string>
<string name="title_Lib2_show_raw_values">Show raw values in graph</string>
Expand Down
50 changes: 34 additions & 16 deletions app/src/main/res/xml/pref_notifications.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,26 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="smart_alerts_screen"
android:title="@string/extra_alerts_xdrip_plus">
<PreferenceCategory
android:summary="@string/summary_persistent_high_alert"
<PreferenceScreen
android:key="persistent_high_alert_page"
android:title="@string/persistent_high_alert">
<SwitchPreference
android:defaultValue="false"
android:key="persistent_high_alert_enabled"
android:summary="@string/alarm_if_above_high_value"
android:title="@string/persistent_high_alert" />
android:title="@string/persistent_high_alert_enable" />
<SwitchPreference
android:defaultValue="true"
android:disableDependentsState="true"
android:key="high_value_is_persistent_high_threshold"
android:summary="@string/summary_persistent_high_threshold_link"
android:title="@string/title_persistent_high_threshold_link" />
<EditTextPreference
android:defaultValue="170"
android:dependency="high_value_is_persistent_high_threshold"
android:inputType="numberDecimal"
android:key="persistent_high_threshold"
android:title="@string/title_persistent_high_threshold" />
<EditTextPreference
android:defaultValue="60"
android:dependency="persistent_high_alert_enabled"
Expand All @@ -323,9 +335,9 @@
android:showSilent="true"
android:summary="@string/choose_sound_used_for_persistent_high_alarm"
android:title="@string/persistent_high_sound" />
</PreferenceCategory>
<PreferenceCategory
android:summary="@string/momentum_indicates_low"
</PreferenceScreen>
<PreferenceScreen
android:key="@string/forecasted_low_alert"
android:title="@string/forecasted_low_alert">
<SwitchPreference
android:defaultValue="true"
Expand Down Expand Up @@ -354,8 +366,21 @@
android:showSilent="true"
android:summary="@string/choose_sound_used_for_predicted_low_alarm"
android:title="@string/predicted_low_sound" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/other_xdrip_plus_alerts">
</PreferenceScreen>
<PreferenceScreen
android:key="@string/title_sens_expiry"
android:title="@string/title_sens_expiry">
<SwitchPreference
android:defaultValue="false"
android:key="alert_raise_for_sensor_expiry"
android:summary="@string/summary_sens_expiry_notify"
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:title="@string/title_sens_expiry_notify" />
</PreferenceScreen>
<PreferenceScreen
android:key="@string/other_xdrip_plus_alerts"
android:title="@string/other_xdrip_plus_alerts">
<SwitchPreference
android:defaultValue="true"
android:key="bridge_battery_alerts"
Expand All @@ -369,13 +394,6 @@
android:key="bridge_battery_alert_level"
android:summary=""
android:title="@string/low_battery_percentage" />
<SwitchPreference
android:defaultValue="false"
android:switchTextOff="@string/short_off_text_for_switches"
android:switchTextOn="@string/short_on_text_for_switches"
android:key="alert_raise_for_sensor_expiry"
android:summary="@string/summary_sens_expiry_notify"
android:title="@string/title_sens_expiry_notify" />
<SwitchPreference
android:defaultValue="true"
android:dependency="engineering_mode"
Expand All @@ -393,7 +411,7 @@
android:key="follower_chime"
android:summary="@string/notify_data_arrives_master"
android:title="@string/follower_chime_new" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
Expand Down
Loading