From 156738a5cb81d1b07e186bbd467f97a3a42e021a Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 21 Mar 2020 23:06:45 +0100 Subject: [PATCH] eliminate IobCobCalculatorPlugin::getPlugin --- .../IobCobStaticCalculatorPlugin.kt | 6 +++-- .../plugins/aps/loop/LoopPlugin.java | 4 +-- .../plugins/general/nsclient/NSUpload.java | 11 ++++---- .../IobCobCalculatorPlugin.java | 27 +++++++------------ .../androidaps/receivers/KeepAliveReceiver.kt | 2 +- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/historyBrowser/IobCobStaticCalculatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/historyBrowser/IobCobStaticCalculatorPlugin.kt index fa18b54818..87276a50a9 100644 --- a/app/src/main/java/info/nightscout/androidaps/historyBrowser/IobCobStaticCalculatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/historyBrowser/IobCobStaticCalculatorPlugin.kt @@ -10,6 +10,7 @@ import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import javax.inject.Inject @@ -25,9 +26,10 @@ class IobCobStaticCalculatorPlugin @Inject constructor( treatmentsPlugin: TreatmentsPlugin, sensitivityOref1Plugin: SensitivityOref1Plugin, sensitivityAAPSPlugin: SensitivityAAPSPlugin, - sensitivityWeightedAveragePlugin: SensitivityWeightedAveragePlugin + sensitivityWeightedAveragePlugin: SensitivityWeightedAveragePlugin, + fabricPrivacy: FabricPrivacy ) : IobCobCalculatorPlugin(injector, aapsLogger, rxBus, sp, resourceHelper, profileFunction, - activePlugin, treatmentsPlugin, sensitivityOref1Plugin, sensitivityAAPSPlugin, sensitivityWeightedAveragePlugin) { + activePlugin, treatmentsPlugin, sensitivityOref1Plugin, sensitivityAAPSPlugin, sensitivityWeightedAveragePlugin, fabricPrivacy) { override fun onStart() { // do not attach to rxbus } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java index c889381c00..4493d51275 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java @@ -391,7 +391,7 @@ public class LoopPlugin extends PluginBase { lastRun.lastSMBEnact = 0; lastRun.lastSMBRequest = 0; - NSUpload.uploadDeviceStatus(this); + NSUpload.uploadDeviceStatus(this, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump()); if (isSuspended()) { getAapsLogger().debug(LTag.APS, resourceHelper.gs(R.string.loopsuspended)); @@ -516,7 +516,7 @@ public class LoopPlugin extends PluginBase { lastRun.lastTBRRequest = lastRun.lastAPSRun.getTime(); lastRun.lastTBREnact = DateUtil.now(); lastRun.lastOpenModeAccept = DateUtil.now(); - NSUpload.uploadDeviceStatus(lp); + NSUpload.uploadDeviceStatus(lp, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump()); sp.incInt(R.string.key_ObjectivesmanualEnacts); } rxBus.send(new EventAcceptOpenLoopChange()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java index afda878640..c27b3896c9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java @@ -33,6 +33,7 @@ import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; +import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; import info.nightscout.androidaps.plugins.aps.loop.APSResult; @@ -160,9 +161,9 @@ public class NSUpload { } } - public static void uploadDeviceStatus(LoopPlugin loopPlugin) { - Profile profile = ConfigBuilderPlugin.getPlugin().getProfileFunction().getProfile(); - String profileName = ConfigBuilderPlugin.getPlugin().getProfileFunction().getProfileName(); + public static void uploadDeviceStatus(LoopPlugin loopPlugin, IobCobCalculatorPlugin iobCobCalculatorPlugin, ProfileFunction profileFunction, PumpInterface pumpInterface) { + Profile profile = profileFunction.getProfile(); + String profileName = profileFunction.getProfileName(); if (profile == null || profileName == null) { log.error("Profile is null. Skipping upload"); @@ -204,14 +205,14 @@ public class NSUpload { } else { if (L.isEnabled(L.NSCLIENT)) log.debug("OpenAPS data too old to upload, sending iob only"); - IobTotal[] iob = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(profile); + IobTotal[] iob = iobCobCalculatorPlugin.calculateIobArrayInDia(profile); if (iob.length > 0) { deviceStatus.iob = iob[0].json(); deviceStatus.iob.put("time", DateUtil.toISOString(DateUtil.now())); } } deviceStatus.device = "openaps://" + Build.MANUFACTURER + " " + Build.MODEL; - JSONObject pumpstatus = PluginStore.Companion.getInstance().getActivePump().getJSONStatus(profile, profileName); + JSONObject pumpstatus = pumpInterface.getJSONStatus(profile, profileName); if (pumpstatus != null) { deviceStatus.pump = pumpstatus; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java index dcedddda4e..602843889c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java @@ -66,18 +66,10 @@ public class IobCobCalculatorPlugin extends PluginBase { private final SensitivityOref1Plugin sensitivityOref1Plugin; private final SensitivityAAPSPlugin sensitivityAAPSPlugin; private final SensitivityWeightedAveragePlugin sensitivityWeightedAveragePlugin; + private final FabricPrivacy fabricPrivacy; private CompositeDisposable disposable = new CompositeDisposable(); - private static IobCobCalculatorPlugin plugin = null; - - @Deprecated - public static IobCobCalculatorPlugin getPlugin() { - if (plugin == null) - throw new IllegalStateException("Accessing IobCobCalculatorPlugin before first instantiation"); - return plugin; - } - 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 @@ -102,7 +94,8 @@ public class IobCobCalculatorPlugin extends PluginBase { TreatmentsPlugin treatmentsPlugin, SensitivityOref1Plugin sensitivityOref1Plugin, SensitivityAAPSPlugin sensitivityAAPSPlugin, - SensitivityWeightedAveragePlugin sensitivityWeightedAveragePlugin + SensitivityWeightedAveragePlugin sensitivityWeightedAveragePlugin, + FabricPrivacy fabricPrivacy ) { super(new PluginDescription() .mainType(PluginType.GENERAL) @@ -112,7 +105,6 @@ public class IobCobCalculatorPlugin extends PluginBase { .alwaysEnabled(true), aapsLogger, resourceHelper, injector ); - this.plugin = this; this.injector = injector; this.sp = sp; this.rxBus = rxBus; @@ -123,6 +115,7 @@ public class IobCobCalculatorPlugin extends PluginBase { this.sensitivityOref1Plugin = sensitivityOref1Plugin; this.sensitivityAAPSPlugin = sensitivityAAPSPlugin; this.sensitivityWeightedAveragePlugin = sensitivityWeightedAveragePlugin; + this.fabricPrivacy = fabricPrivacy; } @Override @@ -140,7 +133,7 @@ public class IobCobCalculatorPlugin extends PluginBase { autosensDataTable = new LongSparseArray<>(); } runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, true, event); - }, exception -> FabricPrivacy.getInstance().logException(exception)) + }, fabricPrivacy::logException) ); // EventNewBasalProfile disposable.add(rxBus @@ -158,7 +151,7 @@ public class IobCobCalculatorPlugin extends PluginBase { basalDataTable = new LongSparseArray<>(); } runCalculation("onNewProfile", System.currentTimeMillis(), false, true, event); - }, exception -> FabricPrivacy.getInstance().logException(exception)) + }, fabricPrivacy::logException) ); // EventNewBG disposable.add(rxBus @@ -167,7 +160,7 @@ public class IobCobCalculatorPlugin extends PluginBase { .subscribe(event -> { stopCalculation("onEventNewBG"); runCalculation("onEventNewBG", System.currentTimeMillis(), true, true, event); - }, exception -> FabricPrivacy.getInstance().logException(exception)) + }, fabricPrivacy::logException) ); // EventPreferenceChange disposable.add(rxBus @@ -192,19 +185,19 @@ public class IobCobCalculatorPlugin extends PluginBase { } runCalculation("onEventPreferenceChange", System.currentTimeMillis(), false, true, event); } - }, exception -> FabricPrivacy.getInstance().logException(exception)) + }, fabricPrivacy::logException) ); // EventAppInitialized disposable.add(rxBus .toObservable(EventAppInitialized.class) .observeOn(Schedulers.io()) - .subscribe(event -> runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, true, event), exception -> FabricPrivacy.getInstance().logException(exception)) + .subscribe(event -> runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, true, event), fabricPrivacy::logException) ); // EventNewHistoryData disposable.add(rxBus .toObservable(EventNewHistoryData.class) .observeOn(Schedulers.io()) - .subscribe(this::newHistoryData, exception -> FabricPrivacy.getInstance().logException(exception)) + .subscribe(this::newHistoryData, fabricPrivacy::logException) ); } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt index 5a11c39b4f..a7d2e4e145 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt @@ -107,7 +107,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() { else if (DateUtil.isOlderThan(usedAPS.lastAPSRun, 5)) shouldUploadStatus = true if (DateUtil.isOlderThan(lastIobUpload, IOB_UPDATE_FREQUENCY) && shouldUploadStatus) { lastIobUpload = DateUtil.now() - NSUpload.uploadDeviceStatus(loopPlugin) + NSUpload.uploadDeviceStatus(loopPlugin, iobCobCalculatorPlugin, profileFunction, activePlugin.activePump) } }