js脚本过滤器
jsFilter
组件:JavaScript脚本过滤器。使用JavaScript脚本对消息(msg)、元数据(metadata)、消息类型(msgType)进行过滤。根据脚本返回值决定消息的路由方向(True或者False链)。
JavaScript脚本支持ECMAScript 5.1(+) 语法规范和部分ES6规范,如:async/await/Promise/let。允许在脚本中调用Go自定义函数,请参考udf 。
# 配置
字段 | 类型 | 说明 | 默认值 |
---|---|---|---|
jsScript | string | js脚本 | 无 |
jsScript
:用于编写过滤逻辑的JavaScript脚本。该字段内容会被封装到以下函数体中:function Filter(msg, metadata, msgType, dataType) { ${jsScript} }
1
2
3参数说明:
- msg:消息内容
- 当dataType=JSON时,类型为
jsonObject
,可直接使用点号访问属性,如msg.temperature
- 当dataType=BINARY时,类型为
Uint8Array
,可直接操作字节数组,如msg[0]
访问第一个字节 - 其他dataType时,类型为
string
- 当dataType=JSON时,类型为
- metadata:消息元数据,类型为
jsonObject
- msgType:消息类型,类型为
string
- dataType:消息数据类型(JSON、TEXT、BINARY等),需要使用
String(dataType)
转换为字符串使用 - 返回值:必须返回
boolean
类型,决定消息的路由方向
- msg:消息内容
注意事项
jsScript
脚本必须有明确的返回值(return true/false)- 脚本执行有超时限制,可通过config.ScriptMaxExecutionTime配置
# Relation Type
- True: 脚本返回true时,消息将沿
True
链路径继续传递 - False: 脚本返回false时,消息将沿
False
链路径继续传递 - Failure: 脚本执行出错时,消息将沿
Failure
链路径继续传递
# 执行结果
该组件是纯过滤组件,不会修改传入的msg
、metadata
和msgType
内容。
# 配置示例
# 基本过滤示例
{
"id": "s1",
"type": "jsFilter",
"name": "过滤",
"configuration": {
"jsScript": "return msg.temperature > 50;"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 根据数据类型过滤示例
{
"id": "s2",
"type": "jsFilter",
"name": "二进制数据过滤",
"configuration": {
"jsScript": "if (String(dataType) === 'BINARY') { return msg.length > 10 && msg[0] === 0xFF; } else { return true; }"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# JSON数据过滤示例
{
"id": "s3",
"type": "jsFilter",
"name": "JSON数据过滤",
"configuration": {
"jsScript": "if (String(dataType) === 'JSON') { return msg.temperature > 25 && msg.humidity < 80; } else { return false; }"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 二进制数据解析过滤示例
{
"id": "s4",
"type": "jsFilter",
"name": "设备协议过滤",
"configuration": {
"jsScript": "if (String(dataType) === 'BINARY' && msg.length >= 4) { var deviceId = (msg[0] << 8) | msg[1]; var functionCode = (msg[2] << 8) | msg[3]; return deviceId === 0x1001 && (functionCode === 0x0001 || functionCode === 0x0002); } return false;"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 文本数据过滤示例
{
"id": "s5",
"type": "jsFilter",
"name": "文本内容过滤",
"configuration": {
"jsScript": "if (String(dataType) === 'TEXT') { return msg.indexOf('ERROR') !== -1 || msg.indexOf('ALARM') !== -1; } return true;"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 复合条件过滤示例
{
"id": "s6",
"type": "jsFilter",
"name": "复合条件过滤",
"configuration": {
"jsScript": "var dt = String(dataType); if (dt === 'JSON') { return msg.level === 'critical'; } else if (dt === 'BINARY') { return msg.length > 0 && msg[0] === 0xAA; } else if (dt === 'TEXT') { return msg.includes('URGENT'); } return false;"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 应用示例
如果msgType是:EVENT_APP1,则把消息推送到:http://192.168.136.26:9099/app1/api/msg,否则推送到:http://192.168.136.26:9099/app2/api/msg
{
"ruleChain": {
"id":"rule01",
"name": "测试规则链",
"root": true
},
"metadata": {
"nodes": [
{
"id": "s1",
"type": "jsFilter",
"name": "过滤",
"configuration": {
"jsScript": "return msgType =='EVENT_APP1';"
}
},
{
"id": "s2",
"type": "restApiCall",
"name": "推送数据-app2",
"configuration": {
"restEndpointUrlPattern": "http://192.168.136.26:9099/app1/api/msg",
"requestMethod": "POST",
"maxParallelRequestsCount": 200
}
},
{
"id": "s3",
"type": "restApiCall",
"name": "推送数据-app2",
"configuration": {
"restEndpointUrlPattern": "http://192.168.136.26:9099/app2/api/msg",
"requestMethod": "POST",
"maxParallelRequestsCount": 200
}
}
],
"connections": [
{
"fromId": "s1",
"toId": "s2",
"type": "True"
},
{
"fromId": "s1",
"toId": "s3",
"type": "False"
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/06/23, 10:45:49