[bindings a-c] Fix exception handling (Jetty HTTP client) (#10467)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-04-06 17:30:12 +02:00 committed by GitHub
parent a509c3b638
commit fed460218e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 148 additions and 114 deletions

View File

@ -18,8 +18,10 @@ import java.util.Base64;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -190,6 +192,7 @@ public class AutelisHandler extends BaseThingHandler {
@Override @Override
public void handleCommand(ChannelUID channelUID, Command command) { public void handleCommand(ChannelUID channelUID, Command command) {
try {
logger.debug("handleCommand channel: {} command: {}", channelUID.getId(), command); logger.debug("handleCommand channel: {} command: {}", channelUID.getId(), command);
if (AutelisBindingConstants.CMD_LIGHTS.equals(channelUID.getId())) { if (AutelisBindingConstants.CMD_LIGHTS.equals(channelUID.getId())) {
/* /*
@ -228,7 +231,8 @@ public class AutelisHandler extends BaseThingHandler {
logger.error("command type {} is not supported", command); logger.error("command type {} is not supported", command);
return; return;
} }
String response = getUrl(baseURL + "/set.cgi?name=" + name + "&" + cmd + "=" + value, TIMEOUT_SECONDS); String response = getUrl(baseURL + "/set.cgi?name=" + name + "&" + cmd + "=" + value,
TIMEOUT_SECONDS);
logger.debug("equipment set {} {} {} : result {}", name, cmd, value, response); logger.debug("equipment set {} {} {} : result {}", name, cmd, value, response);
} else if (AutelisBindingConstants.CMD_TEMP.equals(type)) { } else if (AutelisBindingConstants.CMD_TEMP.equals(type)) {
String value; String value;
@ -270,8 +274,12 @@ public class AutelisHandler extends BaseThingHandler {
} }
} }
clearState(true); clearState(true);
// reset the schedule for our next poll which at that time will reflect if our command was successful or not. // reset the schedule for our next poll which at that time will reflect if our command was successful or
// not.
initPolling(COMMAND_UPDATE_TIME_SECONDS); initPolling(COMMAND_UPDATE_TIME_SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} }
/** /**
@ -349,7 +357,7 @@ public class AutelisHandler extends BaseThingHandler {
* Poll the Autelis controller for updates. This will retrieve various xml documents and update channel states from * Poll the Autelis controller for updates. This will retrieve various xml documents and update channel states from
* its contents. * its contents.
*/ */
private void pollAutelisController() { private void pollAutelisController() throws InterruptedException {
logger.trace("Connecting to {}", baseURL); logger.trace("Connecting to {}", baseURL);
// clear our cached stated IF it is time. // clear our cached stated IF it is time.
@ -466,16 +474,13 @@ public class AutelisHandler extends BaseThingHandler {
* @param timeout * @param timeout
* @return * @return
*/ */
private synchronized String getUrl(String url, int timeout) { private synchronized String getUrl(String url, int timeout) throws InterruptedException {
// throttle commands for a very short time to avoid 'loosing' them // throttle commands for a very short time to avoid 'loosing' them
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long nextReq = lastRequestTime + THROTTLE_TIME_MILLISECONDS; long nextReq = lastRequestTime + THROTTLE_TIME_MILLISECONDS;
if (nextReq > now) { if (nextReq > now) {
try {
logger.trace("Throttling request for {} mills", nextReq - now); logger.trace("Throttling request for {} mills", nextReq - now);
Thread.sleep(nextReq - now); Thread.sleep(nextReq - now);
} catch (InterruptedException ignored) {
}
} }
String getURL = url + (url.contains("?") ? "&" : "?") + "timestamp=" + System.currentTimeMillis(); String getURL = url + (url.contains("?") ? "&" : "?") + "timestamp=" + System.currentTimeMillis();
logger.trace("Getting URL {} ", getURL); logger.trace("Getting URL {} ", getURL);
@ -490,7 +495,7 @@ public class AutelisHandler extends BaseThingHandler {
} }
lastRequestTime = System.currentTimeMillis(); lastRequestTime = System.currentTimeMillis();
return response.getContentAsString(); return response.getContentAsString();
} catch (Exception e) { } catch (ExecutionException | TimeoutException e) {
logger.debug("Could not make http connection", e); logger.debug("Could not make http connection", e);
} }
return null; return null;

View File

@ -90,7 +90,10 @@ public class AutomowerConnectApi extends HusqvarnaApi {
ContentResponse response; ContentResponse response;
try { try {
response = request.send(); response = request.send();
} catch (InterruptedException | TimeoutException | ExecutionException e) { } catch (TimeoutException | ExecutionException e) {
throw new AutomowerCommunicationException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AutomowerCommunicationException(e); throw new AutomowerCommunicationException(e);
} }
return response; return response;

View File

@ -242,9 +242,13 @@ public class FritzAhaWebInterface {
String content = contentResponse.getContentAsString(); String content = contentResponse.getContentAsString();
logger.debug("GET response complete: {}", content); logger.debug("GET response complete: {}", content);
return content; return content;
} catch (ExecutionException | InterruptedException | TimeoutException e) { } catch (ExecutionException | TimeoutException e) {
logger.debug("response failed: {}", e.getLocalizedMessage(), e); logger.debug("response failed: {}", e.getLocalizedMessage(), e);
return null; return null;
} catch (InterruptedException e) {
logger.debug("response interrupted: {}", e.getLocalizedMessage(), e);
Thread.currentThread().interrupt();
return null;
} }
} }

View File

@ -144,10 +144,15 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
if (deviceService.affectedChannels.contains(channelUID.getIdWithoutGroup())) { if (deviceService.affectedChannels.contains(channelUID.getIdWithoutGroup())) {
try { try {
deviceService.service.refreshState(); deviceService.service.refreshState();
} catch (InterruptedException | TimeoutException | ExecutionException | BoschSHCException e) { } catch (TimeoutException | ExecutionException | BoschSHCException e) {
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
String.format("Error when trying to refresh state from service %s: %s", String.format("Error when trying to refresh state from service %s: %s",
deviceService.service.getServiceName(), e.getMessage())); deviceService.service.getServiceName(), e.getMessage()));
} catch (InterruptedException e) {
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
String.format("Interrupted refresh state from service %s: %s",
deviceService.service.getServiceName(), e.getMessage()));
Thread.currentThread().interrupt();
} }
} }
} }
@ -210,10 +215,15 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
try { try {
BoschSHCBridgeHandler bridgeHandler = this.getBridgeHandler(); BoschSHCBridgeHandler bridgeHandler = this.getBridgeHandler();
return bridgeHandler.getState(deviceId, stateName, classOfT); return bridgeHandler.getState(deviceId, stateName, classOfT);
} catch (InterruptedException | TimeoutException | ExecutionException | BoschSHCException e) { } catch (TimeoutException | ExecutionException | BoschSHCException e) {
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
String.format("Error when trying to refresh state from service %s: %s", stateName, e.getMessage())); String.format("Error when trying to refresh state from service %s: %s", stateName, e.getMessage()));
return null; return null;
} catch (InterruptedException e) {
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
String.format("Interrupted refresh state from service %s: %s", stateName, e.getMessage()));
Thread.currentThread().interrupt();
return null;
} }
} }
@ -279,9 +289,13 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
TService service, TState state) { TService service, TState state) {
try { try {
service.setState(state); service.setState(state);
} catch (InterruptedException | TimeoutException | ExecutionException e) { } catch (TimeoutException | ExecutionException e) {
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, String.format( this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, String.format(
"Error when trying to update state for service %s: %s", service.getServiceName(), e.getMessage())); "Error when trying to update state for service %s: %s", service.getServiceName(), e.getMessage()));
} catch (InterruptedException e) {
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, String
.format("Interrupted update state for service %s: %s", service.getServiceName(), e.getMessage()));
Thread.currentThread().interrupt();
} }
} }

