Example Scripts

The following examples illustrate how to use the API objects to write InSight Alert Scripts.

Weekend Use

This example causes an alert to trigger when a vehicle sends a report from a marker in a specified category on a weekend.
 

Because this alert involves time values, you might want to turn on aggressive and time to trigger by setting the Condition Type to ScriptedTimeRangeCondition.
The Target Type of this script is Unit.
The script does not make use of the global State variable, so clear the Keep a persistent state for this script check box.
Be sure that the This script does not require message from vehicles check box is clear.
The script requires you to create a single parameter:
 

Parameter Name

Parameter Type

Meaning

Marker_Category

Category

Vehicles at markers of this category on a weekend trigger the alert.

 

 

ConditionMet = function (Context) {

    // Check whether it is a weekend and the vehicle is at a marker in the specified category

    if ((Context.Time.DayOfWeek.Equals(System.DayOfWeek.Saturday) || 

         Context.Time.DayOfWeek.Equals(System.DayOfWeek.Sunday)) && 

        Context.Unit.IsAtMarkerInCategory(Marker_Category)) {

        return true;

    }

    return false;

};

 

Vehicle Moves Outside a Custom Radius

This example causes an alert to trigger when a vehicle first moves a specified distance from a specified marker.
 

Because this alert does not need to support a time to trigger or aggressive property, the Condition Type is ScriptedCondition.
The Target Type of this script is Unit.
The script makes use of the global State variable, so you must select the Keep a persistent state for this script check box. This script defines a value called insideCustomRadius.
Be sure that the This script does not require message from vehicles check box is clear.
The script requires you to create two parameters:
 

Parameter Name

Parameter Type

Meaning

HQ

Marker

The marker that vehicles must leave to trigger the alert.

Radius

Number

The distance, in feet, that the vehicle must be separated from the marker to count as having left the marker.

 

 
ConditionMet = function (Context) {

       // Check whether the vehicle has moved inside the custom radius

 if (!State.insideCustomRadius && HQ.ContainsLatLonCustomRadius(Context.Unit.Location, Radius))) {

              // Remember that vehicle is inside the custom radius

         State.insideCustomRadius = true;

 }

       // Check whether the vehicle was inside the custom radius but has now moved out

 if (State.insideCustomRadius && !HQ.ContainsLatLonCustomRadius(Context.Unit.Location, Radius)) { 

              // Remember that vehicle is not inside the custom radius any more

         State.insideCustomRadius = false;

              // Trigger the alert because vehicle just left

         return true;

 }

       // The alert condition is not met (vehicle is inside the radius, or did not just leave)

 return false;

};

Vehicle Has Not Returned After Half an Hour

This example causes an alert to trigger when a vehicle leaves a specified marker and does not return within 30 minutes.
 

Because this alert involves time values, you might want to turn on aggressive and time to trigger by setting the Condition Type to ScriptedTimeRangeCondition.
The Target Type of this script is Unit.
The script makes use of the global State variable, so you must select the Keep a persistent state for this script check box. This script defines values named TimeLeftMarker and LastMarkerId.
Be sure that the This script does not require message from vehicles check box is clear.
The script requires you to create one parameter:
 

Parameter Name

Parameter Type

Meaning

SpecifiedMarker

Marker

The marker that vehicles must return to within half an hour.

 

 
ConditionMet = function (Context) {

    var currentMarkerId = Context.Unit.HasCurrentMarker ? Context.Unit.CurrentMarker.Id : 0;

 

       if ((!State.TimeLeftMarker) || ( currentMarkerId === SpecifiedMarker.Id)) { // If at the marker, set TimeLeftMarker to -1

           State.TimeLeftMarker = -1;

       }

 // Check whether Vehicle has moved outside the specified Marker for first time

 if (State.LastMarkerId === SpecifiedMarker.Id && currentMarkerId !== SpecifiedMarker.Id && State.TimeLeftMarker === -1) { 

         State.TimeLeftMarker = Context.Time;

 }

 State.LastMarkerId = currentMarkerId;

 // Check if vehicle left the specified marker (TimeLeftMarker is set) and has been gone for more than 30 minutes

 if (State.TimeLeftMarker !== -1 && ((Context.Time - State.TimeLeftMarker) > 1800000)) { 

         return true;

 }

 return false;

};

