我正在尝试为气象站创建一个流程,在该流程中,它会等待MQTT的大量输入,然后再构建SQL语句并将数据插入数据库中。
我似乎无法让它等待所有输入都被触发之前。
我试图创建一个简单的流程,该流程使用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 “,” payload“:” 99“,” payloadType“:” num“,” repeat“:”“,” crontab“:”“,” once“:false,” onceDelay“:0.1,” x“:180, “ y”:540,“ wires”:[[“” e57cbc71.f5cce“]]},{” id“:” e57cbc71.f5cce“,” type“:” function“,” z“:” fa655376.2f5ea“, “名称”:“等待Trev Sensors”,“ func”:“ context.data = context.data || {}; \ n \ nswitch(msg.topic){\ n案例\” WeatherStation1 / BME280 / Trev1 \“ :\ n context.data.insidetemperature = msg.payload; \ n msg = null; \ n休息; \ n case \“ WeatherStation1 / BME280 / Trev2 \”:\ n context.data.insidehumidity = msg.payload; \ n msg = null; \ n break; \ n默认值:\ 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}“,”输出“:1,” noerr“:0 ,“ x”:480,“ y”:580,“ wires”:[[“” 1f0b9ac2.57f2a5“]]},{” id“:” fc5b276d。 4f9b18“,”类型“:”注入“,” z“:” fa655376.2f5ea“,”名称“:”“,”主题“:” WeatherStation1 / BME280 / Trev2“,”有效载荷“:” 99“,”有效载荷类型“:” num“,” repeat“:”“,” 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”:true,“控制台”:false,“ tostatus”:false,“ complete”:“ true”,“ x”:720,“ y”:580,“电线”:[]}] < / p>
我看不到我在做什么错。道歉,如果很明显:)
答案 0 :(得分:2)
在这种情况下,使用如下配置的联接节点是一种更简单的方法:
测试所附的流程以查看其工作原理:
[{"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"]]}]
答案 1 :(得分:1)
检查您的if子句-
if(context.data.insidetemperature !== null && context.data.insidehumidity !== null) {
...
如果要进入该逻辑,则将两者都置位,并且您很可能缺少重置它们的逻辑。虽然看到context.data
有点惊讶,因为我通常希望看到类似的东西
context.get(..)
或context.global.get(...)
或global.get(..)
或flow.get(..)