Skip to content

Commit

Permalink
Event WS client: Filter by topic instead of type
Browse files Browse the repository at this point in the history
Depends on openhab/openhab-core#4550.

Signed-off-by: Florian Hotze <dev@florianhotze.com>
  • Loading branch information
florian-h05 committed Jan 9, 2025
1 parent a9954c3 commit 37e5129
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions bundles/org.openhab.ui/web/src/js/openhab/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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)
}

Expand Down

0 comments on commit 37e5129

Please sign in to comment.