Skip to content

XSocketsNodeJs Test

Uffe Björklund edited this page Jun 16, 2015 · 17 revisions

TODO: Change to NDC protocol and add NDC protocol tutorial

###Pre-Req

Code

Create a new file with nano sockettest.js and write a simple test. Replace the IP with the IP of your development machine.

var xsockets = require('xsockets.net');  
var c = new xsockets.TcpClient('127.0.0.1', 8080, ['chat']);  
var g = c.controller('chat');  
g.on('say', function(d) {  
    console.log('say',d);  
    g.send('bar','hello from nodejs');  
});        
g.onopen = function(ci) {
    console.log('connected controller chat');
    g.send('say', {text:'bar'});
}
c.onconnected = function(d) {
    console.log('connected', d);
}
c.open();

###Test

Run the test with node sockettest.js

Then open up Putty and connect to the same sever as the script, this would be the 192.168.x.x of your machine. Then type in PuttyProtocol in Putty to do a handshake. Then send a message to nodejs by typing generic|foo|hello from putty

    var sensorTagLocation = 'sensortag';
var xsocketsLocation = 'xsockets.net';
var targetUUID = '5c313e87ad05';
//var server = 'localhost';
//var port = 4502;
var server = 'cc2650.cloudapp.net';
var port = 8080;

var TexasInstrument = {} || TexasInstrument;
TexasInstrument.CC2650 = (function (sensortag, xsockets) {
    var sensor = function () {
        var self = this;
        var tagInstance = undefined;
        //Create a new connection to the Azure worker role
        var conn = new xsockets.TcpClient(server, port, ['sensor']);
        var sensorcontroller = conn.controller('sensor');
        //When the controller is open on the server set the name of the unit and start scanning for SensorTags
        sensorcontroller.onopen = function () {
            //Set Name
            sensorcontroller.setProperty('Name', 'CC2541 - Uffe');
            if (targetUUID) {
                console.log('connecting to ', targetUUID);
                sensortag.discoverByUuid(targetUUID, onDiscovered);
            }
            else {
                console.log('connecting to ONE tag');
                sensortag.discover(onDiscovered);
            }
        };
        sensorcontroller.onclose = function () {
            console.log('close');
        };
        sensorcontroller.onerror = function (e) {
            console.log('error', e);
        };
        //Temp changed.. Notify the server on Azure
        var onChangeTemp = function (ot, at) {
            sensorcontroller.send('irtempchange', { obj: ot, amb: at });
        };
        //When a tag is discovered, connect to it and enable the service(s) to use
        var onDiscovered = function (tag) {
            tag.connect(function () {
                console.log('tag connected');
                tagInstance = tag;
                tagInstance.discoverServicesAndCharacteristics(function () {
                    console.log('characteristics discovered');
                    //Start IrTemp                                    
                    tagInstance.enableIrTemperature(function () {
                        console.log('temp enabled');
                        self.onconnected();
                    });
                    sensorcontroller.on('enableirtemp', self.enableIrTemperature);
                    sensorcontroller.on('disableirtemp', self.disableIrTemperature);
                });
            });
        };
        this.onconnected = function () {
            console.warn('implement onconnected');
        };
        //Enable irtemp
        this.enableIrTemperature = function (cb) {
            tagInstance.notifyIrTemperature(function () {
                console.log('notification on');
                tagInstance.on('irTemperatureChange', onChangeTemp);
                sensorcontroller.send('irtempenabled');
                if (cb) cb();
            });
        };
        //Disable irtemp
        this.disableIrTemperature = function (cb) {
            tagInstance.unnotifyIrTemperature(function () {
                console.log('notification off');
                tagInstance.removeAllListeners('irTemperatureChange');
                sensorcontroller.send('irtempdisabled');
                if (cb) cb();
            });
        };
        //Open the connection!
        conn.open();
    }
    return sensor;
})(require(sensorTagLocation), require(xsocketsLocation));

//Start the script and enable irtemp
var cc2650 = new TexasInstrument.CC2650();
cc2650.onconnected = function () {
    cc2650.enableIrTemperature();
}    

next

Clone this wiki locally