Skip to content

Commit

Permalink
[iotawatt] configurable request timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Rosenberg <prosenb.dev@gmail.com>
  • Loading branch information
PRosenb committed May 7, 2024
1 parent fd9bfc4 commit a03441a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ public class IoTaWattConfiguration {
* The default refresh interval of the IoTaWatt device
*/
public static final int REFRESH_INTERVAL_DEFAULT = 10;
/**
* The default of the request timeout
*/
public static final long REQUEST_TIMEOUT_DEFAULT = 10;

/**
* Configuration parameters
*/
public String hostname = "";
/**
* The refresh interval of the IoTaWatt device
* The request timeout in seconds when fetching data from the IoTaWatt device
*/
public long requestTimeout = REQUEST_TIMEOUT_DEFAULT;
/**
* The refresh interval of the IoTaWatt device in seconds
*/
public int refreshIntervalInSeconds = REFRESH_INTERVAL_DEFAULT;
public int refreshInterval = REFRESH_INTERVAL_DEFAULT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public HttpClient getInsecureClient() {
}

@Override
public IoTaWattClient getIoTaWattClient(String hostname) {
return new IoTaWattClient(hostname, insecureClient, gson);
public IoTaWattClient getIoTaWattClient(String hostname, long requestTimeout) {
return new IoTaWattClient(hostname, requestTimeout, insecureClient, gson);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class IoTaWattClient {
* The hostname the IoTaWattClient connects to
*/
public final String hostname;
private final long requestTimeout;
private final HttpClient httpClient;
private final Gson gson;

Expand All @@ -59,8 +60,9 @@ public class IoTaWattClient {
* @param httpClient The HttpClient to use
* @param gson The Gson decoder to use
*/
public IoTaWattClient(String hostname, HttpClient httpClient, Gson gson) {
public IoTaWattClient(String hostname, long requestTimeout, HttpClient httpClient, Gson gson) {
this.httpClient = httpClient;
this.requestTimeout = requestTimeout;
this.hostname = hostname;
this.gson = gson;
}
Expand All @@ -75,7 +77,8 @@ public IoTaWattClient(String hostname, HttpClient httpClient, Gson gson) {
public Optional<StatusResponse> fetchStatus() throws ThingStatusOfflineException {
try {
final URI uri = new URI(String.format(REQUEST_URL, hostname));
final Request request = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(10, TimeUnit.SECONDS);
final Request request = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(requestTimeout,
TimeUnit.SECONDS);
final ContentResponse response = request.send();
final String content = response.getContentAsString();
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ public interface IoTaWattClientProvider {
* @param hostname The hostname of the IoTaWatt device
* @return The provided IoTaWattClient
*/
IoTaWattClient getIoTaWattClient(String hostname);
IoTaWattClient getIoTaWattClient(String hostname, long requestTimeout);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ public void handleCommand(ChannelUID channelUID, Command command) {
@Override
public void initialize() {
final IoTaWattConfiguration config = getConfigAs(IoTaWattConfiguration.class);
final IoTaWattClient ioTaWattClient = ioTaWattClientProvider.getIoTaWattClient(config.hostname);
final IoTaWattClient ioTaWattClient = ioTaWattClientProvider.getIoTaWattClient(config.hostname,
config.requestTimeout);
fetchDataService.setIoTaWattClient(ioTaWattClient);

updateStatus(ThingStatus.UNKNOWN);

fetchDataJob = scheduler.scheduleWithFixedDelay(fetchDataService::pollDevice, 0,
config.refreshIntervalInSeconds, TimeUnit.SECONDS);
fetchDataJob = scheduler.scheduleWithFixedDelay(fetchDataService::pollDevice, 0, config.refreshInterval,
TimeUnit.SECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<default>10</default>
<advanced>false</advanced>
</parameter>
<parameter name="requestTimeout" type="integer" unit="s" min="1">
<label>Request timeout</label>
<description>The request timeout to call the device in sec.</description>
<default>10</default>
<advanced>false</advanced>
</parameter>
</config-description>
</thing-type>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class IoTaWattClientTest {
void fetchStatus_whenValidJson_returnObject() throws ThingStatusOfflineException, IOException, ExecutionException,
InterruptedException, TimeoutException {
// given
final IoTaWattClient client = new IoTaWattClient("hostname", httpClient, gson);
final IoTaWattClient client = new IoTaWattClient("hostname", 10, httpClient, gson);
Request request = mock(Request.class);
when(httpClient.newRequest(any(URI.class))).thenReturn(request);
when(request.method(any(HttpMethod.class))).thenReturn(request);
Expand Down Expand Up @@ -91,7 +91,7 @@ void fetchStatus_whenValidJson_returnObject() throws ThingStatusOfflineException
@Test
void fetchStatus_whenWrongHost_throwException() {
// given
final IoTaWattClient client = new IoTaWattClient(" ", httpClient, mock(Gson.class));
final IoTaWattClient client = new IoTaWattClient(" ", 10, httpClient, mock(Gson.class));

// when
assertThrows(ThingStatusOfflineException.class, client::fetchStatus);
Expand All @@ -101,7 +101,7 @@ void fetchStatus_whenWrongHost_throwException() {
void fetchStatus_whenInputsAndOutputsEmpty_returnEmpty()
throws ThingStatusOfflineException, ExecutionException, InterruptedException, TimeoutException {
// given
final IoTaWattClient client = new IoTaWattClient("hostname", httpClient, gson);
final IoTaWattClient client = new IoTaWattClient("hostname", 10, httpClient, gson);
Request request = mock(Request.class);
when(httpClient.newRequest(any(URI.class))).thenReturn(request);
when(request.method(any(HttpMethod.class))).thenReturn(request);
Expand Down

0 comments on commit a03441a

Please sign in to comment.