ReportOfflineNodeStatus-add setTimeout, Handle broadcast cmd to rsPort

This commit is contained in:
rasta5man 2024-10-02 18:21:31 +02:00
parent 1c131d0b63
commit ed0fe5b15d
4 changed files with 101 additions and 101 deletions

View file

@ -8,7 +8,6 @@ exports.output = ['red', 'blue', 'yellow', 'blue', 'white'];
//blue - send message to relays //blue - send message to relays
exports.input = 2; exports.input = 2;
exports.author = 'Daniel Segeš';
exports.icon = 'cloud-upload'; exports.icon = 'cloud-upload';
//exports.npm = ['serialport' , 'child_process']; //exports.npm = ['serialport' , 'child_process'];
@ -140,7 +139,6 @@ let nodesData = {};//key is node, value data from db
//helper container for counting resolved group of commands (commands related to set profile) //helper container for counting resolved group of commands (commands related to set profile)
let cmdCounter = {};//key is node, value is counter let cmdCounter = {};//key is node, value is counter
let cmdNOKNodeCounter = {};//key is node, value is counter
//END OF VARIABLE SETTINGS //END OF VARIABLE SETTINGS
//-------------------------------- //--------------------------------
@ -275,16 +273,14 @@ function processNodeProfile(node)
return; return;
} }
logger.debug("processNodeProfile: start - set profile for ", node, profile);
let nodeProfile = nodeObj.profile; let nodeProfile = nodeObj.profile;
logger.debug("processNodeProfile: start - set profile for ", node, nodeProfile);
if(nodeProfile) { if(nodeProfile) {
try { try {
nodeProfile = JSON.parse(nodeProfile); nodeProfile = JSON.parse(nodeProfile);
if(Object.keys(nodeProfile).length === 0) throw ("profile is not defined");
} catch (error) { } catch (error) {
logger.debug("Error parsing node profile", error); logger.debug("Cmd_manager - Error parsing node profile", error);
} }
} }
@ -894,14 +890,16 @@ exports.install = function(instance) {
values["current"] = 0;//prúd values["current"] = 0;//prúd
values["status"] = "OFFLINE"; values["status"] = "OFFLINE";
for (let k in nodesData) { // it happens, that some data did not get to tb after sending
// we setTimeout to make more time for db to process telemetry (eg 150 messages at once)
Object.keys(nodesData).forEach((node, index) => {
setTimeout(function() {
//potrebujem nody k danej linii //potrebujem nody k danej linii
if(line == nodesData[k].line || line == undefined) if(line == nodesData[node].line || line == undefined)
{ {
let tbname = nodesData[k].tbname; let tbname = nodesData[node].tbname;
//logger.debug("node:", tbname);
let dataToTb = { let dataToTb = {
[tbname]: [ [tbname]: [
@ -915,7 +913,9 @@ exports.install = function(instance) {
//instance.send(SEND_TO.tb, dataToTb); //instance.send(SEND_TO.tb, dataToTb);
tbHandler.sendToTb(dataToTb, instance); tbHandler.sendToTb(dataToTb, instance);
} }
}
},(index+1) * 300);
})
} }
@ -1784,7 +1784,6 @@ exports.install = function(instance) {
//toto sa reportuje po prijati dat z dido_controlera //toto sa reportuje po prijati dat z dido_controlera
if(disconnected) if(disconnected)
{ {
let values = {"status": "OFFLINE"}; let values = {"status": "OFFLINE"};
logger.debug("disconnected", values); logger.debug("disconnected", values);
@ -1941,7 +1940,7 @@ exports.install = function(instance) {
let resp = com_generic(nodeAddress, params.recipient, params.rw, register, params.name, params.byte1, params.byte2, params.byte3, params.byte4); let resp = com_generic(nodeAddress, params.recipient, params.rw, register, params.name, params.byte1, params.byte2, params.byte3, params.byte4);
let readBytes = 11; let readBytes = 11;
let timeout = 5000; let timeout = 4000;
// await keyword is important, otherwise incorrect data is returned! // await keyword is important, otherwise incorrect data is returned!
await writeData(rsPort, resp, readBytes, timeout).then(function (data) { await writeData(rsPort, resp, readBytes, timeout).then(function (data) {
@ -2170,6 +2169,7 @@ exports.install = function(instance) {
} }
/** /**
* function handles requests from terminal * function handles requests from terminal
* responseType can be "SUCCESS", "ERROR" or "FAILURE", depending on rsPort data. * responseType can be "SUCCESS", "ERROR" or "FAILURE", depending on rsPort data.
@ -2402,7 +2402,6 @@ exports.install = function(instance) {
{ {
let cmd = flowdata.data.cmd; let cmd = flowdata.data.cmd;
if(cmd == "buildTasks") if(cmd == "buildTasks")
{ {
clearInterval(interval); clearInterval(interval);

View file

@ -32,12 +32,6 @@ class DataToTbHandler {
sendToTb(dataToTb, instance) { sendToTb(dataToTb, instance) {
if(!FLOW.OMS_brokerready)
{
instance.send(this.index, dataToTb);
return;
}
let keys = Object.keys(dataToTb); let keys = Object.keys(dataToTb);
if(keys.length == 0) if(keys.length == 0)

View file

@ -23,9 +23,9 @@ function openPort(port){
port.open(); port.open();
}) })
} }
function runSyncExec(command){ function runSyncExec(command){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => { exec(command, (error, stdout, stderr) => {
@ -34,14 +34,26 @@ function openPort(port){
}); });
}) })
} }
async function writeData(port, data, readbytes, timeout){ async function writeData(port, data, readbytes, timeout){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
//readbytes = 0 = broadcast // If first item in data array is 255, we just write broadcast command to rsPort
if(readbytes == undefined) readbytes = 0; // We wait 3 seconds and resolve([ "b", "r", "o", "a", "d", "c", "a", "s", "t" ])
if(timeout == undefined) timeout = 10000;//10s, default timeout MASTERA je 3s // It is important to resolve with array
if(data[0] == 255) {
port.write(Buffer.from(data), function(err) {
if (err) {
port.removeListener('data', callback);
reject(err.message);
}
});
setTimeout(resolve, 3000, [ "b", "r", "o", "a", "d", "c", "a", "s", "t" ]);
return;
}
//cmd-manager mame http route POST / terminal a tomu sa tiez nastavuje timeout!!! //cmd-manager mame http route POST / terminal a tomu sa tiez nastavuje timeout!!!
@ -70,7 +82,7 @@ function openPort(port){
let rsPortReceivedData = []; let rsPortReceivedData = [];
if(readbytes > 0) port.on('data', callback); port.on('data', callback);
port.write(Buffer.from(data), function(err) { port.write(Buffer.from(data), function(err) {
if (err) { if (err) {
@ -78,11 +90,6 @@ function openPort(port){
reject(err.message); reject(err.message);
} }
if(readbytes == 0)
{
resolve(rsPortReceivedData);
}
}); });
}) })
} }