jsFilter
jsFilter
Component: JavaScript Script Filter. This component uses JavaScript scripts to filter messages (msg
), metadata (metadata
), and message types (msgType
). The routing direction of the message is determined by the return value of the script (True or False chain).
JavaScript scripts support ECMAScript 5.1(+) syntax specifications and some ES6 features, such as async/await/Promise/let. Custom Go functions can be called within the script. Please refer to udf.
# Configuration
Field | Type | Description | Default Value |
---|---|---|---|
jsScript | string | JavaScript script | None |
jsScript
: The JavaScript script used to write filtering logic. The content of this field will be encapsulated within the following function body:function Filter(msg, metadata, msgType, dataType) { ${jsScript} }
1
2
3Parameter Description:
- msg: Message content
- When dataType=JSON, the type is
jsonObject
, and properties can be accessed directly using dot notation, such asmsg.temperature
. - When dataType=BINARY, the type is
Uint8Array
, and the byte array can be manipulated directly, such as accessing the first byte withmsg[0]
. - For other dataTypes, the type is
string
.
- When dataType=JSON, the type is
- metadata: Message metadata, of type
jsonObject
. - msgType: Message type, of type
string
. - dataType: Message data type (JSON, TEXT, BINARY, etc.), which needs to be converted to a string using
String(dataType)
. - Return value: Must return a
boolean
type to determine the routing direction of the message.
- msg: Message content
Notes
- The
jsScript
script must have a clear return value (return true/false). - Script execution has a timeout limit, which can be configured through config.ScriptMaxExecutionTime.
# Relation Type
- True: When the script returns true, the message will continue to be passed along the
True
chain path. - False: When the script returns false, the message will continue to be passed along the
False
chain path. - Failure: When the script execution fails, the message will continue to be passed along the
Failure
chain path.
# Execution Result
This component is a pure filtering component and will not modify the content of the incoming msg
, metadata
, and msgType
.
# Configuration Examples
# Basic Filtering Example
{
"id": "s1",
"type": "jsFilter",
"name": "Filter",
"configuration": {
"jsScript": "return msg.temperature > 50;"
}
}
2
3
4
5
6
7
8
# Filtering Example Based on Data Type
{
"id": "s2",
"type": "jsFilter",
"name": "Binary Data Filtering",
"configuration": {
"jsScript": "if (String(dataType) === 'BINARY') { return msg.length > 10 && msg[0] === 0xFF; } else { return true; }"
}
}
2
3
4
5
6
7
8
# JSON Data Filtering Example
{
"id": "s3",
"type": "jsFilter",
"name": "JSON Data Filtering",
"configuration": {
"jsScript": "if (String(dataType) === 'JSON') { return msg.temperature > 25 && msg.humidity < 80; } else { return false; }"
}
}
2
3
4
5
6
7
8
# Binary Data Parsing and Filtering Example
{
"id": "s4",
"type": "jsFilter",
"name": "Device Protocol Filtering",
"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;"
}
}
2
3
4
5
6
7
8
# Text Data Filtering Example
{
"id": "s5",
"type": "jsFilter",
"name": "Text Content Filtering",
"configuration": {
"jsScript": "if (String(dataType) === 'TEXT') { return msg.indexOf('ERROR') !== -1 || msg.indexOf('ALARM') !== -1; } return true;"
}
}
2
3
4
5
6
7
8
# Composite Condition Filtering Example
{
"id": "s6",
"type": "jsFilter",
"name": "Composite Condition Filtering",
"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;"
}
}
2
3
4
5
6
7
8
# Application Example
If the msgType
is EVENT_APP1
, push the message to: http://192.168.136.26:9099/app1/api/msg (opens new window) ; otherwise, push it to: http://192.168.136.26:9099/app2/api/msg (opens new window)
{
"ruleChain": {
"id":"rule01",
"name": "Test Rule Chain",
"root": true
},
"metadata": {
"nodes": [
{
"id": "s1",
"type": "jsFilter",
"name": "Filter",
"configuration": {
"jsScript": "return msgType =='EVENT_APP1';"
}
},
{
"id": "s2",
"type": "restApiCall",
"name": "Push Data - app1",
"configuration": {
"restEndpointUrlPattern": "http://192.168.136.26:9099/app1/api/msg",
"requestMethod": "POST",
"maxParallelRequestsCount": 200
}
},
{
"id": "s3",
"type": "restApiCall",
"name": "Push Data - 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"
}
]
}
}
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