Skip to content

Commit cde8814

Browse files
authored
[unifi] Make request timeout configurable (#18349)
* Make http timeout configurable Signed-off-by: Arne Seime <[email protected]>
1 parent a766c72 commit cde8814

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

bundles/org.openhab.binding.unifi/README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ It requires a network accessible instance of the [Ubiquiti Networks Controller S
2828

2929
The following table describes the Bridge configuration parameters:
3030

31-
| Parameter | Description | Config | Default |
32-
| ------------------------ | --------------------------------------------------------------------------- |--------- | ------- |
33-
| host | Hostname of IP address of the UniFi Controller | Required | - |
34-
| port | Port of the UniFi Controller. For UniFi OS, the default port is usually 443 | Required | - |
35-
| unifios | If the UniFi Controller is running on UniFi OS | Required | false |
36-
| username | The username to access the UniFi Controller | Required | - |
37-
| password | The password to access the UniFi Controller | Required | - |
38-
| refresh | Refresh interval in seconds | Optional | 10 |
31+
| Parameter | Description | Config | Default |
32+
|----------------|-----------------------------------------------------------------------------|--------- |---------|
33+
| host | Hostname of IP address of the UniFi Controller | Required | - |
34+
| port | Port of the UniFi Controller. For UniFi OS, the default port is usually 443 | Required | - |
35+
| unifios | If the UniFi Controller is running on UniFi OS | Required | false |
36+
| username | The username to access the UniFi Controller | Required | - |
37+
| password | The password to access the UniFi Controller | Required | - |
38+
| refresh | Refresh interval in seconds | Optional | 10 |
39+
| timeoutSeconds | Request timeout in seconds. Increase if you experience TimeoutExceptions | Optional | 5 |
3940

4041
## Thing Configuration
4142

bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/UniFiControllerThingConfig.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class UniFiControllerThingConfig {
3535

3636
private int refresh = 10;
3737

38+
private int timeoutSeconds = 5;
39+
3840
private boolean unifios = false;
3941

4042
public String getHost() {
@@ -82,6 +84,14 @@ private void setRefresh(final int refresh) {
8284
this.refresh = refresh;
8385
}
8486

87+
public int getTimeoutSeconds() {
88+
return timeoutSeconds;
89+
}
90+
91+
public void setTimeoutSeconds(int timeoutSeconds) {
92+
this.timeoutSeconds = timeoutSeconds;
93+
}
94+
8595
public boolean isUniFiOS() {
8696
return unifios;
8797
}
@@ -98,6 +108,7 @@ public boolean isValid() {
98108
@Override
99109
public String toString() {
100110
return "UniFiControllerConfig{host = " + host + ", port = " + port + ", username = " + username
101-
+ ", password = *****, refresh = " + refresh + ", unifios = " + unifios + "}";
111+
+ ", password = *****, refresh = " + refresh + ", timeout = " + timeoutSeconds + ", unifios = "
112+
+ unifios + "}";
102113
}
103114
}

bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/api/UniFiController.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,21 @@ public class UniFiController {
7070
private final String username;
7171
private final String password;
7272
private final boolean unifios;
73+
private final int timeoutSeconds;
7374
private final Gson gson;
7475
private final Gson poeGson;
7576

7677
private String csrfToken;
7778

7879
public UniFiController(final HttpClient httpClient, final String host, final int port, final String username,
79-
final String password, final boolean unifios) {
80+
final String password, final boolean unifios, int timeoutSeconds) {
8081
this.httpClient = httpClient;
8182
this.host = host;
8283
this.port = port;
8384
this.username = username;
8485
this.password = password;
8586
this.unifios = unifios;
87+
this.timeoutSeconds = timeoutSeconds;
8688
this.csrfToken = "";
8789
final UniFiSiteInstanceCreator siteInstanceCreator = new UniFiSiteInstanceCreator(cache);
8890
final UniFiWlanInstanceCreator wlanInstanceCreator = new UniFiWlanInstanceCreator(cache);
@@ -271,7 +273,8 @@ public void revokeVouchers(final UniFiSite site, final List<UniFiVoucher> vouche
271273

272274
private <T> UniFiControllerRequest<T> newRequest(final Class<T> responseType, final HttpMethod method,
273275
final Gson gson) {
274-
return new UniFiControllerRequest<>(responseType, gson, httpClient, method, host, port, csrfToken, unifios);
276+
return new UniFiControllerRequest<>(responseType, gson, httpClient, method, host, port, csrfToken, unifios,
277+
timeoutSeconds);
275278
}
276279

277280
private <T> @Nullable T executeRequest(final UniFiControllerRequest<T> request) throws UniFiException {

bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/api/UniFiControllerRequest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class UniFiControllerRequest<T> {
6464

6565
private static final String CONTENT_TYPE_APPLICATION_JSON_UTF_8 = MimeTypes.Type.APPLICATION_JSON_UTF_8.asString();
6666

67-
private static final long TIMEOUT_SECONDS = 5;
68-
6967
private static final String PROPERTY_DATA = "data";
7068

7169
private final Logger logger = LoggerFactory.getLogger(UniFiControllerRequest.class);
@@ -79,6 +77,7 @@ class UniFiControllerRequest<T> {
7977
private final int port;
8078

8179
private final boolean unifios;
80+
private final int timeoutSeconds;
8281

8382
private final HttpMethod method;
8483

@@ -95,7 +94,8 @@ class UniFiControllerRequest<T> {
9594
// Public API
9695

9796
public UniFiControllerRequest(final Class<T> resultType, final Gson gson, final HttpClient httpClient,
98-
final HttpMethod method, final String host, final int port, final String csrfToken, final boolean unifios) {
97+
final HttpMethod method, final String host, final int port, final String csrfToken, final boolean unifios,
98+
int timeoutSeconds) {
9999
this.resultType = resultType;
100100
this.gson = gson;
101101
this.httpClient = httpClient;
@@ -104,6 +104,7 @@ public UniFiControllerRequest(final Class<T> resultType, final Gson gson, final
104104
this.port = port;
105105
this.csrfToken = csrfToken;
106106
this.unifios = unifios;
107+
this.timeoutSeconds = timeoutSeconds;
107108
}
108109

109110
public void setAPIPath(final String relativePath) {
@@ -189,7 +190,7 @@ private Response getContentResponse(final InputStreamResponseListener listener)
189190
Response response;
190191
try {
191192
request.send(listener);
192-
response = listener.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
193+
response = listener.get(timeoutSeconds, TimeUnit.SECONDS);
193194
} catch (TimeoutException | InterruptedException e) {
194195
throw new UniFiCommunicationException(e);
195196
} catch (final ExecutionException e) {
@@ -237,7 +238,7 @@ public String getCsrfToken() {
237238

238239
private Request newRequest() {
239240
final HttpURI uri = new HttpURI(HttpScheme.HTTPS.asString(), host, port, path);
240-
final Request request = httpClient.newRequest(uri.toString()).timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
241+
final Request request = httpClient.newRequest(uri.toString()).timeout(timeoutSeconds, TimeUnit.SECONDS)
241242
.method(method);
242243
for (final Entry<String, String> entry : queryParameters.entrySet()) {
243244
request.param(entry.getKey(), entry.getValue());

bundles/org.openhab.binding.unifi/src/main/java/org/openhab/binding/unifi/internal/handler/UniFiControllerThingHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void initialize() {
8888
config = getConfigAs(UniFiControllerThingConfig.class);
8989
logger.debug("Initializing the UniFi Controller Handler with config = {}", config);
9090
final UniFiController uc = new UniFiController(httpClient, config.getHost(), config.getPort(),
91-
config.getUsername(), config.getPassword(), config.isUniFiOS());
91+
config.getUsername(), config.getPassword(), config.isUniFiOS(), config.getTimeoutSeconds());
9292

9393
controller = uc;
9494
updateStatus(UNKNOWN);

bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/config/config.xml

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
<description>The refresh interval in seconds to poll the UniFi controller</description>
3636
<default>10</default>
3737
</parameter>
38+
<parameter name="timeoutSeconds" type="integer" required="false">
39+
<label>Connection Timeout</label>
40+
<description>Connection timeout in seconds for API calls to the UniFi Controller.</description>
41+
<default>5</default>
42+
<advanced>true</advanced>
43+
</parameter>
3844
</config-description>
3945

4046
<config-description uri="thing-type:unifi:site">

bundles/org.openhab.binding.unifi/src/main/resources/OH-INF/i18n/unifi.properties

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ thing-type.config.unifi.controller.port.label = Port
4848
thing-type.config.unifi.controller.port.description = Port of the UniFi Controller
4949
thing-type.config.unifi.controller.refresh.label = Refresh Interval
5050
thing-type.config.unifi.controller.refresh.description = The refresh interval in seconds to poll the UniFi controller
51+
thing-type.config.unifi.controller.timeoutSeconds.label = Connection Timeout
52+
thing-type.config.unifi.controller.timeoutSeconds.description = Connection timeout in seconds for API calls to the UniFi Controller.
5153
thing-type.config.unifi.controller.unifios.label = UniFi OS
5254
thing-type.config.unifi.controller.unifios.description = If the UniFi Controller is running on UniFi OS.
5355
thing-type.config.unifi.controller.username.label = Username

0 commit comments

Comments
 (0)