From c8cdd2dfa572351d11b97b4f6d0f3e6d3ae9ca35 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Tue, 7 Mar 2023 20:50:37 +0100 Subject: [PATCH] [tesla] Stop the WebSocket client when disposing the thing handler (#14483) Fix #14341 Signed-off-by: Laurent Garnier --- .../tesla/internal/handler/TeslaEventEndpoint.java | 12 +++++++++++- .../tesla/internal/handler/TeslaVehicleHandler.java | 7 ++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java index beec69d7f..45690511a 100644 --- a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java +++ b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java @@ -75,6 +75,16 @@ public class TeslaEventEndpoint implements WebSocketListener, WebSocketPingPongL } } + public void close() { + try { + if (client.isRunning()) { + client.stop(); + } + } catch (Exception e) { + logger.warn("An exception occurred while stopping the WebSocket client : {}", e.getMessage()); + } + } + public void connect(URI endpointURI) { if (connectionState == ConnectionState.CONNECTED) { return; @@ -114,7 +124,7 @@ public class TeslaEventEndpoint implements WebSocketListener, WebSocketPingPongL this.session = session; } - public void close() { + public void closeConnection() { try { connectionState = ConnectionState.CLOSING; if (session != null && session.isOpen()) { diff --git a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java index 8903a9610..c2cd2d8cc 100644 --- a/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java +++ b/bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java @@ -1212,13 +1212,13 @@ public class TeslaVehicleHandler extends BaseThingHandler { } if (systemTimeStamp - currentTimeStamp > EVENT_TIMESTAMP_MAX_DELTA) { logger.trace("Event : The event endpoint will be reset"); - eventEndpoint.close(); + eventEndpoint.closeConnection(); } } break; case "data:error": logger.debug("Event : Received an error: '{}'/'{}'", event.value, event.error_type); - eventEndpoint.close(); + eventEndpoint.closeConnection(); break; } } @@ -1263,7 +1263,7 @@ public class TeslaVehicleHandler extends BaseThingHandler { "Event : Reached the maximum number of errors ({}) for the current interval ({} seconds)", EVENT_MAXIMUM_ERRORS_IN_INTERVAL, EVENT_ERROR_INTERVAL_SECONDS); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR); - eventEndpoint.close(); + eventEndpoint.closeConnection(); } if ((System.currentTimeMillis() - eventIntervalTimestamp) > 1000 @@ -1301,6 +1301,7 @@ public class TeslaVehicleHandler extends BaseThingHandler { if (Thread.interrupted()) { logger.debug("Event : The event thread was interrupted"); + eventEndpoint.close(); return; } }