100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Fixing issue and null case in polling and heartbeat
jerome.vervier10517655b2f5
diff --git a/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.javaindex 29fcbd6e3..139f6d437 100644--- a/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java@@ -37,6 +37,7 @@ import javax.ws.rs.ProcessingException;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 @@ public class ControllerProtocol extends AbstractProtocol {* @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 @@ public class ControllerProtocol extends AbstractProtocol {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 @@ public class ControllerProtocol extends AbstractProtocol {this.updateConnectionStatus(protocolRef, ConnectionStatus.CONNECTED);//cancel has to be the last stepLOG.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());