[SenecHome] Catch and ignore malformed JSON (#10657)

* 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>
This commit is contained in:
Korbinian Probst 2021-05-19 22:18:15 +02:00 committed by GitHub
parent 8202d57965
commit b049d3d13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 10 deletions

View 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;
}
}
}

View 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());