From bf6e3a9061d1c9a49e03798dc80edf31bbcb6b8f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 14 Feb 2018 07:47:22 +0100 Subject: [PATCH] run calculation after interval change --- .../androidaps/HistoryBrowseActivity.java | 30 +++++++++++++++++-- .../EventCustomCalculationFinished.java | 8 +++++ .../IobCobCalculatorPlugin.java | 12 ++++++-- .../IobCobCalculator/IobCobThread.java | 2 ++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java diff --git a/app/src/main/java/info/nightscout/androidaps/HistoryBrowseActivity.java b/app/src/main/java/info/nightscout/androidaps/HistoryBrowseActivity.java index afb1a18540..b15aa42b84 100644 --- a/app/src/main/java/info/nightscout/androidaps/HistoryBrowseActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/HistoryBrowseActivity.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps; +import android.app.Activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; @@ -8,6 +9,7 @@ import android.widget.CheckBox; import android.widget.SeekBar; import com.jjoe64.graphview.GraphView; +import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,9 +21,11 @@ import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.OnLongClick; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.events.EventCustomCalculationFinished; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; import info.nightscout.androidaps.plugins.Overview.graphData.GraphData; import info.nightscout.utils.DateUtil; @@ -59,6 +63,8 @@ public class HistoryBrowseActivity extends AppCompatActivity { IobCobCalculatorPlugin iobCobCalculatorPlugin; + EventCustomCalculationFinished eventCustomCalculationFinished = new EventCustomCalculationFinished(); + public HistoryBrowseActivity() { iobCobCalculatorPlugin = new IobCobCalculatorPlugin(); } @@ -104,12 +110,16 @@ public class HistoryBrowseActivity extends AppCompatActivity { void onClickLeft() { start -= rangeToDisplay * 60 * 60 * 1000L; updateGUI("left"); + iobCobCalculatorPlugin.clearCache(); + iobCobCalculatorPlugin.runCalculation("onClickLeft", start, true, eventCustomCalculationFinished); } @OnClick(R.id.historybrowse_right) void onClickRight() { start += rangeToDisplay * 60 * 60 * 1000L; updateGUI("right"); + iobCobCalculatorPlugin.clearCache(); + iobCobCalculatorPlugin.runCalculation("onClickRight", start, true, eventCustomCalculationFinished); } @OnClick(R.id.historybrowse_end) @@ -122,6 +132,8 @@ public class HistoryBrowseActivity extends AppCompatActivity { calendar.set(Calendar.HOUR_OF_DAY, 0); start = calendar.getTimeInMillis(); updateGUI("resetToMidnight"); + iobCobCalculatorPlugin.clearCache(); + iobCobCalculatorPlugin.runCalculation("onClickEnd", start, true, eventCustomCalculationFinished); } @OnClick(R.id.historybrowse_zoom) @@ -141,6 +153,8 @@ public class HistoryBrowseActivity extends AppCompatActivity { calendar.set(Calendar.HOUR_OF_DAY, 0); start = calendar.getTimeInMillis(); updateGUI("resetToMidnight"); + iobCobCalculatorPlugin.clearCache(); + iobCobCalculatorPlugin.runCalculation("onLongClickZoom", start, true, eventCustomCalculationFinished); return true; } @@ -152,11 +166,23 @@ public class HistoryBrowseActivity extends AppCompatActivity { void onClickDate(View view) { //((CheckBox) view).toggle(); updateGUI("checkboxToggle"); + iobCobCalculatorPlugin.clearCache(); + iobCobCalculatorPlugin.runCalculation("onClickDate", start, true, eventCustomCalculationFinished); } - void loadData() { - + @Subscribe + public void onStatusEvent(final EventAutosensCalculationFinished e) { + Activity activity = this; + if (activity != null && e.cause == eventCustomCalculationFinished) { + log.debug("EventAutosensCalculationFinished"); + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + updateGUI("EventAutosensCalculationFinished"); + } + }); + } } void updateGUI(String from) { diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java b/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java new file mode 100644 index 0000000000..e52761dc58 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java @@ -0,0 +1,8 @@ +package info.nightscout.androidaps.events; + +/** + * Created by mike on 13.02.2018. + */ + +public class EventCustomCalculationFinished extends Event { +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index 8ac5fe091f..83f71c2430 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -484,7 +484,7 @@ public class IobCobCalculatorPlugin implements PluginBase { return array; } - public IobTotal[] calculateIobArrayForSMB() { + public IobTotal[] calculateIobArrayForSMB() { Profile profile = MainApp.getConfigBuilder().getProfile(); // predict IOB out to DIA plus 30m long time = System.currentTimeMillis(); @@ -549,7 +549,7 @@ public class IobCobCalculatorPlugin implements PluginBase { } } - private void runCalculation(String from, long start, boolean bgDataReload, Event cause) { + public void runCalculation(String from, long start, boolean bgDataReload, Event cause) { log.debug("Starting calculation thread: " + from); if (thread == null || thread.getState() == Thread.State.TERMINATED) { thread = new IobCobThread(this, from, start, bgDataReload, cause); @@ -661,6 +661,14 @@ public class IobCobCalculatorPlugin implements PluginBase { //log.debug("Releasing onNewHistoryData"); } + public void clearCache() { + synchronized (dataLock) { + log.debug("Clearing cached data."); + iobTable = new LongSparseArray<>(); + autosensDataTable = new LongSparseArray<>(); + } + } + // From https://gist.github.com/IceCreamYou/6ffa1b18c4c8f6aeaad2 // Returns the value at a given percentile in a sorted numeric array. // "Linear interpolation between closest ranks" method diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java index af535bbc84..ba0475a26f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java @@ -21,6 +21,7 @@ import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.Event; import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.queue.QueueThread; +import info.nightscout.utils.DateUtil; /** * Created by mike on 23.01.2018. @@ -231,6 +232,7 @@ public class IobCobThread extends Thread { previous = autosensData; autosensDataTable.put(bgTime, autosensData); + log.debug("Running detectSensitivity from: " + DateUtil.dateAndTimeString(oldestTimeWithData) + " to: " + DateUtil.dateAndTimeString(bgTime)); autosensData.autosensRatio = iobCobCalculatorPlugin.detectSensitivity(oldestTimeWithData, bgTime).ratio; if (Config.logAutosensData) log.debug(autosensData.log(bgTime));