Click Scripts: create new messages using notifiers

Using notifiers in click scripts makes it possible to create new messages from a script that behaves just like messages sent from a device. Notifier click scripts are scripts triggered when an event starts or ends. RouteMessage click scripts are a new powerful type of script that is executed whenever a device sends a message. In these scripts, you can use the message object to create new messages for the device that triggered the script or for a different device.

Examples

Modifying the device track history messages adding geofence information

This example shows how to save visited geofences in track history.

1. You can use the Inside Geofence event rule available in Basic One template, or create a new event rule with a geofence expression like this:

MFS_geofence_expression.png

2. In Admin > Device Mappers, map the input Text1 to Custom text

MFS_mapped.png

3. Create a notifier click script in Development > App Builder > + New App

4. Now specify a name for your new app and go to 2. Click Scripts > + New Click Script

5. Select the Kind > Notifier type

6. Click Edit and copy-paste the following script:

// The message object first needs to be connected to
// the target user by id. Since we want to create a new
// message for the same user, we can get the id from
// the user object:
message.connect(user.id);
// By calling connect, we have also loaded the latest data
// for the user into the message object.
 
// To ensure that the message that we are creating
// is visible after the message that triggered this script,
// we will move its utc timestamp by 1 second like this:
var t = user.trackPoint.utcTimestamp.getTime() + 1000;
message.trackPoint.setUtcTimestamp(new Date(t));
 
// Now we set the value of CustomString variable
// of the message that we are creating.
// Note: for this to work, specified variable must be mapped
// in a device mapper used by the target user.
// The new value will come from the event rule.
// Using context.resolve we can resolve any expression
// that can be used in the Notifier section of the rule.
// Here, that is '[GEOFENCE_NAME]'.
message.fields.set(
  'CustomString',
  context.resolve('[GEOFENCE_NAME]')
);
 
// Calling this will trigger the actual processing of
// the message modified in the script.
message.process();

7. Run the script from the editor to ensure that it works. Note that in a run from the editor expression specified in context.resolve is not resolved but just used as-is.
8. Then save the script.

Testing

With this setup, when a message from a vehicle triggers the Inside Geofence rule, another message will immediately be generated for it, containing the name of the geofence as the value of CustomString variable. This variable effectively denotes “name of last visited geofence.”

Driver Variable

You can save driver information to track history in roughly the same way as in the previous example, with the following changes:

- Instead of Inside Geofence rule, use a Driver ID Event Rule

- Use the same script, just with [GEOFENCE_NAME] replaced with [DRIVER_NAME].

This will make CustomString variable contain the name of the last driver.

Avoid circular dependencies

A message created by script X must not end up triggering X again. That will end up in the following error text in the terminal window.