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
Added READ_TIMEOUT to http client protocol to set a custom timeout value Values in AssetAttributeLinkingService are converted to the right ValueType
Michael Rademaker09a4b6ee5463
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.javaindex 740101016..f646e251c 100644--- a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java@@ -536,6 +536,16 @@ public class HttpClientProtocol extends AbstractProtocol {false,null);+ /**+ * Set default read timeout for the request. Useful for requests which need more time completing.+ */+ public static final MetaItemDescriptor META_READ_TIMEOUT_MILLISECONDS = metaItemInteger(+ PROTOCOL_NAME + ":readTimeout",+ ACCESS_PRIVATE,+ false,+ (int) WebTargetBuilder.CONNECTION_TIMEOUT_MILLISECONDS,+ null);+public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(META_ATTRIBUTE_PATH,META_ATTRIBUTE_METHOD,@@ -546,7 +556,8 @@ public class HttpClientProtocol extends AbstractProtocol {META_POLLING_MILLIS,META_QUERY_PARAMETERS,META_FAILURE_CODES,- META_PAGING_ENABLED);+ META_PAGING_ENABLED,+ META_READ_TIMEOUT_MILLISECONDS);public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(META_PROTOCOL_BASE_URI,@@ -564,7 +575,8 @@ public class HttpClientProtocol extends AbstractProtocol {META_QUERY_PARAMETERS,META_HEADERS,META_FAILURE_CODES,- META_PAGING_ENABLED);+ META_PAGING_ENABLED,+ META_READ_TIMEOUT_MILLISECONDS);protected final Map<AttributeRef, Pair<ResteasyWebTarget, List<Integer>>> clientMap = new HashMap<>();protected final Map<AttributeRef, HttpClientRequest> requestMap = new HashMap<>();@@ -734,7 +746,21 @@ public class HttpClientProtocol extends AbstractProtocol {.flatMap(Values::getString).orElse(null);- WebTargetBuilder webTargetBuilder = new WebTargetBuilder(client, uri);+ Double readTimeout = Values.getMetaItemValueOrThrow(+ protocolConfiguration,+ META_READ_TIMEOUT_MILLISECONDS,+ false,+ true)+ .flatMap(Values::getNumber)+ .orElse(null);++ WebTargetBuilder webTargetBuilder;+ if (readTimeout != null) {+ webTargetBuilder = new WebTargetBuilder(WebTargetBuilder.createClient(executorService, WebTargetBuilder.CONNECTION_POOL_SIZE, readTimeout.longValue(), null), uri);+ } else {+ webTargetBuilder = new WebTargetBuilder(client, uri);+ }+if (oAuthGrant.isPresent()) {LOG.info("Adding OAuth");webTargetBuilder.setOAuthAuthentication(oAuthGrant.get());@@ -1149,7 +1175,7 @@ public class HttpClientProtocol extends AbstractProtocol {originalResponse = PagingResponse.fromResponse(originalResponse).entity(entities).build();}} catch (Exception e) {- LOG.log(Level.SEVERE, getProtocolDisplayName() + " exception thrown whilst doing polling request: " + clientRequest.client.getUri() + clientRequest.path, e);+ LOG.log(Level.SEVERE, getProtocolDisplayName() + " exception thrown whilst doing polling request: " + clientRequest.requestTarget.getUriBuilder().build().toString(), e);}responseConsumer.accept(originalResponse);diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.javaindex 385d38598..a5205a8e9 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java@@ -162,7 +162,29 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpdareturn;}- sendAttributeEvent(new AttributeEvent(attributeLink.getAttributeRef(), sendConvertedValue.getValue()));+ Value value = sendConvertedValue.getValue();++ Optional<AssetAttribute> attribute = getAttribute(em, assetStorageService, attributeLink.getAttributeRef());+ if (attribute.isPresent()) {+ // Do built in value conversion+ Optional<ValueType> attributeValueType = attribute.get().getType().map(AttributeValueDescriptor::getValueType);++ if (value != null && attributeValueType.isPresent()) {+ if (attributeValueType.get() != value.getType()) {+ LOG.fine("Trying to convert value: " + value.getType() + " -> " + attributeValueType.get());+ Optional<Value> convertedValue = Values.convert(value, attributeValueType.get());+ if (!convertedValue.isPresent()) {+ LOG.warning("Failed to convert value: " + value.getType() + " -> " + attributeValueType.get());+ LOG.warning("Cannot send linked attribute update");+ return;+ } else {+ value = convertedValue.get();+ }+ }+ }+ }++ sendAttributeEvent(new AttributeEvent(attributeLink.getAttributeRef(), value));}protected ConvertedValue convertValueForLinkedAttribute(EntityManager em,