From 06d367a0dddd33f742450731b5195d79775c7eb3 Mon Sep 17 00:00:00 2001 From: druciak Date: Sat, 21 Nov 2020 07:35:04 +0100 Subject: [PATCH] [satel] Fixes for INT-RS module (#9059) Signed-off-by: Krzysztof Goworek Co-authored-by: Krzysztof Goworek --- .../satel/internal/protocol/IntRSModule.java | 29 +++++++++++++++---- .../satel/internal/protocol/SatelModule.java | 9 ++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/IntRSModule.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/IntRSModule.java index f4fdefe9367..0a72c99bdf3 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/IntRSModule.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/IntRSModule.java @@ -69,8 +69,19 @@ public class IntRSModule extends SatelModule { throw new ConnectionFailureException(String.format("Port %s does not exist", this.port)); } SerialPort serialPort = portIdentifier.open("org.openhab.binding.satel", 2000); + boolean supportsReceiveTimeout = false; serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); - serialPort.enableReceiveTimeout(this.getTimeout()); + try { + serialPort.enableReceiveTimeout(this.getTimeout()); + supportsReceiveTimeout = true; + } catch (UnsupportedCommOperationException e) { + logger.debug("Receive timeout is unsupported for port {}", this.port); + } + try { + serialPort.enableReceiveThreshold(1); + } catch (UnsupportedCommOperationException e) { + logger.debug("Receive threshold is unsupported for port {}", this.port); + } // RXTX serial port library causes high CPU load // Start event listener, which will just sleep and slow down event // loop @@ -84,10 +95,10 @@ public class IntRSModule extends SatelModule { } } }); - serialPort.notifyOnDataAvailable(true); + serialPort.notifyOnDataAvailable(false); logger.info("INT-RS module connected successfuly"); - return new SerialCommunicationChannel(serialPort); + return new SerialCommunicationChannel(serialPort, supportsReceiveTimeout); } catch (PortInUseException e) { throw new ConnectionFailureException(String.format("Port %s in use", this.port), e); } catch (UnsupportedCommOperationException e) { @@ -99,10 +110,13 @@ public class IntRSModule extends SatelModule { private class SerialCommunicationChannel implements CommunicationChannel { - private SerialPort serialPort; + private final SerialPort serialPort; - public SerialCommunicationChannel(SerialPort serialPort) { + private final boolean supportsReceiveTimeout; + + public SerialCommunicationChannel(SerialPort serialPort, boolean supportsReceiveTimeout) { this.serialPort = serialPort; + this.supportsReceiveTimeout = supportsReceiveTimeout; } @Override @@ -133,5 +147,10 @@ public class IntRSModule extends SatelModule { logger.error("An error occurred during closing serial port", e); } } + + @Override + public boolean supportsReceiveTimeout() { + return supportsReceiveTimeout; + } } } diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/SatelModule.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/SatelModule.java index 224b8adc358..c35ec1b5b36 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/SatelModule.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/SatelModule.java @@ -73,6 +73,10 @@ public abstract class SatelModule extends EventDispatcher implements SatelEventL OutputStream getOutputStream() throws IOException; void disconnect(); + + default boolean supportsReceiveTimeout() { + return false; + } } /* @@ -529,9 +533,10 @@ public abstract class SatelModule extends EventDispatcher implements SatelEventL logger.trace("Checking communication thread: {}, {}", thread != null, Boolean.toString(thread != null && thread.isAlive())); if (thread != null && thread.isAlive()) { - long timePassed = (this.lastActivity == 0) ? 0 : System.currentTimeMillis() - this.lastActivity; + final long timePassed = (this.lastActivity == 0) ? 0 : System.currentTimeMillis() - this.lastActivity; + final CommunicationChannel channel = SatelModule.this.channel; - if (timePassed > SatelModule.this.timeout) { + if (channel != null && !channel.supportsReceiveTimeout() && timePassed > SatelModule.this.timeout) { logger.error("Send/receive timeout, disconnecting module."); stop(); thread.interrupt();