jsSwitch
jsSwitch
Component: Script-based Router. This component executes the configured JS script and dynamically routes messages to one or more output chains based on the script's return value.
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. For more details, refer to udf.
# Configuration
Field | Type | Description | Default Value |
---|---|---|---|
jsScript | string | JavaScript script | None |
jsScript
: This field can process and evaluatemsg
,metadata
,msgType
, anddataType
. It is the body of the following function:function Switch(msg, metadata, msgType, dataType) { ${jsScript} }
1
2
3Parameter Description:
- msg: Message content
- When dataType=JSON, it is of type
jsonObject
, and fields can be accessed usingmsg.temperature
. - When dataType=BINARY, it is of type
Uint8Array
, and the byte array can be manipulated directly, such as accessing the first byte withmsg[0]
. - For other dataTypes, it is of type
string
.
- When dataType=JSON, it is of type
- 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 type:
Array
, returning a string array containing one or more chain names to route to.
- msg: Message content
Note
- The script execution timeout configuration can be found at config.ScriptMaxExecutionTime.
- The returned chain names must be defined in the rule chain connections; otherwise, the message will be discarded.
# Relation Type
The routing relationship is dynamically determined by the script's return value. Messages can be routed to one or multiple output chains. If the returned chain name does not have a corresponding connection, the Default
chain will be used for routing.
# Execution Result
This component does not modify the content of msg
, metadata
, and msgType
. It is only used to determine the routing direction of the message.
# Configuration Examples
# Basic Routing Example
{
"id": "s1",
"type": "jsSwitch",
"name": "Script-based Routing",
"configuration": {
"jsScript": "if (msg.temperature > 50) return ['highTemp']; else if (msg.temperature < 10) return ['lowTemp']; else return ['normalTemp'];"
}
}
2
3
4
5
6
7
8
# Multi-Routing Example
{
"id": "s2",
"type": "jsSwitch",
"name": "Multi-Routing",
"configuration": {
"jsScript": "var routes = []; if (msgType === 'ALARM') routes.push('alarm'); if (msg.priority === 'high') routes.push('priority'); return routes;"
}
}
2
3
4
5
6
7
8
# Routing Based on Data Type Example
{
"id": "s3",
"type": "jsSwitch",
"name": "Data Type Routing",
"configuration": {
"jsScript": "var dt = String(dataType); if (dt === 'BINARY') { if (msg.length > 1024) return ['largeBinary']; else return ['smallBinary']; } else if (dt === 'JSON') return ['jsonData']; else return ['textData'];"
}
}
2
3
4
5
6
7
8
# JSON Data Routing Example
{
"id": "s4",
"type": "jsSwitch",
"name": "JSON Data Routing",
"configuration": {
"jsScript": "if (String(dataType) === 'JSON') { var routes = []; if (msg.temperature > 50) routes.push('highTemp'); if (msg.humidity > 80) routes.push('highHumidity'); if (msg.level === 'critical') routes.push('critical'); return routes.length > 0 ? routes : ['normal']; } return ['skip'];"
}
}
2
3
4
5
6
7
8
# Binary Device Data Routing Example
{
"id": "s5",
"type": "jsSwitch",
"name": "Device Data Routing",
"configuration": {
"jsScript": "if (String(dataType) === 'BINARY' && msg.length >= 4) { var deviceId = (msg[0] << 8) | msg[1]; var functionCode = (msg[2] << 8) | msg[3]; if (deviceId === 0x1001) { if (functionCode === 0x0001) return ['sensorData']; else if (functionCode === 0x0002) return ['statusData']; else if (functionCode === 0x0010) return ['commandData']; } return ['unknownDevice']; } return ['invalidData'];"
}
}
2
3
4
5
6
7
8
# Text Log Routing Example
{
"id": "s6",
"type": "jsSwitch",
"name": "Log Routing",
"configuration": {
"jsScript": "if (String(dataType) === 'TEXT') { var routes = []; if (msg.includes('ERROR')) routes.push('errorLog'); if (msg.includes('WARN')) routes.push('warnLog'); if (msg.includes('DEBUG')) routes.push('debugLog'); return routes.length > 0 ? routes : ['infoLog']; } return ['nonTextData'];"
}
}
2
3
4
5
6
7
8
# Mixed Data Handling and Routing Example
{
"id": "s7",
"type": "jsSwitch",
"name": "Mixed Data Routing",
"configuration": {
"jsScript": "var dt = String(dataType); var routes = []; if (msgType === 'ALARM') routes.push('alarm'); if (dt === 'JSON' && msg.priority === 'high') routes.push('priority'); else if (dt === 'BINARY' && msg.length > 0 && msg[0] === 0xFF) routes.push('protocolData'); else if (dt === 'TEXT' && msg.includes('URGENT')) routes.push('urgent'); return routes.length > 0 ? routes : ['default'];"
}
}
2
3
4
5
6
7
8