首页 > 解决方案 > 节点红色功能等待 2 个输入

问题描述

我正在尝试为我的气象站创建一个流,它在构建 SQL 语句并将数据插入数据库之前等待来自 MQTT 的许多输入。

我似乎无法让它在触发之前等待所有输入到达。

我试图创建一个简单的流程,它使用 2 个具有不同主题的注入节点和一个调试节点来报告输出。

但是,一旦它收到一条消息,功能节点就会输出......

这是函数节点中的代码:

context.data = context.data || {};

switch (msg.topic) {
case "WeatherStation1/BME280/Trev1":
    context.data.insidetemperature = msg.payload;
    msg = null;
    break;
case "WeatherStation1/BME280/Trev2":
    context.data.insidehumidity = msg.payload;
    msg = null;
    break;
default:
    msg = null;
    break;
}

if(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {
msg2 = new Object();
msg2 = context.data;
context.data = null;
return msg2;
}

这是节点的代码:

[{"id":"e61a1536.2ea238","type":"inject","z":"fa655376.2f5ea","name":"","topic":"WeatherStation1/BME280/Trev1","有效载荷":"99","payloadType":"num","re​​peat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y" :540,"wires":[["e57cbc71.f5cce"]]},{"id":"e57cbc71.f5cce","type":"function","z":"fa655376.2f5ea","name" :"等待 Trev 传感器","func":"context.data = context.data || {};\n\nswitch (msg.topic) {\n case \"WeatherStation1/BME280/Trev1\":\n context.data.insidetemperature = msg.payload;\n msg = null;\n break;\n case \"WeatherStation1/BME280/Trev2\":\n context.data.insidehumidity = msg.payload;\n msg = null;\n break;\n default:\ n msg = null;\n \tbreak;\n}\n\nif(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {\n\tmsg2 = new Object();\ n msg2 = context.data;\n context.data = null;\n\treturn msg2;\n}","outputs":1,"noerr":0,"x":480,"y":580, "电线":[["1f0b9ac2.57f2a5"]]},{"id":"fc5b276d.4f9b18","type":"inject","z":"fa655376.2f5ea","name":"" ,"topic":"WeatherStation1/BME280/Trev2","payload":"99","payloadType":"num","重复":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":620,"wires":[["e57cbc71.f5cce"]] },{"id":"1f0b9ac2.57f2a5","type":"debug","z":"fa655376.2f5ea","name":"Trev Data","active":true,"tosidebar":真,“控制台”:假,“tostatus”:假,“完成”:“真”,“x”:720,“y”:580,“电线”:[]}]“名称”:“Trev 数据”,“活动”:真,“tosidebar”:真,“控制台”:假,“tostatus”:假,“完成”:“真”,“x”:720,“y” :580,"电线":[]}]“名称”:“Trev 数据”,“活动”:真,“tosidebar”:真,“控制台”:假,“tostatus”:假,“完成”:“真”,“x”:720,“y” :580,"电线":[]}]

我看不出我做错了什么。道歉,如果很明显:)

标签: node-red

解决方案


在这种情况下,使用如下配置的连接节点会更简单:

在此处输入图像描述

测试附加的流程,看看它是如何工作的:

[{"id":"bd70b6f3.d327c8","type":"tab","label":"Join Node","disabled":false,"info":""},{"id":"caa58f4e.316c8","type":"join","z":"bd70b6f3.d327c8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":200,"wires":[["b82136ef.53d8e8"]]},{"id":"85340c81.7988b","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev1","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":120,"wires":[["caa58f4e.316c8"]]},{"id":"fccfc295.2527a","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev2","payload":"99","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":240,"wires":[["caa58f4e.316c8"]]},{"id":"b82136ef.53d8e8","type":"debug","z":"bd70b6f3.d327c8","name":"Trev Data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":620,"y":200,"wires":[]},{"id":"92af39.d98c10c8","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev1","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":160,"wires":[["caa58f4e.316c8"]]},{"id":"21b2e29d.fac10e","type":"inject","z":"bd70b6f3.d327c8","name":"","topic":"WeatherStation1/BME280/Trev2","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":280,"wires":[["caa58f4e.316c8"]]}]

推荐阅读