Skip to content

Commit

Permalink
Fix parsing of SSE topic for groups
Browse files Browse the repository at this point in the history
There can be an optional group in the topic path.

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Oct 19, 2023
1 parent 2ba26cf commit 00db5d1
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -115,6 +114,22 @@ class ItemsControlsProviderService : ControlsProviderService() {
}
}

/**
* Get items from topic string.
* Possible formats:
* openhab/items/<item>/statechanged
* openhab/items/<group item>/<item>/statechanged
*/
private fun getItemsFromTopic(topic: String): List<String> {
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<Int>) {
GlobalScope.launch {
ConnectionFactory.waitForInitialization()
Expand Down

0 comments on commit 00db5d1

Please sign in to comment.