node-red-contrib-artik-cloud
Version:
Nodes to interface with ARTIK Cloud
158 lines (144 loc) • 4.36 kB
JavaScript
var Client = require("node-rest-client").Client;
var WebSocket = require('ws');
var httpPostUrl = "https://api.artik.cloud/v1.1/messages";
var webSocketUrl = "";
var deviceid = "";
var token = "";
var uid = "";
var ws;
module.exports = function(RED) {
var msg = JSON.stringify({"":""});
function build_mesg(payload, deviceid, token, ts) {
var args = {
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token
},
data: {
"sdid": deviceid,
"ts": ts,
"type": "message",
"data": payload
}
};
return args;
}
function ArtikRESTPostNode(config) {
RED.nodes.createNode(this,config);
var node = this;
this.on('input', function(msg) {
var client = new Client();
var payload = msg.payload;
var args = build_mesg(payload, this.credentials.deviceid, this.credentials.token, new Date().valueOf());
client.post(httpPostUrl, args, function(data, response) {
});
});
}
RED.nodes.registerType("artik cloud out", ArtikRESTPostNode,
{
credentials: {
deviceid: {type:"text"},
token: {type:"password"}
}
}
);
function ArtikWebSocketInNode(config) {
RED.nodes.createNode(this,config);
var node = this;
var enableMessage = config.enableMessage;
var enableAction = config.enableAction;
deviceid = this.credentials.deviceid;
token = this.credentials.token;
if (typeof ws !== 'undefined')
ws.close();
if (enableMessage === true) {
webSocketUrl = "wss://api.artik.cloud/v1.1/live?uid=" + this.credentials.uid + "&sdids=" + this.credentials.deviceid + "&Authorization=bearer+" + this.credentials.token;
ws = new WebSocket(webSocketUrl);
ws.on('open', function() {
});
ws.on('message', function(data) {
handleRcvMsg(data,node);
});
ws.on('close', function() {
//console.log("WebSocket for message is closed...");
});
}
if (enableAction === true) {
webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
ws = new WebSocket(webSocketUrl);
ws.on('open', function() {
//console.log("WebSocket connection is open ....");
register(deviceid,token);
});
ws.on('message', function(data) {
//console.log("Received message: " + data + '\n');
handleRcvAction(data,node);
});
ws.on('close', function() {
//console.log("WebSocket for action is closed ....");
});
}
}
function handleRcvMsg(data,node) {
var sdid = "";
var msgObj = null;
if (data.indexOf("\"type\":\"ping\"") < 0 && data.indexOf("\"code\":\"200\"") < 0)
{
var msgObj = JSON.parse(data);
if (msgObj.type)
{
if (msgObj.type == 'action')
return;
}
else
{
sdid = msgObj.sdid;
if (sdid == deviceid)
node.send(msgObj.data);
}
}
}
function register(deviceid,token) {
try {
var registerMessage = '{"type":"register", "sdid":"'+deviceid+'", "Authorization":"bearer '+token+'", "cid":"'+getTimeMillis()+'"}';
ws.send(registerMessage, {mask: true});
}
catch (e) {
console.error('Failed to register messages. Error in registering message: ' + e.toString());
}
}
function handleRcvAction(data,node) {
var sdid = "";
var msgObj = null;
if ( data.indexOf("\"type\":\"ping\"") < 0
&& data.indexOf("\"code\":\"200\"") < 0 )
{
var msgObj = JSON.parse(data);
if (msgObj.type)
{
if (msgObj.type != "action") {
return; //Early return;
} else {
var ddid = msgObj.ddid; // To identify the device id for the intended device
var actions = msgObj.data.actions;
var actionName = actions[0].name; //assume that there is only one action in actions
//console.log("The received action is " + actionName);
if (msgObj.data)
node.send(msgObj.data);
} /* else */
} /* if */
} /* if */
}
function getTimeMillis(){
return parseInt(Date.now().toString());
}
RED.nodes.registerType("artik cloud in", ArtikWebSocketInNode,
{
credentials: {
uid: {type:"text"},
deviceid: {type:"text"},
token: {type:"password"}
}
}
);
}