Node-RED 笔记
2020-02-02 04:05:50 0 举报
AI智能生成
Node-RED 笔记
作者其他创作
大纲/内容
User Guide
Getting Started
Installing Node-RED
Run Local
Getting started
Running locally
Install from git
Docker
DockerHub
Github Dockerfile
Standar version
docker run -d \
--restart=always \
--name 'nodered' \
--hostname 'nodered' \
-e TZ=Asia/Shanghai \
-p 1880:1880 \
-v "${HOME}/data/node-red-data:/data" \
nodered/node-red:latest
--restart=always \
--name 'nodered' \
--hostname 'nodered' \
-e TZ=Asia/Shanghai \
-p 1880:1880 \
-v "${HOME}/data/node-red-data:/data" \
nodered/node-red:latest
Minimal version
The minimal version does not contain build tools or support for projects.
The minimal versions (without python and build tools) are not able to install nodes that require any locally compiled native code.
不能安装需要在本地编译原生代码的节点
docker run -d \
--restart=always \
--name 'nodered-minimal' \
--hostname 'nodered-minimal' \
-e TZ=Asia/Shanghai \
-p 1881:1880 \
-v "${HOME}/data/node-red-data:/data" \
nodered/node-red:latest-minimal
--restart=always \
--name 'nodered-minimal' \
--hostname 'nodered-minimal' \
-e TZ=Asia/Shanghai \
-p 1881:1880 \
-v "${HOME}/data/node-red-data:/data" \
nodered/node-red:latest-minimal
TODO 绑定串口设备
default userDir change to "/data"
Settings file : /data/settings.js
Context store : 'default' [module=memory]
User directory : /data
Projects disabled : editorTheme.projects.enabled=false
Flows file : /data/flows.json
Docker Environment configuration
PORT
set value to uiPort
the port used to serve the editor UI.
Default: 1880.
Default:empty
FLOWS
If you set FLOWS="" then the flow file can be set via the flowFile property in the settings.js file.
Default:flows.json
NODE_RED_ENABLE_SAFE_MODE
setting to true starts Node-RED in safe (not running) mode
Default:empty
NODE_RED_ENABLE_PROJECTS
setting to true starts Node-RED with the projects featur
Default:empty
TZ
TZ=Asia/Shanghai
On a Device
Raspberry Pi
BeagleBone Black
Interacting with Arduino
Android
In the Cloud
IBM Cloud
SenseTecnic FRED
Amazon Web Services
Microsoft Azure
Creating your first flow
Node-RED Concepts
Node
节点
节点
节点是流程的基本构成块。
触发方式
接收到前一个节点的消息
等待外部事件触发
输入输出限制
最多只能有一个输入
任意多个输出
Configuration node
配置节点
配置节点
配置节点是一种特殊类型的节点,它包含可重用的配置,可以在流程中的一些特定节点间共享。
例如
MQTT In 和 MQTT Out 节点使用同一个MQTT Broker config node,代表共用同一个MQTT连接。
Flow
流程
流程
一个流程代表了在编辑工作空间的一个tab选项卡,是组织节点的主要方式。
术语“flow”,也用于非正式的描述一组连接的节点的集合。因此一个tab选项卡也可以包含多个流程。
Context
上下文
上下文
Context是存储信息的一种方式,它可以在多个节点之间共享,无需通过消息的方式在流程中传递。
Scope
作用域
作用域
Node
单个节点可见
Flow
同一个flow所有节点可见(或在同一个tab选型卡编辑空间内所有节点可见)
Global
所有节点可见(跨flow,跨tab)
持久化
默认的,使用 in-memory Context 存储信息,重启后保存的值不能访问。
可以配置成 file-system based store,需要使用存储插件
Message
消息
消息
是指在一个flow中,节点间传输的内容。
Message 是简单 JavaScript 对象,可以包含任意的属性集合。
在编辑器中,通常被定义为msg。
通常,msg.payload 属性包含最有效的信息。
Subflow
子流程
子流程
subflow是一组节点的集合,在工作空间中被折叠成一个单独节点。
用途
在视觉上减少流程的复杂度
把多个节点打包成subflow,作为一个可重用的组件,在多个地方使用。
Wire
连接线
连接线
它连接了节点,展示消息如何在flow中流转。
Palette
节点面板
节点面板
它在编辑器的左侧,并列出了可用节点。
额外的节点也可以安装到palette,通过命令行或节点管理器(Palette Manager)
Workspace
工作空间
工作空间
它是开发flow的主要区域,在这里通过从Palette中拖拽节点和连线的方式开发flow。
Sidebar
侧边栏
侧边栏
在编辑区的右侧的sidebar,包含了很多有用的工具面板。
Using the Node-RED Editor
页面操作介绍
Configuring Node-RED
The Settings file
default path
${HOME}/.node-red/settings.js
Custom settings file
"--settings" command-line argument can be used when starting Node-RED to point at a different file.
default settings file
Configuration options
Runtime Configuration
flowFile
the file used to store the flows.
Default: flows_<hostname>.json
userDir
the directory to store all user data, such as flow and credential files and all library data.
Default: $HOME/.node-red
nodesDir
a directory to search for additional installed nodes.
Node-RED searches the nodes directory under the userDir directory.
This property allows an additional directory to be searched, so that nodes can be installed outside of the Node-RED install structure.
Default: $HOME/.node-red/nodes
uiHost
the interface to listen for connections on.
Default: 0.0.0.0 - all IPv4 interfaces.
uiPort
the port used to serve the editor UI.
Default: 1880.
httpAdminRoot
the root url for the editor UI.
If set to false, all admin endpoints are disabled.
This includes both API endpoints and the editor UI.
To disable just the editor UI, see the disableEditor property
Default: /
httpAdminAuth
Deprecated: see adminAuth
httpNodeRoot
the root url for nodes that provide HTTP endpoints.
If set to false, all node-based HTTP endpoints are disabled.
Default: /
httpNodeAuth
enables HTTP Basic Authentication.
example
{user:"nol", pass:"5f4dcc3b5aa765d61d8327deb882cf99"}
httpRoot
this sets the root url for both admin and node endpoints.
It overrides the values set by httpAdminRoot and httpNodeRoot.
https
enables https, with the specified options object
disableEditor
if set to true, prevents the editor UI from being served by the runtime.
The admin api endpoints remain active.
Default: false.
httpStatic
a local directory from which to serve static web content from.
httpStaticAuth
a local directory from which to serve static web content from.
httpNodeCors
enables cross-origin resource sharing for the nodes that provide HTTP endpoints
httpNodeMiddleware
an HTTP middleware function that is added to all HTTP In nodes.
This allows whatever custom processing, such as authentication, is needed for the nodes.
logging
currently only console logging is supported.
logging level
default: info
Editor Configuration
adminAuth
enables user-level security in the editor and admin API.
paletteCategories
defines the order of categories in the palette.
If a node’s category is not in the list, the category will get added to the end of the palette.
default
['subflows', 'input', 'output', 'function', 'social', 'storage', 'analysis', 'advanced'],
Editor Themes
Dashboard
ui
The home path for the Node-RED-Dashboard add-on nodes can specified.
This is relative to any already defined httpNodeRoot
example
ui : { path: “mydashboard” },
Node Configuration
functionGlobalContext
Function Nodes - a collection of objects to attach to the global function context.
Default: empty
debugMaxLength
Debug Nodes - the maximum length, in characters, of any message sent to the debug sidebar tab.
Default: 1000
mqttReconnectTime
MQTT Nodes - if the connection is lost, how long to wait, in milliseconds, before attempting to reconnect.
Default: 5000
serialReconnectTime
Serial Nodes - how long to wait, in milliseconds, before attempting to reopen a serial port.
Default: 5000
Securing Node-RED
Editor & Admin API security
Username/password based authentication
settings.js -> adminAuth
adminAuth: {
type: "credentials",
users: [
{
username: "admin",
password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.",
permissions: "*"
},
{
username: "george",
password: "$2b$08$wuAqPiKJlVN27eF5qJp.RuQYuy6ZYONW7a/UWYxDTtwKFCdB8F19y",
permissions: "read"
}
]
}
type: "credentials",
users: [
{
username: "admin",
password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.",
permissions: "*"
},
{
username: "george",
password: "$2b$08$wuAqPiKJlVN27eF5qJp.RuQYuy6ZYONW7a/UWYxDTtwKFCdB8F19y",
permissions: "read"
}
]
}
Generating the password hash
node-red-admin
node-red-admin hash-pw
node -e "console.log(require('bcryptjs').hashSync(process.argv[1], 8));" your-password-here
OAuth/OpenID based authentication
Setting a default user
User permissions
Token expiration
Accessing the Admin API
Custom user authentication
HTTP Node security
settings.js -> httpNodeAuth
httpNodeAuth: {user:"user",pass:"$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN."},
Logging
Console logger
level
metrics
audit
Custom logging module
Example logger
logstash
Command line arguments
--port
port to listen on
--settings
use specified settings file
--title
process window title
--userDir
use specified user directory
--verbose
enable verbose output
--safe
enable safe mode
Using Node-RED
The core nodes
Inject
The Inject node can be used to manual trigger a flow by clicking the node’s button within the editor.
It can also be used to automatically trigger flows at regular intervals.
types
a flow or global context property value
a String, number, boolean, Buffer or Object
a Timestamp in milliseconds since January 1st, 1970
Debug
The Debug node can be used to display messages in the Debug sidebar within the editor.
Function
The Function node allows JavaScript code to be run against the messages that are passed through it.
Change
The Change node can be used to modify a message’s properties and set context properties without having to resort to a Function node.
Switch
The Switch node allows messages to be routed to different branches of a flow by evaluating a set of rules against each message.
Template
文本模版
文本模版
The Template node can be used to generate text using a message’s properties to fill out a template.
It uses the Mustache templating language to generate the result.
sequences
序列(消息集合)
序列(消息集合)
Split
消息拆分
消息拆分
Turns a single message into a sequence of messages.
单个消息 -> 消息集合
Join
消息合并
消息合并
Turns a sequence of messages into a single message.
消息集合 -> 单个消息
Sort
消息排序
消息排序
Sorts the sequence based on a property value or JSONata expression result.
Batch
消息合并分组
消息合并分组
多个消息 -> 消息集合
三种模式
Number of messages
按指定数量分组
按指定数量分组
groups messages into sequences of a given length.
The overlap option specifies how many messages at the end of one sequence should be repeated at the start of the next sequence.
“队末队首重叠数量”选项指定在一个集合队末和下一个集合的队首应该有多少条重复的消息。
Time interval
按时间间隔分组
按时间间隔分组
groups messages that arrive within the specified interval.
If no messages arrive within the interval, the node can optionally send on an empty message.
Concatenate Sequences
按主题分组
按主题分组
Adding nodes to the palette
Node-RED library
Using the Editor
使用节点管理器动态添加节点
Installing with npm
命令行添加
Installing individual node files
This is only really recommended for development purposes.
通过文件方式添加,针对开发阶段。
Upgrading nodes
Using the Function node
Working with context
Context scopes
Node - only visible to the node that set the value
Flow - visible to all nodes on the same flow (or tab in the editor)
Global - visible to all nodes
Flow - visible to all nodes on the same flow (or tab in the editor)
Global - visible to all nodes
Context stores
settings.js -> contextStorage
contextStorage: {
default: "memoryOnly",
memoryOnly: { module: 'memory' },
file: { module: 'localfilesystem' }
}
default: "memoryOnly",
memoryOnly: { module: 'memory' },
file: { module: 'localfilesystem' }
}
Using context in a flow
write
Use Change node to set value
read
Various nodes can access context directly.
Using context in a Function node
Using context in a custom node
Deleting context from the file store
Context can be permenantly deleted by using a Change node set to delete.
Working with messages
Using environment variables
Setting a node property
使用方式:${ENV_VAR}
支持节点属性值整体替换
不支持节点属性值部分替换
CLIENT-${HOST}
Using the TypedInput widget
JSONata Expressions
$env('ENV_VAR')
Function node
let foo = env.get("FOO");
Subflow Instance properties
子流程的实例属性是以环境变量的形式维护的。
在实例化子流程的时候,可以通过环境变量的方式维护子流程属性,子流程内部节点可以通过访问环境变量的方式读取数据。
子流程通过环境变量的方式传递入参。
Handling errors
Catchable errors
可捕获异常
可捕获异常
可以通过 catch node 捕获到的异常
每个流程必须添加 catch node 捕获异常
Uncatchable errors
不可捕获异常
不可捕获异常
节点故障了,可能打印错误日志了,但未抛出 exception,不能被 catch node 捕获的异常。
节点开发者应该以合理的方式把异常状态通知给使用方
节点应该提供可选方式处理异常。
例如
更新状态属性(可以被 status node 监控)
发送包含格外属性的消息,指明错误信息
uncaughtException errors
未捕获异常
未捕获异常
uncaughtException 其实是 NodeJS 进程的一个事件。如果进程里产生了一个异常而没有被任何Try Catch捕获会触发这个事件
Node.js是单线程的,如果发生未捕获异常,会导致整个进程shutdown
常见场景,异步任务中抛出异常,而且没有 registered an error handler。
Working with projects
多个项目
Project Dependencies
package.json
node modules the project depends on
Project Settings
flow files
the encryption configuration credentials
local git branches and remote repositories
Command-line Admin
The node-red-admin command-line tool allows you to remotely administer a Node-RED instance.
Advanced
Embedding into an existing app
Frequently Asked Questions
Tutorials
Node-RED Cookbook
Creating Nodes
Developing Node-RED
API Reference
Node-RED module APIs
The APIs provided by npm modules that Node-RED is built from. These can be used to embed Node-RED into existing Node.js applications.
Admin HTTP API
This HTTP-based API can be used to remotely administer the runtime. It is used by the Node-RED Editor and command-line admin tool.
Authentication
Types
Node
Subflow
Complete Flow configuration
A Complete Flow configuration represents the entire set of flows active in the runtime.
It is represented as a flat array of Node objects.
Single Flow configuration
A Single Flow configuration represents what gets presented in the editor as a tab.
Node Module
A Node Module represents the collection of Node Sets provided by an npm package.
Node Set
A Node Set represents the collection of types provided by a single file within a Node Module.
Errors
Methods
API list
Storage API
This API provides a pluggable way to configure where the Node-RED runtime stores data.
Context Store API
This API provides a pluggable way to store context data outside of the runtime.
Editor UI Widgets
A set of jQuery widgets that can be used within a node’s edit template.
常用库
node-red-dashboard
node-red-contrib-modbus
npm packages
Node-RED Library
0 条评论
下一页