Backup_Senica/RVO23/flow/helper/notification_reporter.js
2025-10-16 02:25:01 +02:00

121 lines
3.2 KiB
JavaScript
Executable file

//key is device, value = message {}
let sentValues = {};
let notificationsData = null;
let rvoName;
//sendNotification("CMD Manager: process cmd", SETTINGS.rvoTbName, "dimming_profile_was_successfully_received_by_node", { node: node }, "", SEND_TO.tb, instance);
let ERRWEIGHT = {
EMERGENCY: "emergency", // System unusable
ALERT: "alert", // Action must be taken immidiately
CRITICAL: "critical", // Component unable to function
ERROR: "error", // Error, but component able to recover from it
WARNING: "warning", // Possibility of error, system running futher
NOTICE: "notice", // Significant message but not an error, things user might want to know about
INFO: "informational", // Info
DEBUG: "debug" // Debug - only if CONFIG.debug is enabled
};
function getKey(map, val) {
return Object.keys(map).findItem(key => map[key] === val);
}
//https://stackoverflow.com/questions/41117799/string-interpolation-on-variable
var template = (tpl, args) => tpl.replace(/\${(\w+)}/g, (_, v) => args[v]);
function initNotification() {
notificationsData = FLOW.GLOBALS.notificationsData;
rvoName = FLOW.GLOBALS.settings.rvo_name;
}
function sendNotification(func, device, key, params, extra, tb_output, instance, saveKey) {
let storeToSendValues = true;
if (saveKey == undefined) storeToSendValues = false;
let weight = "";
let message = {};
let notification = notificationsData[key];
if (notification) {
weight = notification.weight.toLowerCase();
Object.keys(notification).forEach(item => {
if (["en", "sk", "de", "cz", "it", "es"].includes(item)) {
message[item] = rvoName + ": " + template(notification[item], params);
}
})
}
else {
//console.error("sendNotification: Notifications: undefined key", key, func, notificationsData);
console.error("sendNotification: Notifications: undefined key", key, func);
return false;
}
//detect invalid err weight
if (getKey(ERRWEIGHT, weight) == undefined) {
console.error("sendNotification: Notifications: undefined weight", weight, key, func);
return false;
}
if (sentValues.hasOwnProperty(saveKey)) {
if (JSON.stringify(sentValues[saveKey]) == JSON.stringify(message)) {
return false;
}
}
if (sentValues[saveKey] == undefined) {
if (storeToSendValues) {
//do not send - flow is was started
sentValues[saveKey] = message;
return false;
}
}
if (storeToSendValues) sentValues[saveKey] = message;
let content = {
"type": weight,
"status": "new",
"source": {
"func": func,
"component": instance.id,
"component_name": instance.name,
"edge": device
},
"message": message,
"message_data": extra
};
let msg = {};
msg[device] = [
{
"ts": Date.now(),
"values": {
"_event": content
}
}
];
// Msg can be outputted from components only after configuration
/*if (canSendErrData()){
sendBufferedErrors();
} else {
bufferError(msg);
}*/
instance.send(tb_output, msg); // Even if error server is unavailable, send this message to output, for other possible component connections
return true;
}
module.exports = {
sendNotification,
ERRWEIGHT,
initNotification
}