Add language notifications; Add power door handel

This commit is contained in:
rasta5man 2025-05-31 22:03:23 +02:00
parent d97d90cf95
commit 0876e73c68
12 changed files with 445 additions and 581 deletions

View file

@ -1,126 +1,91 @@
const { MD5 } = require('./md5.js');
const { networkInterfaces } = require('os');
class ErrorToServiceHandler
{
constructor() {
class ErrorToServiceHandler {
constructor() {
this.previousValues = {};
this.projects_id = undefined;
this.project_id = undefined;
const nets = networkInterfaces();
this.ipAddresses = Object.create(null); // Or just '{}', an empty object
this.ipAddresses = {};
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] = [];
}
this.ipAddresses[name].push(net.address);
}
}
}
//console.log(this.ipAddresses);
}
setProjectsId(projects_id)
{
this.projects_id = projects_id;
setProjectId(project_id) {
this.project_id = project_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(', ');
processMessage(message, seconds) {
if (Array.isArray(message)) message = message.join(', ');
let key = MD5(message);
let timestamp = new Date().getTime();
let ts = Date.now();
//keep in memory
if (seconds === undefined) seconds = 60*60;
//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};
if (!this.previousValues.hasOwnProperty(key)) {
this.previousValues[key] = { ts: ts, duration: seconds };
}
let diff = (timestamp - this.previousValues[key].ts);
if(diff < this.previousValues[key].duration*1000) return;
let diff = (ts - this.previousValues[key].ts);
if (diff < this.previousValues[key].duration * 1000) return false;
this.previousValues[key].ts = timestamp;
*/
this.previousValues[key].ts = ts;
//-------------------------
return message;
}
//send to service
let dataToInfoSender = {id: this.projects_id};
sendMessageToService(message, seconds, message_type) {
// if error occures too early FLOW.GLOBALS.settings.project_id is still undefined
if (this.project_id === undefined) {
console.log("ErrorToServiceHandler.js: no project_id");
return;
}
let f = this.processMessage(message, seconds);
if (f === false) return;
if (message_type === undefined) message_type = "error_message";
let toService = {
id: this.project_id,
ipAddresses: this.ipAddresses
};
//js_error || error_message
dataToInfoSender[message_type] = message;
dataToInfoSender.ipAddresses = this.ipAddresses;
toService[message_type] = message;
console.log("ErrorToServiceHandler------------------------>send to service", dataToInfoSender);
//TODO UGLY!!!
// if error occures too early FLOW.GLOBALs.settings.project_id is still undefined
// if(this.projects_id === undefined) this.projects_id = FLOW.GLOBALS.settings.project_id;
if(this.projects_id === undefined) return;
/*
if(this.projects_id === undefined)
{
console.log("this.projects_id is undefined");
return;
}
*/
console.log("ErrorToServiceHandler------------------------>send to service", toService);
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);
});
builder.method('POST');
builder.post(toService);
builder.url('http://192.168.252.2:8004/sentmessage');
builder.callback(function(err, response, output) {
console.log("process.on error send", err, response, output, toService);
});
});
}
}
module.exports = ErrorToServiceHandler;
const errorHandler = new ErrorToServiceHandler();
module.exports = errorHandler;
//module.exports = ErrorToServiceHandler;