Permalink
Browse files

Fixing issue and null case in polling and heartbeat

  • Loading branch information...
jerome.vervier
jerome.vervier committed Oct 25, 2018
1 parent 31ec8ce commit 10517655b2f530d156d1b7a1d940366c60b345f5
Showing with 32 additions and 26 deletions.
  1. +32 −26 agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
@@ -37,6 +37,7 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -401,33 +402,38 @@ private void executePollingRequest(PollingKey pollingKey, List<String> sensorLis
* @param response Response received from request
*/
private void onPollingResponse(PollingKey pollingKey, List<String> sensorNameList, Response response) {
if (response.getStatusInfo() == Response.Status.OK) {
String responseBodyAsString = response.readEntity(String.class);
LOG.info("### New sensors status received");
LOG.finer("### Polling request body response : " + responseBodyAsString);
Optional<ArrayValue> arrayValue = Values.parse(responseBodyAsString).flatMap(Values::getArray);
Optional<List<ObjectValue>> statuses = Values.getArrayElements(arrayValue.orElse(null), ObjectValue.class, false, false);
if (!statuses.isPresent()) {
LOG.warning("### Polling response is not a JSON array: " + responseBodyAsString);
if(response != null) {
if (response.getStatusInfo() == Response.Status.OK) {
String responseBodyAsString = response.readEntity(String.class);
LOG.info("### New sensors status received");
LOG.finer("### Polling request body response : " + responseBodyAsString);
Optional<ArrayValue> arrayValue = Values.parse(responseBodyAsString).flatMap(Values::getArray);
Optional<List<ObjectValue>> statuses = Values.getArrayElements(arrayValue.orElse(null), ObjectValue.class, false, false);
if (!statuses.isPresent()) {
LOG.warning("### Polling response is not a JSON array or empty: " + responseBodyAsString);
} else {
statuses.get().forEach(status -> {
String name = status.getString("name").orElse(null);
String value = status.getString("value").orElse(null);
/**
* For every sensors in the request body, find the linked attributeref and update value by calling {@link updateAttributeValue}
*/
this.controllersMap.get(pollingKey.getControllerAgentRef()).getSensorsListForDevice(pollingKey.getDeviceName()).stream()
.filter(entry -> entry.getValue().getSensorName().equals(name))
.forEach(e -> this.updateAttributeValue(e.getKey(), value));
});
}
} else if (response.getStatusInfo() == Response.Status.REQUEST_TIMEOUT) {
LOG.info("### Timeout from polling no changes on Controller side given sensors [device=" + pollingKey.getDeviceName() + ", sensors=" + this.formatSensors(sensorNameList) + "]");
} else {
statuses.get().forEach(status -> {
String name = status.getString("name").orElse(null);
String value = status.getString("value").orElse(null);
/**
* For every sensors in the request body, find the linked attributeref and update value by calling {@link updateAttributeValue}
*/
this.controllersMap.get(pollingKey.getControllerAgentRef()).getSensorsListForDevice(pollingKey.getDeviceName()).stream().filter(entry -> entry.getValue().getSensorName().equals(name)).forEach(e -> this.updateAttributeValue(e.getKey(), value));
});
LOG.severe("### Status code received error : " + response.getStatus() + " --> " + response.getStatusInfo().getReasonPhrase());
}
} else if (response.getStatusInfo() == Response.Status.REQUEST_TIMEOUT) {
LOG.info("### Timeout from polling no changes on Controller side given sensors [device=" + pollingKey.getDeviceName() + ", sensors=" + this
.formatSensors(sensorNameList) + "]");
} else {
LOG.severe("### Status code received error : " + response.getStatus() + " --> " + response.getStatusInfo().getReasonPhrase());
LOG.severe("### Received null response from polling (due to previous exception)");
}
//No matter status code, we're continuing to poll
@@ -504,7 +510,7 @@ private void checkIfConnectionRefused(Exception e, AttributeRef protocolRef) {
if (e.getCause() instanceof HttpHostConnectException) {
HttpHostConnectException e2 = (HttpHostConnectException) e.getCause();
if (e2.getCause() instanceof ConnectException) {
if (e2.getCause() instanceof ConnectException || e2.getCause() instanceof UnknownHostException) {
ConnectException e3 = (ConnectException) e2.getCause();
LOG.log(Level.SEVERE, "### Connection refused : " + e3.getMessage());
this.updateConnectionStatus(protocolRef, ConnectionStatus.DISCONNECTED);
@@ -581,7 +587,7 @@ private void onHeartbeatResponse(AttributeRef protocolRef, Response response) {
this.updateConnectionStatus(protocolRef, ConnectionStatus.CONNECTED);
//cancel has to be the last step
LOG.info("### Stop Heartbeat task for " + protocolRef.getAttributeName());
this.controllerHeartbeat.get(protocolRef).cancel(false);
this.controllerHeartbeat.get(protocolRef).cancel(true);
} else {
if (response != null) {
LOG.severe("### Heartbeat check response is " + response.getStatus());

0 comments on commit 1051765

Please sign in to comment.