Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unifi] Make request timeout configurable to deal with slower devices like Unifi Express #18349

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions bundles/org.openhab.binding.unifi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ It requires a network accessible instance of the [Ubiquiti Networks Controller S

The following table describes the Bridge configuration parameters:

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

## Thing Configuration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class UniFiControllerThingConfig {

private int refresh = 10;

private int timeoutSeconds = 5;

private boolean unifios = false;

public String getHost() {
Expand Down Expand Up @@ -82,6 +84,22 @@ private void setRefresh(final int refresh) {
this.refresh = refresh;
}

public int getTimeoutSeconds() {
return timeoutSeconds;
}

public void setTimeoutSeconds(int timeoutSeconds) {
this.timeoutSeconds = timeoutSeconds;
}

public boolean isUnifios() {
return unifios;
}
Comment on lines +95 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated, see lines 103-105.


public void setUnifios(boolean unifios) {
this.unifios = unifios;
}
Comment on lines +99 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated, see lines 107-109.


public boolean isUniFiOS() {
return unifios;
}
Expand All @@ -98,6 +116,7 @@ public boolean isValid() {
@Override
public String toString() {
return "UniFiControllerConfig{host = " + host + ", port = " + port + ", username = " + username
+ ", password = *****, refresh = " + refresh + ", unifios = " + unifios + "}";
+ ", password = *****, refresh = " + refresh + ", timeout = " + timeoutSeconds + ", unifios = "
+ unifios + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,21 @@ public class UniFiController {
private final String username;
private final String password;
private final boolean unifios;
private final int timeoutSeconds;
private final Gson gson;
private final Gson poeGson;

private String csrfToken;

public UniFiController(final HttpClient httpClient, final String host, final int port, final String username,
final String password, final boolean unifios) {
final String password, final boolean unifios, int timeoutSeconds) {
this.httpClient = httpClient;
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.unifios = unifios;
this.timeoutSeconds = timeoutSeconds;
this.csrfToken = "";
final UniFiSiteInstanceCreator siteInstanceCreator = new UniFiSiteInstanceCreator(cache);
final UniFiWlanInstanceCreator wlanInstanceCreator = new UniFiWlanInstanceCreator(cache);
Expand Down Expand Up @@ -271,7 +273,8 @@ public void revokeVouchers(final UniFiSite site, final List<UniFiVoucher> vouche

private <T> UniFiControllerRequest<T> newRequest(final Class<T> responseType, final HttpMethod method,
final Gson gson) {
return new UniFiControllerRequest<>(responseType, gson, httpClient, method, host, port, csrfToken, unifios);
return new UniFiControllerRequest<>(responseType, gson, httpClient, method, host, port, csrfToken, unifios,
timeoutSeconds);
}

private <T> @Nullable T executeRequest(final UniFiControllerRequest<T> request) throws UniFiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class UniFiControllerRequest<T> {

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

private static final long TIMEOUT_SECONDS = 5;

private static final String PROPERTY_DATA = "data";

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

private final boolean unifios;
private final int timeoutSeconds;

private final HttpMethod method;

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

public UniFiControllerRequest(final Class<T> resultType, final Gson gson, final HttpClient httpClient,
final HttpMethod method, final String host, final int port, final String csrfToken, final boolean unifios) {
final HttpMethod method, final String host, final int port, final String csrfToken, final boolean unifios,
int timeoutSeconds) {
this.resultType = resultType;
this.gson = gson;
this.httpClient = httpClient;
Expand All @@ -104,6 +104,7 @@ public UniFiControllerRequest(final Class<T> resultType, final Gson gson, final
this.port = port;
this.csrfToken = csrfToken;
this.unifios = unifios;
this.timeoutSeconds = timeoutSeconds;
}

public void setAPIPath(final String relativePath) {
Expand Down Expand Up @@ -189,7 +190,7 @@ private Response getContentResponse(final InputStreamResponseListener listener)
Response response;
try {
request.send(listener);
response = listener.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
response = listener.get(timeoutSeconds, TimeUnit.SECONDS);
} catch (TimeoutException | InterruptedException e) {
throw new UniFiCommunicationException(e);
} catch (final ExecutionException e) {
Expand Down Expand Up @@ -237,7 +238,7 @@ public String getCsrfToken() {

private Request newRequest() {
final HttpURI uri = new HttpURI(HttpScheme.HTTPS.asString(), host, port, path);
final Request request = httpClient.newRequest(uri.toString()).timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
final Request request = httpClient.newRequest(uri.toString()).timeout(timeoutSeconds, TimeUnit.SECONDS)
.method(method);
for (final Entry<String, String> entry : queryParameters.entrySet()) {
request.param(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void initialize() {
config = getConfigAs(UniFiControllerThingConfig.class);
logger.debug("Initializing the UniFi Controller Handler with config = {}", config);
final UniFiController uc = new UniFiController(httpClient, config.getHost(), config.getPort(),
config.getUsername(), config.getPassword(), config.isUniFiOS());
config.getUsername(), config.getPassword(), config.isUniFiOS(), config.getTimeoutSeconds());

controller = uc;
updateStatus(UNKNOWN);
Expand Down