scExtensions – Usage¶
Math.calculateAverage¶
Calculates the average of an array of numbers.
Parameters
| Parameter | Type | Description |
|---|---|---|
ary |
Array<Number> |
Array of numbers to calculate the average for |
Return value: Number – the average, or 0 if the array is empty.
Math.isOdd¶
Checks whether a number is odd.
Parameters
| Parameter | Type | Description |
|---|---|---|
value |
Number |
The number to check |
Return value: Boolean – true if the value is odd, otherwise false.
Math.isEven¶
Checks whether a number is even.
Parameters
| Parameter | Type | Description |
|---|---|---|
value |
Number |
The number to check |
Return value: Boolean – true if the value is even, otherwise false.
String.prototype.split¶
Splits a string at a specified delimiter and returns an array of the parts.
Parameters
| Parameter | Type | Description |
|---|---|---|
delimiter |
String |
The delimiter to split the string at |
Return value: Array<String> – an array containing the parts of the string.
FindAllAlarms¶
Recursively searches through an object (typically DataStore) and collects all alarms.
Parameters
| Parameter | Type | Description |
|---|---|---|
alarms |
Array |
Empty array that will be populated with the alarms found |
parent |
String |
Starting string for the path – specify "" |
obj |
Object |
The object to search through, typically DataStore |
severity |
Number (optional) |
Filter by a specific severity level. Omit to retrieve all alarms. |
var alarms = new Array();
// Get all alarms
scExtensions.FindAllAlarms(alarms, "", DataStore);
// Get only alarms with severity level 10
scExtensions.FindAllAlarms(alarms, "", DataStore, 10);
Each element in the alarms array has the following structure:
| Property | Type | Description |
|---|---|---|
Alarm |
Object |
Reference to the alarm object |
Name |
String |
The path to the alarm in DataStore |
Active |
Boolean |
Last known active status |
msLeftUntilMidnight¶
Returns the number of milliseconds until midnight, based on the system clock.
Return value: Number – milliseconds remaining until midnight.
var ms = scExtensions.msLeftUntilMidnight();
// Start a timer that expires at midnight
myTimer.start(ms);
bytesToSize¶
Converts a byte value into a human-readable string with an automatically selected unit.
Parameters
| Parameter | Type | Description |
|---|---|---|
bytes |
Number |
The number of bytes to convert |
Return value: String – the formatted value with a unit, e.g. "5.00 MiB".
scExtensions.bytesToSize(5242880); // "5.00 MiB"
scExtensions.bytesToSize(1073741824); // "1.00 GiB"
scExtensions.bytesToSize(0); // "0 B"
The script¶
//***********************************************//
// The purpose of this script library
// is to provide extensions to the script
// engine.
// Version 1.0 (2022-06-13)
// Copyright © 2022 Kentima AB. No rights reserved.
//***********************************************//
try
{
//Calculate average for Array of Numbers
Math.calculateAverage = function (ary)
{
if(ary.length === 0)
return 0;
var totalSum = 0;
for(var i in ary) //Iterate Array object
{
if(isNaN(ary[i]))
throw new Error("Value: '" + ary[i] + "' is not a Number.");
else
totalSum += ary[i];
}
var average = totalSum / ary.length; //Calculate average
return average;
};
//Check if 'value' is odd
Math.isOdd = function(value)
{
if(isNaN(value))
throw new Error("Math.isOdd(): value: '" + value + "' is not a Number"); //Throw error if value is not a number
return (value % 2 == 1); //return if value is odd or not(true/false)
};
//Check if 'value' is even
Math.isEven = function(value)
{
if(isNaN(value))
throw new Error("Math.isEven(): value: '" + value + "' is not a Number");
return (value % 2 == 0); //return if value is even or not(true/false)
};
//Extend String object with split function
String.prototype.split = function (delimiter)
{
if (arguments.length < 1)
{
return [this];
}
var result = []; //Create Array object
var buffer = this.slice();
var dlen = Math.max(1, delimiter.length);
while (true) //Do work until nothing left to split
{
var ind = buffer.indexOf(delimiter);
if (ind != -1 && buffer != "")
{
result.push(buffer.slice(0, ind));
buffer = buffer.slice(ind + dlen);
}
else
{
result.push(buffer);
return result; //Return Array object
}
}
};
//Locate all possible alarms in the system
this.FindAllAlarms = function(alarms, parent, obj, severity)
{
for(prop in obj) //Iterate the object you want to find all alarms in, most likely 'DataStore'
{
if (obj[prop] instanceof Object)
{
this.FindAllAlarms(alarms, parent + prop + ".", obj[prop], severity);
}
else
{
if(obj.hasOwnProperty("Severity"))
{
//Only add Alarm objects
//we can detect them as they have a property named 'Acknowledged'
if(((severity == undefined) ? true : (obj.Severity == severity)) && (prop == "Acknowledged"))
{
alarms[alarms.length] = { "Alarm" : obj, "Name" : parent.substring(0, parent.length-1), "Active" : false };
}
}
}
}
};
//Get number of milliseconds left until midnight
this.msLeftUntilMidnight = function()
{
var milliseconds = ((23 - System.Clock.Hour) * 3600000) + ((60 - System.Clock.Minute) * 60000) + ((60 - System.Clock.Second) * 1000); //Figure out how many milliseconds there are left until midnight.
//Debug.Print("msl: " + milliseconds); //For debugging
return milliseconds;
};
//Format bytes
this.bytesToSize = function(bytes)
{
var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB' , 'PiB', 'EiB', 'ZiB', 'YiB'];
if (bytes == 0) return '0 B';
if (isNaN(bytes)) throw new Error ("Value: '" + bytes + "' is not a number");
var i = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i];
};
}
catch(e)
{
Debug.Print("scExtensions: " + e);
}