节点红色全局数组变量 - 获取/设置似乎不起作用

时间:2018-03-25 22:16:22

标签: arrays variables global node-red

使用Node-Red。我可以跨节点使用常规全局变量并且流没有问题。但是,想使用全局数组变量。

方法A - 所需的功能 我一次读入16个数据点(type = double)并希望它们是索引0-15,然后下一个节点将更新索引16-31;然后是最后两个节点中的32-45和46-64。

然而,

Node Red不允许从索引#16开始从第二个节点更新数组。我得到“TypeError:无法读取属性'indexOf'未定义”错误。

代替方法A,我可以有四个不同的16索引全局数组。但是,访问它们会产生不稳定的结果。尝试访问index [n]会返回其他索引的值 - 即global.get("variable"[0])返回variable[10]global.get("variable"[1])会返回variable[27]的值。

这描述了问题 - https://www.youtube.com/watch?v=cF1bz8bEozI

以下是我的示例流程:

[{"id":"ee1694d.7df4768","type":"i2c in","z":"d556390c.391838","name":"Read Camera","address":"105","command":"128","count":"32","x":240,"y":1480,"wires":[["9e27949c.512c28"]]},{"id":"d9eaa7a4.7f0ed8","type":"inject","z":"d556390c.391838","name":"ON","topic":"1","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"x":70,"y":1480,"wires":[["ee1694d.7df4768"]]},{"id":"6dc0727a.4cf53c","type":"i2c in","z":"d556390c.391838","name":"Read Camera","address":"105","command":"160","count":"32","x":240,"y":1520,"wires":[["a7ac4b94.44ce58"]]},{"id":"d6d80973.784148","type":"i2c in","z":"d556390c.391838","name":"Read Camera","address":"105","command":"192","count":"32","x":240,"y":1560,"wires":[["b90d910d.8e743","ebeeb439.54cf18"]]},{"id":"b90d910d.8e743","type":"i2c in","z":"d556390c.391838","name":"Read Camera","address":"105","command":"224","count":"32","x":240,"y":1600,"wires":[["2f7b8dde.7a9902"]]},{"id":"6b1509e2.8bd4d8","type":"debug","z":"d556390c.391838","name":"Row 3,4","active":true,"console":"false","complete":"payload","x":1020,"y":1520,"wires":[]},{"id":"a828b6d2.40da08","type":"delay","z":"d556390c.391838","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":98,"y":1656,"wires":[["d6d80973.784148"]]},{"id":"ad0a1424.eaae08","type":"function","z":"d556390c.391838","name":"Save Global variables for Temperature","func":"global.set(\"RangeTemperaturesA\", 0);\n\nfor(i=0; i<16; i++){\n global.set(\"RangeTemperaturesA\"[i], msg.payload[i]);   \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1480,"wires":[["6b810d97.0beee4","6dc0727a.4cf53c"]]},{"id":"d386a34f.525d2","type":"function","z":"d556390c.391838","name":"Save Global variables for Temperature","func":"for(i=0; i<16; i++){\n    global.set(\"RangeTemperatureB\"[i], msg.payload[i]);   \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1520,"wires":[["6b1509e2.8bd4d8"]]},{"id":"11c935c.be330ca","type":"function","z":"d556390c.391838","name":"Save Global variables for Temperature","func":"for(i=0; i<16; i++){\n global.set(\"RangeTemperatureC\"[i], msg.payload[i]);   \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1560,"wires":[[]]},{"id":"294185a.d5fe67a","type":"function","z":"d556390c.391838","name":"Save Global variables for Temperature","func":"for(i=0; i<16; i++){\n global.set(\"RangeTemperatureD\"[i], msg.payload[i]);   \n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1600,"wires":[[]]},{"id":"3ffa9e84.cba002","type":"function","z":"d556390c.391838","name":"Find Max Temperature","func":"//n = Math.max(... global.get(\"RangeTemperature\"));\n\nreturn {payload: global.get(\"RangeTemperature\")};","outputs":1,"noerr":0,"x":900,"y":1660,"wires":[[]]},{"id":"9e27949c.512c28","type":"function","z":"d556390c.391838","name":"Get Temps full row","func":"var gridEye = [];\nvar loop=0;\n\nfor(n=0; n<32; n+=2){\n    gridEye[loop] = ((msg.payload[n+1]<<8) | msg.payload[n]) * 0.25;\n    //convert to F\n    gridEye[loop] = ((5.0/3.0) * gridEye[loop] + 32.0).toFixed(2);\n    //add right bitshit to reduce noise\n    loop++;\n}\nmsg.payload=gridEye;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":1480,"wires":[["ad0a1424.eaae08"]]},{"id":"a7ac4b94.44ce58","type":"function","z":"d556390c.391838","name":"Get Temps full row","func":"var gridEye = []; //16-byte array with temperature readings\nvar loop=0;\n\nfor(n=0; n<32; n+=2){\n    // Get raw values - bitshift left 8 bits then bitwise OR.\n    // then take new value and multiply by 0.25 since it reads in 1/4 degree C\n    gridEye[loop] = ((msg.payload[n+1]<<8) | msg.payload[n]) * 0.25;\n    //convert to F\n    gridEye[loop] = ((5.0/3.0) * gridEye[loop] + 32.0).toFixed(2);\n    loop++;\n}\n\nmsg.payload=gridEye;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":1520,"wires":[["d386a34f.525d2"]]},{"id":"ebeeb439.54cf18","type":"function","z":"d556390c.391838","name":"Get Temps full row","func":"var gridEye = [];\nvar loop=0;\n/*\nvar pixel = 4;\nvar tmp = ((msg.payload[pixel*2 + 1]<<8) | msg.payload[pixel*2])*0.25; \n//gridEye reads in .25 degree C\ntmp = ((5/3 * tmp) + 32.0); //convert to F\n*/\n\nfor(n=0; n<32; n+=2){\n    gridEye[loop] = ((msg.payload[n+1]<<8) | msg.payload[n]) * 0.25;\n    //convert to F\n    gridEye[loop] = ((5.0/3.0) * gridEye[loop] + 32.0).toFixed(2);\n    //add right bitshit to reduce noise\n    loop++;\n}\n/*\nfor(n=0; n<8; n++){\n    gridEye[n]= ((n/35536 * 60 ) + 20);\n    //convert to F\n    //gridEye[n] = (((5.0/3.0) * gridEye[n]) + 32).toFixed(2);\n}\n*/\nmsg.payload=gridEye;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":1560,"wires":[["11c935c.be330ca"]]},{"id":"2f7b8dde.7a9902","type":"function","z":"d556390c.391838","name":"Get Temps full row","func":"var gridEye = [];\nvar loop=0;\n/*\nvar pixel = 4;\nvar tmp = ((msg.payload[pixel*2 + 1]<<8) | msg.payload[pixel*2])*0.25; \n//gridEye reads in .25 degree C\ntmp = ((5/3 * tmp) + 32.0); //convert to F\n*/\n\nfor(n=0; n<32; n+=2){\n    gridEye[loop] = ((msg.payload[n+1]<<8) | msg.payload[n]) * 0.25;\n    //convert to F\n    gridEye[loop] = ((5.0/3.0) * gridEye[loop] + 32.0).toFixed(2);\n    //add right bitshit to reduce noise\n    loop++;\n}\n/*\nfor(n=0; n<8; n++){\n    gridEye[n]= ((n/35536 * 60 ) + 20);\n    //convert to F\n    //gridEye[n] = (((5.0/3.0) * gridEye[n]) + 32).toFixed(2);\n}\n*/\nmsg.payload=gridEye;\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":1600,"wires":[["294185a.d5fe67a"]]},{"id":"a0de3101.0c307","type":"function","z":"d556390c.391838","name":"Read from A","func":"var p = global.get(\"RangeTemperaturesA\"[1]);\nmsg.payload = p;\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":1780,"wires":[["dbb0935d.742f7"]]},{"id":"dbb0935d.742f7","type":"debug","z":"d556390c.391838","name":"test A","active":true,"console":"false","complete":"payload","x":670,"y":1860,"wires":[]},{"id":"f6c50374.59f","type":"function","z":"d556390c.391838","name":"Read from B","func":"var n = global.get(\"RangeTemperatureB\"[0]);\nreturn {payload: n};","outputs":1,"noerr":0,"x":770,"y":1780,"wires":[["3e8947bc.be49b8"]]},{"id":"6b810d97.0beee4","type":"debug","z":"d556390c.391838","name":"Row 1,2","active":true,"console":"false","complete":"payload","x":1020,"y":1480,"wires":[]},{"id":"3e8947bc.be49b8","type":"debug","z":"d556390c.391838","name":"test b","active":true,"console":"false","complete":"payload","x":910,"y":1860,"wires":[]},{"id":"23da3f7f.1c4f8","type":"inject","z":"d556390c.391838","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":340,"y":1780,"wires":[["a0de3101.0c307"]]},{"id":"2a146fa7.577fc","type":"inject","z":"d556390c.391838","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":580,"y":1720,"wires":[["f6c50374.59f"]]}]

编辑 - 我做了一个快速测试 - 对于 global.set -

global.set("RangeTemperaturesA", i)[i]; gives "TypeError: Cannot read property '0' of undefined"

global.set("RangeTemperaturesA[i]", i);  gives "Error: Invalid property expression: unexpected i at position 19"

global.set("RangeTemperaturesA"[i], i); appears to probably work.

示例代码:

for(i=0; i<16; i++){
 global.set("RangeTemperaturesA"[i], i); 
 node.warn("Value: " + i);
}
return msg;

Global.get -

global.get("RangeTemperaturesA"[n]) gives erratic results.
global.get("RangeTemperaturesA[n]") gives "Error: Invalid property expression: unexpected n at position 19"
global.get("RangeTemperaturesA")[n] gives "Value: undefined; Count: 0" gives "Value: undefined; Count: 0" which is perhaps the most promising if the array was never populated correctly.

示例代码:

for(n=0; n<16; n++){
node.warn("Value: " + global.get("RangeTemperaturesA")[n] + "; Count: " + n);
}
return msg;

1 个答案:

答案 0 :(得分:5)

问题是由于您尝试处理各个阵列条目的方式。

使用代码:global.get("variable"[0]),您要求它使用字符串"variable"的第0个元素作为传递给get函数的参数。换句话说,它相当于:global.get("v") 同样,global.get("variable"[2])也相当于global.get("r")

您应该在引号内移动数组索引:

global.get("variable[0]");

或访问get函数结果的第0个元素:

global.get("variable")[0];

对于您尝试使用set函数的方式也是如此。

更新以反映对问题的修改

您尝试使用global.set()的所有尝试都不正确:

  • global.set("RangeTemperaturesA", i)[i] - 此处您将全局属性RangeTemperaturesA设置为i的值。函数set不会返回任何内容,因此尝试将其视为数组是错误的。

  • global.set("RangeTemperaturesA[i]", i); - 这是三者中最接近的,但是,您要设置字符串文字RangeTemperaturesA[i] - JavaScript并不知道您想要i in该字符串的中间部分是您的本地变量i的值。

  • global.set("RangeTemperaturesA"[i], i); - 没有。这与原始问题中的错误相同。 "RangeTemperaturesA"[i]将评估字符串RangeTemperaturesA的第i个字符。

要正确执行此操作,您需要使用"RangeTemperaturesA["+i+"]"作为密钥:

global.set("RangeTemperaturesA["+i+"]", i);

i为0时,将生成密钥RangeTemperaturesA[0]

同样适用于global.get

var myValue = global.get("RangeTemperaturesA["+i+"]");

所有这些示例都假设您已将RangeTemperaturesA设置为数组:

global.set("RangeTemperaturesA",[]);