Script examples

Parse data sent by a thing

The thing firmware sends a payload formatted as follow:

geo_coordinates;battery_level;outside_temperature;speed (Example: 42.510446667094236,12.292413332612515;86;31.365;114.639)

Given you want to store information in 4 different parameters (namely geo_coordinates, battery_level, outside_temperature and speed), a correct script could be:

// define empty list of measurements
var outList = new java.util.ArrayList();
// transform bytes payload to a string and split it by ;
var list = OMV.toString(payload).split(";");
// timestamp in this case is the time of execution
var now = new Date();
var timestamp = now.getTime();
// get single value for each information
var latlon = list[0];
var battery = list[1];
// temperature and speed must have 2 decimals
var temperature = parseFloat(list[2]).toFixed(2);
var speed = parseFloat(list[3]).toFixed(2);
// fill the list of measurements and return it
outList.add(new Measurement('latlon', latlon, timestamp));
outList.add(new Measurement('battery', battery, timestamp));
outList.add(new Measurement('temperature', temperature, timestamp));
outList.add(new Measurement('speed', speed, timestamp));
return outList;

Retrieve information from the big data

The thing firmware sends a payload formatted as follow:

speed (Example: 54.334)

Given you want to store information in the speed parameter and compute an estimate of the cost of the fuel consumed, which will be saved in a costEstimate parameter. Assuming the fuel consumption (per kilometer) is contained in a fuelRatio parameter a possible script may be:

// define empty list of measurements
var outList = new java.util.ArrayList();
// read the speed from the payload
var speed = parseFloat(OMV.toString(payload)).toFixed(2);
// retrieve the fuel consumption
var bdc = new BigDataClient(topic, env);
var fuelRatio = bdc.getLatestMeasurement(env.getProperty('resourceId'), 'fuelRatio'); // expressed in $/(kph)
// compute the estimate
var estimate = fuelRatio * speed;
// fill the list of measurements and return it
var now = new Date().getTime();
outList.add(new Measurement('speed', speed, timestamp));
outList.add(new Measurement('costEstimate', estimate, timestamp));
return outList;

Perform HTTP requests

The thing firmware sends a payload formatted as follow:

speed (Example: 54.334)

Given you want to store information in the speed parameter and then make some HTTP calls, for example:

  • an HTTP GET towards a given address;
  • an HTTP POST to send some arbitrary data;
  • an HTTP PUT to forward the information which has been just received.

With the following script example we provide a possible way to do that:

// define empty list of measurements
var outList = new java.util.ArrayList();
// read the speed from the payload
var speed = parseFloat(OMV.toString(payload)).toFixed(2);

// replace the following URLs with the ones you actually want to direct the HTTP requests towards
var urlGet = 'http://requestbin.fullcontact.com/testUrl1';
var urlPost = 'http://requestbin.fullcontact.com/testUrl2';
var urlPut = 'http://requestbin.fullcontact.com/testUrl3';

// HTTP GET call example
var httpGet = new HttpGet(urlGet);
httpGet.send();

// HTTP POST call example
var httpPost = new HttpPost(urlPost);
httpPost.addJson(JSON.stringify({ "sky": "blue" }));
httpPost.send();

// HTTP PUT call example
var httpPut = new HttpPut(urlPut);
httpPut.addJson(JSON.stringify({ "speed": speed }));
httpPut.send();

// fill the list of measurements and return it
var now = new Date().getTime();
outList.add(new Measurement('speed', speed, timestamp));
return outList;

Send messages via MQTT

The thing firmware sends a payload formatted as follow:

speed (Example: 54.334)

Let's suppose you want to store information in the speed parameter, and also:

  • send another message via MQTT (to Omnyvore) if the speed is greater than 100;
  • send a command to the thing via MQTT if the speed is less than 5.

With the following script example we provide a possible way to do that:

// define empty list of measurements
var outList = new java.util.ArrayList();
// read the speed from the payload
var speed = parseFloat(OMV.toString(payload)).toFixed(2);

// replace the following URLs with the ones you actually want to direct the HTTP requests towards
var mqtt = new Mqtt(topic, env);
if(speed > 100) {
    mqtt.publishOut('speedExcess', (speed - 100));
}
if(speed < 5) {
    mqtt.publishIn('accelerate', 'true');
}

// fill the list of measurements and return it
var now = new Date().getTime();
outList.add(new Measurement('speed', speed, timestamp));
return outList;

Command message processing

When a command has been sent, if the message processing is enable, an expression or a script will be executed.

Let's suppose you want to store the number of times that a command has been sent ('nTimes') and the sum of all command payload sent (('sum')).

With the following script example we provide a possible way to do that:

var outList = new java.util.ArrayList();

// get the payload
var payloadStr = OMV.toString(payload)
var payloadValue = parseInt(payloadStr);

var bdc = new BigDataClient(topic, env);
//initialize nTimes and sum to 0
var nTimes = 0;
var sum = 0;

try {
    // get ntimes latest measurement
    var nTimesMeasm = bdc.getLatestMeasurement(env.getProperty('resourceId'), 'ntimes');
    // get payloadsum latest measurement
    var payloadSumMeasm = bdc.getLatestMeasurement(env.getProperty('resourceId'), 'payloadsum');
    if(nTimesMeasm != null) {
        nTimes = nTimesMeasm.value;
    }
    if(payloadSumMeasm != null) {
          sum =  payloadSumMeasm.value;
    }
} catch(err) {
    print("Errore while retrieving data from BigData");
}

// increase nTimes value
nTimes ++;
// sum the new payload value
sum += payloadValue;

// add the new measurements
var now = new Date();
var timestamp = now.getTime();
outList.add(new Measurement('ntimes', nTimes, timestamp));
outList.add(new Measurement('payloadsum', sum, timestamp));

return outList;