Global Scripts

June 2nd, 2020


Global scripts are added to the scripts specified for meta points, point links, and anywhere else that scripts are used. This allows users to declare code such as functions or general initializations in a single place. Multiple global scripts can be added to simplify code management, but a single script can contain an arbitrary amount of declarations. Scripts are applied in alphabetical order by their names.

Global scripts must be independently executable, i.e. they cannot reference variables from other scripts. Saving a global script does not immediately execute it in running script engines (meta points, scripting data sources, etc) and so they must be restarted to use the new global script. Event handlers or Excel Report script engines are created when the report runs.

Scripts are written in JavaScript, also known as ECMAScript. (The full specification for ECMAScript is available here .) JavaScript is arguably the most popular scripting language in use today, it being the only language available for use in every popular web browser. The result is that, by far the common usage of JavaScript is in web pages. The context within the Meta data source is of course different, there being no “window” or “document” objects, among other more subtle differences. Contextual differences aside, a full and complete implementation of ECMAScript is available. Specifically, the Rhino implementation is used.


Simple Global Script Examples

Example 1

This script named “myProduct”, simply takes two arguments, multiplies them together, and returns the product.

function myProduct(arg1, arg2)
{
 return arg1 * arg2;
}

Example 2

This script named “myConvertFtoC” only takes one argument (arg1), which will be a temperature given in degrees Fahrenheit. The function converts the value to degrees Celsius and returns it.

function myConvertFtoC(arg1) 
{
    return (arg1 - 32) * (5/9);
}

How the Global Scripts Look in Mango

Global script

Example of calling a global script from a Meta Data Source script

In this example we have a Meta Data Source named celsiusView which has a script that converts the value from OutsideTemp from Fahrenheit to Celsius using the global script myConvertFtoC.

return myConvertFtoC(OutsideTempF.value);

Copyright © 2020 Radix IoT, LLC.