[satel] Fixes for INT-RS module (#9059)

Signed-off-by: Krzysztof Goworek <krzysztof.goworek@gmail.com>

Co-authored-by: Krzysztof Goworek <krzysztof.goworek@gmail.com>
This commit is contained in:
druciak 2020-11-21 07:35:04 +01:00 committed by GitHub
parent 19ae11dcb4
commit 06d367a0dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View File

@ -69,8 +69,19 @@ public class IntRSModule extends SatelModule {
throw new ConnectionFailureException(String.format("Port %s does not exist", this.port)); throw new ConnectionFailureException(String.format("Port %s does not exist", this.port));
} }
SerialPort serialPort = portIdentifier.open("org.openhab.binding.satel", 2000); 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.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 // RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event // Start event listener, which will just sleep and slow down event
// loop // loop
@ -84,10 +95,10 @@ public class IntRSModule extends SatelModule {
} }
} }
}); });
serialPort.notifyOnDataAvailable(true); serialPort.notifyOnDataAvailable(false);
logger.info("INT-RS module connected successfuly"); logger.info("INT-RS module connected successfuly");
return new SerialCommunicationChannel(serialPort); return new SerialCommunicationChannel(serialPort, supportsReceiveTimeout);
} catch (PortInUseException e) { } catch (PortInUseException e) {
throw new ConnectionFailureException(String.format("Port %s in use", this.port), e); throw new ConnectionFailureException(String.format("Port %s in use", this.port), e);
} catch (UnsupportedCommOperationException e) { } catch (UnsupportedCommOperationException e) {
@ -99,10 +110,13 @@ public class IntRSModule extends SatelModule {
private class SerialCommunicationChannel implements CommunicationChannel { 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.serialPort = serialPort;
this.supportsReceiveTimeout = supportsReceiveTimeout;
} }
@Override @Override
@ -133,5 +147,10 @@ public class IntRSModule extends SatelModule {
logger.error("An error occurred during closing serial port", e); logger.error("An error occurred during closing serial port", e);
} }
} }
@Override
public boolean supportsReceiveTimeout() {
return supportsReceiveTimeout;
}
} }
} }

View File

@ -73,6 +73,10 @@ public abstract class SatelModule extends EventDispatcher implements SatelEventL
OutputStream getOutputStream() throws IOException; OutputStream getOutputStream() throws IOException;
void disconnect(); 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, logger.trace("Checking communication thread: {}, {}", thread != null,
Boolean.toString(thread != null && thread.isAlive())); Boolean.toString(thread != null && thread.isAlive()));
if (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."); logger.error("Send/receive timeout, disconnecting module.");
stop(); stop();
thread.interrupt(); thread.interrupt();