Actual flowserver on Senica rvo

This commit is contained in:
rasta5man 2024-04-13 20:29:31 +02:00
parent 67c503d980
commit 86619fbcff
29 changed files with 5833 additions and 9853 deletions

View file

@ -1,124 +1,124 @@
const { MD5 } = require('./md5.js');
const { networkInterfaces } = require('os');
class ErrorToServiceHandler
{
constructor() {
this.previousValues = {};
this.projects_id = undefined;
this.message_type = "error_message";
const nets = networkInterfaces();
this.ipAddresses = Object.create(null); // Or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === 'IPv4' && !net.internal) {
if (!this.ipAddresses[name]) {
this.ipAddresses[name] = [];
}
this.ipAddresses[name].push(net.address);
}
}
}
//console.log(this.ipAddresses);
}
setProjectsId(projects_id)
{
this.projects_id = projects_id;
}
processMessage(message, seconds, message_type)
{
if(Array.isArray(message)) message = message.join(', ');
//keep in memory - default value is 1h
if (seconds === undefined) seconds = 60*60;
if(message_type) this.message_type = message_type;
let key = MD5(message);
let timestamp = new Date().getTime();
if(!this.previousValues.hasOwnProperty(key))
{
this.previousValues[key] = {ts: timestamp, duration: seconds};
}
let diff = (timestamp - this.previousValues[key].ts);
if(diff < this.previousValues[key].duration*1000) return false;
this.previousValues[key].ts = timestamp;
return true;
}
sendMessageToService(message, seconds, message_type)
{
let f = this.processMessage(message, seconds, message_type);
if(!f) return;
/*
//-------------
if(message_type == undefined) message_type = "error_message";
if(Array.isArray(message)) message = message.join(', ');
let key = MD5(message);
let timestamp = new Date().getTime();
//keep in memory
if (seconds === undefined) seconds = 60*60;
if(!this.previousValues.hasOwnProperty(key))
{
this.previousValues[key] = {ts: timestamp, duration: seconds};
}
let diff = (timestamp - this.previousValues[key].ts);
if(diff < this.previousValues[key].duration*1000) return;
this.previousValues[key].ts = timestamp;
*/
//-------------------------
//send to service
let dataToInfoSender = {id: this.projects_id};
//js_error || error_message
dataToInfoSender[this.message_type] = message;
dataToInfoSender.ipAddresses = this.ipAddresses;
console.log("ErrorToServiceHandler------------------------>send to service", dataToInfoSender);
//TODO UGLY!!!
if(this.projects_id === undefined) this.projects_id = FLOW.OMS_projects_id;
/*
if(this.projects_id === undefined)
{
console.log("this.projects_id is undefined");
return;
}
*/
RESTBuilder.make(function(builder) {
builder.method('POST');
builder.post(dataToInfoSender);
builder.url('http://192.168.252.2:8004/sentmessage');
builder.callback(function(err, response, output) {
console.log("process.on error send", err, response, output, dataToInfoSender);
});
});
}
}
const { MD5 } = require('./md5.js');
const { networkInterfaces } = require('os');
class ErrorToServiceHandler
{
constructor() {
this.previousValues = {};
this.projects_id = undefined;
const nets = networkInterfaces();
this.ipAddresses = Object.create(null); // Or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === 'IPv4' && !net.internal) {
if (!this.ipAddresses[name]) {
this.ipAddresses[name] = [];
}
this.ipAddresses[name].push(net.address);
}
}
}
//console.log(this.ipAddresses);
}
setProjectsId(projects_id)
{
this.projects_id = projects_id;
}
processMessage(message, seconds, message_type)
{
if(message_type == undefined) message_type = "error_message";
if(Array.isArray(message)) message = message.join(', ');
let key = MD5(message);
let timestamp = new Date().getTime();
//keep in memory - default value is 1h
if (seconds === undefined) seconds = 60*60;
if(!this.previousValues.hasOwnProperty(key))
{
this.previousValues[key] = {ts: timestamp, duration: seconds};
}
let diff = (timestamp - this.previousValues[key].ts);
if(diff < this.previousValues[key].duration*1000) return false;
this.previousValues[key].ts = timestamp;
return true;
}
sendMessageToService(message, seconds, message_type)
{
let f = this.processMessage(message, seconds, message_type);
if(!f) return;
/*
//-------------
if(message_type == undefined) message_type = "error_message";
if(Array.isArray(message)) message = message.join(', ');
let key = MD5(message);
let timestamp = new Date().getTime();
//keep in memory
if (seconds === undefined) seconds = 60*60;
if(!this.previousValues.hasOwnProperty(key))
{
this.previousValues[key] = {ts: timestamp, duration: seconds};
}
let diff = (timestamp - this.previousValues[key].ts);
if(diff < this.previousValues[key].duration*1000) return;
this.previousValues[key].ts = timestamp;
*/
//-------------------------
//send to service
let dataToInfoSender = {id: this.projects_id};
//js_error || error_message
dataToInfoSender[message_type] = message;
dataToInfoSender.ipAddresses = this.ipAddresses;
console.log("ErrorToServiceHandler------------------------>send to service", dataToInfoSender);
//TODO UGLY!!!
if(this.projects_id === undefined) this.projects_id = FLOW.OMS_projects_id;
/*
if(this.projects_id === undefined)
{
console.log("this.projects_id is undefined");
return;
}
*/
RESTBuilder.make(function(builder) {
builder.method('POST');
builder.post(dataToInfoSender);
builder.url('http://192.168.252.2:8004/sentmessage');
builder.callback(function(err, response, output) {
console.log("process.on error send", err, response, output, dataToInfoSender);
});
});
}
}
module.exports = ErrorToServiceHandler;

View file

@ -1,135 +1,137 @@
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) {
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
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
}