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 a9543a679c..073ef44b3a 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 @@ -204,7 +204,7 @@ class NSClientV3Plugin @Inject constructor( if (ev.connected) { when { isAllowed && nsClientV3Service?.storageSocket == null -> setClient("CONNECTIVITY") // socket must be created - !isAllowed && nsClientV3Service?.storageSocket != null -> shutdownWebsockets() + !isAllowed && nsClientV3Service?.storageSocket != null -> stopService() } if (isAllowed) executeLoop("CONNECTIVITY", forceNew = false) } @@ -221,7 +221,7 @@ class NSClientV3Plugin @Inject constructor( 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() + stopService() setClient("SETTING CHANGE") } if (ev.isChanged(rh.gs(app.aaps.core.utils.R.string.key_local_profile_last_change))) @@ -310,7 +310,7 @@ class NSClientV3Plugin @Inject constructor( override fun onStop() { handler.removeCallbacksAndMessages(null) disposable.clear() - shutdownWebsockets() + stopService() super.onStop() } @@ -348,24 +348,18 @@ class NSClientV3Plugin @Inject constructor( logger = { msg -> aapsLogger.debug(LTag.HTTP, msg) } ) SystemClock.sleep(2000) - initializeWebSockets(reason) + startService(reason) rxBus.send(EventSWSyncStatus(status)) } - private fun initializeWebSockets(reason: String) { + private fun startService(reason: String) { if (sp.getBoolean(app.aaps.core.utils.R.string.key_ns_use_ws, true)) { context.bindService(Intent(context, NSClientV3Service::class.java), serviceConnection, Context.BIND_AUTO_CREATE) - while (nsClientV3Service == null) { - aapsLogger.debug(LTag.NSCLIENT, "Waiting for service start") - SystemClock.sleep(100) - } - nsClientV3Service?.initializeWebSockets(reason) } } - private fun shutdownWebsockets() { + private fun stopService() { if (nsClientV3Service != null) context.unbindService(serviceConnection) - nsClientV3Service?.shutdownWebsockets() } override fun resend(reason: String) { diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/services/NSClientV3Service.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/services/NSClientV3Service.kt index 1cd66a9783..e2e766cecf 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/services/NSClientV3Service.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/services/NSClientV3Service.kt @@ -70,10 +70,12 @@ class NSClientV3Service : DaggerService() { super.onCreate() wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidAPS:NSClientService") wakeLock?.acquire() + initializeWebSockets("onCreate") } override fun onDestroy() { super.onDestroy() + shutdownWebsockets() disposable.clear() if (wakeLock?.isHeld == true) wakeLock?.release() }