0% found this document useful (0 votes)
5 views15 pages

Mango JavaScript Overview and Functions

Uploaded by

Peter Collmann
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Mango JavaScript Overview and Functions

Uploaded by

Peter Collmann
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

12/12/2025, 14:05 About Mango Java Script — Mango OS Support

Main Site Home Documentation Videos Forum Contact Us 3.0 Legacy Blog

Getting Started
About Mango Sections
Point Value Time
Mango Configuration

Acquiring Data
Java Script Objects
Context objects
Statistical objects
Alarms & Events
Global Utilities
Publishing Data Logging
More Help
Logic and Control

Advanced 7-Day Scheduler

Scripting Overview Mango JavaScript

Meta Data Source Most script editors have an adjacent blue question mark to
open the contextual help. In the related items at the bottom of
Scripting Data Source
that help is "About Mango JavaScript" which contains the most
Global Scripts up to date information for what features exist in that version of
Mango.
About Mango Java Script

The Script area is where the script to execute is entered.


Script Examples
Scripts can be any valid ECMAScript that would be written
General Knowledge within a function.

Reports
In addition to the ECMAScript context, globally-accessible
functions can be defined, such as max(), min(), avg(), and
Communication Protocols
sum(). (These functions are implemented in an modules such
Mango UI as SST Global Scripts.) To use them, simply call them from
your script, for example:
Mango Administration
return max([Link], [Link], [Link]);
Hardware

Troubleshooting This returns the maximum of the present values of 'x', 'y', and
'z'. Any number of parameters can be provided to any of these
Developers global functions.

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 1/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

Once the script has been entered, click the check mark icon
to execute it and attempt to calculate the result.

Point Value Time Objects

Mango stores its data in Point Value Time objects that contain
a value and a time. When accessing internaldata from Mango
it is important to know if you are dealing with a raw value or a
Point Value Time.

{
value: value of object,
time: timestamp in milliseconds since
epoch
}

Time values

The timestamp of value is also available to the script. The


following fields can be useful for conversions:

[Link] - timestamp of the value in milliseconds since the


epoch
[Link] - 0-999 the millisecond portion of [Link]
[Link] - 0-60
[Link] - 0-60
[Link] - 0-23
[Link] - 1-28,31
[Link] - 1-7 where 1 is Sunday
[Link] - 1-365,366
[Link] - 1-12
[Link] - four digits

Context Objects

The script var that represents a point in a script is actually an


'object', in JavaScript terminology. An object is a container of
[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 2/15
12/12/2025, 14:05 About Mango Java Script — Mango OS Support

values and functions that can be referenced by their property


names. To get a description of the properties available for use
in a script var, use the help property, e.g.:

return [Link];

This script works best if the data type is set to alphanumeric,


but this is not required. The help property is identical to the
toString() function, which is available on all context objects (i.e.
not just script vars).

The value property is the present value of the point. The


JavaScript type of the value is analogous to its Mango type:
Binary become boolean, Numeric becomes float, Multistate
becomes integer, and Alphanumeric becomes string.

Each script var also implements additional functions. The


objects returned by these functions depend upon the data
type of the point the var refers to. Again, the help property
can be used to get a description of the returned object's
properties. For the "periodType" parameter in all of the
functions below, the following pre-defined global variables
can be used: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH,
and YEAR.

The ago() function returns the value that the point had the
given amount of time ago. For example, the call "[Link](HOUR,
3)" returns the point's value exactly 3 hours ago.

The past() function returns an object containing statistics over


the given period ending now. See below for a description of
the various statistics objects.

The prev() and previous() functions are identical; the latter is


provided for its linguistic completeness. The functions return
the same statistically object as past(), but over a different time
span. The start and end times are quantized such that they
correspond to the period type. For example, if the period type
is HOURLY and periods is 1, and the function runs at 18:05, the
time span that will be used is from 17:00 (inclusive) to 18:00
(exclusive). If the periods were, say, 3, the time span would be
from 15:00 to 18:00. Similarly, MONTH starts the time span at
midnight on the first day of the previous month and ends it on

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 3/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

the last day of the previous month (when periods is 1). Other
period types work the same. A WEEK starts on Monday at
midnight in accordance with ISO standards.

