Script


The Script node converts the payload into another format using JavaScript.

Node Type

Logic.

Input and Output Ability

This node has 1 entry point and 1 exit point. The input and output can be any format. You can define the format using the JavaScript you write, and the return data is the output message.

Note

Does not support Javascript ES6.

Node Properties

../../_images/script.png


Settings Tab

Node Name

Required.

The name for this node.


Description

Optional.

The description for this node.

Script Tab

Optional

The JavaScript (ES8 supported) for converting the input into the desired format. The supported function is transform with two parameters, msg and metadata. The msg is the input message, and the metadata can be the MQTT topic for example, which can be used in the script.


Click the Test button to test your JavaScript by entering the input message to see if your JavaScript works as designed.

Limitations

  • msg and metadata needs to be returned in the script function.
  • Does not support Javascript ES6.

Samples

Input Sample

The input is dependent on what the script requires.

Output Sample

The output is subject to how the script processes the input.

Script Sample

if(!msg.Data){
  return tools.resultBuilder.build(false);
}
var dataArray = msg.Data.data;
var stream = msg.Data.stream;
var device_id = stream.camera;
var flag = true;

for (var i=0; i<dataArray.length; i++) {
  var data = dataArray[i];
  if(!data.eventName){
    flag = false;
    break;
  }

  var paramsArray = new Array();
  if(data.eventName === "Event1"){
    var mps = {};
    mps["raw_data_count"] = JSON.stringify(data.count);
    mps["alarm_data_event_name"] = data.eventName;
    mps["raw_data_imgpath"] = JSON.stringify(data.imgpath);
    mps["raw_data_start_time"] = data.startTime;
    mps["raw_data_timestamp"] = data.timestamp;
    mps["raw_stream_camera"] = stream.camera;

    var params = {};
    params["measurepoints"] = mps;
    params["time"] = new Date().getTime();
    params["device_id_no"] = device_id;
    paramsArray.push(params);
  }else{
    paramsArray = metadata.params;
  }
}

if(!flag){
  return tools.resultBuilder.build(false);
}

return tools.resultBuilder.build(true,JSON.stringify(paramsArray));