[yioremote] Stop the web socket client when disposing thing handler (#14340)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-02-20 12:39:03 +01:00 committed by GitHub
parent 8306210a13
commit 1f877cae6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,6 +68,7 @@ public class YIOremoteDockHandler extends BaseThingHandler {
private ClientUpgradeRequest yioremoteDockwebSocketClientrequest = new ClientUpgradeRequest(); private ClientUpgradeRequest yioremoteDockwebSocketClientrequest = new ClientUpgradeRequest();
private @Nullable URI websocketAddress; private @Nullable URI websocketAddress;
private YioRemoteDockHandleStatus yioRemoteDockActualStatus = YioRemoteDockHandleStatus.UNINITIALIZED_STATE; private YioRemoteDockHandleStatus yioRemoteDockActualStatus = YioRemoteDockHandleStatus.UNINITIALIZED_STATE;
private @Nullable Future<?> initJob;
private @Nullable Future<?> webSocketPollingJob; private @Nullable Future<?> webSocketPollingJob;
private @Nullable Future<?> webSocketReconnectionPollingJob; private @Nullable Future<?> webSocketReconnectionPollingJob;
public String receivedMessage = ""; public String receivedMessage = "";
@ -90,7 +91,7 @@ public class YIOremoteDockHandler extends BaseThingHandler {
@Override @Override
public void initialize() { public void initialize() {
updateStatus(ThingStatus.UNKNOWN); updateStatus(ThingStatus.UNKNOWN);
scheduler.execute(() -> { initJob = scheduler.submit(() -> {
try { try {
websocketAddress = new URI("ws://" + localConfig.host + ":946"); websocketAddress = new URI("ws://" + localConfig.host + ":946");
yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_PROCESS; yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_PROCESS;
@ -257,8 +258,18 @@ public class YIOremoteDockHandler extends BaseThingHandler {
@Override @Override
public void dispose() { public void dispose() {
Future<?> job = initJob;
if (job != null) {
job.cancel(true);
initJob = null;
}
disposeWebsocketPollingJob(); disposeWebsocketPollingJob();
disposeWebSocketReconnectionPollingJob(); disposeWebSocketReconnectionPollingJob();
try {
webSocketClient.stop();
} catch (Exception e) {
logger.debug("Could not stop webSocketClient, message {}", e.getMessage());
}
} }
@Override @Override
@ -342,21 +353,19 @@ public class YIOremoteDockHandler extends BaseThingHandler {
} }
private void disposeWebsocketPollingJob() { private void disposeWebsocketPollingJob() {
if (webSocketPollingJob != null) { Future<?> job = webSocketPollingJob;
if (!webSocketPollingJob.isCancelled() && webSocketPollingJob != null) { if (job != null) {
webSocketPollingJob.cancel(true); job.cancel(true);
}
webSocketPollingJob = null; webSocketPollingJob = null;
} }
} }
private void disposeWebSocketReconnectionPollingJob() { private void disposeWebSocketReconnectionPollingJob() {
if (webSocketReconnectionPollingJob != null) { Future<?> job = webSocketReconnectionPollingJob;
if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) { if (job != null) {
webSocketReconnectionPollingJob.cancel(true); job.cancel(true);
} webSocketReconnectionPollingJob = null;
} }
webSocketReconnectionPollingJob = null;
logger.debug("disposereconnection"); logger.debug("disposereconnection");
reconnectionCounter = 0; reconnectionCounter = 0;
} }