【问题标题】:Implementing two inputs in Node-RED在 Node-RED 中实现两个输入
【发布时间】:2015-12-26 17:32:38
【问题描述】:

在我当前的项目中,我们正在尝试使用 Node-RED 实现当前的应用程序功能。功能如下所示。在这里,火灾状态接收两个输入:(1) TemperatureSensor (2) SmokeDetector。两个传感器都使用 MQTT 发布者发布数据。而Firestate组件可以通过MQTT订阅者接收数据。

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

【问题讨论】:

  • 我认为您需要将第一个输入保存在某处,当第二个输入进入时,您需要让第一个输入在 if 语句中使用它。在此设置中,您无法同时获得两个输入。如果您不使用 MQTT,您可以采取不同的做法,您的 FireState 需要在一定的时间间隔内向您的传感器询问当前值并在 if 语句中使用它们。

标签: node.js node-red


【解决方案1】:

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

类似这样的:

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
}

更多细节可以在功能节点文档here中找到

【讨论】:

  • 访问上下文的方法变了,请看文档链接
  • 今天documentation 更喜欢使用像context.get('count')||0; 这样的语法。在 0.13 之前,它的使用方式如答案中所述,之后如这里所写。所描述的方法已被弃用。
【解决方案2】:

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

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

对于您最初的问题,您正在寻找一种将来自不同传感器的 2 个有效载荷合并到一个对象中的方法。核心 joinchange 节点可用于此目的,node-red-contrib-bool-gatenode-red-contrib-aggregator 节点也可用于此目的,可在 flow library 站点上找到。

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

[
  {
    "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": [

    ]
  }
]

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多