Different Idle Time Thresholds for Different States

This example causes an alert to trigger when a vehicle idles for more than 30 minutes if it is in California, or for more than 60 minutes if it is in Texas.
 

Because this alert involves time values, turn on aggressive and time to trigger by setting the Condition Type to ScriptedTimeRangeCondition.
The Target Type of this script is Unit.
The script makes use of the global State variable, so you must select the Keep a persistent state for this script check box. This script defines values called LastStatus, IsIdle, and IdleStatusTime.
Be sure that the This script does not require message from vehicles check box is clear.
The script does not use any parameters.

 

ConditionMet = function (Context) {

    // To determine the state (California or Texas), we need to check the address

    // So this alert only triggers if we have a street address

    if (Context.Unit.HasCurrentStreetAddress) {

  // The global State variable holds the vehicle (unit) status and when it started idling

        if ((State.LastStatus != Context.Unit.Status)) {  // only set State variables if vehicle status has changed

                    State.LastStatus = Context.Unit.Status;

         if (State.LastStatus == 1) {

                 State.IsIdle = true;

                 State.IdleStatusTime = Context.TimeUtc;

         } else {

                 State.IsIdle = false;

                 State.IdleStatusTime = '';

         }

 }

       // Alert can only trigger if the vehicle is idling

 if (State.IsIdle) {

         if (Context.Unit.CurrentStreetAddress.Region == "California") {

                     // Check idling time - time of message minus time when vehicle started idling

                 if (Context.TimeUtc.Subtract(State.IdleStatusTime).TotalSeconds >= 1800) {

                         return true;

                 }

         } else if (Context.Unit.CurrentStreetAddress.Region == "Texas") {

                     // Check idling time - time of message minus time when vehicle started idling

                 if (Context.TimeUtc.Subtract(State.IdleStatusTime).TotalSeconds >= 3600) {

                         return true;

                 }

         }

 }

    }

    return false;

};

 

Checking a Diagnostic Value

This example causes an alert to trigger when the vehicle has its rear door open (diagnostic TID 808) and the vehicle is not at a marker in a specified category.
 

Because this alert does not involve time values, you can setting the Condition Type to ScriptedCondition.
The Target Type of this script is Unit.
The script does not use the global State variable, so you should clear the Keep a persistent state for this script check box.
Be sure that the This script does not require message from vehicles check box is clear.
The script requires you to create a single parameter:
 

Parameter Name

Parameter Type

Meaning

Marker_Category

Category

The rear door should only be opened at markers in this category.

 

ConditionMet = function (Context) {

 

 // Unit has rear door TID (808), rear door is open 

 if (Context.Unit.HasDiagnosticValue(808) && (Context.Unit.GetDiagnosticValue(808) > 0) && 

           (!Context.Unit.IsAtMarkerInCategory(Marker_Category))) {

         return true;

 }

 return false;

};

 

Checking for DTCs

This example causes an alert to trigger when the vehicle sends a DTC code.
 

To allow users to make this alert aggressive, set the Condition Type to ScriptedTimeRangeCondition.
The Target Type of this script is Unit.
The script does not use the global State variable, so you should clear the Keep a persistent state for this script check box.
Be sure that the This script does not require message from vehicles check box is clear.
The script does not require any parameters.

 

ConditionMet = function (Context) {

    if (Context.Unit.HasAnyDtc(801)) { // 801 is tid for "OBDII DTC"

        // Add to the global EmailFields so that the DTC codes are reported in alert emails

        EmailFields["OBDII DTC"] = Context.Unit.GetAllDtcCodes(801);

        return true;

    } else {

        return false;

    }

};

 

Last Job

This example causes an alert to trigger when a driver is on site at the last job of his or her scheduled route.
 

