]> git.basschouten.com Git - openhab-addons.git/commitdiff
[haywardomnilogic] Add http timeout retry (#13164)
authorMatt <mmyers75@icloud.com>
Wed, 27 Jul 2022 08:31:40 +0000 (04:31 -0400)
committerGitHub <noreply@github.com>
Wed, 27 Jul 2022 08:31:40 +0000 (10:31 +0200)
* Add HTTP Timeout Retry
* Updated try/catch messaging
* Repositioned some logging to occur before http request

Signed-off-by: Matt Myers <mmyers75@icloud.com>
bundles/org.openhab.binding.haywardomnilogic/src/main/java/org/openhab/binding/haywardomnilogic/internal/handler/HaywardBridgeHandler.java

index 038c2c8aba14074c983147f3b3fa650392a712a1..6440d6911f5ef17e7eaade8935ce859c2dcb7980 100644 (file)
@@ -437,48 +437,59 @@ public class HaywardBridgeHandler extends BaseBridgeHandler {
         String urlParameterslength = Integer.toString(urlParameters.length());
         String statusMessage;
 
-        try {
-            ContentResponse httpResponse = sendRequestBuilder(config.endpointUrl, HttpMethod.POST)
-                    .content(new StringContentProvider(urlParameters), "text/xml; charset=utf-8")
-                    .header(HttpHeader.CONTENT_LENGTH, urlParameterslength).send();
-
-            int status = httpResponse.getStatus();
-            String xmlResponse = httpResponse.getContentAsString();
-
-            if (status == 200) {
-                List<String> statusMessages = evaluateXPath(
-                        "/Response/Parameters//Parameter[@name='StatusMessage']/text()", xmlResponse);
-                if (!(statusMessages.isEmpty())) {
-                    statusMessage = statusMessages.get(0);
-                } else {
-                    statusMessage = httpResponse.getReason();
-                }
+        if (logger.isTraceEnabled()) {
+            logger.trace("Hayward Connection thing:  {} Hayward http command: {}", getCallingMethod(), urlParameters);
+        } else if (logger.isDebugEnabled()) {
+            logger.debug("Hayward Connection thing:  {}", getCallingMethod());
+        }
 
-                if (logger.isTraceEnabled()) {
-                    logger.trace("Hayward Connection thing:  {} Hayward http command: {}", getCallingMethod(),
-                            urlParameters);
-                    logger.trace("Hayward Connection thing:  {} Hayward http response: {} {}", getCallingMethod(),
-                            statusMessage, xmlResponse);
-                }
-                return xmlResponse;
-            } else {
-                if (logger.isDebugEnabled()) {
-                    logger.debug("Hayward Connection thing:  {} Hayward http command: {}", getCallingMethod(),
-                            urlParameters);
-                    logger.debug("Hayward Connection thing:  {} Hayward http response: {} {}", getCallingMethod(),
-                            status, xmlResponse);
+        for (int retry = 0; retry <= 2; retry++) {
+            try {
+                ContentResponse httpResponse = sendRequestBuilder(config.endpointUrl, HttpMethod.POST)
+                        .content(new StringContentProvider(urlParameters), "text/xml; charset=utf-8")
+                        .header(HttpHeader.CONTENT_LENGTH, urlParameterslength).send();
+
+                int status = httpResponse.getStatus();
+                String xmlResponse = httpResponse.getContentAsString();
+
+                if (status == 200) {
+                    List<String> statusMessages = evaluateXPath(
+                            "/Response/Parameters//Parameter[@name='StatusMessage']/text()", xmlResponse);
+                    if (!(statusMessages.isEmpty())) {
+                        statusMessage = statusMessages.get(0);
+                    } else {
+                        statusMessage = httpResponse.getReason();
+                    }
+
+                    if (logger.isTraceEnabled()) {
+                        logger.trace("Hayward Connection thing:  {} Hayward http response: {} {}", getCallingMethod(),
+                                statusMessage, xmlResponse);
+                    }
+                    return xmlResponse;
+                } else {
+                    if (logger.isDebugEnabled()) {
+                        logger.debug("Hayward Connection thing:  {} Hayward http response: {} {}", getCallingMethod(),
+                                status, xmlResponse);
+                    }
+                    return "";
                 }
+            } catch (ExecutionException e) {
+                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
+                        "Unable to resolve host.  Check Hayward hostname and your internet connection. "
+                                + e.getMessage());
                 return "";
+            } catch (TimeoutException e) {
+                if (retry >= 2) {
+                    updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
+                            "Connection Timeout.  Check Hayward hostname and your internet connection. "
+                                    + e.getMessage());
+                    return "";
+                } else {
+                    logger.warn("Hayward Connection thing Timeout:  {} Try:  {} ", getCallingMethod(), retry + 1);
+                }
             }
-        } catch (ExecutionException e) {
-            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                    "Unable to resolve host.  Check Hayward hostname and your internet connection. " + e);
-            return "";
-        } catch (TimeoutException e) {
-            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                    "Connection Timeout.  Check Hayward hostname and your internet connection. " + e);
-            return "";
         }
+        return "";
     }
 
     private String getCallingMethod() {