Receive a VSCP Event from node.js

Here a very simple script that receives events from the VSCP daemon over the TCP/IP interface. The scripts is listed at the end of this document. The scripts is available in the source tree (/src/vscp/samples/node_js/receive_tcpip_server.js) here

You have a send event sample here

You start the event listning with

node receive_tcpip_server.js

You can add 'host' 'port' 'user' 'password' as arguments if you want.

What you will see is a screen that is filled with “+OK” which is something the VSCP daemon send on common intervals to show that the link is open. If events are received they will also be shown here so if you have a thermometer connected you will get readings from it in this stream.

If you don't have anything connected to your server you can send some events yourself using the TCP/IP interface. Open a TCP/IP session to the server

telnet localhost 9598 

after logging in with

user admin<CR><LF>
pass secret<CR><LF>

then send events with

send 0,20,40,0,0,"-",50,1,20

the format is head,vscp-class,vscp-type, obid, timestamp, GUID, data… here we use “-” for GUID (id of the node) which says thar the GUID if the interface should be used for this event. The VSCP-Class is set to 20 which is CLASS1.INFORMATION and the VSCP-Type is set to 40 which say this is a CLASS1.INFORMATION, Level Change event. This event is used to tell that the level for something has changed and the change is expressed by a value between 0-100. In this case this value is set to 50. The 1,20 is the zone and the subzone and group the receivers of this event together.

the “+” is a shorthand in the VSCP Daemon TCP/IP interface you can use to send the last command again.

When you send this event you will receive it in the node.js script like this.

There is just nothing more to it. Easy as a eating ren moon cakes in one day.

Here is the code. Happy hacking.

// Example
// =======
// Connect to a remote VSCP daemon and start to receive events from the
// server. We use the "rcvloop" command here to just sit and wait for
// event when they come in. And alternative is of course to use the
// event polling command "retr" to read an event in the input queue and
// the "chkdata" to check if there are events waiting to be read. You can
// read about the commands in the VCSP TCP/IP interface here
// http://www.vscp.org/docs/vscpd/doku.php?id=vscp_daemon_tcp_ip_protocol_description
//
// Arguments is 'host' 'port' 'user' 'password' 'temperature'
// which defaults to '127.0.0.1' '9598' 'admin' 'secret' '25'
//
// Part of VSCP & Friends - http://www.vscp.org
// akhe@grodansparadis.com, Grodans Paradis AB, http://www.paradiseofthefrog.com
//
var net = require('net');

// State machine
// 0 = Not connected
// 1 = Connected - Username sent
// 2 = Connected - Password sent
// 3 = Connected - Credentials OK

var STATE_NOT_CONNECTED = 0;
var STATE_USERNAME = 1;
var STATE_PASSWORD = 2;
var STATE_CONNECTED = 3;
var nState = STATE_NOT_CONNECTED;

// Get arguments
var HOST = ( typeof process.argv[2] !== 'undefined' ) ? process.argv[2] : '127.0.0.1';
var PORT = ( typeof process.argv[3] !== 'undefined' ) ? process.argv[3] : 9598;
var USER = ( typeof process.argv[4] !== 'undefined' ) ? process.argv[4] : "admin";
var PASSWORD = ( typeof process.argv[5] !== 'undefined' ) ? process.argv[5] : "secret";
var TEMPERATURE = ( typeof process.argv[6] !== 'undefined' ) ? process.argv[6] : 25;

var bDebug = true;  // Set to true to get some console logging

// Create a socket
var client = new net.Socket();

var timeoutConnection = setTimeout( fnTimeout, 3000 );

// Connect to host
client.connect(PORT, HOST );

///////////////////////////////////////////////////////////////////////////////
// Receive data
//

client.on('connect', function() {
    console.log('Connected to "' + HOST + '" on port "' + PORT +'"' );
});


///////////////////////////////////////////////////////////////////////////////
// Receive data
//

client.on('data', function(data) {
    var strReceiveData = data.toString();
    var reg1 = "\+OK Welcome to the VSCP daemon";

    if ( bDebug ) console.log('DATA: ' + data); // Log received data

    if ( STATE_NOT_CONNECTED == nState ) {
        if ( ( -1 != strReceiveData.search("\\+OK Welcome to the VSCP daemon") ) &&
            ( -1 != strReceiveData.search("\\+OK - Success") ) ) {

            if ( bDebug ) console.log('Welcome message received.');

            // OK we have a successful login message
            client.write('user ' + USER + "\r\n" );

            nState = STATE_USERNAME;
        }
    }
    else if ( STATE_USERNAME == nState ) {
        if ( ( -1 != strReceiveData.search('\\+OK') ) ) {
            if ( bDebug ) console.log('Username accepted.');
            nState = STATE_PASSWORD;
            client.write('pass ' + PASSWORD + "\r\n" );
        }
        else {
            nState = STATE_NOT_CONNECTED;
        }
    }
    else if ( STATE_PASSWORD == nState ) {
        if ( ( -1 != strReceiveData.search('\\+OK') ) ) {

            if ( bDebug ) console.log('Password accepted - we can do work.');

            // OK We are connected
            nState = STATE_CONNECTED;

            // OK we managed before timeout
            clearTimeout( global.timeoutConnection );

            // Send temperature event
            if ( bDebug ) console.log('Starting receive loop.');
            client.write('rcvloop'  + '\r\n');
        }
        else {
            nState = STATE_NOT_CONNECTED;
        }
    }
    else if ( STATE_CONNECTED == nState ) {
        if ( ( -1 != strReceiveData.search('\\+OK') ) ) {
            if ( bDebug ) console.log('Command OK');;
        }
        else {
            // This is a received event
            var offset;
            var vscpitems = strReceiveData.split(",");
            var vscphead = parseInt(vscpitems[0]);
            var vscpclass = parseInt(vscpitems[1]);
            var vscptype = parseInt(vscpitems[2]);
            var vscpobid = parseInt(vscpitems[3]);
            var vscptimestamp = parseInt(vscpitems[4]);
            var vscpguid = vscpitems[5];

            // Check if we have Level I events over Level II
            if ( vscpclass >= 512 && vscpclass < 1024 ) {
                offset = 16;    // Offset into data
                vscpclass -= 512;
            }

            // Get the data
            var vscpdata = new Array();
            for (i=0;i<vscpitems.length-6-offset;i++){
                vscpdata[i] = parseInt(vscpitems[offset+6+i]);
            }

            if ( bDebug ) {
                console.log("VSCP event received");
                console.log("===================");
                console.log("VSCP Class =" + vscpclass );
                console.log("VSCP Type =" + vscptype );
                console.log("VSCP GUID =" + vscpguid );
                console.log("VSCP Data =" + vscpdata );
            }
        }
    }
});

///////////////////////////////////////////////////////////////////////////////
// Close
//

client.on('close', function() {
    console.log('Connection closed');
});

///////////////////////////////////////////////////////////////////////////////
// Close
//

client.on('error', function() {
    console.log('Connection error');
});


var fnTimeout = function(){
    console.log("Unable to connect");
    client.destroy();
}
 
howto/node_js_receive_event.txt · Last modified: 2014/10/18 15:07 (external edit)
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki