node.js 在 Node-RED 中实现两个输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34473589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:41:29  来源:igfitidea点击:

Implementing two inputs in Node-RED

node.jsnode-red

提问by Pankesh

In my current project, we are trying to implement the current application functionality using Node-RED. The functionality is shown below. Here, Fire state receives two inputs: (1) TemperatureSensor(2) SmokeDetector. Both Sensors are publishing data using MQTT publishers. and Firestatecomponent can receives data through MQTT subsciber.

在我当前的项目中,我们正在尝试使用Node-RED实现当前的应用程序功能。功能如下所示。在这里, Fire 状态接收两个输入: (1) TemperatureSensor(2) SmokeDetector。两个 Sensor 都使用 MQTT 发布器发布数据。和Firestate部件可通过MQTT subsciber接收数据。

The fire state can produce an output based on the these two parameters that is if temperaturevalue > 70 and Smokevalue == true. In view of this, my question is -- Does Node-RED support the two inputs functionality?If yes, then how can we implement this functionality? If no, then.. Can I say that two input functionality can not be implemented using Node-RED???? As we have seen that Node-RED provides multiple outputs, but not inputs.

火灾状态可以基于这两个参数产生输出,即if temperaturevalue > 70 and Smokevalue == true。鉴于此,我的问题是——Node-RED 是否支持两个输入功能?如果是,那么我们如何实现这个功能?如果不是,那么.. 我可以说使用 Node-RED 不能实现两个输入功能吗????正如我们所见,Node-RED 提供多个输出,但不提供输入

enter image description here

在此处输入图片说明

回答by hardillb

You will need to use a function node and make use of the contextvariable to keep state between messages and use the message topic to determine which input a message came from.

您将需要使用一个函数节点并利用context变量来保持消息之间的状态,并使用消息主题来确定消息来自哪个输入。

Something like this:

像这样的东西:

context.temp = context.temp || 0.0;
context.smoke = context.smoke || false;

if (msg.topic === 'smokeDetector') {
  context.smoke = msg.payload;
} else if (msg.topic === 'tempSensor') {
  context.temp = msg.payload;
}

if (context.temp >= 70.0 && context.smoke) {
  return {topic: 'fireState', payload: 'FIRE!'}
} else {
  return null
}

More details can be found in the function node doc here

可以在此处的功能节点文档中找到更多详细信息

回答by SteveR

You can wire in any number of inputs to any node -- just be aware that your node will only see one input msg at a time. There is no inherent msg aggregation simply because there are multiple input wires.

您可以将任意数量的输入连接到任何节点——请注意,您的节点一次只能看到一个输入 msg。没有固有的 msg 聚合仅仅因为有多个输入线。

Instead, the task of aggregating multiple input msgs is handled by certain nodes -- some of which are built in to the core node-red server, and some that have been contributed by the community. Which one you should choose will depend upon the specific use case. For instance, should two objects be appended into an array, or merged into one big object? Only you will know what you want -- node-red does not make any assumptions, but gives you different nodes to handle many common use cases. For any other use cases, there is always the generic functionnode, in which you can use javascript to implement whatever behavior you need.

相反,聚合多个输入消息的任务由某些节点处理——其中一些内置在核心 node-red 服务器中,一些由社区贡献。您应该选择哪一个取决于具体的用例。例如,应该将两个对象附​​加到一个数组中,还是合并为一个大对象?只有您知道自己想要什么——node-red 不做任何假设,而是为您提供不同的节点来处理许多常见用例。对于任何其他用例,总有通用function节点,您可以在其中使用 javascript 来实现您需要的任何行为。

For your original question, you are looking for a way to merge 2 payloads from different sensors into a single object. The core joinand changenodes can be used for that, as can the node-red-contrib-bool-gateand node-red-contrib-aggregatornodes, found on the flow librarysite.

对于您最初的问题,您正在寻找一种将来自不同传感器的 2 个有效载荷合并为一个对象的方法。核心joinchange节点可以使用,如可以在node-red-contrib-bool-gatenode-red-contrib-aggregator节点,在发现流动图书馆网站。

Here's an example of combining two sensor inputs using the joinnode, and then using a switchnode with the expression payload.temp > 70 and payload.smoketo determine whether to send the msg down the flow:

这是使用join节点组合两个传感器输入的示例,然后使用switch带有表达式的节点payload.temp > 70 and payload.smoke来确定是否将 msg 发送到流中:

[
  {
    "id": "87df68f8.51ad58",
    "type": "inject",
    "z": "f9a2eec9.c2e26",
    "name": "",
    "topic": "smoke",
    "payload": "true",
    "payloadType": "bool",
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "x": 160,
    "y": 1180,
    "wires": [
      [
        "da4182a8.47939"
      ]
    ]
  },
  {
    "id": "3ad419ec.1453a6",
    "type": "inject",
    "z": "f9a2eec9.c2e26",
    "name": "",
    "topic": "smoke",
    "payload": "false",
    "payloadType": "bool",
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "x": 170,
    "y": 1140,
    "wires": [
      [
        "da4182a8.47939"
      ]
    ]
  },
  {
    "id": "a45b3cb0.f3312",
    "type": "inject",
    "z": "f9a2eec9.c2e26",
    "name": "",
    "topic": "temp",
    "payload": "65",
    "payloadType": "num",
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "x": 160,
    "y": 1220,
    "wires": [
      [
        "da4182a8.47939"
      ]
    ]
  },
  {
    "id": "a3b07d81.e6b17",
    "type": "inject",
    "z": "f9a2eec9.c2e26",
    "name": "",
    "topic": "temp",
    "payload": "75",
    "payloadType": "num",
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "x": 160,
    "y": 1260,
    "wires": [
      [
        "da4182a8.47939"
      ]
    ]
  },
  {
    "id": "da4182a8.47939",
    "type": "join",
    "z": "f9a2eec9.c2e26",
    "name": "join payloads",
    "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": 430,
    "y": 1200,
    "wires": [
      [
        "315c9ce3.570d64",
        "50f981b4.be654"
      ]
    ]
  },
  {
    "id": "315c9ce3.570d64",
    "type": "switch",
    "z": "f9a2eec9.c2e26",
    "name": "Trigger Alarm?",
    "property": "payload.temp > 70 and payload.smoke",
    "propertyType": "jsonata",
    "rules": [
      {
        "t": "true"
      }
    ],
    "checkall": "true",
    "repair": false,
    "outputs": 1,
    "x": 640,
    "y": 1200,
    "wires": [
      [
        "50f981b4.be654"
      ]
    ]
  },
  {
    "id": "50f981b4.be654",
    "type": "debug",
    "z": "f9a2eec9.c2e26",
    "name": "",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "x": 690,
    "y": 1260,
    "wires": [

    ]
  }
]

回答by Praveen Kumar Gulati

We can use Join Nodeand change its configurationby setting mode to manual, and use fixed number of messages as 2. Then once both the inputs are received you can invoke next function node. Join node can combine both payload as array or object. And then in last function code you can send the combined data to MQTT after checking your condition.

我们可以使用加入节点并通过将模式设置为手动来更改其配置,并使用固定数量的消息为 2。然后一旦接收到两个输入,您就可以调用下一个功能节点。加入节点可以将有效负载组合为数组或对象。然后在最后一个功能代码中,您可以在检查您的条件后将组合数据发送到 MQTT。