From 853c08e5ef061a625eeb37f4edc24711bccd9676 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 19 Feb 2023 22:09:11 +0100 Subject: [PATCH] NSCv3: improve scheduling --- .../nsShared/events/EventNSClientResend.kt | 5 ---- .../plugins/sync/nsclient/NSClientPlugin.kt | 5 ---- .../sync/nsclientV3/NSClientV3Plugin.kt | 30 +++++++++---------- 3 files changed, 14 insertions(+), 26 deletions(-) delete mode 100644 plugins/sync/src/main/java/info/nightscout/plugins/sync/nsShared/events/EventNSClientResend.kt diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsShared/events/EventNSClientResend.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsShared/events/EventNSClientResend.kt deleted file mode 100644 index 506561809c..0000000000 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsShared/events/EventNSClientResend.kt +++ /dev/null @@ -1,5 +0,0 @@ -package info.nightscout.plugins.sync.nsShared.events - -import info.nightscout.rx.events.Event - -class EventNSClientResend(val reason: String) : Event() \ No newline at end of file diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclient/NSClientPlugin.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclient/NSClientPlugin.kt index 01421ddc4a..a494f22ed8 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclient/NSClientPlugin.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclient/NSClientPlugin.kt @@ -27,7 +27,6 @@ import info.nightscout.interfaces.sync.NsClient import info.nightscout.interfaces.sync.Sync import info.nightscout.plugins.sync.R import info.nightscout.plugins.sync.nsShared.NSClientFragment -import info.nightscout.plugins.sync.nsShared.events.EventNSClientResend import info.nightscout.plugins.sync.nsShared.events.EventNSClientStatus import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGuiData import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGuiStatus @@ -113,10 +112,6 @@ class NSClientPlugin @Inject constructor( addToLog(event) aapsLogger.debug(LTag.NSCLIENT, event.action + " " + event.logText) }, fabricPrivacy::logException) - disposable += rxBus - .toObservable(EventNSClientResend::class.java) - .observeOn(aapsSchedulers.io) - .subscribe({ event -> resend(event.reason) }, fabricPrivacy::logException) } override fun onStop() { diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt index 0196704a0a..19fe7f2266 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt @@ -38,7 +38,6 @@ import info.nightscout.plugins.sync.R import info.nightscout.plugins.sync.nsShared.NSClientFragment import info.nightscout.plugins.sync.nsShared.NsIncomingDataProcessor import info.nightscout.plugins.sync.nsShared.events.EventConnectivityOptionChanged -import info.nightscout.plugins.sync.nsShared.events.EventNSClientResend import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGuiData import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGuiStatus import info.nightscout.plugins.sync.nsclient.ReceiverDelegate @@ -223,10 +222,6 @@ class NSClientV3Plugin @Inject constructor( addToLog(event) aapsLogger.debug(LTag.NSCLIENT, event.action + " " + event.logText) }, fabricPrivacy::logException) - disposable += rxBus - .toObservable(EventNSClientResend::class.java) - .observeOn(aapsSchedulers.io) - .subscribe({ event -> resend(event.reason) }, fabricPrivacy::logException) disposable += rxBus .toObservable(EventNewHistoryData::class.java) .observeOn(aapsSchedulers.io) @@ -234,17 +229,20 @@ class NSClientV3Plugin @Inject constructor( runLoop = Runnable { var refreshInterval = T.mins(5).msecs() - repository.getLastGlucoseValueWrapped().blockingGet().let { - // if last value is older than 5 min or there is no bg - if (it is ValueWrapper.Existing) { - if (it.value.timestamp < dateUtil.now() - T.mins(5).plus(T.secs(20)).msecs()) { - refreshInterval = T.mins(1).msecs() - executeLoop("MAIN_LOOP", forceNew = true) + if (nsClientSource.isEnabled()) + repository.getLastGlucoseValueWrapped().blockingGet().let { + // if last value is older than 5 min or there is no bg + if (it is ValueWrapper.Existing) { + if (it.value.timestamp < dateUtil.now() - T.mins(5).plus(T.secs(20)).msecs()) { + refreshInterval = T.mins(1).msecs() + } } - } else executeLoop("MAIN_LOOP", forceNew = true) - } + } + if (!sp.getBoolean(info.nightscout.core.utils.R.string.key_ns_use_ws, true)) + executeLoop("MAIN_LOOP", forceNew = true) + else + rxBus.send(EventNSClientNewLog("● TICK", "")) handler.postDelayed(runLoop, refreshInterval) - rxBus.send(EventNSClientNewLog("● TICK", "")) } handler.postDelayed(runLoop, T.mins(2).msecs()) } @@ -554,9 +552,9 @@ class NSClientV3Plugin @Inject constructor( override fun resend(reason: String) { if (sp.getBoolean(info.nightscout.core.utils.R.string.key_ns_use_ws, true)) - executeUpload("RESEND", forceNew = false) + executeUpload("START $reason", forceNew = true) else - executeLoop("RESEND", forceNew = false) + executeLoop("START $reason", forceNew = true) } override fun pause(newState: Boolean) {