Web Sockets - how to get external data into an item

Tags: #<Tag:0x00007f5c9593c7b0> #<Tag:0x00007f5c9593c5a8> #<Tag:0x00007f5c9593c4b8>
  • Platform information:
    • Hardware: _x64 Intel
    • OS: Ubuntu 18.04.4 LTS
    • Java Runtime Environment: Zulu 8 amd64
    • openHAB version: 2.5.4

Hi,

I know very little about web sockets, but I have managed to show connectivity between my OH machine and a third-party service that provides a web socket showing data from my alarm system.

If I run wscat from the command line on my OH machine, I see the following live events:

$ wscat -c ws://alarmgw:8088/ws/spc
Connected (press CTRL+C to quit)
< {"status":"success","data":{"sia":{"device_id":"xxxxx","timestamp":"1587394004","sia_code":"ZO","sia_address":"2","description":"Beam Hall¦ZONE¦1¦Home","flags":"","verification_id":"0"}}}
< {"status":"success","data":{"sia":{"device_id":"xxxxx","timestamp":"1587394010","sia_code":"ZC","sia_address":"2","description":"Beam Hall¦ZONE¦1¦Home","flags":"","verification_id":"0"}}}
< {"status":"success","data":{"sia":{"device_id":"xxxxx","timestamp":"1587394013","sia_code":"ZO","sia_address":"4","description":"Back Area¦ZONE¦1¦Home","flags":"","verification_id":"0"}}}
< {"status":"success","data":{"sia":{"device_id":"xxxxx","timestamp":"1587394016","sia_code":"ZC","sia_address":"4","description":"Back Area¦ZONE¦1¦Home","flags":"","verification_id":"0"}}}
> $

As per the documentation for the alarm gateway, I have the following basic config in a transform file:

var websocket_client = require('websocket').client;

// Connect to SPC Web Gateway websocket interface on alarmgw:8088
var ws_client = new websocket_client();
ws_client.connect('ws://alarmgw:8088/ws/spc');

ws_client.on('connectFailed', function(error) {
        console.log('Connect Error: ' + error.toString());
});

ws_client.on('connect', function(connection) {
        console.log('WebSocket client connected');
        connection.on('error', function(error) {
                console.log("Connection Error: " + error.toString());
        });
        connection.on('close', function()
                { console.log('echo-protocol Connection Closed');
        });
        connection.on('message', function(message)
                { if (message.type === 'utf8') {
                        manageSiaEvent(message.utf8Data);
                }
        });
});

// manageSiaEvent
function manageSiaEvent(message){
        var msg = JSON.parse(message);
        if (msg.status === 'success') {
                var sia_code = msg.data.sia.sia_code;

                // Take care of SIA event action
                switch (sia_code){
                        case 'BA': /* Burglar Alarm */
                                break;
                        case 'BR': /* Burglar Alarm Restore */
                                break;
                        case 'BB': /* Inhibited or Isolated */
                                break;
                        case 'ZO': /* Zone Opened */
                                break;
                        case 'ZC': /* Zone Closed */
                                break;
                }
        }
}

I’m stuck as to how to proceed - does anyone know how I can get this data into an item?

It’s not clear to me, is this running as a separate program or is this running as a JavaScript OH Rule?

Hi Rich, to be perfectly honest, I’m not sure. I’m trying to cobble this together under a lack of understanding of web sockets and javascript. The code above is from the gateway device documentation and that says:

“This is a code example written in JavaScript showing how to establish and read messages on a SPC
Web Gateway WebSocket connection. The code is based on node.js and websocket.js.”

I’m not sure if it should all be in a transform or if part should be in a rule. I know that I also need to create an item but I don’t know how to specify the channel for the data.

I think what you need to do is create a separate program running on Node.js (if you want to use that code snippet) to subscribe to the websocket. Then that separate program will use openHAB’s REST API or MQTT or whatever to publish new JSON messages to OH. If using the REST API, the Item will not be linked to any Channels. If using MQTT, you would create MQTT Things and link the Item to the appropriate MQTT Channel.

Thanks @rlkoshak - I got it working using the REST API. Job done! :smiley: