From ef8e855895e1aa44bbd15458c258a01727dbdbf5 Mon Sep 17 00:00:00 2001 From: Sven Strohschein Date: Fri, 21 Aug 2020 10:17:14 +0200 Subject: [PATCH] Revert "[innogysmarthome] Reconnect fixes (#8182) (#8318)" (#8324) This reverts commit b621062f611e52be9c84f2b8482902ea93e20144. Reopens #8182 Signed-off-by: Sven Strohschein --- .../internal/InnogyWebSocket.java | 5 ++-- .../internal/handler/InnogyBridgeHandler.java | 26 ++++++++++--------- .../internal/handler/InnogyDeviceHandler.java | 25 ++++++++++-------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/InnogyWebSocket.java b/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/InnogyWebSocket.java index 42cf624a887a9..93e0195a2578d 100644 --- a/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/InnogyWebSocket.java +++ b/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/InnogyWebSocket.java @@ -52,7 +52,7 @@ public class InnogyWebSocket { /** * Constructs the {@link InnogyWebSocket}. * - * @param eventListener the responsible {@link InnogyBridgeHandler} + * @param bridgeHandler the responsible {@link InnogyBridgeHandler} * @param webSocketURI the {@link URI} of the websocket endpoint * @param maxIdleTimeout */ @@ -128,8 +128,7 @@ public void onConnect(Session session) { public void onClose(int statusCode, String reason) { if (statusCode == StatusCode.NORMAL) { logger.info("Connection to innogy Webservice was closed normally."); - } else if (!closing) { - //An additional reconnect attempt is only required when the close/stop wasn't executed by the binding. + } else { logger.info("Connection to innogy Webservice was closed abnormally (code: {}). Reason: {}", statusCode, reason); eventListener.connectionClosed(); diff --git a/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/handler/InnogyBridgeHandler.java b/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/handler/InnogyBridgeHandler.java index 89024197407c7..76b6c498f6f31 100644 --- a/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/handler/InnogyBridgeHandler.java +++ b/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/handler/InnogyBridgeHandler.java @@ -82,7 +82,7 @@ * The {@link InnogyBridgeHandler} is responsible for handling the innogy SmartHome controller including the connection * to the innogy backend for all communications with the innogy {@link Device}s. *

- * It implements the {@link AccessTokenRefreshListener} to handle updates of the oauth2 tokens and the + * It implements the {@link CredentialRefreshListener} to handle updates of the oauth2 tokens and the * {@link EventListener} to handle {@link Event}s, that are received by the {@link InnogyWebSocket}. *

* The {@link Device}s are organized by the {@link DeviceStructureManager}, which is also responsible for the connection @@ -97,6 +97,8 @@ public class InnogyBridgeHandler extends BaseBridgeHandler public static final Set SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE); + private static final long WEBSOCKET_TIMEOUT_RETRY_SECONDS = 5; + private final Logger logger = LoggerFactory.getLogger(InnogyBridgeHandler.class); private final Gson gson = new Gson(); private final Object lock = new Object(); @@ -240,10 +242,8 @@ private void startClient() { return; } } - - Device bridgeDevice = deviceStructMan.getBridgeDevice(); - setBridgeProperties(bridgeDevice); - bridgeId = bridgeDevice.getId(); + setBridgeProperties(deviceStructMan.getBridgeDevice()); + bridgeId = deviceStructMan.getBridgeDevice().getId(); startWebsocket(); cancelReinitJob(); } @@ -508,7 +508,7 @@ public void onEvent(final String msg) { case BaseEvent.TYPE_DISCONNECT: logger.debug("Websocket disconnected."); - scheduleRestartClient(REINITIALIZE_DELAY_SECONDS); + scheduleRestartClient(0); break; case BaseEvent.TYPE_CONFIGURATION_CHANGED: @@ -892,17 +892,18 @@ public void commandSetRollerShutterStop(final String deviceId, ShutterAction.Shu * @return boolean true, if binding should continue. */ private boolean handleClientException(final Exception e) { - boolean isReinitialize = true; + long reinitialize = REINITIALIZE_DELAY_SECONDS; if (e instanceof SessionExistsException) { logger.debug("Session already exists. Continuing..."); - isReinitialize = false; + reinitialize = -1; } else if (e instanceof InvalidActionTriggeredException) { logger.debug("Error triggering action: {}", e.getMessage()); - isReinitialize = false; + reinitialize = -1; } else if (e instanceof RemoteAccessNotAllowedException) { // Remote access not allowed (usually by IP address change) logger.debug("Remote access not allowed. Dropping access token and reinitializing binding..."); refreshAccessToken(); + reinitialize = 0; } else if (e instanceof ControllerOfflineException) { logger.debug("innogy SmartHome Controller is offline."); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, e.getMessage()); @@ -918,11 +919,12 @@ private boolean handleClientException(final Exception e) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); } else if (e instanceof TimeoutException) { logger.debug("WebSocket timeout: {}", e.getMessage()); + reinitialize = WEBSOCKET_TIMEOUT_RETRY_SECONDS; } else if (e instanceof SocketTimeoutException) { logger.debug("Socket timeout: {}", e.getMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); } else if (e instanceof InterruptedException) { - isReinitialize = false; + reinitialize = -1; Thread.currentThread().interrupt(); } else if (e instanceof ExecutionException) { logger.debug("ExecutionException: {}", ExceptionUtils.getRootCauseMessage(e)); @@ -931,8 +933,8 @@ private boolean handleClientException(final Exception e) { logger.debug("Unknown exception", e); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, e.getMessage()); } - if (isReinitialize) { - scheduleRestartClient(REINITIALIZE_DELAY_SECONDS); + if (reinitialize >= 0) { + scheduleRestartClient(reinitialize); return true; } return false; diff --git a/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/handler/InnogyDeviceHandler.java b/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/handler/InnogyDeviceHandler.java index 6d41112c5f9b7..91d4a76e8f15f 100644 --- a/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/handler/InnogyDeviceHandler.java +++ b/bundles/org.openhab.binding.innogysmarthome/src/main/java/org/openhab/binding/innogysmarthome/internal/handler/InnogyDeviceHandler.java @@ -80,7 +80,7 @@ public InnogyDeviceHandler(final Thing thing) { public void handleCommand(final ChannelUID channelUID, final Command command) { logger.debug("handleCommand called for channel '{}' of type '{}' with command '{}'", channelUID, getThing().getThingTypeUID().getId(), command); - @Nullable final InnogyBridgeHandler innogyBridgeHandler = getInnogyBridgeHandler(); + final InnogyBridgeHandler innogyBridgeHandler = getInnogyBridgeHandler(); if (innogyBridgeHandler == null) { logger.warn("BridgeHandler not found. Cannot handle command without bridge."); return; @@ -91,7 +91,7 @@ public void handleCommand(final ChannelUID channelUID, final Command command) { } if (command instanceof RefreshType) { - @Nullable final Device device = innogyBridgeHandler.getDeviceById(deviceId); + final Device device = innogyBridgeHandler.getDeviceById(deviceId); if (device != null) { onDeviceStateChanged(device); } @@ -102,8 +102,8 @@ public void handleCommand(final ChannelUID channelUID, final Command command) { if (CHANNEL_SWITCH.equals(channelUID.getId())) { // DEBUGGING HELPER // ---------------- - @Nullable final Device device = innogyBridgeHandler.getDeviceById(deviceId); - if (device != null && DEBUG.equals(device.getConfig().getName())) { + final Device device = innogyBridgeHandler.getDeviceById(deviceId); + if (DEBUG.equals(device.getConfig().getName())) { logger.debug("DEBUG SWITCH ACTIVATED!"); if (OnOffType.ON.equals(command)) { innogyBridgeHandler.onEvent( @@ -244,7 +244,7 @@ private void initializeThing(@Nullable final ThingStatus bridgeStatus) { */ private boolean initializeProperties() { synchronized (this.lock) { - @Nullable final Device device = getDevice(); + final Device device = getDevice(); if (device != null) { final Map properties = editProperties(); properties.put(PROPERTY_ID, device.getId()); @@ -327,11 +327,11 @@ private boolean initializeProperties() { private @Nullable InnogyBridgeHandler getInnogyBridgeHandler() { synchronized (this.lock) { if (this.bridgeHandler == null) { - @Nullable final Bridge bridge = getBridge(); + final Bridge bridge = getBridge(); if (bridge == null) { return null; } - @Nullable final ThingHandler handler = bridge.getHandler(); + final ThingHandler handler = bridge.getHandler(); if (handler instanceof InnogyBridgeHandler) { this.bridgeHandler = (InnogyBridgeHandler) handler; this.bridgeHandler.registerDeviceStatusListener(this); @@ -356,7 +356,7 @@ public void onDeviceStateChanged(final Device device) { // DEVICE STATES if (device.hasDeviceState()) { - @Nullable Boolean reachable = null; + Boolean reachable = null; if (device.getDeviceState().hasIsReachableState()) { reachable = device.getDeviceState().isReachable(); } @@ -904,10 +904,10 @@ public void onDeviceStateChanged(final Device changedDevice, final Event event) } else { logger.debug("Unsupported capability type {}.", capability.getType()); } - } else { + } else { // capability.hasState() logger.debug("Capability {} has no state (yet?) - refreshing device.", capability.getName()); + final InnogyBridgeHandler innogyBridgeHandler = getInnogyBridgeHandler(); - @Nullable final InnogyBridgeHandler innogyBridgeHandler = getInnogyBridgeHandler(); if (innogyBridgeHandler != null) { device = innogyBridgeHandler.refreshDevice(deviceId); } @@ -915,6 +915,7 @@ public void onDeviceStateChanged(final Device changedDevice, final Event event) capabilityMap = device.getCapabilityMap(); capability = capabilityMap.get(linkedCapabilityId); if (capability.hasState()) { + capabilityState = capability.getCapabilityState(); deviceChanged = true; } } @@ -928,7 +929,9 @@ public void onDeviceStateChanged(final Device changedDevice, final Event event) onDeviceStateChanged(device); } else { logger.debug("Device {}/{} has no state.", device.getConfig().getName(), device.getId()); + return; } + } } } @@ -944,8 +947,8 @@ private int invertValueIfConfigured(final String channelId, final int value) { logger.debug("Channel {} cannot be inverted.", channelId); return value; } + final Channel channel = getThing().getChannel(channelId); - @Nullable final Channel channel = getThing().getChannel(channelId); if (channel == null) { logger.debug("Channel {} was null! Value not inverted.", channelId); return value;