From 0baf8e5dfab3a831eea7502f6cb11093f1738c15 Mon Sep 17 00:00:00 2001 From: Wouter Denayer Date: Mon, 28 Dec 2020 15:15:10 +0100 Subject: [PATCH] open connection on refresh even if no modules exist Signed-off-by: Wouter Denayer --- .../handler/NikobusPcLinkHandler.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.nikobus/src/main/java/org/openhab/binding/nikobus/internal/handler/NikobusPcLinkHandler.java b/bundles/org.openhab.binding.nikobus/src/main/java/org/openhab/binding/nikobus/internal/handler/NikobusPcLinkHandler.java index bcb2c74a3e60c..e6b999a444425 100644 --- a/bundles/org.openhab.binding.nikobus/src/main/java/org/openhab/binding/nikobus/internal/handler/NikobusPcLinkHandler.java +++ b/bundles/org.openhab.binding.nikobus/src/main/java/org/openhab/binding/nikobus/internal/handler/NikobusPcLinkHandler.java @@ -50,8 +50,8 @@ import org.slf4j.LoggerFactory; /** - * The {@link NikobusPcLinkHandler} is responsible for handling commands, which are - * sent or received from the PC-Link Nikobus component. + * The {@link NikobusPcLinkHandler} is responsible for handling commands, which + * are sent or received from the PC-Link Nikobus component. * * @author Boris Krivonog - Initial contribution */ @@ -329,6 +329,22 @@ private void refresh() { List things = getThing().getThings().stream() .filter(thing -> thing.getHandler() instanceof NikobusModuleHandler).collect(Collectors.toList()); + + // if there are command listeners then we need an open connection even if no modules exist + if (!commandListeners.isEmpty()) { + NikobusConnection connection = this.connection; + if (connection == null) { + return; + } + try { + connectIfNeeded(connection); + } catch (IOException e) { + logger.debug("refresh failed due {}", e.getMessage(), e); + connection.close(); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); + } + } + if (things.isEmpty()) { logger.debug("Nothing to refresh"); return; @@ -349,8 +365,8 @@ private synchronized void connectIfNeeded(NikobusConnection connection) throws I if (!connection.isConnected()) { connection.connect(); - // Send connection sequence, mimicking the Nikobus software. If this is not send, PC-Link - // sometimes does not forward button presses via serial interface. + // Send connection sequence, mimicking the Nikobus software. If this is not + // sent, PC-Link sometimes does not forward button presses via serial interface. Stream.of(new String[] { "++++", "ATH0", "ATZ", "$10110000B8CF9D", "#L0", "#E0", "#L0", "#E1" }) .map(NikobusCommand::new).forEach(this::sendCommand);