ComX-Bridge embeds a lightweight script engine to provide the capability to process data in real-time at the Edge before sending it to the cloud or upstream systems.
- Purpose: Beyond simple data collection, perform filtering, transformation, and conditional control.
- Languages:
- Lua 5.1 (via
gopher-lua) - Fast and lightweight, optimized for embedded environments. - JavaScript (via
goja) - Supports ES5+, familiar to web developers.
- Lua 5.1 (via
- Location: Executed within the Receive Loop of each Gateway.
Tip
AI-Powered Generation: You can use the AI Engine to automatically generate Lua parsing scripts for binary protocols. See AI Engine Documentation.
Specify the rule_script path in the gateway configuration.
gateways:
- name: "sensor-gw"
transport: { ... }
protocol: { ... }
rule_script: "./rules/filter_noise.lua" # Lua script pathYou must define the on_message function. This function is called whenever a message is received.
-- on_message(gateway_name, raw_data_string)
-- return: modified data (string) or nil (drop)
function on_message(gateway, data)
-- 1. Logging
print("Received from " .. gateway .. ": " .. data)
-- 2. Filtering (Ignore if specific string is present)
if string.find(data, "NOISE") then
return nil -- Drop message (do not send)
end
-- 3. Data Transformation (Add Header)
local new_data = "[PROCESSED] " .. data
return new_data
endIf you use the AI Engine to analyze a binary protocol, it can generate a script like this:
-- Generated by ComX-Bridge AI
function on_message(gateway, data)
-- Parse Binary Structure
local header = string.sub(data, 1, 2)
local payload = string.sub(data, 3, 10)
-- Convert to JSON for Cloud
return string.format('{"header":"%s", "payload":"%s"}', header, payload)
endIf you use the .js extension, the JavaScript engine is automatically selected.
gateways:
- name: "iot-gw"
transport: { ... }
protocol: { ... }
rule_script: "./rules/transform.js" # JavaScript scriptDefine the on_message function just like in Lua.
// on_message(gateway, data)
// return: modified data (string/object) or null/undefined (drop)
function on_message(gateway, data) {
// 1. Logging
console.log("Received from " + gateway + ": " + data);
// 2. JSON Parsing and Transformation
try {
var obj = JSON.parse(data);
obj.processed = true;
obj.timestamp = Date.now();
return JSON.stringify(obj);
} catch (e) {
// Return original if not JSON
return data;
}
}Built-in functions available in the JavaScript engine:
| Function | Description |
|---|---|
console.log(msg) |
Print Log |
console.warn(msg) |
Print Warning |
console.error(msg) |
Print Error |
JSON.parse(str) |
Parse JSON string |
JSON.stringify(obj) |
Convert object to JSON string |
hexToBytes(hex) |
Convert Hex string to Byte array |
bytesToHex(bytes) |
Convert Byte array to Hex string |
- Noise Filtering: Reduce network costs by removing meaningless dummy data or error packets in advance.
- Unit Conversion: Convert Celsius to Fahrenheit, or scale Raw values to physical quantities.
- Format Normalization: Unify data formats from different devices into JSON or specific structures.
- Threshold-based Alarms: Send to a separate alarm channel if specific values exceed thresholds.
- Protocol Conversion: Convert legacy protocols to modern JSON/REST formats.