Scripting engine

Scripting rule is function with an single input (message) that can result in one or more new messages created based on custom script. Scripts are writen in common Javascript language (ES6).

Scripting rule

Parameters:

  • Name: user friendly name
  • Status: Active / Disabled - any scripting rule execution can be disable
  • Topic filters: scripting rule will be executed on messages, that matches list of topic filters here
  • Script: JavaScript user defined code
  • Description: human readable description of the scripting rule purpose
  • Notes: other notes

Script execution can result in sending

  • no messages,
  • single message or
  • multiple messages in specific (MQTT) handler.

Output message must be an object having two properites topic (string) and payload (string, Buffer, array). Topic must be compliant with MQTT topic standard and must start with data account root name. Script can publish message also to other data accounts, even to a data accounts in other ogranization. When publishing to other data account, routing permission must be set on target data account to allow accept messages from publishing data account.

Input to the scripting rule is a single message object having two properties 'topic' (string) and payload (Buffer).

To transform payload Buffer to string, use msg.payload.toString([encoding[, start[, end]]]) where 'utf8' is default encoding. Other encodings could be used 'ascii', 'hex' or 'base64'.

To directly convert Buffer to JSON object, use JSON.parse(msg.payload.toString()) method.

Example of script definition

/**
* Source message
* msg.topic // string
* msg.payload // Buffer
*/

const payload = msg.payload.toString(); // Convert msg.payload from Buffer to UTF8 string

const outputMsg = {
  topic: '4EH0ERe7E4Wk6z1sKa6EDK/foo/bar', // Target topic, must begin with target data account
  payload: msg.payload
};

inteliglue.send(outputMsg, 'MQTT');

Scripting rule can be tested with sample topic and message before saving changes. We highly recommend to test your script before applying changes.

Attention

Result message topic must be different from the topics, that are listed in topic filters defined on executing rule. Otherwise there is high chance, that message got cycled in processing. InteliGlue has built in validator, that automatically check for this rule. Output messages having same topic as original message or topics defined in rule will not be processed and will result in error.