This commit is contained in:
Milos Kozak 2023-10-12 15:53:53 +02:00
commit 67802d944d
5 changed files with 56 additions and 40 deletions

View file

@ -129,7 +129,7 @@ class AutomationPlugin @Inject constructor(
init { init {
refreshLoop = Runnable { refreshLoop = Runnable {
processActions() processActions()
handler.postDelayed(refreshLoop, T.mins(1).msecs()) handler.postDelayed(refreshLoop, T.secs(150).msecs())
} }
} }

View file

@ -122,6 +122,8 @@ class NSClientFragment : DaggerFragment(), MenuProvider, PluginFragment {
ID_MENU_CLEAR_LOG -> { ID_MENU_CLEAR_LOG -> {
nsClientPlugin?.listLog?.let { nsClientPlugin?.listLog?.let {
synchronized(it) { synchronized(it) {
val size = it.size
binding.recyclerview.adapter?.notifyItemRangeRemoved(0, size)
it.clear() it.clear()
updateLog() updateLog()
} }
@ -135,7 +137,7 @@ class NSClientFragment : DaggerFragment(), MenuProvider, PluginFragment {
} }
ID_MENU_SEND_NOW -> { ID_MENU_SEND_NOW -> {
nsClientPlugin?.resend("GUI") handler.post { nsClientPlugin?.resend("GUI") }
true true
} }

View file

@ -2,4 +2,4 @@ package app.aaps.plugins.sync.nsShared.events
import app.aaps.core.interfaces.rx.events.Event import app.aaps.core.interfaces.rx.events.Event
class EventConnectivityOptionChanged(val blockingReason: String) : Event() class EventConnectivityOptionChanged(val blockingReason: String, val connected: Boolean) : Event()

View file

@ -94,7 +94,7 @@ class ReceiverDelegate @Inject constructor(
if (newAllowedState != allowed) { if (newAllowedState != allowed) {
allowed = newAllowedState allowed = newAllowedState
if (allowed) blockingReason = "" if (allowed) blockingReason = ""
rxBus.send(EventConnectivityOptionChanged(blockingReason)) rxBus.send(EventConnectivityOptionChanged(blockingReason, receiverStatusStore.isConnected))
} }
} }

View file

@ -197,8 +197,13 @@ class NSClientV3Plugin @Inject constructor(
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ ev -> .subscribe({ ev ->
rxBus.send(EventNSClientNewLog("● CONNECTIVITY", ev.blockingReason)) rxBus.send(EventNSClientNewLog("● CONNECTIVITY", ev.blockingReason))
setClient("CONNECTIVITY") if (ev.connected) {
when {
isAllowed && storageSocket == null -> setClient("CONNECTIVITY") // socket must be created
!isAllowed && storageSocket != null -> shutdownWebsockets()
}
if (isAllowed) executeLoop("CONNECTIVITY", forceNew = false) if (isAllowed) executeLoop("CONNECTIVITY", forceNew = false)
}
rxBus.send(EventNSClientUpdateGuiStatus()) rxBus.send(EventNSClientUpdateGuiStatus())
}, fabricPrivacy::logException) }, fabricPrivacy::logException)
disposable += rxBus disposable += rxBus
@ -211,8 +216,10 @@ class NSClientV3Plugin @Inject constructor(
ev.isChanged(rh.gs(R.string.key_ns_paused)) || 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_alarms)) ||
ev.isChanged(rh.gs(app.aaps.core.utils.R.string.key_ns_announcements)) ev.isChanged(rh.gs(app.aaps.core.utils.R.string.key_ns_announcements))
) ) {
shutdownWebsockets()
setClient("SETTING CHANGE") setClient("SETTING CHANGE")
}
if (ev.isChanged(rh.gs(app.aaps.core.utils.R.string.key_local_profile_last_change))) if (ev.isChanged(rh.gs(app.aaps.core.utils.R.string.key_local_profile_last_change)))
executeUpload("PROFILE_CHANGE", forceNew = true) executeUpload("PROFILE_CHANGE", forceNew = true)
@ -299,10 +306,7 @@ class NSClientV3Plugin @Inject constructor(
override fun onStop() { override fun onStop() {
handler.removeCallbacksAndMessages(null) handler.removeCallbacksAndMessages(null)
disposable.clear() disposable.clear()
storageSocket?.disconnect() shutdownWebsockets()
alarmSocket?.disconnect()
storageSocket = null
alarmSocket = null
super.onStop() super.onStop()
} }
@ -331,19 +335,14 @@ class NSClientV3Plugin @Inject constructor(
} }
private fun setClient(reason: String) { private fun setClient(reason: String) {
if (nsAndroidClient == null)
nsAndroidClient = NSAndroidClientImpl( nsAndroidClient = NSAndroidClientImpl(
baseUrl = sp.getString(app.aaps.core.utils.R.string.key_nsclientinternal_url, "").lowercase().replace("https://", "").replace(Regex("/$"), ""), 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, ""), accessToken = sp.getString(R.string.key_ns_client_token, ""),
context = context, context = context,
logging = true, logging = config.isEngineeringMode() || config.isDev(),
logger = { msg -> aapsLogger.debug(LTag.HTTP, msg) } logger = { msg -> aapsLogger.debug(LTag.HTTP, msg) }
) )
if (wsConnected) {
storageSocket?.disconnect()
alarmSocket?.disconnect()
storageSocket = null
alarmSocket = null
}
SystemClock.sleep(2000) SystemClock.sleep(2000)
initializeWebSockets(reason) initializeWebSockets(reason)
rxBus.send(EventSWSyncStatus(status)) rxBus.send(EventSWSyncStatus(status))
@ -354,8 +353,28 @@ class NSClientV3Plugin @Inject constructor(
**********************/ **********************/
private var storageSocket: Socket? = null private var storageSocket: Socket? = null
private var alarmSocket: Socket? = null private var alarmSocket: Socket? = null
var wsConnected = false internal var wsConnected = false
internal var initialLoadFinished = 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) { private fun initializeWebSockets(reason: String) {
if (!sp.getBoolean(app.aaps.core.utils.R.string.key_ns_use_ws, true)) return 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 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")) rxBus.send(EventNSClientNewLog("► WS", "requesting auth for alarms"))
socket.emit("subscribe", authMessage, Ack { args -> socket.emit("subscribe", authMessage, Ack { args ->
val response = args[0] as JSONObject val response = args[0] as JSONObject
wsConnected = if (response.optBoolean("success")) { if (response.optBoolean("success")) rxBus.send(EventNSClientNewLog("◄ WS", response.optString("message")))
rxBus.send(EventNSClientNewLog("◄ WS", response.optString("message"))) else rxBus.send(EventNSClientNewLog("◄ WS", "Auth failed"))
true
} else {
rxBus.send(EventNSClientNewLog("◄ WS", "Auth failed"))
false
}
}) })
} }
} }