Search in sources :

Example 31 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class ORConsoleGeofenceAssetAdapter method locationPredicateToGeofenceDefinition.

protected GeofenceDefinition locationPredicateToGeofenceDefinition(String assetId, GeofencePredicate geofencePredicate) {
    RadialGeofencePredicate radialLocationPredicate = (RadialGeofencePredicate) geofencePredicate;
    String id = assetId + "_" + radialLocationPredicate.hashCode();
    String url = getWriteAttributeUrl(new AttributeRef(assetId, Asset.LOCATION.getName()));
    return new GeofenceDefinition(id, radialLocationPredicate.getLat(), radialLocationPredicate.getLng(), radialLocationPredicate.getRadius(), WRITE_ATTRIBUTE_HTTP_METHOD, url);
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) GeofenceDefinition(org.openremote.model.rules.geofence.GeofenceDefinition)

Example 32 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class HTTPProtocol method doLinkAttribute.

@Override
protected void doLinkAttribute(String assetId, Attribute<?> attribute, HTTPAgentLink agentLink) {
    AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
    String method = agentLink.getMethod().map(Enum::name).orElse(DEFAULT_HTTP_METHOD);
    String path = agentLink.getPath().orElse(null);
    String contentType = agentLink.getContentType().orElse(null);
    Map<String, List<String>> headers = agentLink.getHeaders().orElse(null);
    Map<String, List<String>> queryParams = agentLink.getQueryParameters().orElse(null);
    Integer pollingMillis = agentLink.getPollingMillis().map(millis -> Math.max(millis, MIN_POLLING_MILLIS)).orElse(null);
    boolean pagingEnabled = agentLink.getPagingMode().orElse(false);
    String pollingAttribute = agentLink.getPollingAttribute().orElse(null);
    if (!TextUtil.isNullOrEmpty(pollingAttribute)) {
        synchronized (pollingLinkedAttributeMap) {
            AttributeRef pollingSourceRef = new AttributeRef(attributeRef.getId(), pollingAttribute);
            pollingLinkedAttributeMap.compute(pollingSourceRef, (ref, links) -> {
                if (links == null) {
                    links = new HashSet<>();
                }
                links.add(attributeRef);
                return links;
            });
        }
    }
    String body = agentLink.getWriteValue().orElse(null);
    if (client == null) {
        LOG.warning("Client is undefined: " + this);
        return;
    }
    HttpClientRequest clientRequest = buildClientRequest(path, method, headers != null ? WebTargetBuilder.mapToMultivaluedMap(headers, new MultivaluedHashMap<>()) : null, queryParams != null ? WebTargetBuilder.mapToMultivaluedMap(queryParams, new MultivaluedHashMap<>()) : null, pagingEnabled, contentType);
    LOG.fine("Creating HTTP request for attributeRef '" + clientRequest + "': " + attributeRef);
    requestMap.put(attributeRef, clientRequest);
    Optional.ofNullable(pollingMillis).ifPresent(seconds -> pollingMap.put(attributeRef, schedulePollingRequest(attributeRef, agentLink, clientRequest, body, seconds)));
}
Also used : java.util(java.util) ScheduledFuture(java.util.concurrent.ScheduledFuture) Protocol(org.openremote.model.asset.agent.Protocol) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) URISyntaxException(java.net.URISyntaxException) AttributeRef(org.openremote.model.attribute.AttributeRef) ResponseBuilderImpl(org.jboss.resteasy.specimpl.ResponseBuilderImpl) ValueUtil(org.openremote.model.util.ValueUtil) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) HttpMethod(javax.ws.rs.HttpMethod) WebTargetBuilder(org.openremote.container.web.WebTargetBuilder) Level(java.util.logging.Level) AgentLink(org.openremote.model.asset.agent.AgentLink) MediaType(javax.ws.rs.core.MediaType) Attribute(org.openremote.model.attribute.Attribute) AttributeEvent(org.openremote.model.attribute.AttributeEvent) SyslogCategory(org.openremote.model.syslog.SyslogCategory) TextUtil(org.openremote.model.util.TextUtil) URI(java.net.URI) AttributeState(org.openremote.model.attribute.AttributeState) OAuthGrant(org.openremote.model.auth.OAuthGrant) ValueType(org.openremote.model.value.ValueType) QUERY_PARAMETERS_PROPERTY(org.openremote.container.web.QueryParameterInjectorFilter.QUERY_PARAMETERS_PROPERTY) URIBuilder(org.apache.http.client.utils.URIBuilder) Invocation(javax.ws.rs.client.Invocation) Logger(java.util.logging.Logger) Entity(javax.ws.rs.client.Entity) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) Container(org.openremote.model.Container) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse) Headers(org.jboss.resteasy.core.Headers) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) Annotation(java.lang.annotation.Annotation) WebTargetBuilder.createClient(org.openremote.container.web.WebTargetBuilder.createClient) UsernamePassword(org.openremote.model.auth.UsernamePassword) WebTarget(javax.ws.rs.client.WebTarget) QueryParameterInjectorFilter(org.openremote.container.web.QueryParameterInjectorFilter) ProtocolUtil(org.openremote.model.protocol.ProtocolUtil) GlobalLock.withLock(org.openremote.container.concurrent.GlobalLock.withLock) AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 33 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class HTTPProtocol method onPollingResponse.

