Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3692 from watou/nest-timeout
Browse files Browse the repository at this point in the history
[nest] Added optional nest:timeout config parameter
  • Loading branch information
watou committed Jan 3, 2016
2 parents 0d8f827 + e625ffa commit 1d68b0f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Expand Up @@ -21,6 +21,7 @@
import java.util.prefs.Preferences;

import org.openhab.binding.nest.NestBindingProvider;
import org.openhab.binding.nest.internal.messages.AbstractRequest;
import org.openhab.binding.nest.internal.messages.AccessTokenRequest;
import org.openhab.binding.nest.internal.messages.AccessTokenResponse;
import org.openhab.binding.nest.internal.messages.DataModel;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class NestBinding extends AbstractActiveBinding<NestBindingProvider> impl
protected static final String CONFIG_CLIENT_ID = "client_id";
protected static final String CONFIG_CLIENT_SECRET = "client_secret";
protected static final String CONFIG_PIN_CODE = "pin_code";
protected static final String CONFIG_TIMEOUT = "timeout";

/**
* the refresh interval which is used to poll values from the Nest server (optional, defaults to 60000ms)
Expand Down Expand Up @@ -442,13 +444,20 @@ public void updated(Dictionary<String, ?> config) throws ConfigurationException
refreshInterval = Long.parseLong(refreshIntervalString);
}

// to override the default HTTP request timeout one has to add a
// parameter to openhab.cfg like nest:timeout=20000
String timeoutString = (String) config.get(CONFIG_TIMEOUT);
if (isNotBlank(timeoutString)) {
AbstractRequest.setHttpRequestTimeout(Integer.parseInt(timeoutString));
}

Enumeration<String> configKeys = config.keys();
while (configKeys.hasMoreElements()) {
String configKey = (String) configKeys.nextElement();

// the config-key enumeration contains additional keys that we
// don't want to process here ...
if (CONFIG_REFRESH.equals(configKey) || "service.pid".equals(configKey)) {
if (CONFIG_REFRESH.equals(configKey) || CONFIG_TIMEOUT.equals(configKey) || "service.pid".equals(configKey)) {
continue;
}

Expand Down
Expand Up @@ -50,7 +50,7 @@ public abstract class AbstractRequest extends AbstractMessage implements Request

private static final Properties HTTP_HEADERS;

private static final int HTTP_REQUEST_TIMEOUT = 10000;
private static final int DEFAULT_HTTP_REQUEST_TIMEOUT = 10000;

protected static final String HTTP_GET = "GET";

Expand All @@ -62,6 +62,8 @@ public abstract class AbstractRequest extends AbstractMessage implements Request

protected static final ObjectMapper JSON = new ObjectMapper();

protected static int httpRequestTimeout = DEFAULT_HTTP_REQUEST_TIMEOUT;

static {
HTTP_HEADERS = new Properties();
HTTP_HEADERS.put("Accept", "application/json");
Expand All @@ -76,6 +78,10 @@ public abstract class AbstractRequest extends AbstractMessage implements Request
JSON.setDateFormat(df);
}

public static void setHttpRequestTimeout(int timeout) {
httpRequestTimeout = timeout;
}

protected final RuntimeException newException(final String message, final Exception cause, final String url,
final String json) {
if (cause instanceof JsonMappingException) {
Expand Down Expand Up @@ -106,7 +112,7 @@ protected final String executeUrl(final String httpMethod, final String url, fin
HttpClient client = new HttpClient();

HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url);
method.getParams().setSoTimeout(HTTP_REQUEST_TIMEOUT);
method.getParams().setSoTimeout(httpRequestTimeout);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));

for (String httpHeaderKey : HTTP_HEADERS.stringPropertyNames()) {
Expand Down
3 changes: 3 additions & 0 deletions distribution/openhabhome/configurations/openhab_default.cfg
Expand Up @@ -1924,6 +1924,9 @@ tcp:refreshinterval=250
# Data refresh interval in ms (optional, defaults to 60000)
#nest:refresh=

# HTTP request timeout in ms (optional, defaults to 10000)
#nest:timeout=

# the Nest Client ID needed to use the API, must be supplied
#nest:client_id=

Expand Down

0 comments on commit 1d68b0f

Please sign in to comment.