137 lines
No EOL
3.3 KiB
JavaScript
137 lines
No EOL
3.3 KiB
JavaScript
|
|
const { promisifyBuilder, makeMapFromDbResult } = require('./db_helper.js');
|
|
const dbNotifications = TABLE("notifications");
|
|
|
|
//key is device, value = str
|
|
let sentValues= {};
|
|
let notificationsData = {};
|
|
|
|
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]);
|
|
|
|
async function initNotifications()
|
|
{
|
|
let response = await promisifyBuilder(dbNotifications.find());
|
|
notificationsData = makeMapFromDbResult(response, "key");
|
|
|
|
console.log("initNotifications done" );
|
|
}
|
|
|
|
function sendNotification(func, device, key, params, extra, tb_output, instance, saveKey) {
|
|
|
|
// return;
|
|
|
|
let storeToSendValues = true;
|
|
if(saveKey == undefined) storeToSendValues = false;
|
|
|
|
let lang = FLOW.OMS_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);
|
|
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.OMS_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,
|
|
initNotifications,
|
|
ERRWEIGHT
|
|
} |