User avatar
johan
GpsGate
Posts: 14258
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

Tips when developing Script Expressions.

Sat Sep 05, 2015 2:36 pm

Here are some general guide lines that will make your development of scripts faster and more stable.

Read this guide on Script Expressions to start with:
http://gpsgate.com/blogComment/script_expressions

Write down all Use Cases you want your script to support. Write down each case step by step. Once you have developed your script, make sure you test all Use Cases.

Create a test application. Always make your development in a separate test application and not in the customer application. The test application can also be on a different server.

Use the Test Script function you have in the editor to test your script. You can set variable and trackPoint inputs in the editor. Geofences, POIs Custom Fields etc. will be taken from the selected user.

Use the log() function to log to the Terminal or the App Console.
App Console: http://gpsgate.com/blogComment/app_console
Terminal: http://forum.gpsgate.com/topic.asp?TOPIC_ID=10740

How to use SimOne to test Event Rules http://forum.gpsgate.com/topic.asp?TOPIC_ID=14038

Export the Event Rule from the test application and import it into the customer application when you are finished with development and testing.
How to: http://gpsgate.com/blogComment/export_and_import_event_rules_and_geofences

Here is a list of script samples (we will add more over time):

Sample: Inside POI http://forum.gpsgate.com/topic.asp?TOPIC_ID=18314

Sample: Geofence rule that checks if a vehcile is inside a geofence named by a custom field.
http://forum.gpsgate.com/topic.asp?TOPIC_ID=18302

Sample: Script Expression example for daily odometer interval.
http://forum.gpsgate.com/topic.asp?TOPIC_ID=17792

Guide: Multiple events per event rule and device. Set Notifier variables from script.
http://forum.gpsgate.com/topic.asp?TOPIC_ID=17167

Guide: Allow users to select Event Rules using Custom Fields with scripting
http://gpsgate.com/blogComment/allow_users_to_select_event_rules_using_custom_fields_with_scripting

Guide: Notifier variables: http://forum.gpsgate.com/topic.asp?TOPIC_ID=18301

Guide: Trip and Idle and Parking report using event rule
http://gpsgate.com/blogComment/trip_and_idle_and_parking_report_using_event_rule

Guide: Trip and Idle in Event Rule Reports
http://gpsgate.com/blogComment/trip_and_idle_data_in_event_rule_reports

Guide: Driver Login Report http://gpsgate.com/blogComment/999

Sample: 5 km gap without position updates http://forum.gpsgate.com/topic.asp?TOPIC_ID=18335

Sample: Distance between POIs (customer sites) http://forum.gpsgate.com/topic.asp?TOPIC_ID=18588

Regards,
Johan

GpsGate Support

piter197
Posts: 808
Joined: Sun Jan 09, 2011 3:32 am

RE: Tips when developing Script Expressions.

Sun Sep 06, 2015 4:15 am

Hi,

Another tip when you use:
var x = session.user.getCustomFieldValue(strLabel)

make sure to have conditional is null check for var x before using it for other method's input parameter.

When Test script inside Script Editor windows, no error message will occur but when saving it inside Event Rule you might get Error in Script expression.


Best Regards,
piter197

User avatar
Josip
Posts: 19
Joined: Tue Mar 11, 2014 2:30 pm
Location: Sweden

RE: Tips when developing Script Expressions.

Wed Jan 20, 2016 1:54 pm

You can use the context.state to store more than just simple string/numeric values.

When using the state to store a more complex JavaScript object (http://www.w3schools.com/js/js_objects.asp), we recommend following these three steps.

1. In the beginning of the script, get the state object, making sure that it is not null:

var st = context.state || {};

Or, alternatively, if the structure of the state is known in advance and you need to set some initial values for the very first execution, use something like this:

var st = context.state || {
sampleVar1: 0,
sampleVar2: 'INITIAL_VALUE'

// ...
};


2. Now read the data from the state through the "st" object. Likewise, do any modifications to the state that are necessary directly on the "st" object.

// read
var oldValue = st.sampleVar1;
// write
st.previousSpeed = trackPoint.velocity.groundSpeed;

Note that as state is stored as a JSON object (https://en.wikipedia.org/wiki/JSON), storing a Date (http://www.w3schools.com/jsref/jsref_obj_date.asp) value to the state will convert it to a string.
When retrieving the date value that was stored in the state, do this:

var properDateValue = new Date(st.someDateValue);


3. Modifications done to the "st" object will not actually be preserved until you do:

context.setState(st);

Best place to do this is after all the necessary uses and modifications, just before returning a value from the script (if the script branches out to more than one "return", do this just before each of them).


Regards,
Josip

GpsGate Support

Return to “GpsGate for Developers”