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;

View file

@ -1,6 +1,9 @@
//key is device, value = str
let sentValues= {};
//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
@ -23,82 +26,67 @@ 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) {
// return;
let storeToSendValues = true;
if(saveKey == undefined) storeToSendValues = false;
if (saveKey == undefined) storeToSendValues = false;
let lang = FLOW.GLOBALS.settings.language;
if(lang != "en" || lang != "sk") lang = "en";
let tpl = key;
let weight = "";
let message = {};
if(notificationsData[key])
{
weight = notificationsData[key].weight;
weight = weight.toLowerCase();
let notification = notificationsData[key];
tpl = notificationsData[key][lang];
tpl = template(tpl, params);
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
{
else {
//console.error("sendNotification: Notifications: undefined key", key, func, notificationsData);
console.error("sendNotification: Notifications: undefined key", key, func );
console.error("sendNotification: Notifications: undefined key", key, func);
return false;
}
//detect invalid err weight
if(getKey(ERRWEIGHT, weight) == undefined)
{
if (getKey(ERRWEIGHT, weight) == undefined) {
console.error("sendNotification: Notifications: undefined weight", weight, key, func);
return false;
}
if(sentValues.hasOwnProperty(saveKey))
{
if(sentValues[saveKey] == tpl)
{
if (sentValues.hasOwnProperty(saveKey)) {
if (JSON.stringify(sentValues[saveKey]) == JSON.stringify(message)) {
return false;
}
}
if(sentValues[saveKey] == undefined)
{
if(storeToSendValues)
{
if (sentValues[saveKey] == undefined) {
if (storeToSendValues) {
//do not send - flow is was started
sentValues[saveKey] = tpl;
sentValues[saveKey] = message;
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;
if (storeToSendValues) sentValues[saveKey] = message;
let content = {
"type": weight,
"status": "new",
"source": {
"func":func,
"component":instance.id,
"component_name":instance.name,
"edge":device
"func": func,
"component": instance.id,
"component_name": instance.name,
"edge": device
},
"message":str,
"message": message,
"message_data": extra
};
@ -107,7 +95,7 @@ function sendNotification(func, device, key, params, extra, tb_output, instance,
{
"ts": Date.now(),
"values": {
"_event":content
"_event": content
}
}
];
@ -118,6 +106,7 @@ function sendNotification(func, device, key, params, extra, tb_output, instance,
} 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;
@ -129,3 +118,4 @@ module.exports = {
ERRWEIGHT,
initNotification
}

View file

@ -1,6 +1,6 @@
const { exec } = require('child_process');
function openPort(port){
function openPort(port) {
return new Promise((resolve, reject) => {
var callbackError = function(err) {
@ -25,24 +25,24 @@ function openPort(port){
})
}
function runSyncExec(command){
function runSyncExec(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if(error == null) resolve(stdout);
if (error == null) resolve(stdout);
reject(error);
});
})
}
async function writeData(port, data, readbytes, timeout){
async function writeData(port, data, readbytes, timeout) {
return new Promise((resolve, reject) => {
// If first item in data array is 255, we just write broadcast command to rsPort
// We wait 3 seconds and resolve(["broadcast"])
// It is important to resolve with array
if(data[0] == 255) {
if (data[0] == 255) {
port.write(Buffer.from(data), function(err) {
if (err) {
@ -55,13 +55,12 @@ async function writeData(port, data, readbytes, timeout){
}
//cmd-manager mame http route POST / terminal a tomu sa tiez nastavuje timeout!!!
var callback = function(data) {
rsPortReceivedData.push(...data);
let l = rsPortReceivedData.length;
if(l >= readbytes)
{
if (l >= readbytes) {
port.removeListener('data', callback);
clearTimeout(t);