Backup_Senica/RVO35/flow_old/helper/notification_reporter.js
2025-10-16 02:25:46 +02:00

131 lines
3.1 KiB
JavaScript
Executable file

//key is device, value = str
let sentValues= {};
let notificationsData = null;
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;
}
function sendNotification(func, device, key, params, extra, tb_output, instance, saveKey) {
// return;
let storeToSendValues = true;
if(saveKey == undefined) storeToSendValues = false;
let lang = FLOW.GLOBALS.settings.language;
if(lang != "en" || lang != "sk") lang = "en";
let tpl = key;
let weight = "";
if(notificationsData[key])
{
weight = notificationsData[key].weight;
weight = weight.toLowerCase();
tpl = notificationsData[key][lang];
tpl = template(tpl, 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(sentValues[saveKey] == tpl)
{
return false;
}
}
if(sentValues[saveKey] == undefined)
{
if(storeToSendValues)
{
//do not send - flow is was started
sentValues[saveKey] = tpl;
return false;
}
}
if(saveKey == "rvo_door")
{
//console.log("******", saveKey, sentValues[saveKey], tpl);
}
if(storeToSendValues) sentValues[saveKey] = tpl;
let str = FLOW.GLOBALS.settings.rvo_name;
if(str != "") str = str + ": ";
str = str + tpl;
let content = {
"type": weight,
"status": "new",
"source": {
"func":func,
"component":instance.id,
"component_name":instance.name,
"edge":device
},
"message":str,
"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
}