Repeat node request 3x; fix rsPort and ws connection after close

This commit is contained in:
rasta5man 2025-06-12 11:34:03 +02:00
parent 0876e73c68
commit 0c993f50b1
6 changed files with 144 additions and 98 deletions

View file

@ -76,6 +76,11 @@ exports.install = function(instance) {
let obj = this;
if (this.socket) {
this.socket.removeAllListeners();
this.socket = null;
}
this.socket = new SerialPort("/dev/ttymxc0", {
baudRate: 9600,
})
@ -86,15 +91,11 @@ exports.install = function(instance) {
}
this.socket.on('error', function(e) {
console.log('socket connection error', e);
if (e.code == 'ECONNREFUSED' || e.code == 'ECONNRESET') {
console.log(exports.title + ' Waiting 10 seconds before trying to connect again');
setTimeout(obj.startSocket, 10000);
}
console.log('Modbus_reader: Socket connection error', e); //'ECONNREFUSED' or 'ECONNRESET' ??
});
this.socket.on('close', function() {
console.log('Socket connection closed ' + exports.title + ' Waiting 10 seconds before trying to connect again');
console.log('Modbus_reader: Socket connection closed - Waiting 10 seconds before connecting again');
setTimeout(obj.startSocket, 10000);
});
@ -115,7 +116,8 @@ exports.install = function(instance) {
this.deviceAddress = dev.deviceAddress; // 1 or 2 or any number
this.device = dev.device; //em340, twilight_sensor
if (this.indexInDeviceConfig == 0) setTimeout(this.readRegisters, this.timeoutInterval);
//if we just start to loop devices from the beginning, or there is just 1 device in config, we wait whole timeoutInterval
if (this.indexInDeviceConfig == 0 || deviceConfig.length === 1) setTimeout(this.readRegisters, this.timeoutInterval);
else setTimeout(this.readRegisters, DELAY_BETWEEN_DEVICES);
}
@ -330,7 +332,8 @@ exports.install = function(instance) {
phases = FLOW.GLOBALS.settings.phases;
tbName = FLOW.GLOBALS.settings.rvoTbName;
noVoltage = FLOW.GLOBALS.settings.no_voltage;
mainSocket = new SocketWithClients();
if (deviceConfig.length) mainSocket = new SocketWithClients();
else console.log("Modbus_reader: no modbus device in configuration");
// this notification is to show, that flow (unipi) has been restarted
sendNotification("modbus_reader", tbName, "flow_restart", {}, "", SEND_TO.slack, instance);