[yioremote] Update and improve of the reconnection of the plugin (#10480)

* changed reconnection

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>

* reverted

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>

* Update and Bugfix reconnection

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>

* Update reconnection handling

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>

* Update codestyle

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>
This commit is contained in:
miloit 2021-04-09 23:18:02 +02:00 committed by GitHub
parent e0f5e858c7
commit 3561388061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 16 deletions

View File

@ -81,6 +81,7 @@ public class YIOremoteDockHandler extends BaseThingHandler {
private AuthenticationMessage authenticationMessageHandler = new AuthenticationMessage();
private IRReceiverMessage irReceiverMessageHandler = new IRReceiverMessage();
private PingMessage pingMessageHandler = new PingMessage();
private int reconnectionCounter = 0;
public YIOremoteDockHandler(Thing thing) {
super(thing);
@ -122,6 +123,7 @@ public class YIOremoteDockHandler extends BaseThingHandler {
authenticateWebsocket();
break;
case COMMUNICATION_ERROR:
disposeWebsocketPollingJob();
reconnectWebsocket();
break;
case AUTHENTICATION_COMPLETE:
@ -142,12 +144,14 @@ public class YIOremoteDockHandler extends BaseThingHandler {
@Override
public void onClose() {
logger.debug("onClose");
disposeWebsocketPollingJob();
reconnectWebsocket();
}
@Override
public void onError(Throwable cause) {
logger.debug("onError");
disposeWebsocketPollingJob();
yioRemoteDockActualStatus = YioRemoteDockHandleStatus.COMMUNICATION_ERROR;
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
@ -254,14 +258,7 @@ public class YIOremoteDockHandler extends BaseThingHandler {
@Override
public void dispose() {
disposeWebsocketPollingJob();
if (webSocketReconnectionPollingJob != null) {
if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) {
webSocketReconnectionPollingJob.cancel(true);
authenticationOk = false;
heartBeat = false;
}
webSocketReconnectionPollingJob = null;
}
disposeWebSocketReconnectionPollingJob();
}
@Override
@ -324,10 +321,12 @@ public class YIOremoteDockHandler extends BaseThingHandler {
case AUTHENTICATION_PROCESS:
if (authenticationOk) {
yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_COMPLETE;
disposeWebSocketReconnectionPollingJob();
reconnectionCounter = 0;
updateStatus(ThingStatus.ONLINE);
updateState(STATUS_STRING_CHANNEL, StringType.EMPTY);
updateState(RECEIVER_SWITCH_CHANNEL, OnOffType.OFF);
webSocketPollingJob = scheduler.scheduleWithFixedDelay(this::pollingWebsocketJob, 0, 60,
webSocketPollingJob = scheduler.scheduleWithFixedDelay(this::pollingWebsocketJob, 0, 40,
TimeUnit.SECONDS);
} else {
yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_FAILED;
@ -351,6 +350,17 @@ public class YIOremoteDockHandler extends BaseThingHandler {
}
}
private void disposeWebSocketReconnectionPollingJob() {
if (webSocketReconnectionPollingJob != null) {
if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) {
webSocketReconnectionPollingJob.cancel(true);
}
}
webSocketReconnectionPollingJob = null;
logger.debug("disposereconnection");
reconnectionCounter = 0;
}
private void pollingWebsocketJob() {
switch (yioRemoteDockActualStatus) {
case AUTHENTICATION_COMPLETE:
@ -395,13 +405,30 @@ public class YIOremoteDockHandler extends BaseThingHandler {
}
public void reconnectWebsocket() {
yioRemoteDockActualStatus = YioRemoteDockHandleStatus.COMMUNICATION_ERROR;
if (webSocketReconnectionPollingJob == null) {
webSocketReconnectionPollingJob = scheduler.scheduleWithFixedDelay(this::reconnectWebsocketJob, 0, 30,
TimeUnit.SECONDS);
} else if (reconnectionCounter == 5) {
reconnectionCounter = 0;
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Connection lost no ping from YIO DOCK");
if (webSocketReconnectionPollingJob == null) {
webSocketReconnectionPollingJob = scheduler.scheduleWithFixedDelay(this::reconnectWebsocketJob, 0, 1,
TimeUnit.MINUTES);
} else {
disposeWebSocketReconnectionPollingJob();
if (webSocketReconnectionPollingJob == null) {
webSocketReconnectionPollingJob = scheduler.scheduleWithFixedDelay(this::reconnectWebsocketJob, 0,
5, TimeUnit.MINUTES);
}
}
} else {
}
}
public void reconnectWebsocketJob() {
reconnectionCounter++;
switch (yioRemoteDockActualStatus) {
case COMMUNICATION_ERROR:
logger.debug("Reconnecting YIORemoteHandler");
@ -431,12 +458,8 @@ public class YIOremoteDockHandler extends BaseThingHandler {
}
break;
case AUTHENTICATION_COMPLETE:
if (webSocketReconnectionPollingJob != null) {
if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) {
webSocketReconnectionPollingJob.cancel(true);
}
webSocketReconnectionPollingJob = null;
}
disposeWebSocketReconnectionPollingJob();
reconnectionCounter = 0;
break;
default:
break;

View File

@ -61,7 +61,7 @@ public class Websocket {
@OnWebSocketError
public void onError(Throwable cause) {
logger.warn("WebSocketError {}", cause.getMessage());
logger.debug("WebSocketError {}", cause.getMessage());
if (websocketHandler != null) {
websocketHandler.onError(cause);
}