Skip to content

Commit

Permalink
Improve SSE error handling
Browse files Browse the repository at this point in the history
Avoid ANR by not trying to get events from a closed subscription. Instead wait 5 seconds and create a new channel then.

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jul 2, 2024
1 parent 143fe53 commit e07ae74
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
6 changes: 1 addition & 5 deletions mobile/src/main/java/org/openhab/habdroid/ui/DayDream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ class DayDream : DreamService(), CoroutineScope {
getString(R.string.screensaver_error_loading_item, item)
}

ItemClient.listenForItemChange(
this,
connection,
item
) { _, payload ->
ItemClient.listenForItemChange(this, connection, item) { _, payload ->
val state = payload.getString("value")
Log.d(TAG, "Got state by event: $state")
textView.text = state
Expand Down
6 changes: 1 addition & 5 deletions mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1488,11 +1488,7 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener {
}

private suspend fun listenUiCommandItem(item: String) {
ItemClient.listenForItemChange(
this,
connection ?: return,
item
) { _, payload ->
ItemClient.listenForItemChange(this, connection ?: return, item) { _, payload ->
val state = payload.getString("value")
Log.d(TAG, "Got state by event: $state")
handleUiCommand(state)
Expand Down
14 changes: 10 additions & 4 deletions mobile/src/main/java/org/openhab/habdroid/util/ItemClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import java.io.IOException
import java.io.StringReader
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.ParserConfigurationException
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import org.json.JSONArray
import org.json.JSONException
Expand Down Expand Up @@ -109,10 +111,11 @@ object ItemClient {
item: String,
callback: (topicPath: List<String>, payload: JSONObject) -> Unit
) {
val eventSubscription = connection.httpClient.makeSse(
// Support for both the "openhab" and the older "smarthome" root topic by using a wildcard
connection.httpClient.buildUrl("rest/events?topics=*/items/$item/command")
)
fun createSubscription() = connection.httpClient.makeSse(
// Support for both the "openhab" and the older "smarthome" root topic by using a wildcard
connection.httpClient.buildUrl("rest/events?topics=*/items/$item/command")
)
var eventSubscription = createSubscription()

try {
while (scope.isActive) {
Expand All @@ -139,6 +142,9 @@ object ItemClient {
Log.e(TAG, "Failed parsing JSON of state change event for item $item", e)
} catch (e: HttpClient.SseFailureException) {
Log.e(TAG, "SSE failure for item $item", e)
eventSubscription.cancel()
delay(5.seconds)
eventSubscription = createSubscription()
}
}
} finally {
Expand Down

0 comments on commit e07ae74

Please sign in to comment.