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

Ketoacidosis protection #107

Closed
wants to merge 13 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.interfaces.ActivePluginProvider;
import info.nightscout.androidaps.plugins.aps.loop.APSResult;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.sharedPreferences.SP;

Expand Down Expand Up @@ -46,6 +49,27 @@ private DetermineBasalResultSMB(HasAndroidInjector injector) {
tempBasalRequested = true;
rate = result.getDouble("rate");
if (rate < 0d) rate = 0d;

// Ketocidosis Protection
// Calculate IOB
treatmentsPlugin.updateTotalIOBTreatments();
treatmentsPlugin.updateTotalIOBTempBasals();
final IobTotal bolusIob = treatmentsPlugin.getLastCalculationTreatments();
final IobTotal basalIob = treatmentsPlugin.getLastCalculationTempBasals();
// Get active BaseBasalRate
double baseBasalRate = activePlugin.getActivePump().getBaseBasalRate();
// Activate a small TBR
if ( sp.getBoolean(R.string.key_keto_protect, false) && sp.getBoolean(R.string.key_variable_keto_protect_strategy, true) && (bolusIob.iob + basalIob.basaliob) < (0 - baseBasalRate) && -(bolusIob.activity + basalIob.activity) > 0) {
// Variable strategy
double cutoff = baseBasalRate * (sp.getDouble(R.string.keto_protect_basal, 20d) * 0.01);
if (rate < cutoff) rate = cutoff;
} else if ( sp.getBoolean(R.string.key_keto_protect, false) && !sp.getBoolean(R.string.key_variable_keto_protect_strategy, true) ) {
// Continuous strategy
double cutoff = baseBasalRate * ( sp.getDouble(R.string.keto_protect_basal, 20d) * 0.01 );
if (rate < cutoff) rate = cutoff;
}
// End Ketoacidosis Protetion

duration = result.getInt("duration");
} else {
rate = -1;
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,16 @@
<string name="filter">Filter</string>
<string name="copytolocalprofile_invalid">Unable to create local profile. Profile is invalid.</string>
<string name="cta_dont_kill_my_app_info">Don\'t kill my app?</string>
<string name="key_keto_protect" translatable="false">keto_protect</string>
<string name="key_variable_keto_protect_strategy" translatable="false">variable_keto_protect_strategy</string>
<string name="keto_protect_basal" translatable="false">keto_protect_basal</string>
<string name="keto_protect_title">Ketoacidosis Protection</string>
<string name="keto_protect_summary">Enables a small safety TBR to reduce the ketoacidosis risk.</string>
<string name="keto_protect_strategy_title">Variable protection?</string>
<string name="keto_protect_strategy_summary">If activated the small safety TBR kicks in when IOB is in negative range as if no basal insulin has been delivered for one hour.\nIf deactivated every Zero Temp is replaced with the small TBR.</string>
<string name="keto_protect_basal_title">Quantity of the safety TBR [%]</string>
<string name="keto_protect_basal_summary">Quantity of the small safety TBR in % which is given to avoid ketoacidosis.</string>

<string name="alarm">Alarm</string>

</resources>
34 changes: 34 additions & 0 deletions app/src/main/res/xml/pref_openapssmb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,40 @@
validate:maxNumber="10"
validate:minNumber="1"
validate:testType="numericRange"/>

<androidx.preference.PreferenceScreen
android:key="keto_protect_settings"
android:title="@string/keto_protect_title">

<SwitchPreference
android:defaultValue="false"
android:key="@string/key_keto_protect"
android:summary="@string/keto_protect_summary"
android:title="@string/keto_protect_title" />

<SwitchPreference
android:defaultValue="true"
android:dependency="@string/key_keto_protect"
android:key="@string/key_variable_keto_protect_strategy"
android:summary="@string/keto_protect_strategy_summary"
android:title="@string/keto_protect_strategy_title" />

<info.nightscout.androidaps.utils.textValidator.ValidatingEditTextPreference
android:defaultValue="20"
android:dependency="@string/key_keto_protect"
android:dialogMessage="@string/keto_protect_basal_summary"
android:digits="0123456789"
android:inputType="number"
android:key="@string/keto_protect_basal"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/keto_protect_basal_title"
validate:maxNumber="30"
validate:minNumber="0"
validate:testType="numericRange" />

</androidx.preference.PreferenceScreen>

<androidx.preference.PreferenceScreen
android:key="absorption_smb_advanced"
Expand Down