]> git.basschouten.com Git - openhab-addons.git/commitdiff
[tesla] Stop the WebSocket client when disposing the thing handler (#14483)
authorlolodomo <lg.hc@free.fr>
Tue, 7 Mar 2023 19:50:37 +0000 (20:50 +0100)
committerGitHub <noreply@github.com>
Tue, 7 Mar 2023 19:50:37 +0000 (20:50 +0100)
Fix #14341

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaEventEndpoint.java
bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java

index beec69d7f9fd32fc770b312620b53079286cd7c4..45690511a503e40c2ae9c3e51e883e2efb5dff91 100644 (file)
@@ -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()) {
index 8903a961020493b0465460a06063fe6a56c50a4b..c2cd2d8cc9275e65d292b1536f92452b362f67b4 100644 (file)
@@ -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;
                 }
             }