Json-Node-RED提取 [英] Json - Node-RED Extract

查看:841
本文介绍了Json-Node-RED提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Noob正在寻求帮助...

Noob looking for help...

我有一个看起来像这样的JSON数据流.

I have a JSON stream of data which looks like this..

{
  "header" : {
    "content" : "telegram",
    "gateway" : "EN-GW",
    "timestamp" : "2016-08-08T13:45:47.032+0100"
  },
  "telegram" : {
    "deviceId" : "01864892",
    "friendlyId" : "Boardroom-CO2-Sensor",
    "timestamp" : "2016-08-08T13:45:47.032+0100",
    "direction" : "from",
    "functions" : [ {
      "key" : "humidity",
      "value" : 39.00,
      "unit" : "%"
    }, {
      "key" : "concentration",
      "value" : 830.00,
      "unit" : "ppm"
    } ],
    "telegramInfo" : {
      "data" : "4E53820E",
      "status" : "0",
      "dbm" : -67,
      "rorg" : "A5"
    }
  }
}

在Node-RED中,我有一个看起来像这样的功能节点...

From this in Node-RED i have a function node which looks like this...

return [msg.payload.telegram.functions];

哪个返回这些

{ "key": "concentration", "value": 830, "unit": "ppm", "_msgid": "ff5b0f47.00a4f" }

{ "key": "humidity", "value": 39, "unit": "%", "_msgid": "ff5b0f47.00a4f" }

{ "key": "temperature", "value": 26.6, "unit": "°C", "_msgid": "ef2d6de7.10d29" }

从这些我想从每个例如得到一个单一的价值830专心.然后让它对照我在具有两个输出的节点中设置的阈值进行检查.例如,如果输出1000大于1000,则输出2小于1000.

From these i would like to get a single value from each e.g. 830 for concentration. Then have it check against thresholds set by me in a node with two outputs. For example if more than 1000 output 1, less than 1000 output 2.

我正在努力实现甚至在Node-RED中实现的目标吗?

Is what i'm trying to achieve even possible in Node-RED??

很抱歉遇到NOOB问题,我们将不胜感激.

Sorry for the possible NOOB question any help would be appreciated.

推荐答案

问题可能是您没有返回格式正确的消息对象.从函数返回的是JSON对象,但它不符合Node-RED约定.

The problem is probably that you are not returning a well formatted message object. While what you are returning from the function is a JSON object it is not conforming to Node-RED convention.

如果希望其他节点能够轻松处理它,则从功能节点返回的所有内容都应具有有效负载字段.

Anything you return from a function node should have a payload field if you want other nodes to be able to handle it easily.

因此,如果您将退货更改为如下所示:

So if you change the return to look something like this:

return { payload: msg.payload.telegram.functions }

然后下游节点将知道在msg.payload中查找消息的有用内容.

Then downstream nodes will know to look in the msg.payload for the useful content of the message.

关于比较消息中的键以设置值并输出相对简单的不同值.在一个新的功能节点中,您可以这样:

As for comparing keys within that message to set values and outputting different values that's relatively simple. In a new function node you could do it like this:

//check concentration exists
for (var i=0;i<msg.payload.length; i++) {
    if (msg.payload[i].concentration) {
       //more than 1000
       if (msg.payload.conentration >= 1000) {
           return {payload: 1};
       //less than 1000
       } else {
           return {payload: 0};
       }
    }
}

这篇关于Json-Node-RED提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