I have written a driver in JavaScript that decodes the payload of the sensor which is hexadecimal encoded. It receives the payload and the port details to the pre-defined function 'decode' which is the body of the driver provided by one of the IoT platforms and the driver has to be developed with this as an entry point and cannot be deleted.
The sensor I am using sends a payload which contains same kind of measurement (temperature, depth, signal strength) measured 4 times.(the values might change).
In my driver I have written the logic for 4 measurements all the four times. When the logic is still same. Instead I wan't to write it once and retrieve part of payload for every single measurement.
'1000000001121B7701131BAA01121BA90114F274'
this is how the sample payload looks and it contains 4 measurements.
The ideal situation would be a function say measurement
which calculates temperature, depth, etc., divides my payload into 4 parts and then uses it with functions for each sub payload rather than writing the whole measurement function four times. The current code works but is far from perfect.
Can anyone help? On how to improve the use of functions?
/* Arguments:
* 'payload' - an array of bytes, raw payload from sensor,
* 'port' - an integer value in range [0, 255], f_port value from sensor*/
/* diver for measurement sensor payload, one complete payload*/
function decode(payload, port) {
var result=new Object();
var payloadType = {};
var Temperature = {};
var Level = {};
var Status = {};
var Alarm = {};
var SRCSRSSI = {};
var Temperature1 = {};
var Temperature2 = {};
var Temperature3 = {};
var Level1 = {};
var Level2 = {};
var Level3 = {};
var SRCSRSSI1 = {};
var SRCSRSSI2 = {};
var SRCSRSSI3 = {};
// check if its right device
if (payload[1] != 00) {
console.log(" Not TEK 766 payload");
}else {
// correct sensor payload type
console.log("TEK 766 payload")
}
// defines the payload type is measurement
if (payload[0] == 16) {
console.log("valid payload, Measurement");
result.payloadType = 'Measurement';
//measurement 1 starts here
var level1 = payload[4]; //Defines the ullage reading in cms
var level2 = payload[5]; //Defines the ullage reading in cms
var temp1 = payload[6]; //Defines the temperature in °C
result.Level = ((level1 * 256) + level2);
if (temp1 < 32) {
result.Temperature = temp1;
}else {
result.Temperature = (temp1 - 256);
}
if (payload[2] != 0){
// payload describes alarm
console.log("Alarm Limit Reached");
var lim = (parseInt(payload[2], 16).toString(2))
lim = lim.slice(4);
var x = (lim&1) ? "Alarm Reached Level-1":"Alarm Not-ReachedReached Level-1"
console.log(x);
result.Alarm = x;
var y = (lim&2&3) ? "Alarm Reached Level-2":"Alarm Not-Reached Level-2";
console.log(y);
result.Alarm = y;
var z = (lim&4&5&6&7) ? "Alarm Reached Level-3":"Not Reached Level-3";
console.log(z);
result.Alarm = z;
}else {
result.Alarm = 'Alarm Level Not-Reached';
}
//defines SRC major nibble
var src = payload[7];
var srssi = payload[7];
result.SRCSRSSI = (src|srssi);
//measurement 2 starts here
var level11 = payload[8]; //Defines the ullage reading in cms
var level12 = payload[9]; //Defines the ullage reading in cms
var temp11 = payload[10]; //Defines the temperature in °C
result.Level1 = ((level11 * 256) + level12);
if (temp11 < 32) {
result.Temperature1 = temp11;
}else {
result.Temperature1 = (temp11 - 256);
}
//defines SRC major nibble
var src1 = payload[11];
var srssi1 = payload[11];
result.SRCSRSSI1 = (src1|srssi1);
//measurement3 starts here
var level13 = payload[12]; //Defines the ullage reading in cms
var level14 = payload[13]; //Defines the ullage reading in cms
var temp12 = payload[14]; //Defines the temperature in °C
result.Level2 = ((level13 * 256) + level14);
if (temp12 < 32) {
result.Temperature2 = temp12;
}else {
result.Temperature2 = (temp12 - 256);
}
//defines SRC major nibble
var src2 = payload[15];
var srssi2 = payload[15];
result.SRCSRSSI2 = (src2|srssi2);
//measurement 4 starts here
var level15 = payload[16]; //Defines the ullage reading in cms
var level16 = payload[17]; //Defines the ullage reading in cms
var temp13 = payload[18]; //Defines the temperature in °C
result.Level3 = ((level15 * 256) + level16);
if (temp13 < 32) {
result.Temperature3 = temp13;
}else {
result.Temperature3 = (temp13 - 256);
}
//defines SRC major nibble
var src3 = payload[19];
var srssi3 = payload[19];
result.SRCSRSSI3 = (src3|srssi3);
return {
"result": result,
"payload": payload,
"port": port
}
}
}
This code runs on an IoT platform named thingsHub which offers middleware services.