Skip to content
Important! Yanzi IoT product line will be sunsetted end of 2025. Click here to learn more.

Node Quick Start

Quickstart package.json file

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

JSON
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "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

JavaScript
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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();