Repeat node request 3x; fix rsPort and ws connection after close
This commit is contained in:
parent
0876e73c68
commit
0c993f50b1
6 changed files with 144 additions and 98 deletions
|
|
@ -124,8 +124,8 @@ exports.install = function(instance) {
|
|||
};
|
||||
*/
|
||||
|
||||
//status for calculating Statecodes
|
||||
let deviceStatus = { //key is device name: temperature,....
|
||||
//status for calculating Statecodes-we make it global to see it from outside
|
||||
FLOW.deviceStatus = { //key is device name: temperature,....
|
||||
"state_of_main_switch": "Off", //Hlavny istic (alebo druhy dverovy kontakt)
|
||||
"rotary_switch_state": "Off", //Prevadzkovy
|
||||
"door_condition": "closed", //Dverový kontakt
|
||||
|
|
@ -139,6 +139,7 @@ exports.install = function(instance) {
|
|||
"state_of_contactor": {}, //"Off",//Stykac
|
||||
"twilight_sensor": "OK" //lux sensor
|
||||
};
|
||||
let deviceStatus = FLOW.deviceStatus;
|
||||
|
||||
|
||||
function main() {
|
||||
|
|
@ -211,18 +212,17 @@ exports.install = function(instance) {
|
|||
|
||||
|
||||
function handleRsPort() {
|
||||
|
||||
if (rsPort) {
|
||||
rsPort.removeAllListeners();
|
||||
rsPort = null;
|
||||
}
|
||||
|
||||
//TODO build according to pins!!!
|
||||
//! rsPort to open are the same for lm and unipi and electromer ("/dev/ttymxc0")
|
||||
const setRSPortData = [0xAA, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 1, 1, 1, 1, 0, 0, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 15, 15, 15, 15, 15, 15, 0, 15, 15, 15, 0, 0, 0, 0, 0, 0, 30, 0, 0, 0];
|
||||
rsPort = new SerialPort("/dev/ttymxc0", { autoOpen: false });
|
||||
|
||||
rsPort.on('error', function(err) {
|
||||
logger.debug("rsPort opened error - failed", err.message);
|
||||
instance.send(SEND_TO.debug, err.message);
|
||||
|
||||
errorHandler.sendMessageToService(exports.title + " rsPort opened error - failed: " + err.message);
|
||||
})
|
||||
|
||||
rsPort.on('open', async function() {
|
||||
|
||||
await runSyncExec("stty -F /dev/ttymxc0 115200 min 1 time 5 ignbrk -brkint -icrnl -imaxbel -opost -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke").then(function(status) {
|
||||
|
|
@ -283,8 +283,18 @@ exports.install = function(instance) {
|
|||
|
||||
});
|
||||
|
||||
rsPort.on('error', err => {
|
||||
let message = "Dido: rsPort error: " + err.message;
|
||||
logger.debug(message);
|
||||
monitor.info(message);
|
||||
errorHandler.sendMessageToService(message);
|
||||
})
|
||||
|
||||
rsPort.on("close", () => {
|
||||
rsPort.close();
|
||||
let message = "Dido: rsPort closed - reconnecting ...";
|
||||
logger.debug(message);
|
||||
monitor.info(message);
|
||||
setTimeout(handleRsPort, 1000);
|
||||
})
|
||||
|
||||
rsPort.open();
|
||||
|
|
@ -293,6 +303,11 @@ exports.install = function(instance) {
|
|||
|
||||
function handleWebSocket() {
|
||||
|
||||
if (ws) {
|
||||
ws.removeAllListeners();
|
||||
ws = null;
|
||||
}
|
||||
|
||||
//to keep websocket opened, we send request every 150 seconds
|
||||
let startRequests = null;
|
||||
|
||||
|
|
@ -356,23 +371,13 @@ exports.install = function(instance) {
|
|||
})
|
||||
}
|
||||
|
||||
|
||||
ws.on('error', (err) => {
|
||||
monitor.info('websocket error, reconnect')
|
||||
instance.send(SEND_TO.debug, err.message);
|
||||
clearInterval(startRequests);
|
||||
ws = null;
|
||||
setTimeout(handleWebSocket, 1000);
|
||||
ws.on('error', err => {
|
||||
logger.debug('Dido: websocket error', err);
|
||||
})
|
||||
|
||||
|
||||
ws.onclose = function() {
|
||||
// connection closed, discard old websocket and create a new one in 5s
|
||||
// stopRequests();
|
||||
monitor.info('websocket onclose, reconnect')
|
||||
logger.debug('Dido: websocket onclose, reconnecting...')
|
||||
clearInterval(startRequests);
|
||||
ws = null;
|
||||
console.log("ws is null now, reconnecting...");
|
||||
setTimeout(handleWebSocket, 1000);
|
||||
}
|
||||
}
|
||||
|
|
@ -382,7 +387,6 @@ exports.install = function(instance) {
|
|||
if (ws) ws.close();
|
||||
})
|
||||
|
||||
|
||||
function getPin(line) {
|
||||
//conversionTable
|
||||
let keys = Object.keys(pinsData);
|
||||
|
|
@ -483,14 +487,14 @@ exports.install = function(instance) {
|
|||
if (!force) {
|
||||
if (relaysData[line].contactor == value) {
|
||||
instance.send(SEND_TO.debug, `line is already ${onOrOff} ` + line);
|
||||
logger.debug(`turnLine: line is already ${onOrOff} `, line);
|
||||
logger.debug(`Dido: turnLine: line is already ${onOrOff} `, line);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// if(!rsPort.isOpen && !ws)
|
||||
if (!rsPort && !ws) {
|
||||
errLogger.error("dido controller - port or websocket is not opened");
|
||||
errLogger.error("Dido - port or websocket is not opened");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -502,18 +506,18 @@ exports.install = function(instance) {
|
|||
|
||||
rsPort.write(Buffer.from(arr), function(err) {
|
||||
if (err === undefined) {
|
||||
monitor.info(`turnLine ${onOrOff} zapisal do rsPort-u`, line, pin, arr, info);
|
||||
monitor.info(`Dido: turnLine ${onOrOff} zapisal do rsPort-u`, line, pin, arr, info);
|
||||
switchLogic(arr);
|
||||
}
|
||||
else {
|
||||
monitor.info(`turnLine ${onOrOff} WRITE error`, err);
|
||||
monitor.info(`Dido: turnLine ${onOrOff} WRITE error`, err);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
else if (ws) {
|
||||
//pin = "relay1_03" or "input1_01" ... we must make just "1_01" with slice method
|
||||
monitor.info(`turnLine ${onOrOff} - (line, pin, force)`, line, pin, force, info);
|
||||
monitor.info(`Dido: turnLine ${onOrOff} - (line, pin, force)`, line, pin, force, info);
|
||||
let cmd = { "cmd": "set", "dev": "relay", "circuit": pin.slice(5), "value": value };
|
||||
ws.send(JSON.stringify(cmd));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue