mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-02-04 03:14:07 +01:00
[dominoswiss] Fix Bridge-Loading with OH3_3 and OH3_4 (#14172)
* Fix Loading of Bridge with OH3.3 Signed-off-by: Frieso Aeschbacher <frieso.aeschbacher@gmail.com>
This commit is contained in:
parent
2b6803e506
commit
4927585f25
@ -55,7 +55,7 @@ public class EGateHandler extends BaseBridgeHandler {
|
|||||||
|
|
||||||
private int port;
|
private int port;
|
||||||
private @Nullable String host;
|
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 final Object lock = new Object();
|
||||||
private @Nullable BufferedWriter writer;
|
private @Nullable BufferedWriter writer;
|
||||||
private @Nullable BufferedReader reader;
|
private @Nullable BufferedReader reader;
|
||||||
@ -84,9 +84,15 @@ public class EGateHandler extends BaseBridgeHandler {
|
|||||||
|
|
||||||
if (host != null && port > 0) {
|
if (host != null && port > 0) {
|
||||||
// Create a socket to eGate
|
// 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()));
|
writer = new BufferedWriter(new OutputStreamWriter(localEgateSocket.getOutputStream()));
|
||||||
egateSocket = localEgateSocket;
|
egateSocket = localEgateSocket;
|
||||||
|
updateStatus(ThingStatus.ONLINE);
|
||||||
|
logger.debug("Egate successfully connected {}", egateSocket.toString());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.debug("IOException in initialize: {} host {} port {}", e.toString(), host, port);
|
logger.debug("IOException in initialize: {} host {} port {}", e.toString(), host, port);
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.toString());
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.toString());
|
||||||
@ -133,12 +139,16 @@ public class EGateHandler extends BaseBridgeHandler {
|
|||||||
public synchronized boolean isConnected() {
|
public synchronized boolean isConnected() {
|
||||||
Socket localEGateSocket = egateSocket;
|
Socket localEGateSocket = egateSocket;
|
||||||
if (localEGateSocket == null) {
|
if (localEGateSocket == null) {
|
||||||
|
logger.debug("EGate is not connected, Socket is null");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: isConnected() returns true once a connection is made and will
|
// NOTE: isConnected() returns true once a connection is made and will
|
||||||
// always return true even after the socket is closed
|
// always return true even after the socket is closed
|
||||||
// http://stackoverflow.com/questions/10163358/
|
// http://stackoverflow.com/questions/10163358/
|
||||||
|
logger.debug("EGate isconnected() {}, isClosed() {}", localEGateSocket.isConnected(),
|
||||||
|
localEGateSocket.isClosed());
|
||||||
|
|
||||||
return localEGateSocket.isConnected() && !localEGateSocket.isClosed();
|
return localEGateSocket.isConnected() && !localEGateSocket.isClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,7 +206,7 @@ public class EGateHandler extends BaseBridgeHandler {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
private void sendCommand(String command) {
|
private void sendCommand(String command) {
|
||||||
sendCommand(command, SOCKET_TIMEOUT_SEC);
|
sendCommand(command, SOCKET_TIMEOUT_MILLISEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void sendCommand(String command, int timeout) {
|
private synchronized void sendCommand(String command, int timeout) {
|
||||||
@ -204,6 +214,7 @@ public class EGateHandler extends BaseBridgeHandler {
|
|||||||
Socket localEGateSocket = egateSocket;
|
Socket localEGateSocket = egateSocket;
|
||||||
BufferedWriter localWriter = writer;
|
BufferedWriter localWriter = writer;
|
||||||
if (localEGateSocket == null || localWriter == null) {
|
if (localEGateSocket == null || localWriter == null) {
|
||||||
|
logger.debug("Error eGateSocket null, writer null, returning...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!isConnected()) {
|
if (!isConnected()) {
|
||||||
@ -213,7 +224,7 @@ public class EGateHandler extends BaseBridgeHandler {
|
|||||||
|
|
||||||
// Send plain string to eGate Server,
|
// Send plain string to eGate Server,
|
||||||
try {
|
try {
|
||||||
localEGateSocket.setSoTimeout(SOCKET_TIMEOUT_SEC);
|
localEGateSocket.setSoTimeout(timeout);
|
||||||
localWriter.write(command);
|
localWriter.write(command);
|
||||||
localWriter.flush();
|
localWriter.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -223,15 +234,18 @@ public class EGateHandler extends BaseBridgeHandler {
|
|||||||
|
|
||||||
private void pollingConfig() {
|
private void pollingConfig() {
|
||||||
if (!isConnected()) {
|
if (!isConnected()) {
|
||||||
|
logger.debug("PollingConfig Run, is not connected so let's connect");
|
||||||
Socket localEGateSocket = egateSocket;
|
Socket localEGateSocket = egateSocket;
|
||||||
BufferedWriter localWriter = writer;
|
BufferedWriter localWriter = writer;
|
||||||
if (localEGateSocket == null || localWriter == null) {
|
if (localEGateSocket == null || localWriter == null) {
|
||||||
|
logger.debug("Error eGateSocket null, writer null in pollingConfig(), returning...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
try {
|
try {
|
||||||
localEGateSocket.connect(new InetSocketAddress(host, port));
|
localEGateSocket.connect(new InetSocketAddress(host, port), SOCKET_TIMEOUT_MILLISEC);
|
||||||
localEGateSocket.setSoTimeout(SOCKET_TIMEOUT_SEC);
|
logger.debug("pollingConfig() successsully connected {}", localEGateSocket.isClosed());
|
||||||
localWriter.write("SilenceModeSet;Value=0;" + CR);
|
localWriter.write("SilenceModeSet;Value=0;" + CR);
|
||||||
localWriter.flush();
|
localWriter.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -239,7 +253,7 @@ public class EGateHandler extends BaseBridgeHandler {
|
|||||||
try {
|
try {
|
||||||
localEGateSocket.close();
|
localEGateSocket.close();
|
||||||
egateSocket = null;
|
egateSocket = null;
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.toString());
|
logger.debug("EGate closed");
|
||||||
} catch (IOException e1) {
|
} catch (IOException e1) {
|
||||||
logger.debug("EGate Socket not closed {}", e1.toString());
|
logger.debug("EGate Socket not closed {}", e1.toString());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user