The last() function return a list of the latest point value/time


objects up to the given limit. The list will never be null, but
could have a size from 0 to n depending on how many
historical values there actually are. Values are sorted from
most newest to oldest so that [Link](0) will return the most
recent value. The list is a [Link] object, and so has all of
the methods available in that interface, including get(index)
and size(). The get(index) method will throw an
ArrayIndexOutOfBoundsException if you ask for an index that
is >= the size, so be sure to check the list size before using
get. Point value/time objects have "value" and "time"
properties.

The lastValue() function returns a single point value/time


object, or null if the index is invalid. (Size checking in the list
is done automatically, so an
ArrayIndexOutOfBoundsException will never be thrown.)
lastValue() and lastValue(0) will return the most recent value,
lastValue(1) will return the second-most recent value, etc.

The pointValuesBetween(from, to) function return a list of


point value/time objects between the given timestamps. The
returned values are inclusive of the from time and exclusive
of the end time. The list will never be null, but could have a
size from 0 to n depending on how many historical values
there actually are. Values are sorted in time order so that
[Link](0) will return the oldest value. The list is a [Link]
object, and so has all of the methods available in that
interface, including get(index) and size(). The get(index)
method will throw an ArrayIndexOutOfBoundsException if you
ask for an index that is >= the size, so be sure to check the
list size before using get. Point value/time objects have
"value" and "time" properties.

The pointValuesSince(timestamp) function return a list of


point value/time objects since the given timestamp. The
returned values are inclusive of the timestamp. The list will
never be null, but could have a size from 0 to n depending on
how many historical values there actually are. Values are
sorted in time order so that [Link](0) will return the oldest

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 4/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

value. The list is a [Link] object, and so has all of the


methods available in that interface, including get(index) and
size(). The get(index) method will throw an
ArrayIndexOutOfBoundsException if you ask for an index that
is >= the size, so be sure to check the list size before using
get. Point value/time objects have "value" and "time"
properties.

The pointValueBefore(timestamp) function returns the


nearest point value/time object prior to timestamp, or null if
there is not one.

The pointValueAfter(timestamp) function returns the nearest


point value/time object after to timestamp, or null if there is
not one.

The pointValueAt(timestamp) function returns the a point


value/time object that was recorded exactly at the timestamp,
or null if there is not one.

Statistical objects

Statistics objects are returned from the past(), prev(), and


previous(), getStats(from, to) functions. (See "Context objects"
above.) The properties of the object returned depend upon
the data type of point upon which they were generated. Time
values in objects are stored as integers, but represent the
number of milliseconds since midnight Jan 1, 1970.

Note 1: getStats(from, to) operates on timestamps, usage:

//Get the stats for the past 20 minutes


var end = new Date();
var start = new Date([Link]() - 1000
* 60 * 20); //20 min before now
var stats = [Link]([Link](),
[Link]());

Note 2: Statistics are returned as Javascript Objects so


Mathematical operations must use the .doubleValue() method.

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 5/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

For example:

[Link](HOUR).[Link]() +
[Link](HOUR).[Link]()

The AnalogStatistics object is returned by Numeric points. It


contains the following properties:

minimumValue: (float) the minimum value the point


reached over the period
minimumTime: (integer) the time at which the minimum
value was reached
maximumValue: (float) the maximum value the point
reached over the period
maximumTime: (integer) the time at which the maximum
value was reached
average: (float) the average value of the point over the
period
integral: (float) the integral value of the point over the
period
sum: (float) the sum of all value updates over the period
(appropriate for pulse counting)
startValue: the value before or exactly at the period
start time
firstValue: (float) the first value in the period
firstTime: (integer) the time of the first value
lastValue: (float) the last value in the period
lastTime: (integer) the time of the last value
count: (integer) the number of updates over the period
delta: (float) the cumulative change in value from
firstValue to lastValue
periodStartTime (integer) the start time used for the
calculation
periodEndTime (integer) the end time used for the
calculation

For example, the following returns the minimum value of 'n'


over the past hour:

[Link](HOUR).minimumValue;

The StartsAndRuntimeList object is returned by Binary and


