From b8496e3208230f424531d8779a18e20a30aea728 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 14 Aug 2017 00:43:49 +0200 Subject: [PATCH] insulincurves - working with default params --- .../info/nightscout/androidaps/MainApp.java | 2 + .../InsulinRapidActingOrefFragment.java | 57 ++++++++ .../InsulinRapidActingOrefPlugin.java | 134 ++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefFragment.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefPlugin.java diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 817b0adafb..ba2f14a2de 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -32,6 +32,7 @@ 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.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalFragment; @@ -113,6 +114,7 @@ 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(SensitivityOref0Plugin.getPlugin()); pluginsList.add(SensitivityAAPSPlugin.getPlugin()); pluginsList.add(SensitivityWeightedAveragePlugin.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefFragment.java new file mode 100644 index 0000000000..52e805c05c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefFragment.java @@ -0,0 +1,57 @@ +package info.nightscout.androidaps.plugins.InsulinRapidActingOref; + +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 mike on 17.04.2017. + */ + +public class InsulinRapidActingOrefFragment extends Fragment { + static InsulinRapidActingOrefPlugin insulinRapidActingOrefPlugin = new InsulinRapidActingOrefPlugin(); + + static public InsulinRapidActingOrefPlugin getPlugin() { + return insulinRapidActingOrefPlugin; + } + + 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(insulinRapidActingOrefPlugin.getFriendlyName()); + insulinComment.setText(insulinRapidActingOrefPlugin.getComment()); + insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinRapidActingOrefPlugin.getDia()).toString() + "h"); + insulinGraph.show(insulinRapidActingOrefPlugin); + } + +} 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 new file mode 100644 index 0000000000..bdb92334ee --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/InsulinRapidActingOref/InsulinRapidActingOrefPlugin.java @@ -0,0 +1,134 @@ +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; + } +}