Тема: Callback Closure
Hallo
I need a ‘for’ loop to build an array of all the units with custom sensor values, which means more than one callback is scheduled which is not completed in the same loop iteration. I know I need some sort of ‘callback closure’ here, but I don’t know how to implement it with the MessagesLoader.loadInterval method. Could you please assist?
My code below:
function loadMessages(units) {
var sess = wialon.core.Session.getInstance();
var to = sess.getServerTime(); // get ServerTime
var from = to - 300; // 5 minutes
var ml = sess.getMessagesLoader();
for (var i = 0; i < units.length; i++) {
var unitId = units(i).unitId;
const temp = ml.loadInterval(unitId, from, to, 0, 0, 10,
function (code, data) { // loadInterval callback
if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code
else {
let custSensorValues = [];
for (var i = data.messages.length - 1; i > 0; i--) { // get last received message with values
if (data.messages(i).p.data_type === "E6") { // E6 = custom sensor values??
let custSensorValue = {};
custSensorValue.t11p = (data.messages(i).p.t11_pressure * 0.0689475729).toFixed(2);
custSensorValue.t14p = (data.messages(i).p.t14_pressure * 0.0689475729).toFixed(2);
custSensorValues.push(custSensorValue);
units(i).custSensorValues = custSensorValues;
}
}
}
}
);
}
}
Thank you in advance.