From 00db5d10ea9fb0271589b86a4671d3a299cb664c Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Thu, 19 Oct 2023 19:24:54 +0200 Subject: [PATCH] Fix parsing of SSE topic for groups There can be an optional group in the topic path. Signed-off-by: mueller-ma --- .../ItemsControlsProviderService.kt | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/mobile/src/main/java/org/openhab/habdroid/background/ItemsControlsProviderService.kt b/mobile/src/main/java/org/openhab/habdroid/background/ItemsControlsProviderService.kt index e97d33693d1..9fdb865bca7 100644 --- a/mobile/src/main/java/org/openhab/habdroid/background/ItemsControlsProviderService.kt +++ b/mobile/src/main/java/org/openhab/habdroid/background/ItemsControlsProviderService.kt @@ -96,15 +96,14 @@ class ItemsControlsProviderService : ControlsProviderService() { continue } val topic = event.getString("topic") - val topicPath = topic.split('/') - if (topicPath.size != 4) { - throw JSONException("Unexpected topic path $topic") - } - val item = allItems[topicPath[2]] - if (item != null) { - val payload = JSONObject(event.getString("payload")) - val newItem = item.copy(state = payload.getString("value").toParsedState()) - factory.maybeCreateControl(newItem)?.let { control -> send(control) } + val items = getItemsFromTopic(topic) + items.forEach { + val item = allItems[it] + if (item != null) { + val payload = JSONObject(event.getString("payload")) + val newItem = item.copy(state = payload.getString("value").toParsedState()) + factory.maybeCreateControl(newItem)?.let { control -> send(control) } + } } } catch (e: JSONException) { Log.e(TAG, "Failed parsing JSON of state change event", e) @@ -115,6 +114,22 @@ class ItemsControlsProviderService : ControlsProviderService() { } } + /** + * Get items from topic string. + * Possible formats: + * openhab/items//statechanged + * openhab/items///statechanged + */ + private fun getItemsFromTopic(topic: String): List { + val topicPath = topic.split('/') + if (topicPath.size !in intArrayOf(4, 5)) { + throw JSONException("Unexpected topic path $topic") + } + return topicPath + .drop(2) + .dropLast(1) + } + override fun performControlAction(controlId: String, action: ControlAction, consumer: Consumer) { GlobalScope.launch { ConnectionFactory.waitForInitialization()