To define a new script, you need to override the ConditionMet function. When ConditionMet returns true, the Alert goes on. When it returns false, the Alert goes off. Aggressive and TimeToTrigger are handled automatically for you, as long as you set the Condition Type to ScriptedTimeRangeCondition.
For example, a single script to detect off-hours usage on weekdays at a Depot marker would look like the following:
ConditionMet = function (Context) {
if (!Context.Time.DayOfWeek.Equals(System.DayOfWeek.Saturday) &&
!Context.Time.DayOfWeek.Equals(System.DayOfWeek.Sunday) &&
Context.Unit.IsAtMarkerInCategory(Depot)) {
if ((5 <= Context.Time.Hour) && (Context.Time.Hour <= 19)) {
return false;
}
return Context.Unit.Ignition;
}
return false;
};
|
In this example, the script makes use of a parameter that has its Name set to Depot, which specifies a marker category. The TargetType for this script is Unit, so that the Context object has a defined Unit field. The script starts by checking that it is a weekday and that the vehicle is at a marker with the same category as the Depot parameter. If so, it checks the current hour, and returns true only if it is outside of normal hours and the ignition is on.
If you need special handling for the way the new Alert triggers and is turned off (for example, to create an alert that is never deactivated), you can also override the Process function. The default value of the process function is as follows:
function Process(Context) {
if (ConditionMet(Context) {
Activate(Context);
} else {
Deactivate(Context);
}
}
|
Global Variables and Methods
Within a script, certain global variables are available to you, which can provide information that is useful for determining when the new Alert condition is met:
|
|
Context
|
This provides information about the Unit or Driver that is the target of the alert. This object is the same as the Context that is passed as an argument to the function you create. It is the entry point to using the API objects for InSight Alert scripts.
|
State
|
State is an empty dictionary that can be used to store information between calls to your script. That is, it is a place for you to define and store any named values that you want. By default, State does not have any properties or pre-defined values. You add values by making an assignment such as State.NAME = VALUE. It is good practice to initialize the values you are using to a default value the first time the script is called, so that you do not try to read an undefined value: if (typeof State.Name === 'undefined') { State.NAME = 0; }
For example, if you want to activate an alert if a driver hard brakes more than 5 times, you could do the following:
ConditionMet = function(Context) {
if (Context.Unit.HasDiagnosticValue(770) and
Context.Unit.GetDiagnosticValue(770) > 0 and
not IsAlreadyBraking) {
IsAlreadyBraking = true;
State["BrakingCount"] += 1;
} else {
IsAlreadyBraking = false;
}
if (State["BrakingCount"] > 5) {
return true;
} else {
return false;
}
}
|
The State variable is only available if you select the Keep a persistent state for this script check box.
|
Be careful not to add too much information to the persistent state variable. If this dictionary contains too much information, it can slow down your scripts to the point where they are automatically disabled.
|
|
EmailFields
|
This is a dictionary of fields that can be included in emails that are sent when the alert triggers or is turned off. In the email, the name and value are listed as part of the email message. For an example showing how to use this global, see Checking DTCs.
|
Inside your scripts, you can also call the following global method
|
|
|
|
SetTriggerValue
|
APIContext context,
string triggerName,
object triggerValue
|
void
|
Allows a script to specify the trigger value that is displayed in the InSight Alerts inbox and in alert emails. The context argument should be set to the global Context object (which is also the Context parameter of the ConditionMet method you are writing). Typical usage is something such as the following:
SetTriggerValue(Context, "SMR (Engine Hours)", currentHours + " h");
This example displays the SMR (Engine Hours) from a parameter called currentHours, where the result is formatted to look like
SMR (Engine Hours) : 1000h
|
|