View File

@ -12,8 +12,7 @@
*/ */
package org.openhab.binding.boschshc.internal.devices.bridge; package org.openhab.binding.boschshc.internal.devices.bridge;
import static org.eclipse.jetty.http.HttpMethod.GET; import static org.eclipse.jetty.http.HttpMethod.*;
import static org.eclipse.jetty.http.HttpMethod.PUT;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
@ -30,7 +29,10 @@ import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler; import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler;
import org.openhab.binding.boschshc.internal.devices.bridge.dto.*; import org.openhab.binding.boschshc.internal.devices.bridge.dto.Device;
import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceStatusUpdate;
import org.openhab.binding.boschshc.internal.devices.bridge.dto.LongPollResult;
import org.openhab.binding.boschshc.internal.devices.bridge.dto.Room;
import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException; import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
import org.openhab.binding.boschshc.internal.exceptions.LongPollingFailedException; import org.openhab.binding.boschshc.internal.exceptions.LongPollingFailedException;
import org.openhab.binding.boschshc.internal.exceptions.PairingFailedException; import org.openhab.binding.boschshc.internal.exceptions.PairingFailedException;
@ -233,6 +235,7 @@ public class BoschSHCBridgeHandler extends BaseBridgeHandler {
} catch (InterruptedException e) { } catch (InterruptedException e) {
this.updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.UNKNOWN.NONE, "@text/offline.interrupted"); this.updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.UNKNOWN.NONE, "@text/offline.interrupted");
Thread.currentThread().interrupt();
} }
} }

View File

@ -116,10 +116,15 @@ public class LongPolling {
logger.debug("Subscribe: Got subscription ID: {} {}", response.getResult(), response.getJsonrpc()); logger.debug("Subscribe: Got subscription ID: {} {}", response.getResult(), response.getJsonrpc());
String subscriptionId = response.getResult(); String subscriptionId = response.getResult();
return subscriptionId; return subscriptionId;
} catch (TimeoutException | ExecutionException | InterruptedException e) { } catch (TimeoutException | ExecutionException e) {
throw new LongPollingFailedException( throw new LongPollingFailedException(
String.format("Error on subscribe (Http client: %s): %s", httpClient.toString(), e.getMessage()), String.format("Error on subscribe (Http client: %s): %s", httpClient.toString(), e.getMessage()),
e); e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new LongPollingFailedException(
String.format("Interrupted subscribe (Http client: %s): %s", httpClient.toString(), e.getMessage()),
e);
} }
} }