Event WS client: Filter by topic instead of type
Depends on https://github.com/openhab/openhab-core/pull/4550. Signed-off-by: Florian Hotze <dev@florianhotze.com>pull/2997/head
parent
a9954c3ee9
commit
37e5129f25
|
@ -23,7 +23,7 @@ function arrayToSerialisedString (arr) {
|
||||||
* Build a event source filter message for the given WebSocket client id and the given sources.
|
* 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.
|
* Source filters can be used to remove events from a specific source from the event WS.
|
||||||
* @param {string} id WS client id
|
* @param {string} id WS client id
|
||||||
* @param {string[]} sources event sources to filter out
|
* @param {string[]} sources event sources to exclude
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
function eventSourceFilterMessage (id, sources) {
|
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.
|
* 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.
|
* 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 {string} id WS client id
|
||||||
* @param types
|
* @param {string[]} types event types to include
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
function eventTypeFilterMessage (id, types) {
|
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 = []
|
const openWSConnections = []
|
||||||
|
|
||||||
function newWSConnection (path, messageCallback, readyCallback, errorCallback, heartbeatCallback, heartbeatInterval) {
|
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.
|
* 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.
|
* 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} messageCallback message callback to handle incoming messages
|
||||||
* @param {fn} [readyCallback] ready callback
|
* @param {fn} [readyCallback] ready callback
|
||||||
* @param {fn} [errorCallback] error callback
|
* @param {fn} [errorCallback] error callback
|
||||||
* @return {WebSocket}
|
* @return {WebSocket}
|
||||||
*/
|
*/
|
||||||
events (types, messageCallback, readyCallback, errorCallback) {
|
events (topics, messageCallback, readyCallback, errorCallback) {
|
||||||
let socket
|
let socket
|
||||||
|
|
||||||
const extendedMessageCallback = (event) => {
|
const extendedMessageCallback = (event) => {
|
||||||
|
@ -144,7 +160,7 @@ export default {
|
||||||
|
|
||||||
const extendedReadyCallback = (event) => {
|
const extendedReadyCallback = (event) => {
|
||||||
socket.send(eventSourceFilterMessage(socket.id, [socket.id]))
|
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)
|
if (readyCallback) readyCallback(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue