【NodeRed】Function Node

428 阅读4分钟

1. Document

Reference: nodered.17coding.net/docs/writin…

1.單個輸出

var newMsg = { payload: msg.payload.length };
return newMsg;

2.多個輸出

var newMsg = { payload: msg.payload.length };
return [msg, newMsg];

3.payload切割,逐條輸出

var outputMsgs = [];
var words = msg.payload.split(" ");
for (var w in words) {
    outputMsgs.push({payload:words[w]});
}
return [ outputMsgs ];

4.Async

doSomeAsyncWork(msg, function(result) {
    node.send({payload:result});
});
return;

5.log

node.log("Something happened");
node.warn("Something happened you should know about");
node.error("Oh no, something bad happened");

6.Variable

  • context
  • flow
  • global
// 如果计数器不存在则将其初始化为0
var count = context.get('count')||0;
count += 1;
// 保存数据
context.set('count',count);
// 将其作为输出消息的一部分
msg.count = count;

2. Buffer

Buffer.from  复制传入的 Buffer 数据,并返回一个新的 Buffer

var buf = Buffer.from([2, 3, 0, 0, 0, 2, 0XC4, 0X38]);	

Buffer.alloc  返回一个指定大小的 Buffer 实例

var buf1 = Buffer.alloc(10);			//实例为设置一个内容全为0,长度为10的数组
var buf2 = Buffer.alloc(10,0x0EE);		//实例为设置一个内容全为0xEE,长度为10的数组

Buffer.write :写入 Node 缓冲区,返回值为写入的大小

var buf = Buffer.alloc(10);
var len = buf.write("1");
msg.buf = buf;  					//输出内容为[0x31,0,0,0,0,0,0,0,0,0] 默认从第一个字节开始写
msg.len = len;  					//输出内容1   表示写入了1个数据

buffer.write(string[, offset[, length]][, encoding]) :写入 Node 缓冲区,返回值为写入的大小。string:需要写入的字符,offset:开始写入的地址,length:写入的长度

var buf = Buffer.alloc(10);
var len = buf.write("123",1,2);		
msg.buf = buf;  					//输出内容为[0,0x31,0x32,0,0,0,0,0,0,0] 默认从第一个字节开始写,写入了2个字节,所以传入的string里第三个数据被省略了
msg.len = len;  					//输出内容2   表示写入了2个数据

3. Date

new Date().toLocaleDateString() 获取当前日期
new Date().toLocaleTimeString() 获取当前时间
new Date().toLocaleString() 获取日期和时间
new Date().getDullYear(); 获取年(yyyy)
new Date().getMonth+1 获取月(1-12)
new Date().getDate() 获取日(1-31)
new Date().getHours() 获取小时(0-23)
new Date().getMinutes() 获取分(0-59)
new Date().getSeconds() 获取秒(0-59)
new Date().getMilliseconds() 获取毫秒(0-999)
new Date().getDay() 获取星期几(0-6)
new Date().getTime() 获取從 1970-01-01 00:00:00 UTC 累計的毫秒數

4. String Compare

1."=="

  • "等同"操作符,简单判断2个变量值是否相等,变量类型不一致时,会做类型转换,即忽略变量的类型进行比较
var str1 = "123456" ; // 字符串
var str2 = "123456" ; // 字符串
alert(str1==str2) ; // 打印出 true,即相等


var str1 = 123456 ; // 整型
var str2 = "123456" ; // 字符串
alert(str1==str2) ; // 打印出 true,即相等

2."==="

  • "恒等"操作符,先比较2个变量类型是否一致,不一致返回false,一致时,再比较2个变量的值是否相等

3.localeCompare

  • 如果两个字符串在当前语言环境下无法比较,则会返回一个 NaN 值
const str1 = 'apple'
const str2 = 'apple'

if (str1.localeCompare(str2) === 0) {
console.log('两个字符串相等')
} else {
console.log('两个字符串不相等')

5. Array

1.Create Array

var points = new Array();         // 差
var points = [];                  // 优

var points = new Array(40, 100, 1, 5, 25, 10); // 差
var points = [40, 100, 1, 5, 25, 10];          // 优

2.識別 Array

var fruits = ["Banana", "Orange", "Apple", "Mango"];

typeof fruits;             // 返回 object

Array.isArray(fruits);     // 返回 true

3.Method

6. Demo


var counter = 0;
var array = []
var resultCounter = global.get('counter') || counter;
var resultArray = global.get('notifies') || array;

var body = msg.payload.trx.body;
if(body.L02_W_data.r0 === "Error"){

    resultCounter++;
    global.set('counter',resultCounter);
    resultArray.push(body);
    global.set('notifies',resultArray);
}

console.log(resultCounter);
console.log(resultArray);

if(resultCounter >= 3){
    var start = resultArray[0].lasttime;
    var end = resultArray[2].lasttime;
    
    var gap = (new Date(end).getTime() - new Date(start).getTime())/(1000*60*60);

    if(gap > 4){
        resultArray.shift();
        global.set('notifies', resultArray);
        resultCounter--;
        global.set('counter', resultCounter);
        msg.payload = global.get('notifies');
        msg.type = "NG";
        msg.to = "nan.zhao@deltaww.com";

    } else{
        var newArray = [];
        global.set('notifies', undefined);
        global.set('counter', 0);
        msg.type = "OK";
        msg.payload = "1111111111111";
    }

}

return msg;