Limit, what we send

This commit is contained in:
rasta5man 2025-01-01 13:12:02 +01:00
parent 41d1ec28dd
commit ef7817bf22
5 changed files with 122 additions and 74 deletions

View file

@ -3,7 +3,6 @@ exports.title = 'CMD Manager';
exports.group = 'Worksys';
exports.color = '#5D9CEC';
exports.version = '0.0.3';
//blue - send message to relays
exports.output = ['red', 'blue', 'yellow', 'blue', 'white'];
exports.input = 3;
exports.icon = 'cloud-upload';
@ -141,6 +140,9 @@ exports.install = function(instance) {
//helper container for counting resolved group of commands (commands related to set profile)
let cmdCounter = {};//key is node, value is counter
//if sending of profile to node fails, we send notification and push node into set, so we do not send notification twice
const nodeProfileSendFail = new Set();
//END OF VARIABLE SETTINGS
//--------------------------------
@ -1403,7 +1405,7 @@ exports.install = function(instance) {
if ((currentTimestamp - reportDuskDawn.dusk_time) < 60 * 1000) {
//reportovali sme?
if (reportDuskDawn.dusk_time_reported != sunCalcResult.dusk_time) {
sendNotification("CMD Manager: calculated Time of dusk", SETTINGS.rvoTbName, "dusk_has_occured", { value: sunCalcResult["dusk"] }, "", SEND_TO.tb, instance);
//sendNotification("CMD Manager: calculated Time of dusk", SETTINGS.rvoTbName, "dusk_has_occured", { value: sunCalcResult["dusk"] }, "", SEND_TO.tb, instance);
reportDuskDawn.dusk_time_reported = sunCalcResult.dusk_time;
}
}
@ -1420,7 +1422,7 @@ exports.install = function(instance) {
if ((currentTimestamp - reportDuskDawn.dawn_time) < 60 * 1000) {
//reportovali sme?
if (reportDuskDawn.dawn_time_reported != sunCalcResult.dawn_time) {
sendNotification("CMD Manager: calculated Time of dawn", SETTINGS.rvoTbName, "dawn_has_occured", { value: sunCalcResult["dawn"] }, "", SEND_TO.tb, instance);
//sendNotification("CMD Manager: calculated Time of dawn", SETTINGS.rvoTbName, "dawn_has_occured", { value: sunCalcResult["dawn"] }, "", SEND_TO.tb, instance);
reportDuskDawn.dawn_time_reported = sunCalcResult.dawn_time;
}
}
@ -1471,12 +1473,12 @@ exports.install = function(instance) {
let type = params.type;
let tbname = params.tbname;
let nodeAddress = params.address;
let node = params.address;
let line = null;
//rpc related
if (nodesData[nodeAddress] !== undefined) line = nodesData[nodeAddress].line;
if (nodesData[node] !== undefined) line = nodesData[node].line;
if (params.line !== undefined) line = params.line;
let repeatTask = false;
@ -1510,6 +1512,8 @@ exports.install = function(instance) {
const value = params.value;
let date = new Date();
let hour = date.getHours();
date.setDate(date.getDate() + 1);//next day
let sunCalcResult;
@ -1532,12 +1536,11 @@ exports.install = function(instance) {
}
let info = "aplikovany bod profilu";
let message = "";
value == 1 ? message = "on" : message = "off";
let onOrOff = "";
value == 1 ? onOrOff = "on" : onOrOff = "off";
turnLine(message, params.line, info);
sendNotification("CMD Manager: process cmd", SETTINGS.rvoTbName, "switching_profile_point_applied_to_line", { line: params.line, value: message }, "", SEND_TO.tb, instance);
turnLine(onOrOff, params.line, info);
interval = setInterval(runTasks, SHORT_INTERVAL);
return;
}
@ -1590,25 +1593,26 @@ exports.install = function(instance) {
}
}
let relayStatus = 1;
if (relaysData[line] != undefined) {
relayStatus = relaysData[line].contactor;
}
//TODO -> status offline for rvo if rotary_switch_state is OFF
//let relayStatus = 1;
//if (relaysData[line] != undefined) {
// relayStatus = relaysData[line].contactor;
//}
if (line == 0) relayStatus = 0;
if (type == "cmd-terminal") relayStatus = 1;
//if (line == 0) relayStatus = 0;
//if (type == "cmd-terminal") relayStatus = 1;
//check if rotary_switch_state == "Off"
if (relayStatus == 0) {
//if (relayStatus == 0) {
//console.log("------------------------------------relayStatus", relayStatus, line);
let values = { "status": "OFFLINE" };
//let values = { "status": "OFFLINE" };
sendTelemetry(values, tbname)
//if(tbname) sendTelemetry(values, tbname)
interval = setInterval(runTasks, SHORT_INTERVAL);
return;
}
//interval = setInterval(runTasks, SHORT_INTERVAL);
//return;
//}
if (!rsPort.isOpen) {
interval = setInterval(runTasks, LONG_INTERVAL);
@ -1667,7 +1671,7 @@ exports.install = function(instance) {
}
//-----------------------
instance.send(SEND_TO.debug, "address: " + nodeAddress + " register:" + register + "type: " + type);
instance.send(SEND_TO.debug, "address: " + node + " register:" + register + "type: " + type);
var startTime, endTime;
startTime = new Date();
@ -1676,7 +1680,7 @@ exports.install = function(instance) {
if (!tbname) saveToTb = false;
let itIsNodeCommand = listOfCommands.includes(register); //reading data from node (voltage, current, dimming, status)
let resp = com_generic(nodeAddress, params.recipient, params.rw, register, params.name, params.byte1, params.byte2, params.byte3, params.byte4);
let resp = com_generic(node, params.recipient, params.rw, register, params.name, params.byte1, params.byte2, params.byte3, params.byte4);
let readBytes = 11;
let timeout = 4000;
@ -1717,19 +1721,20 @@ exports.install = function(instance) {
//CMD FINISHED
if (message == "OK") {
updateNodeStatus(nodeAddress, true);
updateNodeStatus(node, true);
//write
if (type == "set_node_profile") {
let result = cmdCounterResolve(nodeAddress);
let result = cmdCounterResolve(node);
if (result == 0) {
dbNodes.modify({ processed: true }).where("node", nodeAddress).make(function(builder) {
dbNodes.modify({ processed: true }).where("node", node).make(function(builder) {
builder.callback(function(err, response) {
sendNotification("CMD Manager: process cmd", SETTINGS.rvoTbName, "dimming_profile_was_successfully_received_by_node", { node: nodeAddress }, "", SEND_TO.tb, instance);
sendNotification("CMD Manager: process cmd", SETTINGS.rvoTbName, "dimming_profile_was_successfully_received_by_node", { node: node }, "", SEND_TO.tb, instance);
logger.debug("--> profil úspešne odoslaný na node č. " + nodeAddress);
nodesData[nodeAddress].processed = true;
logger.debug("--> profil úspešne odoslaný na node č. " + node);
nodesData[node].processed = true;
nodeProfileSendFail.delete(node);
});
});
}
@ -1746,7 +1751,7 @@ exports.install = function(instance) {
}
//master node
if (nodeAddress == 0) {
if (node == 0) {
sendNotification("CMD Manager: process cmd", SETTINGS.rvoTbName, "master_node_is_responding_again", {}, "", SEND_TO.tb, instance, "rvo_status");
SETTINGS.masterNodeIsResponding = true;
if (register == 4) values["edge_fw_version"] = SETTINGS.edge_fw_version;
@ -1779,7 +1784,7 @@ exports.install = function(instance) {
if (params.hasOwnProperty("debug")) {
if (params.debug) {
//logger.debug("writeData err: ", error, result, params);
logger.debug("writeData err: ", tbname, nodeAddress, register, values);
logger.debug("writeData err: ", tbname, node, register, values);
}
}
@ -1803,11 +1808,9 @@ exports.install = function(instance) {
}
else {
// if(currentTask.debug)
// {
// //currentTask.timestamp <= currentTimestamp
// logger.debug("currentTask is not processed - task is in the future", currentTask);
// }
if(currentTask.debug) {
// currentTask.timestamp <= currentTimestamp && logger.debug("currentTask is not processed - task is in the future", currentTask);
}
interval = setInterval(runTasks, LONG_INTERVAL);
return;
@ -1843,7 +1846,11 @@ exports.install = function(instance) {
if (type == "set_node_profile") {
delete cmdCounter[node];
logger.debug("profil nebol úspešne odoslaný na node č. ", params);
sendNotification("CMD Manager: process cmd", tbName, "configuration_of_dimming_profile_to_node_failed", { node: node }, "", SEND_TO.tb, instance);
if(!nodeProfileSendFail.has(node)) {
sendNotification("CMD Manager: process cmd", tbName, "configuration_of_dimming_profile_to_node_failed", { node: node }, "", SEND_TO.tb, instance);
nodeProfileSendFail.add(node);
}
}
if (itIsNodeCommand) {
@ -2870,7 +2877,7 @@ exports.install = function(instance) {
//if(byte1 < 10) s = "0" + byte1;
var d = new Date();
d.setHours(h, m, 0);
d.setHours(h, m, 0, 0);
timestamp = d.getTime();
}