Context

Context object is an input point for accessing context-dependent objects, their methods and fields.

Fields

Name

Description

expression

Contains an object of Expression type with information about the expression being executed

currentTrigger

Contains the name of the trigger that initiated the execution of the expression (string)

data

Contains an object of DataMap type for obtaining values of data triggers at the moment of expression execution

bundle

Contains a Bundle object for transferring data between expressions

token

Intended to store arbitrary data obtained during the execution of an expression

signalTime

Contains the time when the trigger was fired

Examples

The following example demonstrates a script that adds 1 or 1000 to the current value of the expression, depending on which of the triggers was activated. In this example, the expression has two periodic triggers at intervals of 100 ms (trig_100ms) and 1000 ms (trig_1000ms).

// For brevity, we will save the current value of the expression in a variable
var val = context.expression.currentState.value;

// Summarization
if (context.currentTrigger == 'trig_100ms')
  return val + 1;
else if (context.currentTrigger == 'trig_1000ms')
  return val + 1000;
else return MosUtils.doNothing;

The next example demonstrates the use of the token property. The code in the listing returns the arithmetic mean of the last three values obtained from the tag. This is possible because the value of the token property is stored and available during the next execution of the expression. In this example, the expression has a data trigger (trig_number) with a numeric tag.

// For brevity, we will save the current tag value in a variable
var tagValue = context.data['tag_number'].state.value;

// If the script is executed for the first time, we initiate an array of 3 elements in token
if (context.token == null)
{
  context.token = new Array(3);
  context.token[0] = 0;
  context.token[1] = 0;
  context.token[2] = tagValue;
}
else {    
  // ...otherwise, we “shift” the array elements
  context.token[0] = context.token[1];
  context.token[1] = context.token[2];
  context.token[2] = tagValue;
}

return (context.token[0] + context.token[1] + context.token[2]) / 3;

Last updated