Search in sources :

Example 1 with ValueUtil

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);
}
Also used : java.util(java.util) ScheduledFuture(java.util.concurrent.ScheduledFuture) ControllerCommandMapped(org.openremote.agent.protocol.controller.command.ControllerCommandMapped) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) URISyntaxException(java.net.URISyntaxException) AttributeRef(org.openremote.model.attribute.AttributeRef) ValueDescriptor(org.openremote.model.value.ValueDescriptor) ValueUtil(org.openremote.model.util.ValueUtil) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) WebTargetBuilder(org.openremote.container.web.WebTargetBuilder) Level(java.util.logging.Level) GlobalLock.withLockReturning(org.openremote.container.concurrent.GlobalLock.withLockReturning) Future(java.util.concurrent.Future) Attribute(org.openremote.model.attribute.Attribute) AttributeEvent(org.openremote.model.attribute.AttributeEvent) CONNECTION_POOL_SIZE(org.openremote.container.web.WebTargetBuilder.CONNECTION_POOL_SIZE) ConnectException(java.net.ConnectException) SyslogCategory(org.openremote.model.syslog.SyslogCategory) URI(java.net.URI) ControllerCommandBasic(org.openremote.agent.protocol.controller.command.ControllerCommandBasic) AttributeState(org.openremote.model.attribute.AttributeState) URIBuilder(org.apache.http.client.utils.URIBuilder) HTTPProtocol(org.openremote.agent.protocol.http.HTTPProtocol) Logger(java.util.logging.Logger) UnknownHostException(java.net.UnknownHostException) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) Container(org.openremote.model.Container) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Response(javax.ws.rs.core.Response) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ProcessingException(javax.ws.rs.ProcessingException) WebTargetBuilder.createClient(org.openremote.container.web.WebTargetBuilder.createClient) GlobalLock.withLock(org.openremote.container.concurrent.GlobalLock.withLock) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ConnectException (java.net.ConnectException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 java.util (java.util)1 Future (java.util.concurrent.Future)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 Consumer (java.util.function.Consumer)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 ProcessingException (javax.ws.rs.ProcessingException)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Response (javax.ws.rs.core.Response)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)1 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)1 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)1