]> git.basschouten.com Git - openhab-addons.git/commitdiff
[SenecHome] Catch and ignore malformed JSON (#10657)
authorKorbinian Probst <27731930+KorbinianP@users.noreply.github.com>
Wed, 19 May 2021 20:18:15 +0000 (22:18 +0200)
committerGitHub <noreply@github.com>
Wed, 19 May 2021 20:18:15 +0000 (22:18 +0200)
* SENEC Home: Add SocketTimeoutException and MalformedJsonException

Signed-off-by: Korbinian Probst <kp.droid.dev@gmail.com>
* SENEC: Revert last commit

Signed-off-by: Korbinian Probst <kp.droid.dev@gmail.com>
* SENEC: Implement a counter that keeps the device online for random errors

Signed-off-by: Korbinian Probst <kp.droid.dev@gmail.com>
* Implement a print of the faulty response

Signed-off-by: Korbinian Probst <kp.droid.dev@gmail.com>
* Improve print of faulty response

Signed-off-by: Korbinian Probst <kp.droid.dev@gmail.com>
* Print all responses for debugging, catch MalformedJsonException explicitly

Signed-off-by: Korbinian Probst <korbinian.probst@gmx.de>
* Add Infor print if JSON is wrong. Catch JsonSyntaxException. Remove errorCounter

Signed-off-by: Korbinian Probst <korbinian.probst@gmx.de>
* Remove most debug code, move remaining messages to trace log level

Signed-off-by: Korbinian Probst <korbinian.probst@gmx.de>
* Bring back catch of MalformedJsonException. Avoid null pointer exception in case response is null.

Signed-off-by: Korbinian Probst <korbinian.probst@gmx.de>
* Collect error to print it in one message

Signed-off-by: Korbinian Probst <korbinian.probst@gmx.de>
Co-authored-by: Korbinian Probst <korbinian.probst@gmx.de>
bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeApi.java
bundles/org.openhab.binding.senechome/src/main/java/org/openhab/binding/senechome/internal/SenecHomeHandler.java

index 70d03b84fe20aa82a7e5785a80cda54de8d2f774..afdd61a268aef11294b641a76de89b236a5bcc6c 100644 (file)
@@ -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;
         }
     }
 }
index ef7134df0e2ca65298131fb160b94bb5e906e759..e70a97abae5ca85ed269413116bbae9f5c2257ba 100644 (file)
@@ -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());