diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index ba2f14a2de..4012dbc20a 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -32,7 +32,8 @@ 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.InsulinRapidActingOref.InsulinRapidActingOrefFragment; +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; @@ -114,7 +115,8 @@ public class MainApp extends Application { if (Config.ACTION) pluginsList.add(ActionsFragment.getPlugin()); pluginsList.add(InsulinFastactingFragment.getPlugin()); pluginsList.add(InsulinFastactingProlongedFragment.getPlugin()); - pluginsList.add(InsulinRapidActingOrefFragment.getPlugin()); + pluginsList.add(InsulinOrefRapidActingFragment.getPlugin()); + pluginsList.add(InsulinOrefUltraRapidActingFragment.getPlugin()); pluginsList.add(SensitivityOref0Plugin.getPlugin()); pluginsList.add(SensitivityAAPSPlugin.getPlugin()); pluginsList.add(SensitivityWeightedAveragePlugin.getPlugin()); 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/InsulinRapidActingOref/InsulinRapidActingOrefFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBaseFragment.java similarity index 66% rename from app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefFragment.java rename to app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBaseFragment.java index 52e805c05c..d9cd795792 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBaseFragment.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.InsulinRapidActingOref; +package info.nightscout.androidaps.plugins.InsulinOrefCurves; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -15,12 +15,9 @@ import info.nightscout.androidaps.plugins.InsulinFastacting.ActivityGraph; * Created by mike on 17.04.2017. */ -public class InsulinRapidActingOrefFragment extends Fragment { - static InsulinRapidActingOrefPlugin insulinRapidActingOrefPlugin = new InsulinRapidActingOrefPlugin(); +public abstract class InsulinOrefBaseFragment extends Fragment { - static public InsulinRapidActingOrefPlugin getPlugin() { - return insulinRapidActingOrefPlugin; - } + static InsulinOrefBasePlugin insulinPlugin = null; TextView insulinName; TextView insulinComment; @@ -48,10 +45,10 @@ public class InsulinRapidActingOrefFragment extends Fragment { } private void updateGUI() { - insulinName.setText(insulinRapidActingOrefPlugin.getFriendlyName()); - insulinComment.setText(insulinRapidActingOrefPlugin.getComment()); - insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinRapidActingOrefPlugin.getDia()).toString() + "h"); - insulinGraph.show(insulinRapidActingOrefPlugin); + 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/InsulinOrefBasePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBasePlugin.java new file mode 100644 index 0000000000..0cb2362a2d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinOrefCurves/InsulinOrefBasePlugin.java @@ -0,0 +1,83 @@ +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; + +/** + * Created by adrian on 13.08.2017. + */ + +public abstract class InsulinOrefBasePlugin implements PluginBase, InsulinInterface { + + public static double MIN_DIA = 5; + + @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 = MainApp.getConfigBuilder().getProfile() != null ? MainApp.getConfigBuilder().getProfile().getDia() : Constants.defaultDIA; + return Math.max(MIN_DIA, dia); + //TODO: show on chart if restriction applied + } + + @Override + public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia) { + Iob result = new Iob(); + + + //curveDefaults + + 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; + } + + abstract int getPeak(); + +} 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..93ba75ebde --- /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 "Rapid-Acting Oref"; + } + + @Override + public String getFragmentClass() { + return InsulinOrefRapidActingFragment.class.getName(); + } + + @Override + public String getFriendlyName() { + return "Rapid-Acting Oref"; + } + + @Override + public String getComment() { + 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..d538d1d470 --- /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 "Ultra-Rapid-Acting Oref"; + } + + @Override + public String getFragmentClass() { + return InsulinOrefUltraRapidActingFragment.class.getName(); + } + + @Override + public String getFriendlyName() { + return "Ultra-Rapid-Acting Oref"; + } + + @Override + public String getComment() { + return "Fiasp"; + } + + @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/InsulinRapidActingOref/InsulinRapidActingOrefPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefPlugin.java deleted file mode 100644 index bdb92334ee..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefPlugin.java +++ /dev/null @@ -1,134 +0,0 @@ -package info.nightscout.androidaps.plugins.InsulinRapidActingOref; - -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; - -/** - * Created by mike on 17.04.2017. - */ - -public class InsulinRapidActingOrefPlugin implements PluginBase, InsulinInterface { - - private static boolean fragmentEnabled = false; - private static boolean fragmentVisible = false; - - @Override - public int getType() { - return INSULIN; - } - - @Override - public String getFragmentClass() { - return InsulinRapidActingOrefFragment.class.getName(); - } - - @Override - public String getName() { - return "Rapid-Acting Oref"; - } - - @Override - public String getNameShort() { - return MainApp.sResources.getString(R.string.insulin_shortname); - } - - @Override - public boolean isEnabled(int type) { - return type == INSULIN && fragmentEnabled; - } - - @Override - public boolean isVisibleInTabs(int type) { - return type == INSULIN && fragmentVisible; - } - - @Override - public boolean canBeHidden(int type) { - return true; - } - - @Override - public boolean hasFragment() { - return true; - } - - @Override - public boolean showInList(int type) { - return true; - } - - @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; - } - - // Insulin interface - @Override - public int getId() { - return FASTACTINGINSULINPROLONGED; - } - - @Override - public String getFriendlyName() { - return "Rapid-Acting Oref"; - } - - @Override - public String getComment() { - return MainApp.sResources.getString(R.string.fastactinginsulincomment); - } - - @Override - public double getDia() { - //TODO: dynamic dia fetching - return 6d; - //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(); - - - //curveDefaults - - int peak= 75; - - - if (treatment.insulin != 0d) { - - long bolusTime = treatment.date; - double t = (time - bolusTime) / 1000d / 60d; - - double td = getDia()*60; - - if(getDia() < 5){ - //TODO: Check that DIA is > 5 hours for this plugin! - td = 300; - } - - double tp = peak; - - - // force the IOB to 0 if over 5 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; - } -}