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);
}
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)));
}
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)));
}
}
}
}
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));
}
});
}
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);
}
});
}
Aggregations