[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
6 changed files with 148 additions and 114 deletions

View File

@@ -56,7 +56,7 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
class DeviceService<TState extends BoschSHCServiceState> {
/**
* Constructor.
*
*
* @param service Service which belongs to the device.
* @param affectedChannels Channels which are affected by the state of this service.
*/
@@ -99,7 +99,7 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
/**
* Returns the unique id of the Bosch device.
*
*
* @return Unique id of the Bosch device.
*/
public @Nullable String getBoschID() {
@@ -132,7 +132,7 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
/**
* Handles the refresh command of all registered services. Override it to handle custom commands (e.g. to update
* states of services).
*
*
* @param channelUID {@link ChannelUID} of the channel to which the command was sent
* @param command {@link Command}
*/
@@ -144,10 +144,15 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
if (deviceService.affectedChannels.contains(channelUID.getIdWithoutGroup())) {
try {
deviceService.service.refreshState();
} catch (InterruptedException | TimeoutException | ExecutionException | BoschSHCException e) {
} catch (TimeoutException | ExecutionException | BoschSHCException e) {
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
String.format("Error when trying to refresh state from service %s: %s",
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();
}
}
}
@@ -156,7 +161,7 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
/**
* Processes an update which is received from the bridge.
*
*
* @param serviceName Name of service the update came from.
* @param stateData Current state of device service. Serialized as JSON.
*/
@@ -178,7 +183,7 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
/**
* Returns the bridge handler for this thing handler.
*
*
* @return Bridge handler for this thing handler. Null if no or an invalid bridge was set in the configuration.
* @throws BoschSHCException If bridge for handler is not set or an invalid bridge is set.
*/
@@ -196,7 +201,7 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
/**
* Query the Bosch Smart Home Controller for the state of the service with the specified name.
*
*
* @note Use services instead of directly requesting a state.
*
* @param stateName Name of the service to query
@@ -210,16 +215,21 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
try {
BoschSHCBridgeHandler bridgeHandler = this.getBridgeHandler();
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,
String.format("Error when trying to refresh state from service %s: %s", stateName, e.getMessage()));
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;
}
}
/**
* Creates and registers a new service for this device.
*
*
* @param <TService> Type of service.
* @param <TState> Type of service state.
* @param newService Supplier function to create a new instance of the service.
@@ -240,7 +250,7 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
/**
* Registers a service for this device.
*
*
* @param <TService> Type of service.
* @param <TState> Type of service state.
* @param service Service to register.
@@ -269,7 +279,7 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
/**
* Updates the state of a device service.
* Sets the status of the device to offline if setting the state fails.
*
*
* @param <TService> Type of service.
* @param <TState> Type of service state.
* @param service Service to set state for.
@@ -279,15 +289,19 @@ public abstract class BoschSHCHandler extends BaseThingHandler {
TService service, TState state) {
try {
service.setState(state);
} catch (InterruptedException | TimeoutException | ExecutionException e) {
} catch (TimeoutException | ExecutionException e) {
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, String.format(
"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();
}
}
/**
* Registers a service of this device.
*
*
* @param service Service which belongs to this device
* @param affectedChannels Channels which are affected by the state of this
* service

View File

@@ -12,8 +12,7 @@
*/
package org.openhab.binding.boschshc.internal.devices.bridge;
import static org.eclipse.jetty.http.HttpMethod.GET;
import static org.eclipse.jetty.http.HttpMethod.PUT;
import static org.eclipse.jetty.http.HttpMethod.*;
import java.lang.reflect.Type;
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.util.ssl.SslContextFactory;
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.LongPollingFailedException;
import org.openhab.binding.boschshc.internal.exceptions.PairingFailedException;
@@ -233,12 +235,13 @@ public class BoschSHCBridgeHandler extends BaseBridgeHandler {
} catch (InterruptedException e) {
this.updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.UNKNOWN.NONE, "@text/offline.interrupted");
Thread.currentThread().interrupt();
}
}
/**
* Get a list of connected devices from the Smart-Home Controller
*
*
* @throws InterruptedException in case bridge is stopped
*/
private boolean getDevices() throws InterruptedException {
@@ -354,7 +357,7 @@ public class BoschSHCBridgeHandler extends BaseBridgeHandler {
/**
* Get a list of rooms from the Smart-Home controller
*
*
* @throws InterruptedException in case bridge is stopped
*/
private boolean getRooms() throws InterruptedException {
@@ -451,11 +454,11 @@ public class BoschSHCBridgeHandler extends BaseBridgeHandler {
/**
* Sends a state change for a device to the controller
*
*
* @param deviceId Id of device to change state for
* @param serviceName Name of service of device to change state for
* @param state New state data to set for service
*
*
* @return Response of request
* @throws InterruptedException
* @throws ExecutionException

View File

@@ -37,7 +37,7 @@ import com.google.gson.Gson;
/**
* Handles the long polling to the Smart Home Controller.
*
*
* @author Christian Oeing - Initial contribution
*/
@NonNullByDefault
@@ -100,7 +100,7 @@ public class LongPolling {
/**
* Subscribe to events and store the subscription ID needed for long polling.
*
*
* @param httpClient Http client to use for sending subscription request
* @return Subscription id
*/
@@ -116,16 +116,21 @@ public class LongPolling {
logger.debug("Subscribe: Got subscription ID: {} {}", response.getResult(), response.getJsonrpc());
String subscriptionId = response.getResult();
return subscriptionId;
} catch (TimeoutException | ExecutionException | InterruptedException e) {
} catch (TimeoutException | ExecutionException e) {
throw new LongPollingFailedException(
String.format("Error on subscribe (Http client: %s): %s", httpClient.toString(), e.getMessage()),
e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new LongPollingFailedException(
String.format("Interrupted subscribe (Http client: %s): %s", httpClient.toString(), e.getMessage()),
e);
}
}
/**
* Create a new subscription for long polling.
*
*
* @param httpClient Http client to send requests to
*/
private void resubscribe(BoschHttpClient httpClient) {
@@ -171,7 +176,7 @@ public class LongPolling {
/**
* This is the handler for responses of long poll requests.
*
*
* @param httpClient HTTP client which received the response
* @param subscriptionId Id of subscription the response is for
* @param result Complete result of the response