Because this alert involves time, turn on aggressive and time to trigger by setting the Condition Type to ScriptedTimeRangeCondition.
The Target Type of this script is Driver.
The script does not use the global State variable, so you should clear the Keep a persistent state for this script check box.
Be sure that the This script does not require message from vehicles check box is clear.
The script does not require you to define any parameters.

 

ConditionMet = function (Context) {

       if (Context.Driver.CurrentRoute)

       {

             if ((CurrentRoute.LastJob.Status.IsCurrent) && (CurrentRoute.LastJob.Status.Status == 3)) { 

                         return true;

         }

         return false;

 }

};

 

Speeding With Weather

This example causes an alert to trigger when a vehicle exceeds the speed limit, adjusting the speed limit down by 10% if it is raining.
 

Because this alert involves time, turn on aggressive and time to trigger by setting the Condition Type to ScriptedTimeRangeCondition.
The Target Type of this script is Unit.
The script does not use the global State variable, so you should clear the Keep a persistent state for this script check box.
Be sure that the This script does not require message from vehicles check box is clear.
The script does not require you to define any parameters.

 

ConditionMet = function (Context) {

 var limit = 100.0 // initialize

 // the speed limit is stored on the address, so check that we have both vehicle speed and limit

 if (Context.Unit.HasCurrentStreetAddress && Context.Unit.HasSpeed) {

         limit = Context.Unit.CurrentStreetAddress.SpeedLimit * 1.8520 // convert knots to km/h

         // Check Diagnostic TID 717 (raining) and adjust limit if necessary

         if (Context.Unit.HasDiagnosticValue(717) && Context.Unit.GetDiagnosticValue(717)) {//raining

                 limit = limit * 0.90;

         if (Context.Unit.SpeedKph > limit)

                 return true;

 }

 return false;

}

 

Checking Tags

This example causes an alert to trigger when a vehicle with a specified tag starts moving and the assigned driver does not also have the same tag.
 

Because this alert involves time, turn on aggressive and time to trigger by setting the Condition Type to ScriptedTimeRangeCondition.
The Target Type of this script is Driver.
The script does not use the global State variable, so you should clear the Keep a persistent state for this script check box.
Be sure that the This script does not require message from vehicles check box is clear.
The script requires you to create a single parameter:
 

Parameter Name

Parameter Type

Meaning

tagName

string

The name of the tag on the vehicle that should match a driver tag.

 

//Note that this script makes use of a helper function (defined below) called 'contains' 

ConditionMet = function (Context) {

            var unit = Context.Driver.Unit;

 if (unit) {

         // Check whether the assigned vehicle is moving and has the specified tag

         if(unit.Status === 2 && unit.Tags !== null && contains(unit.Tags,tagName)){

                        if(Context.Driver.Tags === null || !contains(Context.Driver.Tags,tagName)){

                                    //the driver does not have the same tag

                                    return true;

                        }

                    }

 }

 return false;

};

 

//returns true if the specified item is in the given array

function contains(array, item) {

    for (var i = 0; i < array.length-1; i++) {

        if (array[i] === item) {

            return true;

        }

    }

    return false;

}

 

Checking License for a Fleet

This example causes an alert to trigger when a driver assigned to a vehicle in a specified fleet does not have a specified class of license.
 

Because this alert involves time, turn on aggressive and time to trigger by setting the Condition Type to ScriptedTimeRangeCondition.
The Target Type of this script is Driver.
The script does not use the global State variable, so you should clear the Keep a persistent state for this script check box.
Select the This script does not require message from vehicles check box.
The script requires you to create the following parameters:
 

Parameter Name

Parameter Type

Meaning

expected_class

string

The class of license the driver should have to drive vehicles in the specified fleet.

big_fleet

Fleet

The fleet that requires the special license class.

 

 

ConditionMet = function (Context) {

 if (Context.Driver.LicenseClass !== expected_class) {

         // Check whether the assigned vehicle is in the specified fleet

         if(!big_fleet.IsMember(Context.Driver.Unit)){

                        return true;

                    }

 }

 return false;

};