diff --git a/app/src/main/java/info/nightscout/androidaps/HistoryBrowseActivity.java b/app/src/main/java/info/nightscout/androidaps/HistoryBrowseActivity.java index 5fae3136fa..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,8 +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; @@ -56,6 +61,14 @@ public class HistoryBrowseActivity extends AppCompatActivity { private int rangeToDisplay = 24; // for graph private long start; + IobCobCalculatorPlugin iobCobCalculatorPlugin; + + EventCustomCalculationFinished eventCustomCalculationFinished = new EventCustomCalculationFinished(); + + public HistoryBrowseActivity() { + iobCobCalculatorPlugin = new IobCobCalculatorPlugin(); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -97,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) @@ -115,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) @@ -134,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; } @@ -145,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) { @@ -195,7 +228,7 @@ public class HistoryBrowseActivity extends AppCompatActivity { // ------------------ 1st graph - final GraphData graphData = new GraphData(bgGraph); + final GraphData graphData = new GraphData(bgGraph, IobCobCalculatorPlugin.getPlugin()); // **** In range Area **** graphData.addInRangeArea(fromTime, toTime, lowLine, highLine); @@ -223,7 +256,7 @@ public class HistoryBrowseActivity extends AppCompatActivity { // ------------------ 2nd graph - final GraphData secondGraphData = new GraphData(iobGraph); + final GraphData secondGraphData = new GraphData(iobGraph, iobCobCalculatorPlugin); boolean useIobForScale = false; boolean useCobForScale = false; diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index b242c4c099..2bb7d8f9f0 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -600,7 +600,8 @@ public class DataService extends IntentService { if (trJson.has("eventType") && trJson.getString("eventType").equals(CareportalEvent.ANNOUNCEMENT)) { long date = trJson.getLong("mills"); long now = System.currentTimeMillis(); - if (date > now - 15 * 60 * 1000L && trJson.has("notes")) { + if (date > now - 15 * 60 * 1000L && trJson.has("notes") + && !(trJson.has("enteredBy") && trJson.getString("enteredBy").equals(SP.getString("careportal_enteredby", "AndroidAPS")))) { Notification announcement = new Notification(Notification.NSANNOUNCEMENT, trJson.getString("notes"), Notification.ANNOUNCEMENT, 60); MainApp.bus().post(new EventNewNotification(announcement)); } diff --git a/app/src/main/java/info/nightscout/androidaps/data/QuickWizardEntry.java b/app/src/main/java/info/nightscout/androidaps/data/QuickWizardEntry.java index 35a7265f2a..b08926f119 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/QuickWizardEntry.java +++ b/app/src/main/java/info/nightscout/androidaps/data/QuickWizardEntry.java @@ -82,9 +82,9 @@ public class QuickWizardEntry { double cob = 0d; AutosensData autosensData; if (_synchronized) - autosensData = IobCobCalculatorPlugin.getLastAutosensDataSynchronized("QuickWizard COB"); + autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensDataSynchronized("QuickWizard COB"); else - autosensData = IobCobCalculatorPlugin.getLastAutosensData("QuickWizard COB"); + autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensData("QuickWizard COB"); if (autosensData != null && useCOB() == YES) { cob = autosensData.cob; diff --git a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java index 9f49b64280..e2e4e6d639 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java +++ b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java @@ -5,6 +5,7 @@ import android.graphics.Color; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; +import org.apache.commons.lang3.StringUtils; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -184,7 +185,7 @@ public class CareportalEvent implements DataPointWithLabelInterface { try { JSONObject object = new JSONObject(json); if (object.has("notes")) - return object.getString("notes"); + return StringUtils.abbreviate(object.getString("notes"), 40); } catch (JSONException e) { log.error("Unhandled exception", e); } 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 c891f60d49..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 @@ -41,19 +41,19 @@ import info.nightscout.utils.DateUtil; public class IobCobCalculatorPlugin implements PluginBase { private static Logger log = LoggerFactory.getLogger(IobCobCalculatorPlugin.class); - private static LongSparseArray iobTable = new LongSparseArray<>(); // oldest at index 0 - private static LongSparseArray autosensDataTable = new LongSparseArray<>(); // oldest at index 0 - private static LongSparseArray basalDataTable = new LongSparseArray<>(); // oldest at index 0 + private LongSparseArray iobTable = new LongSparseArray<>(); // oldest at index 0 + private LongSparseArray autosensDataTable = new LongSparseArray<>(); // oldest at index 0 + private LongSparseArray basalDataTable = new LongSparseArray<>(); // oldest at index 0 - private static volatile List bgReadings = null; // newest at index 0 - private static volatile List bucketed_data = null; + private volatile List bgReadings = null; // newest at index 0 + private volatile List bucketed_data = null; - private static double dia = Constants.defaultDIA; + private double dia = Constants.defaultDIA; - static final Object dataLock = new Object(); + final Object dataLock = new Object(); boolean stopCalculationTrigger = false; - IobCobThread thread = null; + private IobCobThread thread = null; private static IobCobCalculatorPlugin plugin = null; @@ -63,11 +63,11 @@ public class IobCobCalculatorPlugin implements PluginBase { return plugin; } - public static LongSparseArray getAutosensDataTable() { + public LongSparseArray getAutosensDataTable() { return autosensDataTable; } - public static List getBucketedData() { + public List getBucketedData() { return bucketed_data; } @@ -131,12 +131,12 @@ public class IobCobCalculatorPlugin implements PluginBase { return -1; } - private IobCobCalculatorPlugin() { + public IobCobCalculatorPlugin() { MainApp.bus().register(this); } @Nullable - public static List getBucketedData(long fromTime) { + public List getBucketedData(long fromTime) { //log.debug("Locking getBucketedData"); synchronized (dataLock) { if (bucketed_data == null) { @@ -154,7 +154,7 @@ public class IobCobCalculatorPlugin implements PluginBase { return null; } - private static int indexNewerThan(long time) { + private int indexNewerThan(long time) { for (int index = 0; index < bucketed_data.size(); index++) { if (bucketed_data.get(index).date < time) return index - 1; @@ -169,9 +169,9 @@ public class IobCobCalculatorPlugin implements PluginBase { return rouded; } - void loadBgData() { - bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime((long) (System.currentTimeMillis() - 60 * 60 * 1000L * (24 + dia)), false); - log.debug("BG data loaded. Size: " + bgReadings.size()); + void loadBgData(long start) { + bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime((long) (start - 60 * 60 * 1000L * (24 + dia)), false); + log.debug("BG data loaded. Size: " + bgReadings.size() + " Start date: " + DateUtil.dateAndTimeString(start)); } private boolean isAbout5minData() { @@ -332,13 +332,13 @@ public class IobCobCalculatorPlugin implements PluginBase { return getBGDataFrom; } - public static IobTotal calculateFromTreatmentsAndTempsSynchronized(long time) { + public IobTotal calculateFromTreatmentsAndTempsSynchronized(long time) { synchronized (dataLock) { return calculateFromTreatmentsAndTemps(time); } } - public static IobTotal calculateFromTreatmentsAndTemps(long time) { + public IobTotal calculateFromTreatmentsAndTemps(long time) { long now = System.currentTimeMillis(); time = roundUpTime(time); if (time < now && iobTable.get(time) != null) { @@ -373,7 +373,7 @@ public class IobCobCalculatorPlugin implements PluginBase { } @Nullable - private static Long findPreviousTimeFromBucketedData(long time) { + private Long findPreviousTimeFromBucketedData(long time) { if (bucketed_data == null) return null; for (int index = 0; index < bucketed_data.size(); index++) { @@ -383,7 +383,7 @@ public class IobCobCalculatorPlugin implements PluginBase { return null; } - public static BasalData getBasalData(long time) { + public BasalData getBasalData(long time) { long now = System.currentTimeMillis(); time = roundUpTime(time); BasalData retval = basalDataTable.get(time); @@ -409,7 +409,7 @@ public class IobCobCalculatorPlugin implements PluginBase { } @Nullable - public static AutosensData getAutosensData(long time) { + public AutosensData getAutosensData(long time) { synchronized (dataLock) { long now = System.currentTimeMillis(); if (time > now) @@ -434,7 +434,7 @@ public class IobCobCalculatorPlugin implements PluginBase { } @Nullable - public static AutosensData getLastAutosensDataSynchronized(String reason) { + public AutosensData getLastAutosensDataSynchronized(String reason) { synchronized (dataLock) { return getLastAutosensData(reason); } @@ -442,7 +442,7 @@ public class IobCobCalculatorPlugin implements PluginBase { @Nullable - public static AutosensData getLastAutosensData(String reason) { + public AutosensData getLastAutosensData(String reason) { if (autosensDataTable.size() < 1) { log.debug("AUTOSENSDATA null: autosensDataTable empty (" + reason + ")"); return null; @@ -467,7 +467,7 @@ public class IobCobCalculatorPlugin implements PluginBase { } } - public static IobTotal[] calculateIobArrayInDia() { + public IobTotal[] calculateIobArrayInDia() { Profile profile = MainApp.getConfigBuilder().getProfile(); // predict IOB out to DIA plus 30m long time = System.currentTimeMillis(); @@ -484,7 +484,7 @@ public class IobCobCalculatorPlugin implements PluginBase { return array; } - public static IobTotal[] calculateIobArrayForSMB() { + public IobTotal[] calculateIobArrayForSMB() { Profile profile = MainApp.getConfigBuilder().getProfile(); // predict IOB out to DIA plus 30m long time = System.currentTimeMillis(); @@ -501,7 +501,7 @@ public class IobCobCalculatorPlugin implements PluginBase { return array; } - public static AutosensResult detectSensitivityWithLock(long fromTime, long toTime) { + public AutosensResult detectSensitivityWithLock(long fromTime, long toTime) { synchronized (dataLock) { return detectSensitivity(fromTime, toTime); } @@ -521,13 +521,21 @@ public class IobCobCalculatorPlugin implements PluginBase { @Subscribe public void onEventAppInitialized(EventAppInitialized ev) { - runCalculation("onEventAppInitialized", true, ev); + if (this != getPlugin()) { + log.debug("Ignoring event for non default instance"); + return; + } + runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, ev); } @Subscribe public void onEventNewBG(EventNewBG ev) { + if (this != getPlugin()) { + log.debug("Ignoring event for non default instance"); + return; + } stopCalculation("onEventNewBG"); - runCalculation("onEventNewBG", true, ev); + runCalculation("onEventNewBG", System.currentTimeMillis(), true, ev); } private void stopCalculation(String from) { @@ -541,16 +549,20 @@ public class IobCobCalculatorPlugin implements PluginBase { } } - private void runCalculation(String from, 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, bgDataReload, cause); + thread = new IobCobThread(this, from, start, bgDataReload, cause); thread.start(); } } @Subscribe public void onNewProfile(EventNewBasalProfile ev) { + if (this != getPlugin()) { + log.debug("Ignoring event for non default instance"); + return; + } if (MainApp.getConfigBuilder() == null) return; // app still initializing Profile profile = MainApp.getConfigBuilder().getProfile(); @@ -566,11 +578,15 @@ public class IobCobCalculatorPlugin implements PluginBase { iobTable = new LongSparseArray<>(); autosensDataTable = new LongSparseArray<>(); } - runCalculation("onNewProfile", false, ev); + runCalculation("onNewProfile", System.currentTimeMillis(), false, ev); } @Subscribe public void onEventPreferenceChange(EventPreferenceChange ev) { + if (this != getPlugin()) { + log.debug("Ignoring event for non default instance"); + return; + } if (ev.isChanged(R.string.key_openapsama_autosens_period) || ev.isChanged(R.string.key_age) || ev.isChanged(R.string.key_absorption_maxtime) @@ -581,24 +597,32 @@ public class IobCobCalculatorPlugin implements PluginBase { iobTable = new LongSparseArray<>(); autosensDataTable = new LongSparseArray<>(); } - runCalculation("onEventPreferenceChange", false, ev); + runCalculation("onEventPreferenceChange", System.currentTimeMillis(), false, ev); } } @Subscribe public void onEventConfigBuilderChange(EventConfigBuilderChange ev) { + if (this != getPlugin()) { + log.debug("Ignoring event for non default instance"); + return; + } stopCalculation("onEventConfigBuilderChange"); synchronized (dataLock) { log.debug("Invalidating cached data because of configuration change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); iobTable = new LongSparseArray<>(); autosensDataTable = new LongSparseArray<>(); } - runCalculation("onEventConfigBuilderChange", false, ev); + runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, ev); } // When historical data is changed (comming from NS etc) finished calculations after this date must be invalidated @Subscribe public void onEventNewHistoryData(EventNewHistoryData ev) { + if (this != getPlugin()) { + log.debug("Ignoring event for non default instance"); + return; + } //log.debug("Locking onNewHistoryData"); stopCalculation("onEventNewHistoryData"); synchronized (dataLock) { @@ -633,10 +657,18 @@ public class IobCobCalculatorPlugin implements PluginBase { } } } - runCalculation("onEventNewHistoryData", false, ev); + runCalculation("onEventNewHistoryData", System.currentTimeMillis(), false, ev); //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 4f63cacf62..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,10 +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 static info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin.getBucketedData; -import static info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin.oldestDataAvailable; -import static info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin.roundUpTime; +import info.nightscout.utils.DateUtil; /** * Created by mike on 23.01.2018. @@ -37,16 +34,18 @@ public class IobCobThread extends Thread { private IobCobCalculatorPlugin iobCobCalculatorPlugin; private boolean bgDataReload; private String from; + private long start; private PowerManager.WakeLock mWakeLock; - public IobCobThread(IobCobCalculatorPlugin plugin, String from, boolean bgDataReload, Event cause) { + public IobCobThread(IobCobCalculatorPlugin plugin, String from, long start, boolean bgDataReload, Event cause) { super(); this.iobCobCalculatorPlugin = plugin; this.bgDataReload = bgDataReload; this.from = from; this.cause = cause; + this.start = start; PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE); mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "iobCobThread"); @@ -68,14 +67,14 @@ public class IobCobThread extends Thread { Object dataLock = iobCobCalculatorPlugin.dataLock; - long oldestTimeWithData = oldestDataAvailable(); + long oldestTimeWithData = iobCobCalculatorPlugin.oldestDataAvailable(); synchronized (dataLock) { if (bgDataReload) { - iobCobCalculatorPlugin.loadBgData(); + iobCobCalculatorPlugin.loadBgData(start); iobCobCalculatorPlugin.createBucketedData(); } - List bucketed_data = getBucketedData(); + List bucketed_data = iobCobCalculatorPlugin.getBucketedData(); LongSparseArray autosensDataTable = iobCobCalculatorPlugin.getAutosensDataTable(); if (bucketed_data == null || bucketed_data.size() < 3) { @@ -83,7 +82,7 @@ public class IobCobThread extends Thread { return; } - long prevDataTime = roundUpTime(bucketed_data.get(bucketed_data.size() - 3).date); + long prevDataTime = iobCobCalculatorPlugin.roundUpTime(bucketed_data.get(bucketed_data.size() - 3).date); log.debug("Prev data time: " + new Date(prevDataTime).toLocaleString()); AutosensData previous = autosensDataTable.get(prevDataTime); // start from oldest to be able sub cob @@ -95,7 +94,7 @@ public class IobCobThread extends Thread { } // check if data already exists long bgTime = bucketed_data.get(i).date; - bgTime = roundUpTime(bgTime); + bgTime = iobCobCalculatorPlugin.roundUpTime(bgTime); if (bgTime > System.currentTimeMillis()) continue; Profile profile = MainApp.getConfigBuilder().getProfile(bgTime); @@ -233,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)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java index 19cbecd469..bfe4fedf1f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java @@ -183,7 +183,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { Date start = new Date(); Date startPart = new Date(); - IobTotal[] iobArray = IobCobCalculatorPlugin.calculateIobArrayInDia(); + IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(); Profiler.log(log, "calculateIobArrayInDia()", startPart); startPart = new Date(); @@ -222,7 +222,7 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { startPart = new Date(); if (MainApp.getConfigBuilder().isAMAModeEnabled()) { - lastAutosensResult = IobCobCalculatorPlugin.detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); + lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java index 2bed09d744..c992f0e739 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java @@ -188,7 +188,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { Date start = new Date(); Date startPart = new Date(); - IobTotal[] iobArray = IobCobCalculatorPlugin.calculateIobArrayForSMB(); + IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayForSMB(); Profiler.log(log, "calculateIobArrayInDia()", startPart); startPart = new Date(); @@ -224,7 +224,7 @@ public class OpenAPSSMBPlugin implements PluginBase, APSInterface { startPart = new Date(); if (MainApp.getConfigBuilder().isAMAModeEnabled()) { - lastAutosensResult = IobCobCalculatorPlugin.detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); + lastAutosensResult = IobCobCalculatorPlugin.getPlugin().detectSensitivityWithLock(IobCobCalculatorPlugin.oldestDataAvailable(), System.currentTimeMillis()); } else { lastAutosensResult = new AutosensResult(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/ErrorHelperActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/ErrorHelperActivity.java index 259c9c76eb..90cf9aecd5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/ErrorHelperActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/ErrorHelperActivity.java @@ -3,6 +3,10 @@ package info.nightscout.androidaps.plugins.Overview.Dialogs; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import info.nightscout.androidaps.R; +import info.nightscout.utils.NSUpload; +import info.nightscout.utils.SP; + public class ErrorHelperActivity extends AppCompatActivity { public ErrorHelperActivity() { super(); @@ -17,5 +21,9 @@ public class ErrorHelperActivity extends AppCompatActivity { errorDialog.setSound(getIntent().getIntExtra("soundid", 0)); errorDialog.setTitle(getIntent().getStringExtra("title")); errorDialog.show(this.getSupportFragmentManager(), "Error"); + + if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) { + NSUpload.uploadError(getIntent().getStringExtra("status")); + } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index d74c9caadf..185ee5996d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -476,7 +476,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com // COB Double c_cob = 0d; if (cobCheckbox.isChecked()) { - AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData("Wizard COB"); + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Wizard COB"); if(autosensData != null) { c_cob = autosensData.cob; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index adde4474de..fef0a5d4bd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -1276,7 +1276,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, // cob if (cobView != null) { // view must not exists String cobText = ""; - AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData("Overview COB"); + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensData("Overview COB"); if (autosensData != null) cobText = (int) autosensData.cob + " g"; cobView.setText(cobText); @@ -1362,7 +1362,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, // ------------------ 1st graph Profiler.log(log, from + " - 1st graph - START", updateGUIStart); - final GraphData graphData = new GraphData(bgGraph); + final GraphData graphData = new GraphData(bgGraph, IobCobCalculatorPlugin.getPlugin()); // **** In range Area **** graphData.addInRangeArea(fromTime, endTime, lowLine, highLine); @@ -1393,7 +1393,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, // ------------------ 2nd graph Profiler.log(log, from + " - 2nd graph - START", updateGUIStart); - final GraphData secondGraphData = new GraphData(iobGraph); + final GraphData secondGraphData = new GraphData(iobGraph, IobCobCalculatorPlugin.getPlugin()); boolean useIobForScale = false; boolean useCobForScale = false; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java index c84046b477..02bb1a7b3d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java @@ -53,9 +53,12 @@ public class GraphData { private String units; private List series = new ArrayList<>(); - public GraphData(GraphView graph) { + private IobCobCalculatorPlugin iobCobCalculatorPlugin; + + public GraphData(GraphView graph, IobCobCalculatorPlugin iobCobCalculatorPlugin) { units = MainApp.getConfigBuilder().getProfileUnits(); this.graph = graph; + this.iobCobCalculatorPlugin = iobCobCalculatorPlugin; } public void addBgReadings(long fromTime, long toTime, double lowLine, double highLine, APSResult apsResult) { @@ -129,7 +132,7 @@ public class GraphData { double lastBaseBasal = 0; double lastTempBasal = 0; for (long time = fromTime; time < toTime; time += 60 * 1000L) { - BasalData basalData = IobCobCalculatorPlugin.getBasalData(time); + BasalData basalData = IobCobCalculatorPlugin.getPlugin().getBasalData(time); double baseBasalValue = basalData.basal; double absoluteLineValue = baseBasalValue; double tempBasalValue = 0; @@ -335,7 +338,7 @@ public class GraphData { Scale iobScale = new Scale(); for (long time = fromTime; time <= toTime; time += 5 * 60 * 1000L) { - double iob = IobCobCalculatorPlugin.calculateFromTreatmentsAndTempsSynchronized(time).iob; + double iob = IobCobCalculatorPlugin.getPlugin().calculateFromTreatmentsAndTempsSynchronized(time).iob; if (Math.abs(lastIob - iob) > 0.02) { if (Math.abs(lastIob - iob) > 0.2) iobArray.add(new ScaledDataPoint(time, lastIob, iobScale)); @@ -370,7 +373,7 @@ public class GraphData { Scale cobScale = new Scale(); for (long time = fromTime; time <= toTime; time += 5 * 60 * 1000L) { - AutosensData autosensData = IobCobCalculatorPlugin.getAutosensData(time); + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getAutosensData(time); if (autosensData != null) { int cob = (int) autosensData.cob; if (cob != lastCob) { @@ -417,7 +420,7 @@ public class GraphData { Scale devScale = new Scale(); for (long time = fromTime; time <= toTime; time += 5 * 60 * 1000L) { - AutosensData autosensData = IobCobCalculatorPlugin.getAutosensData(time); + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getAutosensData(time); if (autosensData != null) { int color = Color.BLACK; // "=" if (autosensData.pastSensitivity.equals("C")) color = Color.GRAY; @@ -455,7 +458,7 @@ public class GraphData { Scale ratioScale = new Scale(-1d); for (long time = fromTime; time <= toTime; time += 5 * 60 * 1000L) { - AutosensData autosensData = IobCobCalculatorPlugin.getAutosensData(time); + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getAutosensData(time); if (autosensData != null) { ratioArray.add(new ScaledDataPoint(time, autosensData.autosensRatio, ratioScale)); maxRatioValueFound = Math.max(maxRatioValueFound, Math.abs(autosensData.autosensRatio)); @@ -489,7 +492,7 @@ public class GraphData { Scale dsMinScale = new Scale(); for (long time = fromTime; time <= toTime; time += 5 * 60 * 1000L) { - AutosensData autosensData = IobCobCalculatorPlugin.getAutosensData(time); + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getAutosensData(time); if (autosensData != null) { dsMaxArray.add(new ScaledDataPoint(time, autosensData.slopeFromMaxDeviation, dsMaxScale)); dsMinArray.add(new ScaledDataPoint(time, autosensData.slopeFromMinDeviation, dsMinScale)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityAAPS/SensitivityAAPSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityAAPS/SensitivityAAPSPlugin.java index f45d6cec40..ebe8ddfe31 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityAAPS/SensitivityAAPSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityAAPS/SensitivityAAPSPlugin.java @@ -105,7 +105,7 @@ public class SensitivityAAPSPlugin implements PluginBase, SensitivityInterface{ @Override public AutosensResult detectSensitivity(long fromTime, long toTime) { - LongSparseArray autosensDataTable = IobCobCalculatorPlugin.getAutosensDataTable(); + LongSparseArray autosensDataTable = IobCobCalculatorPlugin.getPlugin().getAutosensDataTable(); String age = SP.getString(R.string.key_age, ""); int defaultHours = 24; @@ -119,7 +119,7 @@ public class SensitivityAAPSPlugin implements PluginBase, SensitivityInterface{ return new AutosensResult(); } - AutosensData current = IobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already + AutosensData current = IobCobCalculatorPlugin.getPlugin().getAutosensData(toTime); // this is running inside lock already if (current == null) { log.debug("No autosens data available"); return new AutosensResult(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java index bf7d6ce8f5..eaabb4ecc9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityOref0/SensitivityOref0Plugin.java @@ -104,7 +104,7 @@ public class SensitivityOref0Plugin implements PluginBase, SensitivityInterface @Override public AutosensResult detectSensitivity(long fromTime, long toTime) { - LongSparseArray autosensDataTable = IobCobCalculatorPlugin.getAutosensDataTable(); + LongSparseArray autosensDataTable = IobCobCalculatorPlugin.getPlugin().getAutosensDataTable(); String age = SP.getString(R.string.key_age, ""); int defaultHours = 24; @@ -120,7 +120,7 @@ public class SensitivityOref0Plugin implements PluginBase, SensitivityInterface return new AutosensResult(); } - AutosensData current = IobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already + AutosensData current = IobCobCalculatorPlugin.getPlugin().getAutosensData(toTime); // this is running inside lock already if (current == null) { log.debug("No current autosens data available"); return new AutosensResult(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityWeightedAverage/SensitivityWeightedAveragePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityWeightedAverage/SensitivityWeightedAveragePlugin.java index 5f9e996a8a..91053e2654 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityWeightedAverage/SensitivityWeightedAveragePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SensitivityWeightedAverage/SensitivityWeightedAveragePlugin.java @@ -101,7 +101,7 @@ public class SensitivityWeightedAveragePlugin implements PluginBase, Sensitivity @Override public AutosensResult detectSensitivity(long fromTime, long toTime) { - LongSparseArray autosensDataTable = IobCobCalculatorPlugin.getAutosensDataTable(); + LongSparseArray autosensDataTable = IobCobCalculatorPlugin.getPlugin().getAutosensDataTable(); String age = SP.getString(R.string.key_age, ""); int defaultHours = 24; @@ -116,7 +116,7 @@ public class SensitivityWeightedAveragePlugin implements PluginBase, Sensitivity return new AutosensResult(); } - AutosensData current = IobCobCalculatorPlugin.getAutosensData(toTime); // this is running inside lock already + AutosensData current = IobCobCalculatorPlugin.getPlugin().getAutosensData(toTime); // this is running inside lock already if (current == null) { if (Config.logAutosensData) log.debug("No autosens data available"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index dad53a0560..1842d3b504 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -252,7 +252,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { } } - AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensDataSynchronized("getMealData()"); + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensDataSynchronized("getMealData()"); if (autosensData != null) { result.mealCOB = autosensData.cob; result.slopeFromMinDeviation = autosensData.slopeFromMinDeviation; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java index 4adec23560..74423fe7ac 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/wearintegration/WatchUpdaterService.java @@ -663,7 +663,7 @@ public class WatchUpdaterService extends WearableListenerService implements private String generateCOBString() { String cobStringResult = "--"; - AutosensData autosensData = IobCobCalculatorPlugin.getLastAutosensData("WatcherUpdaterService"); + AutosensData autosensData = IobCobCalculatorPlugin.getPlugin().getLastAutosensData("WatcherUpdaterService"); if (autosensData != null) { cobStringResult = (int) autosensData.cob + "g"; } diff --git a/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java b/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java index e74818d447..10bfa52013 100644 --- a/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java +++ b/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java @@ -18,6 +18,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.receivers.KeepAliveReceiver; +import info.nightscout.utils.NSUpload; /** * Created by adrian on 17/12/17. @@ -42,6 +43,9 @@ public class LocalAlertUtils { n.soundId = R.raw.alarm; SP.putLong("nextPumpDisconnectedAlarm", System.currentTimeMillis() + pumpUnreachableThreshold()); MainApp.bus().post(new EventNewNotification(n)); + if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) { + NSUpload.uploadError(n.text); + } } } @@ -91,6 +95,9 @@ public class LocalAlertUtils { n.soundId = R.raw.alarm; SP.putLong("nextMissedReadingsAlarm", System.currentTimeMillis() + missedReadingsThreshold()); MainApp.bus().post(new EventNewNotification(n)); + if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) { + NSUpload.uploadError(n.text); + } } } } diff --git a/app/src/main/java/info/nightscout/utils/NSUpload.java b/app/src/main/java/info/nightscout/utils/NSUpload.java index 2f8893aa7b..690b239128 100644 --- a/app/src/main/java/info/nightscout/utils/NSUpload.java +++ b/app/src/main/java/info/nightscout/utils/NSUpload.java @@ -399,6 +399,7 @@ public class NSUpload { try { data.put("eventType", "Announcement"); data.put("created_at", DateUtil.toISOString(new Date())); + data.put("enteredBy", SP.getString("careportal_enteredby", MainApp.gs(R.string.app_name))); data.put("notes", error); data.put("isAnnouncement", true); } catch (JSONException e) { diff --git a/app/src/main/res/layout/activity_historybrowse.xml b/app/src/main/res/layout/activity_historybrowse.xml index 0dde0a1b10..a4959eb3f7 100644 --- a/app/src/main/res/layout/activity_historybrowse.xml +++ b/app/src/main/res/layout/activity_historybrowse.xml @@ -179,7 +179,8 @@ + android:layout_height="0dp" + android:layout_weight="1" /> "Firmware pompe incompatible " Transmettre les données G sur xDrip+ Dans xDrip+ veuillez séléctionner 640g/Eversense comme source de données - "G NSClient " + "Glycémie NSClient" "Valeur du basal remplacée par la valeur minimale supportée " Calcul de G Calcul IR du Bolus diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 915e6c0442..b8f1f9c90c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -931,5 +931,8 @@ History browser Notify on SMB Show SMB on the watch like a standard bolus. + ns_create_announcements_from_errors + Create announcements from errors + Create Nightscout announcement for error dialogs and local alerts (also viewable in Careportal under Treatments) diff --git a/app/src/main/res/xml/pref_nsclientinternal.xml b/app/src/main/res/xml/pref_nsclientinternal.xml index 5f401d6c0e..bbfb9e6a2c 100644 --- a/app/src/main/res/xml/pref_nsclientinternal.xml +++ b/app/src/main/res/xml/pref_nsclientinternal.xml @@ -27,6 +27,12 @@ android:key="@string/key_ns_logappstartedevent" android:title="@string/ns_logappstartedevent" /> + +