*/
package org.openhab.binding.opensprinkler.internal.api;
-import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.*;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.CMD_DISABLE_MANUAL_MODE;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.CMD_ENABLE_MANUAL_MODE;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.CMD_OPTIONS_INFO;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.CMD_PASSWORD;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.CMD_STATION_INFO;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.CMD_STATUS_INFO;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.DEFAULT_STATION_COUNT;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.HTTPS_REQUEST_URL_PREFIX;
+import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.HTTP_REQUEST_URL_PREFIX;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
} else {
location = url;
}
- ContentResponse response;
- try {
- response = withGeneralProperties(httpClient.newRequest(location)).timeout(5, TimeUnit.SECONDS)
- .method(HttpMethod.GET).send();
- } catch (InterruptedException | TimeoutException | ExecutionException e) {
- throw new CommunicationApiException("Request to OpenSprinkler device failed: " + e.getMessage());
+ ContentResponse response = null;
+ int retriesLeft = Math.max(1, config.retry);
+ boolean connectionSuccess = false;
+ while (connectionSuccess == false && retriesLeft > 0) {
+ retriesLeft--;
+ try {
+ response = withGeneralProperties(httpClient.newRequest(location))
+ .timeout(config.timeout, TimeUnit.SECONDS).method(HttpMethod.GET).send();
+ connectionSuccess = true;
+ } catch (InterruptedException | TimeoutException | ExecutionException e) {
+ logger.warn("Request to OpenSprinkler device failed (retries left: {}): {}", retriesLeft,
+ e.getMessage());
+ }
}
- if (response.getStatus() != HTTP_OK_CODE) {
+ if (connectionSuccess == false) {
+ throw new CommunicationApiException("Request to OpenSprinkler device failed");
+ }
+ if (response != null && response.getStatus() != HTTP_OK_CODE) {
throw new CommunicationApiException(
"Error sending HTTP GET request to " + url + ". Got response code: " + response.getStatus());
+ } else if (response != null) {
+ String content = response.getContentAsString();
+ if ("{\"result\":2}".equals(content)) {
+ throw new UnauthorizedApiException("Unauthorized, check your password is correct");
+ }
+ return content;
}
- String content = response.getContentAsString();
- if ("{\"result\":2}".equals(content)) {
- throw new UnauthorizedApiException("Unauthorized, check your password is correct");
- }
- return content;
+ return "";
}
private Request withGeneralProperties(Request request) {
thing-type.config.opensprinkler.http.port.description = Port of the OpenSprinkler Web API interface.
thing-type.config.opensprinkler.http.refresh.label = Refresh Interval
thing-type.config.opensprinkler.http.refresh.description = Specifies the refresh interval in seconds.
+thing-type.config.opensprinkler.http.retry.label = Retry Count
+thing-type.config.opensprinkler.http.retry.description = Specifies the number of retries on connection timeouts.
+thing-type.config.opensprinkler.http.timeout.label = Timeout
+thing-type.config.opensprinkler.http.timeout.description = Specifies the connection timeout in seconds.
thing-type.config.opensprinkler.station.stationIndex.label = Station Index
thing-type.config.opensprinkler.station.stationIndex.description = The index of the station, starting with 0, of the station.
<description>Specifies the refresh interval in seconds.</description>
<default>60</default>
</parameter>
+ <parameter name="retry" type="integer">
+ <label>Retry Count</label>
+ <description>Specifies the number of retries on connection timeouts.</description>
+ <default>3</default>
+ <advanced>true</advanced>
+ </parameter>
+ <parameter name="timeout" type="integer" unit="s">
+ <label>Timeout</label>
+ <description>Specifies the connection timeout in seconds.</description>
+ <default>5</default>
+ <advanced>true</advanced>
+ </parameter>
<parameter name="basicUsername" type="text">
<label>Basic Auth Username</label>
<description>Used if the OpenSprinkler device is behind a basic auth protected reverse proxy.</description>