From b870de1e078696a842b934287edf09c7754b313d Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 11 Oct 2023 21:09:57 +0200 Subject: [PATCH 1/2] NSCv3: improve WS connection --- .../plugins/sync/nsShared/NSClientFragment.kt | 4 +- .../events/EventConnectivityOptionChanged.kt | 2 +- .../plugins/sync/nsclient/ReceiverDelegate.kt | 2 +- .../sync/nsclientV3/NSClientV3Plugin.kt | 86 +++++++++++-------- 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/NSClientFragment.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/NSClientFragment.kt index b3b0d61e12..25a2d0eb3a 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/NSClientFragment.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/NSClientFragment.kt @@ -122,6 +122,8 @@ class NSClientFragment : DaggerFragment(), MenuProvider, PluginFragment { ID_MENU_CLEAR_LOG -> { nsClientPlugin?.listLog?.let { synchronized(it) { + val size = it.size + binding.recyclerview.adapter?.notifyItemRangeRemoved(0, size) it.clear() updateLog() } @@ -135,7 +137,7 @@ class NSClientFragment : DaggerFragment(), MenuProvider, PluginFragment { } ID_MENU_SEND_NOW -> { - nsClientPlugin?.resend("GUI") + handler.post { nsClientPlugin?.resend("GUI") } true } diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/events/EventConnectivityOptionChanged.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/events/EventConnectivityOptionChanged.kt index eabc9b4813..4240e5ce85 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/events/EventConnectivityOptionChanged.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/events/EventConnectivityOptionChanged.kt @@ -2,4 +2,4 @@ package app.aaps.plugins.sync.nsShared.events import app.aaps.core.interfaces.rx.events.Event -class EventConnectivityOptionChanged(val blockingReason: String) : Event() \ No newline at end of file +class EventConnectivityOptionChanged(val blockingReason: String, val connected: Boolean) : Event() \ No newline at end of file diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclient/ReceiverDelegate.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclient/ReceiverDelegate.kt index cf20d81516..cceb6490f0 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclient/ReceiverDelegate.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclient/ReceiverDelegate.kt @@ -94,7 +94,7 @@ class ReceiverDelegate @Inject constructor( if (newAllowedState != allowed) { allowed = newAllowedState if (allowed) blockingReason = "" - rxBus.send(EventConnectivityOptionChanged(blockingReason)) + rxBus.send(EventConnectivityOptionChanged(blockingReason, receiverStatusStore.isConnected)) } } diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/NSClientV3Plugin.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/NSClientV3Plugin.kt index ae351b9260..66432b7e78 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/NSClientV3Plugin.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/NSClientV3Plugin.kt @@ -156,16 +156,16 @@ class NSClientV3Plugin @Inject constructor( override val status get() = when { - sp.getBoolean(R.string.key_ns_paused, false) -> rh.gs(app.aaps.core.ui.R.string.paused) - isAllowed.not() -> blockingReason + sp.getBoolean(R.string.key_ns_paused, false) -> rh.gs(app.aaps.core.ui.R.string.paused) + isAllowed.not() -> blockingReason sp.getBoolean(app.aaps.core.utils.R.string.key_ns_use_ws, true) && wsConnected -> "WS: " + rh.gs(app.aaps.core.interfaces.R.string.connected) sp.getBoolean(app.aaps.core.utils.R.string.key_ns_use_ws, true) && !wsConnected -> "WS: " + rh.gs(R.string.not_connected) - lastOperationError != null -> rh.gs(app.aaps.core.ui.R.string.error) - nsAndroidClient?.lastStatus == null -> rh.gs(R.string.not_connected) - workIsRunning() -> rh.gs(R.string.working) - nsAndroidClient?.lastStatus?.apiPermissions?.isFull() == true -> rh.gs(app.aaps.core.interfaces.R.string.connected) - nsAndroidClient?.lastStatus?.apiPermissions?.isRead() == true -> rh.gs(R.string.read_only) - else -> rh.gs(app.aaps.core.ui.R.string.unknown) + lastOperationError != null -> rh.gs(app.aaps.core.ui.R.string.error) + nsAndroidClient?.lastStatus == null -> rh.gs(R.string.not_connected) + workIsRunning() -> rh.gs(R.string.working) + nsAndroidClient?.lastStatus?.apiPermissions?.isFull() == true -> rh.gs(app.aaps.core.interfaces.R.string.connected) + nsAndroidClient?.lastStatus?.apiPermissions?.isRead() == true -> rh.gs(R.string.read_only) + else -> rh.gs(app.aaps.core.ui.R.string.unknown) } var lastOperationError: String? = null @@ -197,8 +197,13 @@ class NSClientV3Plugin @Inject constructor( .observeOn(aapsSchedulers.io) .subscribe({ ev -> rxBus.send(EventNSClientNewLog("● CONNECTIVITY", ev.blockingReason)) - setClient("CONNECTIVITY") - if (isAllowed) executeLoop("CONNECTIVITY", forceNew = false) + if (ev.connected) { + when { + isAllowed && storageSocket == null -> setClient("CONNECTIVITY") // socket must be created + !isAllowed && storageSocket != null -> shutdownWebsockets() + } + if (isAllowed) executeLoop("CONNECTIVITY", forceNew = false) + } rxBus.send(EventNSClientUpdateGuiStatus()) }, fabricPrivacy::logException) disposable += rxBus @@ -211,8 +216,10 @@ class NSClientV3Plugin @Inject constructor( ev.isChanged(rh.gs(R.string.key_ns_paused)) || ev.isChanged(rh.gs(app.aaps.core.utils.R.string.key_ns_alarms)) || ev.isChanged(rh.gs(app.aaps.core.utils.R.string.key_ns_announcements)) - ) + ) { + shutdownWebsockets() setClient("SETTING CHANGE") + } if (ev.isChanged(rh.gs(app.aaps.core.utils.R.string.key_local_profile_last_change))) executeUpload("PROFILE_CHANGE", forceNew = true) @@ -299,10 +306,7 @@ class NSClientV3Plugin @Inject constructor( override fun onStop() { handler.removeCallbacksAndMessages(null) disposable.clear() - storageSocket?.disconnect() - alarmSocket?.disconnect() - storageSocket = null - alarmSocket = null + shutdownWebsockets() super.onStop() } @@ -331,19 +335,14 @@ class NSClientV3Plugin @Inject constructor( } private fun setClient(reason: String) { - nsAndroidClient = NSAndroidClientImpl( - baseUrl = sp.getString(app.aaps.core.utils.R.string.key_nsclientinternal_url, "").lowercase().replace("https://", "").replace(Regex("/$"), ""), - accessToken = sp.getString(R.string.key_ns_client_token, ""), - context = context, - logging = true, - logger = { msg -> aapsLogger.debug(LTag.HTTP, msg) } - ) - if (wsConnected) { - storageSocket?.disconnect() - alarmSocket?.disconnect() - storageSocket = null - alarmSocket = null - } + if (nsAndroidClient == null) + nsAndroidClient = NSAndroidClientImpl( + baseUrl = sp.getString(app.aaps.core.utils.R.string.key_nsclientinternal_url, "").lowercase().replace("https://", "").replace(Regex("/$"), ""), + accessToken = sp.getString(R.string.key_ns_client_token, ""), + context = context, + logging = config.isEngineeringMode() || config.isDev(), + logger = { msg -> aapsLogger.debug(LTag.HTTP, msg) } + ) SystemClock.sleep(2000) initializeWebSockets(reason) rxBus.send(EventSWSyncStatus(status)) @@ -354,8 +353,28 @@ class NSClientV3Plugin @Inject constructor( **********************/ private var storageSocket: Socket? = null private var alarmSocket: Socket? = null - var wsConnected = false + internal var wsConnected = false internal var initialLoadFinished = false + + private fun shutdownWebsockets() { + storageSocket?.on(Socket.EVENT_CONNECT, onConnectStorage) + storageSocket?.on(Socket.EVENT_DISCONNECT, onDisconnectStorage) + storageSocket?.on("create", onDataCreateUpdate) + storageSocket?.on("update", onDataCreateUpdate) + storageSocket?.on("delete", onDataDelete) + storageSocket?.disconnect() + alarmSocket?.on(Socket.EVENT_CONNECT, onConnectAlarms) + alarmSocket?.on(Socket.EVENT_DISCONNECT, onDisconnectAlarm) + alarmSocket?.on("announcement", onAnnouncement) + alarmSocket?.on("alarm", onAlarm) + alarmSocket?.on("urgent_alarm", onUrgentAlarm) + alarmSocket?.on("clear_alarm", onClearAlarm) + alarmSocket?.disconnect() + wsConnected = false + storageSocket = null + alarmSocket = null + } + private fun initializeWebSockets(reason: String) { if (!sp.getBoolean(app.aaps.core.utils.R.string.key_ns_use_ws, true)) return if (sp.getString(app.aaps.core.utils.R.string.key_nsclientinternal_url, "").isEmpty()) return @@ -435,13 +454,8 @@ class NSClientV3Plugin @Inject constructor( rxBus.send(EventNSClientNewLog("► WS", "requesting auth for alarms")) socket.emit("subscribe", authMessage, Ack { args -> val response = args[0] as JSONObject - wsConnected = if (response.optBoolean("success")) { - rxBus.send(EventNSClientNewLog("◄ WS", response.optString("message"))) - true - } else { - rxBus.send(EventNSClientNewLog("◄ WS", "Auth failed")) - false - } + if (response.optBoolean("success")) rxBus.send(EventNSClientNewLog("◄ WS", response.optString("message"))) + else rxBus.send(EventNSClientNewLog("◄ WS", "Auth failed")) }) } } From 6b747d40f8f694d9f83b508069af8c9f42ac91ae Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 11 Oct 2023 21:19:55 +0200 Subject: [PATCH 2/2] Automation: periodic 1 -> 2.5 min --- .../main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt index 89e821b52b..64a3a11bd5 100644 --- a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt @@ -129,7 +129,7 @@ class AutomationPlugin @Inject constructor( init { refreshLoop = Runnable { processActions() - handler.postDelayed(refreshLoop, T.mins(1).msecs()) + handler.postDelayed(refreshLoop, T.secs(150).msecs()) } }