来自Web服务器的节点红色拆分值



我正在尝试节点red来轮询我的daikin热泵中的Web服务器,以进入内部和外部temeperatures,以便我可以根据我还配置了该设备的时间表。

在为值进行轮询时,它返回此字符串文本

" ret = ok,htemp = 21.5,hhum = - ,otemp = 18.0,err = 0,cmpfreq = 0"

我在节点红色函数中使用此代码将它们分开在逗号

msg.payload = msg.payload.split(",");
return msg;

它返回一个带有6个字段的数组:

array[6]
0: "ret=OK"
1: "htemp=21.5"
2: "hhum=-"
3: "otemp=18.0"
4: "err=0"
5: "cmpfreq=0"

然后将其放入node-red inderdb中,而数字字段是创建的字段。

我无法分析这些数据,因为它在infuxdb中最终具有文字。我无法弄清楚如何删除左侧的文本和包括=符号的情况,而不会在节点red中创建错误。

至少,我希望数据这样:

array[6]
0: "OK"
1: "21.5"
2: "-"
3: "18.0"
4: "0"
5: "0"

最好是将=符号左侧的文本用作数组中的字段值,以便它们在InfluxDB中填充。这将使分析更容易。

然后看起来像这样(额外的空间来排列结肠以获得可读性(:

    array[6]
    ret: "OK"
  htemp: "21.5"
   hhum: "-"
  otemp: "18.0"
    err: "0"
cmpfreq: "0" 

我知道该语法是基于JavaScript的,但是我在该地区还不够强大,无法弄清楚这一点。我已经过了一天了,没有运气。

谢谢!

P.S。对于任何拥有WiFi Daikin US7并想通过IP控制的人,我一直在使用此库来获取我需要发送给WebServer的值。

更新带有请求的信息

infuxdb是v0.10.0,webui说go1.6rc1Node-red为V.0.16.2Node-red使用node-red-contrib-influxdb v0.1.1

当我在函数中使用此代码时:

msg.payload = msg.payload.split(",").split("=")[0];
return msg;

将调试标签放在函数的输出上时,我遇到了此错误:

function : (error)
"TypeError: msg.payload.split(...).split is not a function"

流程布局和错误

根据文档,输出节点期望对象有效载荷,并带有您要推入influxdb的属性/值。

因此,您需要将该输入字符串拆分,为此,您对.split(',')的第一个调用是正确的。但这返回一系列字符串,无法用.split('=')再次分开(因此您遇到的错误(。

这是您如何将key=value对的第二级分开并将其放在对象中:

var o = {}; // new empty Object that will be used for next payload
msg.payload.split(',').forEach(function(kv) { // loop on 'key=value' entries in array
    var pair = kv.split('='); // split entry to isolate key and value
    var key = pair[0];
    var value = pair[1];
    o[key] = value; // save that as new property for our Object
});
msg.payload = o; // Make that object the new payload

(用注释编辑(

为此,您只需利用节点核心中的querystring模块。

const querystring = require('querystring');
const parsed = querystring.parse(msg.payload, ',');
// convert the numbers to numbers instead of strings
Object.keys(parsed).forEach(function (key) {
    const numberValue = +parsed[key];
    // if the value converted to number is Not A Number then don't change it 
    if (isNaN(numberValue)) return;
    parsed[key] = numberValue;
});
msg.payload = parsed;

querystring.parse的第二个参数是分隔符,请检查文档以获取更多信息。

最新更新