I'm having issues with having my node to start interval function , I need to start function when get data from input like my code cmd == "01" and stop function when i get cmd == "06" i try can't run
Here my flow
[{"id":"8a66f209.73b0a","type":"mqtt out","z":"68b22cc5.bf042c","name":"","topic":"SUB/867724030292780","qos":"","retain":"","broker":"1b33bbc4.fa5024","x":1476.3333740234375,"y":901.5333251953125,"wires":[]},{"id":"3c583e87.f57792","type":"function-npm","z":"68b22cc5.bf042c","name":"Check Null Test","func":"var moment = require('moment-timezone');\n\nvar current = moment().tz(\"Asia/Bangkok\").format(\"YYYYMMDDHHmmss\");\nvar x = global.get('YourVariable');\n\n if (msg.payload === undefined || msg.payload.length === 0){\n \n node.send({\n payload:\"UHM\".concat(\"21\").concat(\"06\").concat(\"867724030292780\").concat(current).concat(\"ff\")\n });\n } else{\n \n node.send({\n payload:\"UHM\".concat(\"21\").concat(\"03\").concat(\"867724030292780\").concat(current).concat(\"ff\")\n });\n }\n \n\n\nreturn null;\n","outputs":1,"noerr":0,"x":1152.36669921875,"y":900,"wires":[["8a66f209.73b0a"]]},{"id":"237e9cde.764f04","type":"mysql","z":"68b22cc5.bf042c","mydb":"3941ded9.ccf0b2","name":"","x":850.75,"y":761.5,"wires":[["3c583e87.f57792","5117888c.a10a2"]]},{"id":"5117888c.a10a2","type":"debug","z":"68b22cc5.bf042c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1217.36669921875,"y":780.75,"wires":[]},{"id":"dab2b97e.1f7f78","type":"function-npm","z":"68b22cc5.bf042c","name":"Check CMD","func":"var converter = require('hex2dec');\n\nvar x;\n\nfunction start(){\n\n x = setInterval(function(){\n \n msg.topic = \"SELECT `casename`, id , imei ,start_time FROM operation_database WHERE imei = 867724030292780 AND casename = ' ' ORDER by id DESC LIMIT 1\";\n return msg;\n \n }, 1000);\n\n}\n\nfunction stop(){\n\n clearInterval(x);\n\n}\n\nvar newMsg = msg.payload;\n\nglobal.set('YourVariable', newMsg); \n\n\nvar header = newMsg.substr(0,3);\nvar lengths = newMsg.substr(3,2);\nvar cmd = newMsg.substr(5,2);\n\nif(cmd == \"01\"){\n \n \n var imei = newMsg.substr(7,15);\n var sim_id = newMsg.substr(22,15);\n var ip_1 = converter.hexToDec(newMsg.substr(37,2));\n var ip_2 = converter.hexToDec(newMsg.substr(39,2));\n var ip_3 = converter.hexToDec(newMsg.substr(41,2));\n var ip_4 = converter.hexToDec(newMsg.substr(43,2));\n var ip = ip_1.concat(\".\").concat(ip_2).concat(\".\").concat(ip_3).concat(\".\").concat(ip_4);\n var port = newMsg.substr(45,5);\n var device_timestamp = newMsg.substr(50,14);\n var device_date = newMsg.substr(50,8);\n var device_time = newMsg.substr(58,6);\n var battery = converter.hexToDec(newMsg.substr(64,2));\n var rssi = converter.hexToDec(newMsg.substr(66,2));\n var rssi_before = converter.hexToDec(newMsg.substr(68,2));\n var event_setup = newMsg.substr(70,1);\n var byte_check = newMsg.substr(71,2);\n\n \n node.send([null, {payload:\"UHM\".concat(\"15\").concat(\"01\").concat(\"867724030292780\").concat(\"N\").concat(\"N\").concat(\"ff\")},null]);\n \n if (lengths == \"44\"){\n msg.topic = \"SELECT `casename`, id , imei ,start_time FROM operation_database WHERE imei = 867724030292780 AND casename = ' ' ORDER by id DESC LIMIT 1\";\n return[msg,null,null];\n \n }\n \n}\n\nif(cmd == \"06\"){\n \n var imei = newMsg.substr(7,15);\n var check1 = newMsg.substr(22,1);\n var check2 = newMsg.substr(23,1);\n var byte_check = newMsg.substr(24,2);\n \n msg.topic = \"INSERT INTO `device_ack` (`header`, `length`, `cmd`, `imei`, `check1`, `check2`, `byte_check`) VALUES ('\"+header+\"', '\"+lengths+\"', '\"+cmd+\"', '\"+imei+\"', '\"+check1+\"', '\"+check2+\"', '\"+byte_check+\"')\";\n return[null,null,msg];\n}\n\n\n\n\n\n","outputs":3,"noerr":0,"x":482.36669921875,"y":849.75,"wires":[["237e9cde.764f04"],["86d8f9a3.34d978"],["748d064f.1020d8"]]},{"id":"79c8542a.0a7db4","type":"mqtt in","z":"68b22cc5.bf042c","name":"","topic":"PUB/867724030292780","qos":"2","broker":"1b33bbc4.fa5024","x":147.36666870117188,"y":930,"wires":[["dab2b97e.1f7f78"]]},{"id":"b91edbb1.b55588","type":"inject","z":"68b22cc5.bf042c","name":"","topic":"","payload":"UHM440186772403029278052004510000143236e3cd7d1210620190220141500645a5aNff","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":116.75,"y":775.5,"wires":[["dab2b97e.1f7f78"]]},{"id":"86d8f9a3.34d978","type":"mqtt out","z":"68b22cc5.bf042c","name":"","topic":"SUB/867724030292780","qos":"","retain":"","broker":"1b33bbc4.fa5024","x":840.3333129882812,"y":821.5333251953125,"wires":[]},{"id":"748d064f.1020d8","type":"mysql","z":"68b22cc5.bf042c","mydb":"3941ded9.ccf0b2","name":"","x":852,"y":875,"wires":[[]]},{"id":"1b33bbc4.fa5024","type":"mqtt-broker","z":"","name":"","broker":"m15.cloudmqtt.com","port":"12106","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"3941ded9.ccf0b2","type":"MySQLdatabase","z":"","host":"43.241.57.67","port":"3306","db":"vnsoftt_flushvalve_phuket","tz":""}]
Here my code
var converter = require('hex2dec');
var x;
function start(){
x = setInterval(function(){
msg.topic = "SELECT `casename`, id , imei ,start_time FROM operation_database WHERE imei = 867724030292780 AND casename = ' ' ORDER by id DESC LIMIT 1";
return msg;
}, 1000);
}
function stop(){
clearInterval(x);
}
var newMsg = msg.payload;
global.set('YourVariable', newMsg);
var header = newMsg.substr(0,3);
var lengths = newMsg.substr(3,2);
var cmd = newMsg.substr(5,2);
if(cmd == "01"){
var imei = newMsg.substr(7,15);
var sim_id = newMsg.substr(22,15);
var ip_1 = converter.hexToDec(newMsg.substr(37,2));
var ip_2 = converter.hexToDec(newMsg.substr(39,2));
var ip_3 = converter.hexToDec(newMsg.substr(41,2));
var ip_4 = converter.hexToDec(newMsg.substr(43,2));
var ip = ip_1.concat(".").concat(ip_2).concat(".").concat(ip_3).concat(".").concat(ip_4);
var port = newMsg.substr(45,5);
var device_timestamp = newMsg.substr(50,14);
var device_date = newMsg.substr(50,8);
var device_time = newMsg.substr(58,6);
var battery = converter.hexToDec(newMsg.substr(64,2));
var rssi = converter.hexToDec(newMsg.substr(66,2));
var rssi_before = converter.hexToDec(newMsg.substr(68,2));
var event_setup = newMsg.substr(70,1);
var byte_check = newMsg.substr(71,2);
node.send([null, {payload:"UHM".concat("15").concat("01").concat("867724030292780").concat("N").concat("N").concat("ff")},null]);
if (lengths == "44"){
msg.topic = "SELECT `casename`, id , imei ,start_time FROM operation_database WHERE imei = 867724030292780 AND casename = ' ' ORDER by id DESC LIMIT 1";
return[msg,null,null];
}
}
if(cmd == "06"){
var imei = newMsg.substr(7,15);
var check1 = newMsg.substr(22,1);
var check2 = newMsg.substr(23,1);
var byte_check = newMsg.substr(24,2);
msg.topic = "INSERT INTO `device_ack` (`header`, `length`, `cmd`, `imei`, `check1`, `check2`, `byte_check`) VALUES ('"+header+"', '"+lengths+"', '"+cmd+"', '"+imei+"', '"+check1+"', '"+check2+"', '"+byte_check+"')";
return[null,null,msg];
}