From b049d3d13ba39535b577191d12fc11743d68de96 Mon Sep 17 00:00:00 2001 From: Korbinian Probst <27731930+KorbinianP@users.noreply.github.com> Date: Wed, 19 May 2021 22:18:15 +0200 Subject: [PATCH] [SenecHome] Catch and ignore malformed JSON (#10657) * SENEC Home: Add SocketTimeoutException and MalformedJsonException Signed-off-by: Korbinian Probst * SENEC: Revert last commit Signed-off-by: Korbinian Probst * SENEC: Implement a counter that keeps the device online for random errors Signed-off-by: Korbinian Probst * Implement a print of the faulty response Signed-off-by: Korbinian Probst * Improve print of faulty response Signed-off-by: Korbinian Probst * Print all responses for debugging, catch MalformedJsonException explicitly Signed-off-by: Korbinian Probst * Add Infor print if JSON is wrong. Catch JsonSyntaxException. Remove errorCounter Signed-off-by: Korbinian Probst * Remove most debug code, move remaining messages to trace log level Signed-off-by: Korbinian Probst * Bring back catch of MalformedJsonException. Avoid null pointer exception in case response is null. Signed-off-by: Korbinian Probst * Collect error to print it in one message Signed-off-by: Korbinian Probst Co-authored-by: Korbinian Probst --- .../senechome/internal/SenecHomeApi.java | 38 +++++++++++++++---- .../senechome/internal/SenecHomeHandler.java | 12 +++++- 2 files changed, 40 insertions(+), 10 deletions(-) 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());