use of org.openremote.model.util.ValueUtil in project openremote by openremote.
the class ControllerProtocol method onPollingResponse.
/**
* Polling request should return three different responses :
* <ul>
* <li>OK (200) : new values are available for at least one of the sensor provided in queryParam</li>
* <li>TIMEOUT (408) : during the last 60 seconds following the start of the request, none of the sensors have new values</li>
* <li>Others : error</li>
* </ul>
* <p>
* In every case, we should start a new polling request directly. Only the 200 response induce an update of every linked attribute having a sensor
* status updated.
*/
private void onPollingResponse(String deviceName, List<String> sensorNameList, Response response) {
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);
ArrayNode statusArray = ValueUtil.convert(responseBodyAsString, ArrayNode.class);
if (statusArray == null) {
LOG.warning("### Polling response is not a JSON array or empty: " + responseBodyAsString);
} else {
statusArray.forEach(status -> {
String name = Optional.ofNullable(status.get("name")).flatMap(ValueUtil::getString).orElse(null);
String value = Optional.ofNullable(status.get("value")).flatMap(ValueUtil::getString).orElse(null);
/**
* For every sensors in the request body, find the linked attributeref and update value by calling {@link #updateAttributeValue}
*/
controller.getSensorsListForDevice(deviceName).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=" + deviceName + ", sensors=" + this.formatSensors(sensorNameList) + "]");
} else {
LOG.severe("### Status code received error : " + response.getStatus() + " --> " + response.getStatusInfo().getReasonPhrase());
}
} else {
LOG.severe("### Received null response from polling (due to previous exception)");
}
// No matter status code, we're continuing to poll
this.schedulePollingTask(deviceName);
}
Aggregations