diff --git a/bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/elk/ElkAlarmConnection.java b/bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/elk/ElkAlarmConnection.java index 17744a4b5708e..67efa00e32c33 100644 --- a/bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/elk/ElkAlarmConnection.java +++ b/bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/elk/ElkAlarmConnection.java @@ -35,6 +35,7 @@ import javax.net.ssl.HandshakeCompletedEvent; import javax.net.ssl.HandshakeCompletedListener; import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import javax.net.ssl.X509TrustManager; @@ -175,6 +176,10 @@ public boolean sslLogin() { } catch (UnknownHostException e) { logger.error("Unable to open connection to Elk alarm: {}:{}", config.host, config.port, e); return false; + } catch (SSLException e) { + logger.error("Unable to open connection to Elk alarm: {}:{}. Must use secure Elk port.", config.host, + config.port, e); + return false; } catch (IOException e) { logger.error("Unable to open connection to Elk alarm: {}:{}", config.host, config.port, e); return false; @@ -276,8 +281,12 @@ public void run() { } catch (IOException e) { if (e.getMessage().equals("Socket closed")) { logger.error("Error reading from Elk alarm. Socket Closed. {}:{}", config.host, config.port); + return; } else { - logger.error("Error reading from Elk alarm: {}:{}", config.host, config.port, e); + logger.error( + "Error reading from Elk alarm: {}:{}. Check hostname/address and secure/non-secure port.", + config.host, config.port, e); + return; } } } diff --git a/bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/handler/ElkM1BridgeHandler.java b/bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/handler/ElkM1BridgeHandler.java index 7f570d2e3c912..7705b31621b08 100644 --- a/bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/handler/ElkM1BridgeHandler.java +++ b/bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/handler/ElkM1BridgeHandler.java @@ -125,20 +125,20 @@ public void removeListener(ElkM1HandlerListener listener) { public void initialize() { updateStatus(ThingStatus.UNKNOWN); initializeFuture = scheduler.schedule(this::scheduledInitialize, 1, TimeUnit.SECONDS); - commWatchdogFuture = scheduler.scheduleWithFixedDelay(commWatchdog, 30, 120, TimeUnit.SECONDS); return; } public void scheduledInitialize() { + commWatchdogFuture = scheduler.scheduleWithFixedDelay(commWatchdog, 120, 120, TimeUnit.SECONDS); ElkAlarmConfig config = getConfigAs(ElkAlarmConfig.class); messageFactory = new ElkMessageFactory(); areas = new boolean[ElkMessageFactory.MAX_AREAS]; if (config.useSSL) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, + updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING, "Opening SSL/TLS server connection"); } else { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Opening server connection"); + updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING, "Opening server connection"); } connection = new ElkAlarmConnection(config, messageFactory); @@ -150,7 +150,7 @@ public void scheduledInitialize() { connection.sendCommand(new ZonePartition()); connection.sendCommand(new ZoneStatus()); connection.sendCommand(new ArmingStatus()); - updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Requesting version from alarm"); + updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING, "Requesting version from alarm"); } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Unable to open socket to alarm"); } @@ -360,7 +360,7 @@ public void onZoneAdded(ElkM1ZoneHandler elkM1ZoneHandler) { */ public void updateArmedState(int area, ElkAlarmArmedState armed) throws Exception { ElkAlarmConfig config = getConfigAs(ElkAlarmConfig.class); - String pincode = String.format("%06d", config.userCode); + String pincode = ("000000" + config.userCode).substring(config.userCode.length()); switch (armed) { case ArmedAway: connection.sendCommand(new ArmAway(area, pincode)); @@ -426,14 +426,26 @@ Thing getThingForType(ElkTypeToRequest type, int num) { return null; } + /** + * Cancel Future + */ + public void cancelFuture(@Nullable ScheduledFuture future) { + if (future != null) { + future.cancel(false); + } + } + /** * Shutdown the bridge */ @Override public void dispose() { - connection.shutdown(); + cancelFuture(commWatchdogFuture); + if (connection != null) { + connection.shutdown(); + connection = null; + } areas = new boolean[0]; - connection = null; assert (listeners.isEmpty()); super.dispose(); }