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 399667f
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,20 @@ class ItemsControlsProviderService : ControlsProviderService() {
}
val topic = event.getString("topic")
val topicPath = topic.split('/')
if (topicPath.size != 4) {
/* Possible formats:
* - openhab/items/<item>/statechanged
* - openhab/items/<group item>/<item>/statechanged
* When an update for a group is sent, there's also one for the individual item.
* Therefore always take the element on index two.
*/
if (topicPath.size !in intArrayOf(4, 5)) {
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())
Log.e("topic", "item: ${item.name}, state: ${newItem.state?.asString}")
factory.maybeCreateControl(newItem)?.let { control -> send(control) }
}
} catch (e: JSONException) {
Expand Down

0 comments on commit 399667f

Please sign in to comment.