luaFilter
luaFilter Component: Lua Script Filter. It can filter msg, metadata, and msgType using Lua scripts. The message is routed to the True or False chain based on the script's return value. It can also load third-party Lua libraries to perform advanced operations such as encryption/decryption, I/O, networking, and file handling.
To enable advanced operations such as encryption/decryption, I/O, networking, and file handling in Lua scripts, the following configuration is required:
config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")
Lua scripts support Lua 5.1 syntax. For more details, refer to gopher-lua (opens new window).
# Configuration
| Field | Type | Description | Default Value |
|---|---|---|---|
| script | string | Lua script or file path of a Lua script with a .lua suffix | None |
script: This can filtermsg,metadata, andmsgType. Only the function body content is required. If it is a file path, a complete script function must be provided:function Filter(msg, metadata, msgType, dataType) ${script} end1
2
3- msg: Message content. If dataType=JSON, it can be accessed using
msg.temperature. If dataType is of another type, the field type isstring. - metadata: Message metadata, type:
jsonObject. - msgType: Message type.
- dataType: Message data type (JSON, TEXT, BINARY, etc.), which needs to be used as a string in Lua.
- Return value type:
boolean, which determines the connection to the next node (Relation Type).
- msg: Message content. If dataType=JSON, it can be accessed using
Data Type Handling
- JSON Type: The
msgparameter is a Lua table, and fields can be accessed directly, such asmsg.temperature. - BINARY Type: The
msgparameter is a Lua table (byte array), and the first byte can be accessed usingmsg[1](1-based index). - Other Types: The
msgparameter is a string.
# Relation Type
- True: Send the message to the
Truechain. - False: Send the message to the
Falsechain. - Failure: In case of execution failure, send the message to the
Failurechain.
# Execution Result
This component does not modify the content of msg, metadata, and msgType.
# Configuration Examples
# Basic JSON Filtering Example
{
"id": "s1",
"type": "x/luaFilter",
"name": "Filter",
"configuration": {
"script": "return msg.temperature > 50"
}
}
2
3
4
5
6
7
8
# Data Type Detection Example
{
"id": "s2",
"type": "x/luaFilter",
"name": "Data Type Filtering",
"configuration": {
"script": "if dataType == 'JSON' then return msg.temperature > 25 elseif dataType == 'BINARY' then return #msg > 10 else return string.len(msg) > 5 end"
}
}
2
3
4
5
6
7
8
# Binary Data Filtering Example
{
"id": "s3",
"type": "x/luaFilter",
"name": "Binary Header Check",
"configuration": {
"script": "return dataType == 'BINARY' and #msg >= 2 and msg[1] == 255 and msg[2] == 254"
}
}
2
3
4
5
6
7
8
# Device Protocol Filtering Example
{
"id": "s4",
"type": "x/luaFilter",
"name": "Device Data Filtering",
"configuration": {
"script": "if dataType == 'BINARY' and #msg >= 4 then local deviceId = msg[1] * 256 + msg[2]; local functionCode = msg[3] * 256 + msg[4]; return deviceId == 4097 and (functionCode == 1 or functionCode == 2) else return false end"
}
}
2
3
4
5
6
7
8
# Text Content Filtering Example
{
"id": "s5",
"type": "x/luaFilter",
"name": "Log Level Filtering",
"configuration": {
"script": "return dataType == 'TEXT' and (string.find(msg, 'ERROR') ~= nil or string.find(msg, 'WARN') ~= nil)"
}
}
2
3
4
5
6
7
8
# Custom Functions
You can register golang functions with config.RegisterUdf and use them in Lua scripts. The registration method and usage are the same as Js. For details, refer to Udf.
# Loading Lua Modules
You can extend Lua capabilities by loading Lua custom or third-party module libraries. Use the following method to register Lua modules to the engine:
import luaEngine "github.com/rulego/rulego-components/pkg/lua_engine"
luaEngine.Preloader.Register(func(state *lua.LState) {
//load module
//libs.Preload(state)
})
2
3
4
5
6
Creating a module by Go (opens new window)
In addition, the framework has built-in a large number of Lua third-party library modules. Use the following method to enable them, which are not loaded by default:
config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")
After starting, you can perform advanced operations such as encryption/decryption, I/O, network, database, file, etc. The list of third-party libraries is:
- argparse (opens new window) argparse CLI parsing https://github.com/luarocks/argparse (opens new window)
- base64 (opens new window) encoding/base64 (opens new window) api
- cloudwatch (opens new window) aws cloudwatch log access
- cert_util (opens new window) monitoring ssl certs
- chef (opens new window) chef client api
- cmd (opens new window) cmd port
- crypto (opens new window) calculate md5, sha256 hash for string
- db (opens new window) access to databases
- filepath (opens new window) path.filepath port
- goos (opens new window) os port
- http (opens new window) http.client && http.server
- humanize (opens new window) humanize github.com/dustin/go-humanize (opens new window) port
- inspect (opens new window) pretty print github.com/kikito/inspect.lua (opens new window)
- ioutil (opens new window) io/ioutil port
- json (opens new window) json implementation
- log (opens new window) log port
- plugin (opens new window) run lua code in lua code
- pprof (opens new window) pprof http-server for golang from lua
- prometheus (opens new window) prometheus exporter
- regexp (opens new window) regexp port
- runtime runtime port
- pb (opens new window) https://github.com/cheggaaa/pb (opens new window) port (v3)
- shellescape (opens new window) shellescape https://github.com/alessio/shellescape (opens new window) port
- stats (opens new window) stats https://github.com/montanaflynn/stats (opens new window) port
- storage (opens new window) package for store persist data and share values between lua states
- strings (opens new window) strings port (utf supported)
- tac (opens new window) tac line-by-line scanner (from end of file to up)
- tcp (opens new window) raw tcp client lib
- telegram (opens new window) telegram bot
- template (opens new window) template engines
- time (opens new window) time port
- xmlpath (opens new window) gopkg.in/xmlpath.v2 (opens new window) port
- yaml (opens new window) gopkg.in/yaml.v2 (opens new window) port
- zabbix (opens new window) zabbix bot