]> git.basschouten.com Git - openhab-addons.git/commitdiff
[satel] Fixes for INT-RS module (#9072)
authordruciak <druciak@users.noreply.github.com>
Sat, 21 Nov 2020 06:33:54 +0000 (07:33 +0100)
committerGitHub <noreply@github.com>
Sat, 21 Nov 2020 06:33:54 +0000 (22:33 -0800)
Signed-off-by: Krzysztof Goworek <krzysztof.goworek@gmail.com>
Co-authored-by: Krzysztof Goworek <krzysztof.goworek@gmail.com>
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/IntRSModule.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/SatelModule.java

index 88a81f8bb2963a4d07f5e08fda71e79c7700cef9..13abc1531cc9a103cfea9efb1632b5009940619f 100644 (file)
@@ -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;
+        }
     }
 }
index 20e272725504efe0952f2898b4b73b9c522278df..21003753ce43e64e1a8e24cd60192e453d0f5804 100644 (file)
@@ -73,6 +73,10 @@ public abstract class SatelModule extends EventDispatcher implements SatelEventL
         OutputStream getOutputStream() throws IOException;
 
         void disconnect();
+
+        default boolean supportsReceiveTimeout() {
+            return false;
+        }
     }
 
     /*
@@ -530,9 +534,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();