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

View File

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