Skip to content

Commit

Permalink
[dominoswiss] Fix Bridge-Loading with OH3_3 and OH3_4 (#14172)
Browse files Browse the repository at this point in the history
* Fix Loading of Bridge with OH3.3

Signed-off-by: Frieso Aeschbacher <[email protected]>
  • Loading branch information
Friesoch authored Jan 22, 2023
1 parent 2b6803e commit 4927585
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class EGateHandler extends BaseBridgeHandler {

private int port;
private @Nullable String host;
private static final int SOCKET_TIMEOUT_SEC = 250;
private static final int SOCKET_TIMEOUT_MILLISEC = 1000;
private final Object lock = new Object();
private @Nullable BufferedWriter writer;
private @Nullable BufferedReader reader;
Expand Down Expand Up @@ -84,9 +84,15 @@ public void initialize() {

if (host != null && port > 0) {
// Create a socket to eGate
try (Socket localEgateSocket = new Socket(host, port)) {

InetSocketAddress socketAddress = new InetSocketAddress(host, port);
Socket localEgateSocket = new Socket();
try {
localEgateSocket.connect(socketAddress, SOCKET_TIMEOUT_MILLISEC);
writer = new BufferedWriter(new OutputStreamWriter(localEgateSocket.getOutputStream()));
egateSocket = localEgateSocket;
updateStatus(ThingStatus.ONLINE);
logger.debug("Egate successfully connected {}", egateSocket.toString());
} catch (IOException e) {
logger.debug("IOException in initialize: {} host {} port {}", e.toString(), host, port);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.toString());
Expand Down Expand Up @@ -133,12 +139,16 @@ public void dispose() {
public synchronized boolean isConnected() {
Socket localEGateSocket = egateSocket;
if (localEGateSocket == null) {
logger.debug("EGate is not connected, Socket is null");
return false;
}

// NOTE: isConnected() returns true once a connection is made and will
// always return true even after the socket is closed
// http://stackoverflow.com/questions/10163358/
logger.debug("EGate isconnected() {}, isClosed() {}", localEGateSocket.isConnected(),
localEGateSocket.isClosed());

return localEGateSocket.isConnected() && !localEGateSocket.isClosed();
}

Expand Down Expand Up @@ -196,14 +206,15 @@ public void registerBlind(String id, ThingUID uid) {
*/

private void sendCommand(String command) {
sendCommand(command, SOCKET_TIMEOUT_SEC);
sendCommand(command, SOCKET_TIMEOUT_MILLISEC);
}

private synchronized void sendCommand(String command, int timeout) {
logger.debug("EGate got command: {}", command);
Socket localEGateSocket = egateSocket;
BufferedWriter localWriter = writer;
if (localEGateSocket == null || localWriter == null) {
logger.debug("Error eGateSocket null, writer null, returning...");
return;
}
if (!isConnected()) {
Expand All @@ -213,7 +224,7 @@ private synchronized void sendCommand(String command, int timeout) {

// Send plain string to eGate Server,
try {
localEGateSocket.setSoTimeout(SOCKET_TIMEOUT_SEC);
localEGateSocket.setSoTimeout(timeout);
localWriter.write(command);
localWriter.flush();
} catch (IOException e) {
Expand All @@ -223,23 +234,26 @@ private synchronized void sendCommand(String command, int timeout) {

private void pollingConfig() {
if (!isConnected()) {
logger.debug("PollingConfig Run, is not connected so let's connect");
Socket localEGateSocket = egateSocket;
BufferedWriter localWriter = writer;
if (localEGateSocket == null || localWriter == null) {
logger.debug("Error eGateSocket null, writer null in pollingConfig(), returning...");
return;
}

synchronized (lock) {
try {
localEGateSocket.connect(new InetSocketAddress(host, port));
localEGateSocket.setSoTimeout(SOCKET_TIMEOUT_SEC);
localEGateSocket.connect(new InetSocketAddress(host, port), SOCKET_TIMEOUT_MILLISEC);
logger.debug("pollingConfig() successsully connected {}", localEGateSocket.isClosed());
localWriter.write("SilenceModeSet;Value=0;" + CR);
localWriter.flush();
} catch (IOException e) {
logger.debug("IOException in pollingConfig: {} host {} port {}", e.toString(), host, port);
try {
localEGateSocket.close();
egateSocket = null;
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.toString());
logger.debug("EGate closed");
} catch (IOException e1) {
logger.debug("EGate Socket not closed {}", e1.toString());
}
Expand Down

0 comments on commit 4927585

Please sign in to comment.