Script to check geofence conditions

This guide shows how you can write a geofence rule that checks if a vehcile is inside a geofence named by a custom field. In this way you can write one single event rule, which checks for different geofences depending on vehicle.

Test Setup

Create a Custom Field named Fence.

Create a Geofence Tag named "Pipelines".
Create a Geofence called e.g. "Pipeline33". Add Pipeline33 to Geofence Tag Pipelines

Edit your test vehicle and set the custom filed "Fence" to "Pipeline33"

Create Event Rule

Create an Event Rule with a Script Expression like the one below:

// Find geofences at current position.
var fences = geofence.find(trackPoint.position.longitude, trackPoint.position.latitude, 'Pipelines');
var isInside = false;

context.setVariable('PipeName', '-');

if(fences.length > 0)
{
  // Get name of geofence from Custom Field
  var pipeName = session.user.getCustomFieldValue('Fence');

  if(pipeName != null)
  {
	// Check if named geofence is as current position.
	
	for(var iIndex = 0; iIndex < fences.length; iIndex++)
	{
	  if(fences[iIndex].name == pipeName)
	  {
		isInside = true;
		// Use [PipeName] as variable in Notifiers
		context.setVariable('PipeName', pipeName);
		break;
	  }
	}
  }
}

// Return true if inside named geofence.
return isInside;

 

Notifiers

Use the function context.setVariable() to create variables you can use in the Notifiers. For example a "Report Argument" if you want to create a column in the EV1000 report. Or an "Argument" notifier if you want to create on column in the Event window in the Workspace.

In the example above, the named geofence will be in variable [PipeName]