protected void onPollingResponse(HttpClientRequest request, Response response, AttributeRef attributeRef, HTTPAgentLink agentLink) {
    int responseCode = response != null ? response.getStatus() : 500;
    Object value = null;
    if (response != null && response.hasEntity() && response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
        try {
            boolean binaryMode = agent.getMessageConvertBinary().orElse(agentLink.isMessageConvertBinary());
            boolean hexMode = agent.getMessageConvertHex().orElse(agentLink.isMessageConvertHex());
            if (hexMode || binaryMode) {
                byte[] bytes = response.readEntity(byte[].class);
                value = hexMode ? ProtocolUtil.bytesToHexString(bytes) : ProtocolUtil.bytesToBinaryString(bytes);
            } else {
                value = response.readEntity(String.class);
            }
        } catch (Exception e) {
            LOG.log(Level.WARNING, "Error occurred whilst trying to read response body", e);
            response.close();
        }
    } else {
        LOG.fine(prefixLogMessage("Request returned an un-successful response code (" + responseCode + "):" + request.requestTarget.getUriBuilder().build().toString()));
        return;
    }
    if (attributeRef != null) {
        updateLinkedAttribute(new AttributeState(attributeRef, value));
        // Look for any attributes that also want to use this polling response
        synchronized (pollingLinkedAttributeMap) {
            Set<AttributeRef> linkedRefs = pollingLinkedAttributeMap.get(attributeRef);
            if (linkedRefs != null) {
                Object finalValue = value;
                linkedRefs.forEach(ref -> updateLinkedAttribute(new AttributeState(ref, finalValue)));
            }
        }
    }
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState) AttributeRef(org.openremote.model.attribute.AttributeRef) URISyntaxException(java.net.URISyntaxException)

Example 34 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class HTTPProtocol method doUnlinkAttribute.

@Override
protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, HTTPAgentLink agentLink) {
    AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
    requestMap.remove(attributeRef);
    cancelPolling(attributeRef);
    agentLink.getPollingMillis().ifPresent(pollingAttribute -> {
        synchronized (pollingLinkedAttributeMap) {
            pollingLinkedAttributeMap.remove(attributeRef);
            pollingLinkedAttributeMap.values().forEach(links -> links.remove(attributeRef));
        }
    });
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 35 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class MQTTProtocol method doUnlinkAttribute.

@Override
protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, MQTTAgentLink agentLink) {
    agentLink.getSubscriptionTopic().ifPresent(topic -> {
        AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
        Consumer<MQTTMessage<String>> messageConsumer = protocolMessageConsumers.remove(attributeRef);
        if (messageConsumer != null) {
            client.removeMessageConsumer(topic, messageConsumer);
        }
    });
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Aggregations

AttributeRef (org.openremote.model.attribute.AttributeRef)42 Logger (java.util.logging.Logger)9 AttributeEvent (org.openremote.model.attribute.AttributeEvent)8 AttributeState (org.openremote.model.attribute.AttributeState)8 Attribute (org.openremote.model.attribute.Attribute)7 List (java.util.List)6 Level (java.util.logging.Level)6 Container (org.openremote.model.Container)6 SyslogCategory (org.openremote.model.syslog.SyslogCategory)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ScheduledFuture (java.util.concurrent.ScheduledFuture)5 Consumer (java.util.function.Consumer)5 Asset (org.openremote.model.asset.Asset)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 HashMap (java.util.HashMap)4 TimeUnit (java.util.concurrent.TimeUnit)4 AbstractProtocol (org.openremote.agent.protocol.AbstractProtocol)4 AssetStorageService (org.openremote.manager.asset.AssetStorageService)4 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)4