node-red 实践记录

node-red 实践记录

1、设置流程全局变量

  1. flow.set(“key”,value),在需要使用的地方 flow.get(“key”) 即可

  2. 通过 msg.key=value,在需要使用的地方 msg.key 即可

  3. 在function中通过设置msg.url 这样通过http-request时请求的url就不用填,默认会使用当前的msg.url值,这里有小伙伴可能比较疑惑,直接填url不香么,为何要多此一举?因为我的所有请求的前缀都是相同的,一旦接口的前缀一改变,那么我所有请求的前缀要每个节点挨个更改,所以通过此方法再结合flow.get(“key”)的方法就能解决这个麻烦了

    node-red 实践记录
    image.png
[
    {
        "id": "b36aa30.3a7276",
        "type": "http request",
        "z": "562d388907ed4689",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "url": "",
        "x": 660,
        "y": 640,
        "wires": [
            [
                "1ef9987c.956c78"
            ]
        ]
    },
    {
        "id": "11167f67.5d5031",
        "type": "inject",
        "z": "562d388907ed4689",
        "name": "cars on craigslist",
        "props": [
            {
                "p": "payload",
                "v": "http://vancouver.craigslist.org/search/sss?format=rss&query=cars",
                "vt": "str"
            },
            {
                "p": "topic",
                "v": "",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "topic": "",
        "payload": "http://vancouver.craigslist.org/search/sss?format=rss&query=cars",
        "payloadType": "str",
        "x": 330,
        "y": 640,
        "wires": [
            [
                "ef9470c51197abbd"
            ]
        ]
    },
    {
        "id": "1ef9987c.956c78",
        "type": "debug",
        "z": "562d388907ed4689",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 820,
        "y": 640,
        "wires": []
    },
    {
        "id": "ef9470c51197abbd",
        "type": "function",
        "z": "562d388907ed4689",
        "name": "",
        "func": "msg.url=msg.payloadnreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 510,
        "y": 640,
        "wires": [
            [
                "b36aa30.3a7276"
            ]
        ]
    }
]
  1. 通过设置msg.filename,可以设置全局的文件路径

2、数据转csv格式存储文件

node-red 实践记录
image.png
[
    {
        "id": "457d9ad6.b737b4",
        "type": "inject",
        "z": "562d388907ed4689",
        "name": "single",
        "props": [
            {
                "p": "payload",
                "v": "",
                "vt": "date"
            },
            {
                "p": "topic",
                "v": "",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 410,
        "y": 840,
        "wires": [
            [
                "1e05fafd.887b05"
            ]
        ]
    },
    {
        "id": "1e05fafd.887b05",
        "type": "change",
        "z": "562d388907ed4689",
        "name": "Generate single payload",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{ "a":$floor(100*$random()),"b":$floor(100*$random()),"c":$floor(100*$random())}",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 590,
        "y": 840,
        "wires": [
            [
                "e9546682.b39898"
            ]
        ]
    },
    {
        "id": "e9546682.b39898",
        "type": "csv",
        "z": "562d388907ed4689",
        "name": "",
        "sep": ",",
        "hdrin": "",
        "hdrout": "none",
        "multi": "one",
        "ret": "n",
        "temp": "a,b,c",
        "skip": "0",
        "strings": true,
        "x": 770,
        "y": 840,
        "wires": [
            [
                "8b9bfe29e6bdbfb3"
            ]
        ]
    },
    {
        "id": "f83ad3b0.78d32",
        "type": "debug",
        "z": "562d388907ed4689",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 1510,
        "y": 840,
        "wires": []
    },
    {
        "id": "8b9bfe29e6bdbfb3",
        "type": "file",
        "z": "562d388907ed4689",
        "name": "",
        "filename": "C:/Users/qinaguo/.node-red/asset/beixiang/user.json",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 1140,
        "y": 840,
        "wires": [
            [
                "f83ad3b0.78d32"
            ]
        ]
    }
]

本文章来源于互联网,如有侵权,请联系删除!原文地址:node-red 实践记录

相关推荐: 从无人机、物联网到 AI,极飞智慧农业落地开花

2019 年 10 月 28 日,一年一度的农业科技盛会—— XAAC 极飞科技年度大会 (XAG ANNUAL CONFERENCE ) 在广州召开。极飞科技CEO彭斌发布了全新的XP 2020款农业无人机系统和XIot 农业物联系统,引领作物管理智能化,重…