Skip to content

Node Quick Start

Quickstart package.json file

Create a file called package.json in the same folder as your app.js file

{
  "name": "node",
  "version": "1.0.0",
  "description": "Yanzi Cirrus API Node.js Quick Start. Youll be up and purring in no time.",
  "main": "app.js",
  "author": "Yanzi Networks AB, Kista, Sweden",
  "license": "ISC",
  "dependencies": {
    "websocket": "^1.0.28"
  }
}

Quickstart app.js file

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

//Set up endpoint, you'll probably need to change this
var cirrusAPIendpoint = "cirrus21.yanzi.se";

// ##########CHANGE BELOW TO YOUR OWN DATA##########

//Set up credentials. Please DONT have your credentials in your code when running on production
var username = "YOUR USERNAME";
var password = "YOUR PASSWORD";

//Set up Location ID and Device ID, please change this to your own, can be found in Yanzi Live
var locationId = "LOCATION ID" //Usually a 6 digit number
var deviceID = "DEVICE ID" //Found in Yanzi Live, ends with "-Temp"

// ################################################

// Create a web socket client initialized with the options as above
var client = new WebSocketClient();

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

client.on('connect', function(connection) {
    console.log('Websocket open!');
    console.log("Checking API service status with ServiceRequest.");
    sendServiceRequest();

    // Handle messages
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
        var json = JSON.parse(message.utf8Data);
            if (json.messageType == 'ServiceResponse') {
                console.log("ServiceRequest succeeded, sending LoginRequest");
                sendLoginRequest();
            } else if (json.messageType == 'LoginResponse') {
                if (json.responseCode.name == 'success') {
                    console.log("LoginRequest succeeded, let's get some data...");
                    sendGetSamplesRequest();
                } else {
                    console.log("Couldn't login, check your username and passoword");
            connection.close();
                }
            } else if (json.messageType == 'GetSamplesResponse') {
                if (json.responseCode.name == 'success') {
                    console.log("Yaaaay, temperaturedata in abundance!");
            console.log(json.sampleListDto.list);
                    connection.close();
                } else {
                    console.log("Couldn't get samples.");
            connection.close();
                }
            }
        }
    });

    connection.on('error', function(error) {
        console.log("Connection Error: " + error.toString());
    });

    connection.on('close', function(error) {
        console.log('Connection closed!');
    });

    function sendMessage(message) {
        if (connection.connected) {
            // Create the text to be sent
            var json = JSON.stringify(message, null, 1);
            connection.sendUTF(json);
        } else {
            console.log("sendMessage: Couldn't send message, the connection is not open");
        }
    }

    function sendServiceRequest() {
        var request = {
            "messageType" : "ServiceRequest"
        }
        sendMessage(request);
    }

    function sendLoginRequest() {
        var request = {
            "messageType" : "LoginRequest",
            "username" : username,
        "password": password
        }
        sendMessage(request);
    }

    function sendGetSamplesRequest() {
        var now = new Date().getTime();
    var nowMinusOneHour = now - 60 * 60 * 1000;
        var request = {
            "messageType" : "GetSamplesRequest",
            "dataSourceAddress" : {
                "resourceType" : "DataSourceAddress",
                "did" : deviceID,
                "locationId" : locationId,
        "variableName": {
                    "resourceType": "VariableName",
                    "name": "temperatureC"
        }
            },
            "timeSerieSelection" : {
                "resourceType" : "TimeSerieSelection",
                "timeStart" : nowMinusOneHour,
                "timeEnd" : now
            }
        };
        sendMessage(request);
    }
});

function processArgs() {
    if (!username) {
        console.error("The username has to be set");
        return;
    }
    if (!password) {
        console.error("The password has to be set");
        return;
    }
    client.connect("wss://" + cirrusAPIendpoint + "/cirrusAPI");
    console.log("Connecting to wss://" + cirrusAPIendpoint + "/cirrusAPI using username " + username);
}

processArgs();
Back to top