I use function and settime out for five case if case one can set timeout ok but when get case 2 that repeat timeout in case one again or get case 3 - 5 that repeat older again
Here my code
var timeout = context.get("timeout") || 0
function stop_timeout() {
clearTimeout(timeout)
}
if (lengths == "21") {
if (timer) {
clearInterval(timer)
context.set("timer", 0)
}
var imei = newMsg.substr(7, 15);
var casename = newMsg.substr(22, 1);
var status_device = newMsg.substr(23, 1);
var byte_check = newMsg.substr(24, 2);
if (casename == "1"){
if (timeout) stop_timeout();
timeout = setTimeout(function(){
node.send([{
"topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
}], null);
}, 1000 * 60 * 4 );
clearTimeout(timeout)
context.set("timeout", 0)
}
if (casename == "2"){
if (timeout) stop_timeout();
timeout = setTimeout(function(){
node.send([{
"topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
}], null);
}, 1000 * 60 * 6 );
clearTimeout(timeout)
context.set("timeout", 0)
}
if (casename == "3"){
if (timeout) stop_timeout();
timeout = setTimeout(function(){
node.send([{
"topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
}], null);
}, 1000 * 60 * 21 );
clearTimeout(timeout)
context.set("timeout", 0)
}
if (casename == "4"){
if (timeout) stop_timeout();
timeout = setTimeout(function(){
node.send([{
"topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
}], null);
}, 1000 * 60 * 6 );
clearTimeout(timeout)
context.set("timeout", 0)
}
if (casename == "5"){
if (timeout) stop_timeout();
timeout = setTimeout(function(){
node.send([{
"topic": "UPDATE device_request SET `status`='" + "9" + "',`start_flow`='" + "1" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
}], null);
}, 1000 * 60 * 4 );
clearTimeout(timeout)
context.set("timeout", 0)
}
node.send([{
topic: "UPDATE device_request SET `status`='" + "2" + "',`start_flow`='" + "2" + "' WHERE imei = '867724030292780' ORDER BY id DESC LIMIT 1"
}], null);
node.send([{
topic: "INSERT INTO device_ack (`header`, `length`, `cmd`, `imei`, `check1`, `check2`, `casename`, `status_device`, `byte_check`) VALUES ('" + header + "', '" + lengths + "', '" + cmd + "', '" + imei + "', '" + "" + "', '" + "" + "', '" + casename + "', '" + status_device + "', '" + byte_check + "')"
}], null);
// msg.topic = "INSERT INTO device_ack (`header`, `length`, `cmd`, `imei`, `check1`, `check2`, `casename`, `status_device`, `byte_check`) VALUES ('" + header + "', '" + lengths + "', '" + cmd + "', '" + imei + "', '" + "" + "', '" + "" + "', '" + casename + "', '" + status_device + "', '" + byte_check + "')";
// return [msg, null];
}