2022-10-13

Node-RED

Node-RED is a programming tool for wiring(连接/装线) together hardware devices, APIs and online services in new and interesting ways.

官页 官文档 官文档-getting-started

Running Node-RED locally

  1. 安装 npm

  2. npm install -g --unsafe-perm node-red 全局

  3. Once installed as a global module you can use the node-red command to start Node-RED in your terminal.

first flow

[first-flow]https://nodered.org/docs/tutorials/first-flow

  1. Access the editor With Node-RED running, open the editor in a web browser.

If you are using a browser on the same computer that is running Node-RED, you can access it with the url: http://localhost:1880.

If you are using a browser on another computer, you will need to use the ip address of the computer running Node-RED: http://<ip-address>:1880.

  1. Add an Inject node The Inject node allows you to inject messages into a flow, either by clicking the button on the node, or setting a time interval between injects.

Drag one onto the workspace from the palette.

Select the newly added Inject node to see information about its properties and a description of what it does in the Information sidebar pane.

  1. Add a Debug node The Debug node causes any message to be displayed in the Debug sidebar. By default, it just displays the payload of the message, but it is possible to display the entire message object.

  2. Wire the two together Connect the Inject and Debug nodes together by dragging between the output port of one to the input port of the other.

  3. Deploy At this point, the nodes only exist in the editor and must be deployed to the server.

Click the Deploy button.

  1. Inject With the Debug sidebar tab selected, click the Inject button (the small square button next to your inject node). You should see numbers appear in the sidebar. By default, the Inject node uses the number of milliseconds since January 1st, 1970 as its payload.

  2. Add a Function node The Function node allows you to pass each message though a JavaScript function.

Delete the existing wire (select it and press delete on the keyboard).

Wire a Function node in between the Inject and Debug nodes.

Double-click on the Function node to bring up the edit dialog. Copy the following code into the function field:

// Create a Date object from the payload
var date = new Date(msg.payload);
// Change the payload to be a formatted Date string
msg.payload = date.toString();
// Return the message so it can be sent on
return msg;

Click Done to close the edit dialog and then click the deploy button.

Now when you click the Inject button, the messages in the sidebar will now be formatted is readable timestamps.

flow 管理

flow 管理: 右上菜单 flows flow 禁用: 右上tab info

核心节点 The Core Nodes

The Core Nodes

common/inject

The Inject node allows you to inject messages into a flow, either by clicking the button on the node, or setting a time interval between injects.

注入节点可以启动带有特定载荷的流。 默认的载荷是时间戳, 时间戳的意思是从1970年1月1日到当前时间, 经历了多少毫秒。 该节点还支持注入字符串. 数字. 布尔值. JavaScript对象或流/全局上下文值。 默认情况下, 通过单击编辑器中的按钮来手动触发节点。它也可以被设置为定期注入或根据时间表(在特定的时间段内)进行注入。

in short 手动注入数据的一个节点, 调试很有用

function

主要对负载数据 进行处理, 使用JavaScript语法

上下文

在脚本中 可以是使用内置对象 context, context.set(key, val) context.get(key)访问和修改数据

 
var count = context.get('counter') || 0;
count += 1 
context.set('counter', count);
msg.payload = count;
return msg;

template

Dashboard / template 不同这个数据负载模版

change

可以对负载的属性 进行修改,删除, 属性 对负载内容替换,

switch

判断切换分支

range

类似, 归一化操作, 例如: 可以将01024 映射到 0255 ;

几个学习样例

Dashboard 节点库

安装

右上菜单 Setting Palette install 搜 node-red-dashboard

注意!! Dashboard 所有的Widget 需要添加到一个 Group 中方会显示; 而 Group 需要添加到一个 Tab

类似结构

Tab 
 |-Group
   |- Widget(button)
   |- Widget(text)
   |- ...

部署后访问: http://127.0.0.1:1880/ui/

可双击 Widget 选择其所在的 Group, 管理 Tab …

button

类似 inject, 按一下发送一个msg; http://127.0.0.1:1880/ui/

下拉框, 若 options 为空的话, 它可以接受上游的 msg.options的数组属性, 转为下拉选项

[{
    "名称1": 1
},
{
    "名称2": 2
}]

布局管理

在 config On all flows 可以配置全局信息; 例如 存放ui组件的 ui_group

template

template 其内容是html, 使用 mustache 模版语法

Mustache的模板语法很简单, 就那么几个:

{{keyName}} {{{keyName}}} {{#keyName}} {{/keyName}} {{^keyName}} {{/keyName}} {{.}} {{!comments}} {{>partials}}

  1. {{keyName}}简单的变量替换。
  2. {{#keyName}} {{/keyName}}以#开始. 以/结束表示区块, 它会根据当前上下文中的键值来对区块进行一次或多次渲染。它的功能很强大, 有类似if. foreach的功能。
  3. {{^keyName}} {{/keyName}}该语法与{{#keyName}} {{/keyName}}类似, 不同在于它是当keyName值为null, undefined, false时才渲染输出该区块内容。
  4. {{.}} {{.}}表示枚举, 可以循环输出整个数组
  5. {{! }}表示注释
  6. {{>partials}}以>开始表示子模块, 当结构比较复杂时, 我们可以使用该语法将复杂的结构拆分成几个小的子模块。

MySQL

需安装 node-red-node-mysql

mysql 节点, 流入属性topic 是执行的sql语句, 流出属性 payload是指令执行的结果