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;
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;
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:
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;
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:
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;
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;