Script to check geofence conditions

This guide shows how you can write a geofence rule that checks if a asset 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 the asset.

Setup:

1. Create a custom field named Fence.

2. Create a geofence group named Pipelines.

3. Create a geofence called e.g. Pipeline33. Add Pipeline33 to the geofence group Pipelines

Screenshot 2024-09-18 at 11.17.08.png

4. Edit your test asset and set the custom filed "Fence" to "Pipeline33"'

Screenshot 2024-09-18 at 11.17.52.png

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, create a Report Argument if you want to create a column in the EV1000 report. Or create an Argument notifier if you want to create on the column in the Event window in the Workspace.

mceclip0.png

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

Result: 

Screenshot 2024-09-18 at 11.25.53.png