简体   繁体   English

Json - Node-RED Extract

[英]Json - Node-RED Extract

Noob looking for help... Noob寻求帮助......

I have a JSON stream of data which looks like this.. 我有一个JSON数据流,看起来像这样..

{
  "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"
    }
  }
}

From this in Node-RED i have a function node which looks like this... 从Node-RED中我有一个功能节点,看起来像这样......

return [msg.payload.telegram.functions];

Which returns these 哪些返回这些

{ "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" }

From these i would like to get a single value from each eg 830 for concentration. 从这些我希望从每个例如830获得单一值以进行浓缩。 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. 例如,如果输出1超过1000,则输出小于1000。

Is what i'm trying to achieve even possible in Node-RED?? 我想在Node-RED中实现什么?

Sorry for the possible NOOB question any help would be appreciated. 对不起可能的NOOB问题,任何帮助将不胜感激。

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. 虽然您从函数返回的是JSON对象,但它不符合Node-RED约定。

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 }

Then downstream nodes will know to look in the msg.payload for the useful content of the message. 然后,下游节点将知道在msg.payload中查找消息的有用内容。

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};
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM