diff --git a/bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeApi.java b/bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeApi.java index 70d03b84f..afdd61a26 100644 --- a/bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeApi.java +++ b/bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeApi.java @@ -32,6 +32,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import com.google.gson.stream.MalformedJsonException; /** * The {@link SenecHomeApi} class configures http client and @@ -75,14 +77,34 @@ public class SenecHomeApi { Request request = httpClient.newRequest(location); request.header(HttpHeader.ACCEPT, MimeTypes.Type.APPLICATION_JSON.asString()); request.header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()); - ContentResponse response = request.method(HttpMethod.POST) - .content(new StringContentProvider(gson.toJson(new SenecHomeResponse()))).send(); - - if (response.getStatus() == HttpStatus.OK_200) { - return Objects.requireNonNull(gson.fromJson(response.getContentAsString(), SenecHomeResponse.class)); - } else { - logger.trace("Got unexpected response code {}", response.getStatus()); - throw new IOException("Got unexpected response code " + response.getStatus()); + ContentResponse response = null; + try { + response = request.method(HttpMethod.POST) + .content(new StringContentProvider(gson.toJson(new SenecHomeResponse()))).send(); + if (response.getStatus() == HttpStatus.OK_200) { + return Objects.requireNonNull(gson.fromJson(response.getContentAsString(), SenecHomeResponse.class)); + } else { + logger.trace("Got unexpected response code {}", response.getStatus()); + throw new IOException("Got unexpected response code " + response.getStatus()); + } + } catch (MalformedJsonException | JsonSyntaxException | InterruptedException | TimeoutException + | ExecutionException e) { + String errorMessage = "\nlocation: " + location; + errorMessage += "\nrequest: " + request.toString(); + errorMessage += "\nrequest.getHeaders: " + request.getHeaders(); + if (response == null) { + errorMessage += "\nresponse: null"; + } else { + errorMessage += "\nresponse: " + response.toString(); + errorMessage += "\nresponse.getHeaders: " + response.getHeaders(); + if (response.getContent() == null) { + errorMessage += "\nresponse.getContent is null"; + } else { + errorMessage += "\nresponse.getContentAsString: " + response.getContentAsString(); + } + } + logger.trace("Issue with getting SenecHomeResponse\n{}", errorMessage); + throw e; } } } diff --git a/bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeHandler.java b/bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeHandler.java index ef7134df0..e70a97aba 100644 --- a/bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeHandler.java +++ b/bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeHandler.java @@ -51,6 +51,8 @@ import org.openhab.core.types.Command; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.JsonSyntaxException; + /** * The {@link SenecHomeHandler} is responsible for handling commands, which are * sent to one of the channels. @@ -119,8 +121,9 @@ public class SenecHomeHandler extends BaseThingHandler { } public @Nullable Boolean refreshState() { + SenecHomeResponse response = null; try { - SenecHomeResponse response = senecHomeApi.getStatistics(); + response = senecHomeApi.getStatistics(); logger.trace("received {}", response); BigDecimal pvLimitation = new BigDecimal(100).subtract(getSenecValue(response.limitation.powerLimitation)) @@ -276,7 +279,12 @@ public class SenecHomeHandler extends BaseThingHandler { updateGridPowerValues(getSenecValue(response.grid.currentGridValue)); updateStatus(ThingStatus.ONLINE); - } catch (IOException | InterruptedException | TimeoutException | ExecutionException e) { + } catch (JsonSyntaxException | IOException | InterruptedException | TimeoutException | ExecutionException e) { + if (response == null) { + logger.trace("Faulty response: is null"); + } else { + logger.trace("Faulty response: {}", response.toString()); + } logger.warn("Error refreshing source '{}'", getThing().getUID(), e); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Could not connect to Senec web interface:" + e.getMessage());