Multistate points. It contains the following properties:
[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 6/15
12/12/2025, 14:05 About Mango Java Script — Mango OS Support

periodStartTime (integer) the start time used for the


calculation
periodEndTime (integer) the end time used for the
calculation
count (integer) count of the total number of entries
startValue: the value before or exactly at the period
start time
firstValue: the first value in the period
firstTime: (integer) the time of the first value
lastValue: the last value in the period
lastTime: (integer) the time of the last value
data: (array) the list of individual StartAndRuntime
objects.

Each StartAndRuntime object has the following properties:

value: (boolean for Binary, integer for Multistate) the


point state to which the remaining properties apply
starts: (integer) the number of times the state was
entered over the period
runtime: (integer) the amount of time in milliseconds the
point was in the state over the period
proportion: (float) the proportion of the period the point
was in the state (runtime / real duration)
percentage: (float) proportion * 100

To access a specific StartAndRuntime object in the list, use


the get() function. For example, the following returns the
proportion of time that 'b' was in state 'false' over the previous
2 months.

return [Link](MONTH, 2).get(false).proportion;

The ValueChangeCounter object is returned by


Alphanumeric points. It contains a property changes, which
is the number of times the point changed during the period.
For example, the following returns the number of times 'a'
changed during the previous 45 minutes.

[Link](MINUTE, 45);

Each ValueChangeCounter has the following properties:

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 7/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

periodStartTime (integer) the start time used for the


calculation
periodEndTime (integer) the end time used for the
calculation
count (integer) count of the total number of entries
startValue: the value before or exactly at the period
start time
firstValue: the first value in the period
firstTime: (integer) the time of the first value
lastValue: the last value in the period
lastTime: (integer) the time of the last value
count: (integer) count of the number of samples
changes: (integer) number of unique sample values

Global Utilities

The script context has some utilites that are globally available.

DateTimeUtility

This utility is accessible via DateTimeUtility.{method}.

getOffset(timezoneId, timestamp)

timezoneId - "Europe/Rome" timestamp - ms timestamp of any


date return - timezone offset in milliseconds at that point in
time

getTimezone()

return - Timezone from where the script is being run.

getUtcOffset(timestamp)

timestamp - ms timestamp of any date return - the UTC offset


in milliseconds from where the script is being run

parseDate(format, dateString, timezoneId)

format - See here dateString - String date and is related to


above timezoneId - Timezone ID to use return - millisecond
date

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 8/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support
formatDate(formate, timestamp, timezoneId)

format - See here timestamp - milliseconds since


epochtimezoneId - timezone to use return - String formatted
date

RuntimeManager

This utility is accessible via RuntimeManager.{method}. The


enable/disable methods return a status of:

-1 - Does Not Exist


0 - No change in state
1 - Operation successful

Available Methods:

refreshDataPoint(xid)

Refreshing a data point suggests to the Data Source that the


point value should be collected ASAP. This will only happen if
the underlyingdata source has implemented this functionality.
xid - Xid for data point returns:

-1 - Point does not exist


0 - Point is not enabled
1 - Suggestion made to data source

isDataSourceEnabled(xid)

xid - Xid for a data source return true if enabled false if not or
DNE

enableDataSource(xid)

xid - Xid for a data source, return status

disableDataSource(xid)

xid - Xid for a data source, return status

isDataPointEnabled(xid)

xid - Xid for a data point return true if enabled false if not or
DNE or User does not have permissions

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 9/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

Note: a disabled data point is a data point that is not


collecting data, which can be due to either the
pointbeing disabled or its data source being disabled.

enableDataPoint(xid)

xid - Xid for a data point, return status

disableDataPoint(xid)

xid - Xid for a data point, return status

sleep(milliseconds

milliseconds- Milliseconds to sleep the script\s thread


execution for, nothing returned

Data Point Queries

Data points can be queried by using the DataPointQuery


utility. This utility has one method:
[Link](stringRql);

DataPointWrapper

The DataPointWrapper returned has some of the basic


information on a data point in addition to the Runtime member
which is the same as the Object that wraps the context points
for a script. The following are members of the
DataPointWrapper Object:

extendedName - String
settable - Boolean
deviceName - String
enabled - Boolean
xid - String
name - String
unit - String
dataSourceName - String
dataSourceXid - String
runtime - Point Context object

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 10/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

Note: The runtime is the object you would expect to


see by adding a point to the script context. This is the
objectthat contains the .ago(), .past() and .prev()
methods as described above. Use caution as this
member is null if the data point is not enabled.

RQL

RQL is a query language that is highly flexible, more info here.


For example to find all points with a name that starts with Volts
use this query: like(name, Volts*) Note the use of the asterisk
wildcard, this is the default for all Mango RQL. Also to see
what is available to use in the query go here.

RQL Examples

This example will query for a set of the first 10 data points
who's names contain the word 'boiler' and return an average
their values. There is an additional check to ensure that only
points that are enabled are used. Note that this could also be
done in the RQL statement.

var points = [Link]('like(name,


*boiler*)&limit(10)');
var average = 0.0;

[Link]('Found ' + [Link]() + ' points


to compute average.');

for(var i=0; i<[Link](); i++){


if([Link](i).runtime !== null){
[Link]('Adding ' +
[Link](i).[Link] + ' to the
average.');
average = average +
[Link](i).[Link];
}
}

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 11/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

return average/[Link]();

Data Source Queries

Data sources can be queried in much the same way. There is


one method, [Link](stringRql); A returned
object will contain:

name - String
xid - String
enabled - boolean
type - String
points - List<DataPointWrapper>

Point Value Queries

While point values and statistics can be gotten through point


wrappers, one need use the PointValueQuery utility to easily
stream values or collate multiple points' values by time. A
second method is provided to optionally roll the data up into
rollup periods, and then to pass a part or the whole statistics
object to the callback. To see the method signatures, run
print(PointValueQuery); from within the scripting environment.

JsonEmport Utility

The JsonEmport utility provides methods to pass JSON or get


JSON as one would through an import/export page. A script
cannot save itself through the emport utility. If it tried, that item
in the JSON will be ignored. This utility is accessible through
the context key "JsonEmport" and has the following functions:

getFullConfiguration() - String
[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 12/15
12/12/2025, 14:05 About Mango Java Script — Mango OS Support

getConfiguration(String configurationKey) - String


dataPointQuery(stringRql) - String
dataSourceQuery(stringRql) - String
doImport(String) - void
doImportGetStatus(String) - List<String>

Logging

Logging can be achieved during script testing on the edit


page by enabling the logging level desired and then using
the appropriate log statement.

[Link]('trace');
[Link]('debug');
[Link]('info');
[Link]('warn');
[Link]('error');
[Link]('fatal');

HttpBuilder

HTTP exchanges can be sent and handled from within the


script body without using Java directly. The HttpBuilder object
provides GET, POST, PUT and DELETE with synchronous
callbacks. By default, responses with status 200 will be
passed to the resp function if defined, other responses to the
err function if defined, and local exceptions to the excp
function if defined, or the err function if that is defined instead
with status -1. Two forms of usage are supported:

//First format, method chaining


print([Link]("[Link]
.err(function(status, headers, content) { //
throw "Request got bad response: " + sta
}).resp(function(status, headers, content) {

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 13/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

print(content);
return true;
}).excp(function(exception) { //setException
throw [Link]();
}).execute()); //will print the help page th

//Second format, full request


print([Link]({
path: "[Link]
method: "GET",
headers: {},
parameters: {},
//content: "GETs don't have content!",
err: function(status, headers, content) { //
throw "Request got bad response: " + sta
},
resp: function(status, headers, content) { /
print(content);
return true; //will print in wrapping pr
}
excp: function(exception) { //exceptionCallb
throw [Link]();
}
}));

CONTEXT_POINTS

 

CONTEXT_POINTS is a variable declared in all scripting


environments. It is a map of variable names to DataPointRT
objects for the points which have been added to the script
context.

More Help

For general discussion on scripting in Mango see here.

Copyright © 2021 Radix IoT, LLC.

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 14/15


12/12/2025, 14:05 About Mango Java Script — Mango OS Support

[Link] JavaScript&text=Scripts can be any valid,such as SST Global Scripts. 15/15

You might also like