From a54da2161149c8cefcf869b33051c3703524567a Mon Sep 17 00:00:00 2001 From: Andrew Fiddian-Green Date: Wed, 8 Jun 2022 08:45:35 +0200 Subject: [PATCH] [neohub] check for connection refused (#12906) Signed-off-by: Andrew Fiddian-Green --- bundles/org.openhab.binding.neohub/README.md | 10 ++++++++- .../internal/NeoHubBindingConstants.java | 1 + .../neohub/internal/NeoHubHandler.java | 21 ++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.neohub/README.md b/bundles/org.openhab.binding.neohub/README.md index 7e9e3a427c53f..9b0b64eb174f3 100644 --- a/bundles/org.openhab.binding.neohub/README.md +++ b/bundles/org.openhab.binding.neohub/README.md @@ -39,7 +39,15 @@ Before the binding can communicate with the hub, the following Configuration Par | portNumber | Port number of the NeoHub (Default=4242) | | pollingInterval | Time (seconds) between polling requests to the NeoHub (Min=4, Max=60, Default=60) | | socketTimeout | Time (seconds) to allow for TCP socket connections to the hub to succeed (Min=4, Max=20, Default=5) | -| preferLegacyApi | Prefer if the binding should use the legacy API; this only works so long as the legacy API is still supported; otherwise the binding will switch to the new API anyway (Default=false) | +| preferLegacyApi | ADVANCED: Prefer the binding to use older API calls; if these are not supported, it switches to the new calls (Default=false) | + +## Connection Refused Errors + +From early 2022 Heatmiser introduced NeoHub firmware that has the ability to enable / disable the NeoHub `portNumber` 4242. +If this port is disabled the OpenHAB binding cannot connect and the binding will report a *"Connection Refused"* warning in the log. +In prior firmware versions the port was always enabled. +But in the new firmware the port is initially enabled on power up but if no communication occurs for 48 hours it is automatically disabled. +Alternatively the Heatmiser mobile App has a setting (Settings | System | API Access | Legacy API Enable | On) whereby the port can be permanently enabled. ## Thing Configuration for "NeoStat" and "NeoPlug" diff --git a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubBindingConstants.java b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubBindingConstants.java index 997ae17cf4669..710328cf325b1 100644 --- a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubBindingConstants.java +++ b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubBindingConstants.java @@ -157,6 +157,7 @@ public static enum NeoHubReturnResult { public static final String CMD_CODE_TIMER = "{\"TIMER_%s\":\"%s\"}"; public static final String CMD_CODE_MANUAL = "{\"MANUAL_%s\":\"%s\"}"; public static final String CMD_CODE_READ_DCB = "{\"READ_DCB\":100}"; + public static final String CMD_CODE_FIRMWARE = "{\"FIRMWARE\":0}"; /* * note: from NeoHub rev2.6 onwards the INFO command is "deprecated" and it diff --git a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java index 53eb9ea994068..be17626a9a719 100644 --- a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java +++ b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java @@ -58,6 +58,8 @@ @NonNullByDefault public class NeoHubHandler extends BaseBridgeHandler { + private static final String SEE_README = "See documentation chapter \"Connection Refused Errors\""; + private final Logger logger = LoggerFactory.getLogger(NeoHubHandler.class); private final Map connectionStates = new HashMap<>(); @@ -140,9 +142,26 @@ public void initialize() { logger.debug("hub '{}' preferLegacyApi={}", getThing().getUID(), config.preferLegacyApi); } - socket = new NeoHubSocket(config.hostName, config.portNumber, config.socketTimeout); + NeoHubSocket socket = this.socket = new NeoHubSocket(config.hostName, config.portNumber, config.socketTimeout); this.config = config; + /* + * Try to 'ping' the hub, and if there is a 'connection refused', it is probably due to the mobile App | + * Settings | Legacy API Enable switch not being On, so go offline and log a warning message. + */ + try { + socket.sendMessage(CMD_CODE_FIRMWARE); + } catch (IOException e) { + String error = e.getMessage(); + if (error != null && error.toLowerCase().startsWith("connection refused")) { + logger.warn("CONNECTION REFUSED!! (hub '{}') => {}", getThing().getUID(), SEE_README); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, SEE_README); + return; + } + } catch (NeoHubException e) { + // NeoHubException won't actually occur here + } + if (logger.isDebugEnabled()) { logger.debug("hub '{}' start background polling..", getThing().getUID()); }