From 5a7be93670c0cb6230eefbec450e51a19a4463c2 Mon Sep 17 00:00:00 2001 From: Arjen Korevaar Date: Wed, 27 Jan 2021 15:32:50 +0100 Subject: [PATCH] [openthermgateway] fix automatic reconnect after connection attempt fails (#9965) * Added callback to disconnected() when connection attempt fails Signed-off-by: Arjen Korevaar * Updated version number Signed-off-by: Arjen Korevaar * Cancel reconnect task when disposing Signed-off-by: Arjen Korevaar --- .../handler/OpenThermGatewayHandler.java | 80 ++++++++----------- .../OpenThermGatewaySocketConnector.java | 1 + .../src/main/resources/OH-INF/thing/otgw.xml | 3 +- 3 files changed, 37 insertions(+), 47 deletions(-) diff --git a/bundles/org.openhab.binding.openthermgateway/src/main/java/org/openhab/binding/openthermgateway/handler/OpenThermGatewayHandler.java b/bundles/org.openhab.binding.openthermgateway/src/main/java/org/openhab/binding/openthermgateway/handler/OpenThermGatewayHandler.java index 8e16a7acc..14bd28474 100644 --- a/bundles/org.openhab.binding.openthermgateway/src/main/java/org/openhab/binding/openthermgateway/handler/OpenThermGatewayHandler.java +++ b/bundles/org.openhab.binding.openthermgateway/src/main/java/org/openhab/binding/openthermgateway/handler/OpenThermGatewayHandler.java @@ -12,6 +12,7 @@ */ package org.openhab.binding.openthermgateway.handler; +import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import javax.measure.Unit; @@ -56,11 +57,10 @@ public class OpenThermGatewayHandler extends BaseThingHandler implements OpenThe private final Logger logger = LoggerFactory.getLogger(OpenThermGatewayHandler.class); private @Nullable OpenThermGatewayConfiguration config; - private @Nullable OpenThermGatewayConnector connector; + private @Nullable ScheduledFuture reconnectTask; private boolean connecting = false; - private boolean explicitDisconnect = false; public OpenThermGatewayHandler(Thing thing) { @@ -80,6 +80,9 @@ public class OpenThermGatewayHandler extends BaseThingHandler implements OpenThe @Override public void handleCommand(ChannelUID channelUID, Command command) { + @Nullable + OpenThermGatewayConnector conn = connector; + logger.debug("Received channel: {}, command: {}", channelUID, command); if (!(command instanceof RefreshType)) { @@ -105,23 +108,19 @@ public class OpenThermGatewayHandler extends BaseThingHandler implements OpenThe gatewayCommand = GatewayCommand.parse(code, command.toFullString()); } - if (checkConnection()) { - @Nullable - OpenThermGatewayConnector conn = connector; + if (conn != null && conn.isConnected()) { + conn.sendCommand(gatewayCommand); - if (conn != null) { - conn.sendCommand(gatewayCommand); - - if (code == GatewayCommandCode.ControlSetpoint) { - if (gatewayCommand.getMessage().equals("0.0")) { - updateState( - OpenThermGatewayBindingConstants.CHANNEL_OVERRIDE_CENTRAL_HEATING_WATER_SETPOINT, - UnDefType.UNDEF); - } - updateState(OpenThermGatewayBindingConstants.CHANNEL_OVERRIDE_CENTRAL_HEATING_ENABLED, - OnOffType.from(!gatewayCommand.getMessage().equals("0.0"))); + if (code == GatewayCommandCode.ControlSetpoint) { + if (gatewayCommand.getMessage().equals("0.0")) { + updateState(OpenThermGatewayBindingConstants.CHANNEL_OVERRIDE_CENTRAL_HEATING_WATER_SETPOINT, + UnDefType.UNDEF); } + updateState(OpenThermGatewayBindingConstants.CHANNEL_OVERRIDE_CENTRAL_HEATING_ENABLED, + OnOffType.from(!gatewayCommand.getMessage().equals("0.0"))); } + } else { + connect(); } } } @@ -140,9 +139,6 @@ public class OpenThermGatewayHandler extends BaseThingHandler implements OpenThe @Override public void disconnected() { - @Nullable - OpenThermGatewayConnector conn = connector; - @Nullable OpenThermGatewayConfiguration conf = config; @@ -151,12 +147,9 @@ public class OpenThermGatewayHandler extends BaseThingHandler implements OpenThe updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Disconnected"); // retry connection if disconnect is not explicitly requested - if (conf != null && !explicitDisconnect && conf.connectionRetryInterval > 0) { - scheduler.schedule(() -> { - if (conn != null && !connecting && !conn.isConnected()) { - connect(); - } - }, conf.connectionRetryInterval, TimeUnit.SECONDS); + if (!explicitDisconnect && conf != null && conf.connectionRetryInterval > 0) { + logger.debug("Scheduling to reconnect in {} seconds.", conf.connectionRetryInterval); + reconnectTask = scheduler.schedule(this::connect, conf.connectionRetryInterval, TimeUnit.SECONDS); } } @@ -215,31 +208,32 @@ public class OpenThermGatewayHandler extends BaseThingHandler implements OpenThe @Override public void dispose() { disconnect(); + + ScheduledFuture localReconnectTask = reconnectTask; + if (localReconnectTask != null) { + localReconnectTask.cancel(true); + reconnectTask = null; + } + super.dispose(); } - private boolean checkConnection() { - @Nullable - OpenThermGatewayConnector conn = connector; - - if (conn != null && conn.isConnected()) { - return true; - } - - return connect(); - } - - private boolean connect() { + private void connect() { @Nullable OpenThermGatewayConfiguration conf = config; + explicitDisconnect = false; + + if (connecting) { + logger.debug("OpenTherm Gateway connector is already connecting ..."); + return; + } + disconnect(); if (conf != null) { logger.debug("Starting OpenTherm Gateway connector"); - explicitDisconnect = false; - connector = new OpenThermGatewaySocketConnector(this, conf.ipaddress, conf.port); Thread thread = new Thread(connector, "OpenTherm Gateway Binding - socket listener thread"); @@ -247,22 +241,18 @@ public class OpenThermGatewayHandler extends BaseThingHandler implements OpenThe thread.start(); logger.debug("OpenTherm Gateway connector started"); - - return true; } - - return false; } private void disconnect() { @Nullable OpenThermGatewayConnector conn = connector; + explicitDisconnect = true; + if (conn != null) { if (conn.isConnected()) { logger.debug("Stopping OpenTherm Gateway connector"); - - explicitDisconnect = true; conn.stop(); } diff --git a/bundles/org.openhab.binding.openthermgateway/src/main/java/org/openhab/binding/openthermgateway/internal/OpenThermGatewaySocketConnector.java b/bundles/org.openhab.binding.openthermgateway/src/main/java/org/openhab/binding/openthermgateway/internal/OpenThermGatewaySocketConnector.java index 67c1cac83..0f2ad9c68 100644 --- a/bundles/org.openhab.binding.openthermgateway/src/main/java/org/openhab/binding/openthermgateway/internal/OpenThermGatewaySocketConnector.java +++ b/bundles/org.openhab.binding.openthermgateway/src/main/java/org/openhab/binding/openthermgateway/internal/OpenThermGatewaySocketConnector.java @@ -110,6 +110,7 @@ public class OpenThermGatewaySocketConnector implements OpenThermGatewayConnecto } } catch (IOException ex) { logger.warn("Unable to connect to the OpenTherm Gateway.", ex); + callback.disconnected(); } } diff --git a/bundles/org.openhab.binding.openthermgateway/src/main/resources/OH-INF/thing/otgw.xml b/bundles/org.openhab.binding.openthermgateway/src/main/resources/OH-INF/thing/otgw.xml index 583c8efd4..32ad1a0a5 100644 --- a/bundles/org.openhab.binding.openthermgateway/src/main/resources/OH-INF/thing/otgw.xml +++ b/bundles/org.openhab.binding.openthermgateway/src/main/resources/OH-INF/thing/otgw.xml @@ -38,11 +38,10 @@ - - 1.1.0 + 1.1.1