From 394c704cb71f8c7b8bd323614df078bbca862b9f Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Mon, 10 Mar 2025 15:13:50 +0100 Subject: [PATCH] Event WS client: Filter by topic instead of type (#2997) Depends on https://github.com/openhab/openhab-core/pull/4550. Signed-off-by: Florian Hotze --- .../org.openhab.ui/web/src/js/openhab/ws.js | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/js/openhab/ws.js b/bundles/org.openhab.ui/web/src/js/openhab/ws.js index ec4270f7b4..6d6a1c1dda 100644 --- a/bundles/org.openhab.ui/web/src/js/openhab/ws.js +++ b/bundles/org.openhab.ui/web/src/js/openhab/ws.js @@ -23,7 +23,7 @@ function arrayToSerialisedString (arr) { * Build a event source filter message for the given WebSocket client id and the given sources. * Source filters can be used to remove events from a specific source from the event WS. * @param {string} id WS client id - * @param {string[]} sources event sources to filter out + * @param {string[]} sources event sources to exclude * @return {string} */ function eventSourceFilterMessage (id, sources) { @@ -39,7 +39,7 @@ function eventSourceFilterMessage (id, sources) { * Build an event type filter message for the given WebSocket client id and the given event types. * Event type filters can be used to select a sub-set of all available events for the event WS. * @param {string} id WS client id - * @param types + * @param {string[]} types event types to include * @return {string} */ function eventTypeFilterMessage (id, types) { @@ -51,6 +51,22 @@ function eventTypeFilterMessage (id, types) { }) } +/** + * Build an event topic filter message for the given WebSocket client id and the given event topics. + * Event topic filters can be used to select a sub-set of all available events for the event WS. + * @param {string} id WS client id + * @param {string[]} topics event topics to include + * @returns {string} + */ +function eventTopicFilterMesssage (id, topics) { + return JSON.stringify({ + type: 'WebSocketEvent', + topic: 'openhab/websocket/filter/topic', + payload: arrayToSerialisedString(topics), + source: id + }) +} + const openWSConnections = [] function newWSConnection (path, messageCallback, readyCallback, errorCallback, heartbeatCallback, heartbeatInterval) { @@ -128,13 +144,13 @@ export default { * Connect to the event WebSocket, which provides direct access to the EventBus. * This convenience method takes care of the keepalive mechanism as well as filter setup. * - * @param {string[]} types array of event types to filter by, if empty all events are received + * @param {string[]} topics array of event topics to filter by, if empty all events are received * @param {fn} messageCallback message callback to handle incoming messages * @param {fn} [readyCallback] ready callback * @param {fn} [errorCallback] error callback * @return {WebSocket} */ - events (types, messageCallback, readyCallback, errorCallback) { + events (topics, messageCallback, readyCallback, errorCallback) { let socket const extendedMessageCallback = (event) => { @@ -144,7 +160,7 @@ export default { const extendedReadyCallback = (event) => { socket.send(eventSourceFilterMessage(socket.id, [socket.id])) - if (Array.isArray(types) && types.length > 0) socket.send(eventTypeFilterMessage(socket.id, types)) + if (Array.isArray(topics) && topics.length > 0) socket.send(eventTopicFilterMesssage(socket.id, topics)) if (readyCallback) readyCallback(event) }