Saved Value Device Mapper Scripts

Saved value Device Mapper scripts open up many scripting possibilities.

  • The result of these scripts will be saved and the script will not be re-evaluated when reading tracks or generating reports.
  • This saved value field scripts can be used to keep a history of Custom Fields.
  • When changing the script or reprocessing tracks, the stored value won't be modified.

Developers may also think of saved value to mean persistable field scripts.

1. Make sure you're familiar with the concepts of Device Mapper and Device Mapper Scripts

2. Login into an Application and go to Admin > Device Mapper 4. Scripts and enable Persist Result > Yes

Usage examples

  • Example #1 Custom Fields history
  • Example #2 Handle key=value data from device
  • Example #3 Counter (+1)
  • Example #4 Counter (cumulative) 

1. Example: Custom Fields history

Output -> TemperatureThreshold (Custom Variable created in SiteAdmin of type double)

Script:

// -----------------------------

var temp = user.getCustomFieldValue('TempThreshold'); // User custom field, need to be defined first and should be of type number

if(temp != null && temp != undefined) {
return parseFloat(temp);
}

return null

// -----------------------------

Now TempThreshold can be used in Event rules or reporting.

 

2. Example: Handle key=value data from device (2 scripts)

Script 1
Output -> Temperature1

// ------------- SCRIPT ------------------
var val1 = fields.get('CustomString', null);

if(val1) {
var kvp = val1.split('=');
if(kvp.length ==2 && kvp[0] == 'temp1') {
return parseFloat(kvp[1]);
}
}

return null; // keep previous value

// ----------- END SCRIPT ----------------

 

Script 2
Output -> Temperature2

// ------------- SCRIPT ------------------
var val1 = fields.get('CustomString', null);

if(val1) {
var kvp = val1.split('=');
if(kvp.length ==2 && kvp[0] == 'temp2') {
return parseFloat(kvp[1]);
}
}

return null; // keep previous value

// ----------- END SCRIPT ----------------

 

3. Example: Counter (+1)

Output -> CustomAnalog1

Script:

var crashed = fields.get('Crash', false);
var currentCrashCount = fields.get('CustomAnalog1', 0);

if(crashed) {
return currentCrashCount + 1;
}

return currentCrashCount;

 

4. Example: Counter (cumulative)

Output -> CustomAnalog1. (Note: fuelDiff needs to be a difference of fuel between the current device report and the previous one)

Script:

var fuelDiff = fields.get('fuelDiff', 0);
var totalFuel = fields.get('CustomAnalog1', 0);

return totalFuel + fuelDiff;