diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 817b0adafb..f83f47d299 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -32,6 +32,9 @@ import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesFragme import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingFragment; import info.nightscout.androidaps.plugins.InsulinFastactingProlonged.InsulinFastactingProlongedFragment; +import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefFreePeakFragment; +import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefRapidActingFragment; +import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefUltraRapidActingFragment; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalFragment; @@ -113,6 +116,9 @@ public class MainApp extends Application { if (Config.ACTION) pluginsList.add(ActionsFragment.getPlugin()); pluginsList.add(InsulinFastactingFragment.getPlugin()); pluginsList.add(InsulinFastactingProlongedFragment.getPlugin()); + pluginsList.add(InsulinOrefRapidActingFragment.getPlugin()); + pluginsList.add(InsulinOrefUltraRapidActingFragment.getPlugin()); + pluginsList.add(InsulinOrefFreePeakFragment.getPlugin()); pluginsList.add(SensitivityOref0Plugin.getPlugin()); pluginsList.add(SensitivityAAPSPlugin.getPlugin()); pluginsList.add(SensitivityWeightedAveragePlugin.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java index c5d8769639..d486e898ce 100644 --- a/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/PreferencesActivity.java @@ -14,6 +14,7 @@ import android.preference.PreferenceManager; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefFreePeakPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.BluetoothDevicePreference; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin; @@ -139,6 +140,11 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre if (virtualPumpPlugin != null && virtualPumpPlugin.isEnabled(PluginBase.PUMP)) { addPreferencesFromResource(R.xml.pref_virtualpump); } + InsulinOrefFreePeakPlugin insulinOrefFreePeakPlugin = (InsulinOrefFreePeakPlugin) MainApp.getSpecificPlugin(InsulinOrefFreePeakPlugin.class); + if(insulinOrefFreePeakPlugin.isEnabled(PluginBase.INSULIN)){ + addPreferencesFromResource(R.xml.pref_insulinoreffreepeak); + } + NSClientInternalPlugin nsClientInternalPlugin = (NSClientInternalPlugin) MainApp.getSpecificPlugin(NSClientInternalPlugin.class); if (nsClientInternalPlugin != null && nsClientInternalPlugin.isEnabled(PluginBase.GENERAL)) { addPreferencesFromResource(R.xml.pref_nsclientinternal); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/InsulinInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/InsulinInterface.java index 20c563dede..0803f65373 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/InsulinInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/InsulinInterface.java @@ -12,6 +12,10 @@ import info.nightscout.androidaps.db.Treatment; public interface InsulinInterface { final int FASTACTINGINSULIN = 0; final int FASTACTINGINSULINPROLONGED = 1; + final int OREF_RAPID_ACTING = 2; + final int OREF_ULTRA_RAPID_ACTING = 3; + final int OREF_FREE_PEAK = 4; + int getId(); String getFriendlyName(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java index a2181a3a15..5458337782 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinFastacting/ActivityGraph.java @@ -21,7 +21,6 @@ import info.nightscout.androidaps.interfaces.InsulinInterface; */ public class ActivityGraph extends GraphView { - Context context; public ActivityGraph(Context context) { @@ -35,6 +34,8 @@ public class ActivityGraph extends GraphView { } public void show(InsulinInterface insulin) { + removeAllSeries(); + mSecondScale = null; double dia = insulin.getDia(); int hours = (int) Math.floor(dia + 1); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBasePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBasePlugin.java new file mode 100644 index 0000000000..10dfd5dce5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBasePlugin.java @@ -0,0 +1,108 @@ +package info.nightscout.androidaps.plugins.InsulinOrefCurves; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.Iob; +import info.nightscout.androidaps.db.Treatment; +import info.nightscout.androidaps.interfaces.InsulinInterface; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.plugins.Overview.Notification; +import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; + +/** + * Created by adrian on 13.08.2017. + */ + +public abstract class InsulinOrefBasePlugin implements PluginBase, InsulinInterface { + + public static double MIN_DIA = 5; + + long lastWarned = 0; + + @Override + public int getType() { + return INSULIN; + } + + @Override + public String getNameShort() { + return MainApp.sResources.getString(R.string.insulin_shortname); + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public boolean hasFragment() { + return true; + } + + @Override + public boolean showInList(int type) { + return true; + } + + @Override + public double getDia() { + double dia = getUserDefinedDia(); + if(dia >= MIN_DIA){ + return dia; + } else { + if((System.currentTimeMillis() - lastWarned) > 60*1000) { + lastWarned = System.currentTimeMillis(); + Notification notification = new Notification(Notification.SHORT_DIA, String.format(MainApp.sResources.getString(R.string.dia_too_short), dia, MIN_DIA), Notification.URGENT); + MainApp.bus().post(new EventNewNotification(notification)); + } + return MIN_DIA; + } + } + + public double getUserDefinedDia() { + return MainApp.getConfigBuilder().getProfile() != null ? MainApp.getConfigBuilder().getProfile().getDia() : Constants.defaultDIA; + } + + @Override + public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia) { + Iob result = new Iob(); + + int peak = getPeak(); + + + if (treatment.insulin != 0d) { + + long bolusTime = treatment.date; + double t = (time - bolusTime) / 1000d / 60d; + + double td = getDia()*60; //getDIA() always > 5 + double tp = peak; + + // force the IOB to 0 if over DIA hours have passed + if (t < td) { + double tau = tp * (1 - tp / td) / (1 - 2 * tp / td); + double a = 2 * tau / td; + double S = 1 / (1 - a + (1 + a) * Math.exp(-td / tau)); + result.activityContrib = treatment.insulin * (S / Math.pow(tau, 2)) * t * (1 - t / td) * Math.exp(-t / tau); + result.iobContrib = treatment.insulin * (1 - S * (1 - a) * ((Math.pow(t, 2) / (tau * td * (1 - a)) - t / tau - 1) * Math.exp(-t / tau) + 1)); + } + } + return result; + } + + @Override + public String getComment() { + String comment = commentStandardText(); + double userDia = getUserDefinedDia(); + if(userDia < MIN_DIA){ + comment += "\n" + String.format(MainApp.sResources.getString(R.string.dia_too_short), userDia, MIN_DIA); + } + return comment; + } + + abstract int getPeak(); + + abstract String commentStandardText(); + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakFragment.java new file mode 100644 index 0000000000..5db351fea5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakFragment.java @@ -0,0 +1,58 @@ +package info.nightscout.androidaps.plugins.InsulinOrefCurves; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.InsulinFastacting.ActivityGraph; + +/** + * Created by adrian on 14/08/17. + */ + +public class InsulinOrefFreePeakFragment extends Fragment { + + static InsulinOrefFreePeakPlugin insulinPlugin = new InsulinOrefFreePeakPlugin(); + + static public InsulinOrefFreePeakPlugin getPlugin() { + return insulinPlugin; + } + + TextView insulinName; + TextView insulinComment; + TextView insulinDia; + ActivityGraph insulinGraph; + + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.insulin_fragment, container, false); + + insulinName = (TextView) view.findViewById(R.id.insulin_name); + insulinComment = (TextView) view.findViewById(R.id.insulin_comment); + insulinDia = (TextView) view.findViewById(R.id.insulin_dia); + insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph); + + updateGUI(); + + return view; + } + + @Override + public void onResume() { + super.onResume(); + updateGUI(); + } + + private void updateGUI() { + insulinName.setText(insulinPlugin.getFriendlyName()); + insulinComment.setText(insulinPlugin.getComment()); + insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinPlugin.getDia()).toString() + "h"); + insulinGraph.show(insulinPlugin); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakPlugin.java new file mode 100644 index 0000000000..747eabda5b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefFreePeakPlugin.java @@ -0,0 +1,67 @@ +package info.nightscout.androidaps.plugins.InsulinOrefCurves; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.utils.SP; + +/** + * Created by adrian on 14/08/17. + */ + +public class InsulinOrefFreePeakPlugin extends InsulinOrefBasePlugin { + + private static boolean fragmentEnabled = false; + private static boolean fragmentVisible = false; + + public static final int DEFAULT_PEAK = 75; + + @Override + public int getId() { + return OREF_FREE_PEAK; + } + + @Override + public String getName() { + return MainApp.sResources.getString(R.string.free_peak_oref); + } + + @Override + public String getFragmentClass() { + return InsulinOrefFreePeakFragment.class.getName(); + } + + @Override + public String getFriendlyName() { + return MainApp.sResources.getString(R.string.free_peak_oref); + } + + @Override + public String commentStandardText() { + return MainApp.sResources.getString(R.string.insulin_peak_time) + ": " + getPeak(); + } + + @Override + public boolean isEnabled(int type) { + return type == INSULIN && fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == INSULIN && fragmentVisible; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == INSULIN) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == INSULIN) this.fragmentVisible = fragmentVisible; + } + + @Override + int getPeak() { + return SP.getInt(R.string.key_insulin_oref_peak, DEFAULT_PEAK); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingFragment.java new file mode 100644 index 0000000000..ef11435805 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingFragment.java @@ -0,0 +1,57 @@ +package info.nightscout.androidaps.plugins.InsulinOrefCurves; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.InsulinFastacting.ActivityGraph; + +/** + * Created by adrian on 14/08/17. + */ + +public class InsulinOrefRapidActingFragment extends Fragment { + + static InsulinOrefRapidActingPlugin insulinPlugin = new InsulinOrefRapidActingPlugin(); + + static public InsulinOrefRapidActingPlugin getPlugin() { + return insulinPlugin; + } + + TextView insulinName; + TextView insulinComment; + TextView insulinDia; + ActivityGraph insulinGraph; + + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.insulin_fragment, container, false); + + insulinName = (TextView) view.findViewById(R.id.insulin_name); + insulinComment = (TextView) view.findViewById(R.id.insulin_comment); + insulinDia = (TextView) view.findViewById(R.id.insulin_dia); + insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph); + + updateGUI(); + + return view; + } + + @Override + public void onResume() { + super.onResume(); + updateGUI(); + } + + private void updateGUI() { + insulinName.setText(insulinPlugin.getFriendlyName()); + insulinComment.setText(insulinPlugin.getComment()); + insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinPlugin.getDia()).toString() + "h"); + insulinGraph.show(insulinPlugin); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingPlugin.java new file mode 100644 index 0000000000..6f6973bc58 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefRapidActingPlugin.java @@ -0,0 +1,66 @@ +package info.nightscout.androidaps.plugins.InsulinOrefCurves; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; + +/** + * Created by adrian on 14/08/17. + */ + +public class InsulinOrefRapidActingPlugin extends InsulinOrefBasePlugin { + + private static boolean fragmentEnabled = false; + private static boolean fragmentVisible = false; + + public static final int PEAK = 75; + + @Override + public int getId() { + return OREF_RAPID_ACTING; + } + + @Override + public String getName() { + return MainApp.sResources.getString(R.string.rapid_acting_oref); + } + + @Override + public String getFragmentClass() { + return InsulinOrefRapidActingFragment.class.getName(); + } + + @Override + public String getFriendlyName() { + return MainApp.sResources.getString(R.string.rapid_acting_oref); + } + + @Override + public String commentStandardText() { + return MainApp.sResources.getString(R.string.fastactinginsulincomment); + } + + @Override + public boolean isEnabled(int type) { + return type == INSULIN && fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == INSULIN && fragmentVisible; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == INSULIN) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == INSULIN) this.fragmentVisible = fragmentVisible; + } + + @Override + int getPeak() { + return PEAK; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingFragment.java new file mode 100644 index 0000000000..f09ba4e01d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingFragment.java @@ -0,0 +1,58 @@ +package info.nightscout.androidaps.plugins.InsulinOrefCurves; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.InsulinFastacting.ActivityGraph; + +/** + * Created by adrian on 14/08/17. + */ + +public class InsulinOrefUltraRapidActingFragment extends Fragment { + + static InsulinOrefUltraRapidActingPlugin insulinPlugin = new InsulinOrefUltraRapidActingPlugin(); + + static public InsulinOrefUltraRapidActingPlugin getPlugin() { + return insulinPlugin; + } + + TextView insulinName; + TextView insulinComment; + TextView insulinDia; + ActivityGraph insulinGraph; + + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.insulin_fragment, container, false); + + insulinName = (TextView) view.findViewById(R.id.insulin_name); + insulinComment = (TextView) view.findViewById(R.id.insulin_comment); + insulinDia = (TextView) view.findViewById(R.id.insulin_dia); + insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph); + + updateGUI(); + + return view; + } + + @Override + public void onResume() { + super.onResume(); + updateGUI(); + } + + private void updateGUI() { + insulinName.setText(insulinPlugin.getFriendlyName()); + insulinComment.setText(insulinPlugin.getComment()); + insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinPlugin.getDia()).toString() + "h"); + insulinGraph.show(insulinPlugin); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingPlugin.java new file mode 100644 index 0000000000..975707d38b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefUltraRapidActingPlugin.java @@ -0,0 +1,66 @@ +package info.nightscout.androidaps.plugins.InsulinOrefCurves; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; + +/** + * Created by adrian on 14/08/17. + */ + +public class InsulinOrefUltraRapidActingPlugin extends InsulinOrefBasePlugin { + + private static boolean fragmentEnabled = false; + private static boolean fragmentVisible = false; + + public static final int PEAK = 55; + + @Override + public int getId() { + return OREF_ULTRA_RAPID_ACTING; + } + + @Override + public String getName() { + return MainApp.sResources.getString(R.string.ultrarapid_oref); + } + + @Override + public String getFragmentClass() { + return InsulinOrefUltraRapidActingFragment.class.getName(); + } + + @Override + public String getFriendlyName() { + return MainApp.sResources.getString(R.string.ultrarapid_oref); + } + + @Override + public String commentStandardText() { + return MainApp.sResources.getString(R.string.ultrafastactinginsulincomment); + } + + @Override + public boolean isEnabled(int type) { + return type == INSULIN && fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == INSULIN && fragmentVisible; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == INSULIN) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == INSULIN) this.fragmentVisible = fragmentVisible; + } + + @Override + int getPeak() { + return PEAK; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java index 9f35599b36..d3db3c2902 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java @@ -47,6 +47,8 @@ public class Notification { public static final int NSANNOUNCEMENT = 18; public static final int NSALARM = 19; public static final int NSURGENTALARM = 20; + public static final int SHORT_DIA = 21; + public int id; public Date date; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index efe849acbd..08d0008156 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -581,6 +581,7 @@ Insulin Fast Acting Insulin Novorapid, Novolog, Humalog + Fiasp INS Fast Acting Insulin Prolonged key_usersuperbolus @@ -690,5 +691,16 @@ PUMP Basal value [U/h] Duration [min] + insulin_oref_peak + IOB Curve Peak Time + Peak Time [min] + Free-Peak Oref + Rapid-Acting Oref + Ultra-Rapid Oref + "DIA of %s too short - using %s instead!" + + + + diff --git a/app/src/main/res/xml/pref_insulinoreffreepeak.xml b/app/src/main/res/xml/pref_insulinoreffreepeak.xml new file mode 100644 index 0000000000..726d0595e2 --- /dev/null +++ b/app/src/main/res/xml/pref_insulinoreffreepeak.xml @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file