Click Scripts: Geofence

We are now extending the scripting capabilities beyond the messages and events originating from a device. Click Scripts for geofences enable you to trigger custom http requests whenever an operator creates, modifies or removes a geofence.


When a user modifies a geofence, a Click Script for geofences can access the data of that geofence and see the type of action that was performed. With this information, you can create http GET or POST requests, forwarding the information in custom format composed within the script, enabling you to integrate VehicleTracker with your own web services. Furthermore, a geofence modification is performed only if the script is successfully executed to make sure that the data is forwarded accurately and completely, as defined by the script.

Required Knowledge

To successfully utilize http requests, it is assumed that you know how to set up and use web services, or at least have an existing web service where the format of data it can receive and interpret correctly is known.

Installation
  1. Login to SiteAdmin.
  2. Go to Plugins tab.
  3. Set the Repository URL to http://update.gpsgate.com
  4. Under My Plugins tab, make sure that Patch.v4 is installed and up to date.
Enabling Click Scripts

To be able to create Click Scripts, you need to enable _EditClickScript privilege. To do this, follow these steps:

  1. Login to SiteAdmin.
  2. Go to Applications tab. Click Search and Manage.
  3. Click on the application for which you want to enable Click Scripts.
  4. In Privileges and Roles section, under ClickScript (BETA: ScriptApp), check _EditClickScript (BETA: and _EditScriptApp) privilege and click Save.
  5. Login to Vehicle Tracker Application. From the main menu, go to “Admin” > “Roles”.
  6. In Roles window, click on the role for which you want to enable Click Scripts.
  7. Under Privileges, enable Click Scripts in the same way you did for the application.
Creating a Click Script

To add a click script, go to Development -> App Builder. This will show you a list of your Apps. Click on an App or create a new one. In the Click Scripts section click on Add new Click Script, this will open up the Edit Click Script window.

Edit Click Script

Here you should specify a name for the new Click Script. Choose the “Geofence” kind. “Enabled” option determines whether the script will be executed on actual geofence modifications. “Edit” button will take you to the “Script Editor” window.

Click Script Editor

The autogenerated script on the left is there to show the basic way of handling different geofence operations. On the right, you can see the objects and methods available in Click Scripts for geofences:

  • geofence stores geofence information - geofence.name is the name of the geofence, etc (point the cursor to a particular item in the object tree to get more information).
  • click.operation stores the information on the performed action.
  • http.GET(url,query) and http.POST(url,body) are methods used to execute http requests. After one of those is invoked, response data can be read from http.statusCode, http.statusMessage and http.response.
  • log(x) can be used to log data from the script for testing purposes.
Example Use

Basic scenario: we want to send the information about each updated or removed geofence to a web service.

  1. Set up a web service to receive post requests. In this example, request body is expected to contain a JSON representation of an object containing the information on the action performed and the geofence it was performed on, similar to this:

     

  2. Go to “Admin” ? “Click Scripts”, and add a new Click Script, naming it, for example, “ForwardGeofenceData”. Leave it enabled, and click “Edit” to enter the script editor.
  3. Delete the default script, and replace it with:

    In the first line, you format the body in the described format. Note that geofence object is automatically converted into JSON representation when used like this.
    In the second line, ADDRESS_OF_YOUR_SERVICE should be the address of the web service you have set up.

  4. Click “Test Script” to execute a test run of the script. Note that this will send the actual request, so you will have to handle and clean up test data manually. Furthermore, to successfully execute a test, you have to select an existing geofence in the Geofence panel. That will be the geofence that the test run will be executed for, i.e. the data of that geofence will be available to the script.
    Finally, note that the test run simulates the situation where a selected type of operation was performed on the selected geofence, but it does not alter the geofence in any way.

  5. If the run was successful, the output panel of the editor will contain only the timestamp. If it fails, it will contain text of the error. If you use the log() function anywhere in the script, it will also contain the data you have logged for test purposes. You may also check the http response and status of the last successful request in the appropriate fields in the script editor.
  6. Accept and Save the script. Note that if you modified the script after the last test run, you will be prompted to run the script once more when saving. This does not insure that the script will always be executed successfully, but it helps prevent saving the scripts with evident syntax errors.
  7. Try to add, edit or remove a geofence. If the operation was successful, the web service should have received the request.
Advanced options

Here are hints on some other things you could do:

  • You might want to format the data in a different format. You could do this by using JavaScript string formatting options, and accessing geofence fields directly, such as using geofence.center.longitude to get the longitude of the center point of a circular geofence. In that case, make sure to handle the possibility that a geofence might not be circular, in which case the geofence.center property is null, and the use of geofence.center.longitude would make the script fail, preventing the addition or modification of all but circular types of geofences.
  • You could set up the service and the script to work in multiple steps, and/or make the script execution dynamic based on the response body received back from the service. This would be performed by a combination of http.GET / http.POST requests and reading and interpreting the body of the http.response string. Make sure, however, that the script is always accurately synchronized with the service, as the failing script would prevent the geofence modification.
Enable Logging at Runtime

You may enable the log function and logging of errors from Click Scripts at runtime by editing the NLog.config file in the IIS folder under the GpsGate Server installation folder (if you are not hosting your own server, you may contact support for access to files).
Under <targets>, you should add a new target entry like this:

where TARGET_NAME is an unique name for the file target and FILE_NAME is the name of the log file you wish to be created with full path to the file.
Under <rules>, add an entry like this:

where NAME_OF_CLICK_SCRIPT is the name of the click script which you want logged to the file, and TARGET_NAME is the unique name of the target you added above.
Errors and data logged through log method in that Click Script will then be written to the specified file.


  Discuss this blog post on the forum please



Download free GpsGate Server

Install it on your own server. The installation is free for 5 users.

Download Now