Search in sources :

Example 1 with DataTypeDictionary

use of org.eclipse.milo.opcua.stack.core.types.DataTypeDictionary in project milo by eclipse.

the class DataTypeDictionaryReader method registerCustomStructCodecs.

private CompletableFuture<DataTypeDictionary<?>> registerCustomStructCodecs(NodeId dictionaryNodeId, OpcUaBinaryDataTypeDictionary dictionary, List<CodecDescription> structCodecs) {
    CompletableFuture<List<NodeId>> descriptionNodeIds = browseDataTypeDescriptionNodeIds(dictionaryNodeId);
    CompletableFuture<List<String>> descriptionValues = descriptionNodeIds.thenCompose(this::readDataTypeDescriptionValues);
    if (logger.isTraceEnabled()) {
        try {
            List<NodeId> ids = descriptionNodeIds.get();
            List<String> values = descriptionValues.get();
            if (ids.size() != values.size()) {
                throw new IllegalStateException("size mismatch");
            }
            for (int i = 0; i < ids.size(); i++) {
                NodeId id = ids.get(i);
                String value = values.get(i);
                logger.trace("description NodeId={} value={}", id, value);
            }
        } catch (Exception e) {
            logger.error("Error reading description NodeIds", e);
        }
    }
    CompletableFuture<List<NodeId>> encodingIdsFuture = descriptionNodeIds.thenCompose(this::browseDataTypeEncodingNodeIds);
    return encodingIdsFuture.thenCompose(encodingIds -> browseDataTypeIds(encodingIds).thenCompose(dataTypeIds -> descriptionValues.thenApply(descriptions -> {
        Map<String, NodeId> encodingIdMap = new HashMap<>();
        Map<String, NodeId> dataTypeIdMap = new HashMap<>();
        if (descriptions.size() != encodingIds.size()) {
            throw new IllegalStateException(String.format("descriptions.size() != encodingIds.size() (%s != %s)", descriptions.size(), encodingIds.size()));
        }
        if (encodingIds.size() != dataTypeIds.size()) {
            throw new IllegalStateException(String.format("encodingIds.size() != dataTypeIds.size() (%s != %s)", encodingIds.size(), dataTypeIds.size()));
        }
        Iterator<String> descriptionIter = descriptions.iterator();
        Iterator<NodeId> encodingIdIter = encodingIds.iterator();
        Iterator<NodeId> dataTypeIdIter = dataTypeIds.iterator();
        while (descriptionIter.hasNext() && encodingIdIter.hasNext() && dataTypeIdIter.hasNext()) {
            String description = descriptionIter.next();
            encodingIdMap.put(description, encodingIdIter.next());
            dataTypeIdMap.put(description, dataTypeIdIter.next());
        }
        structCodecs.forEach(cd -> {
            String description = cd.getDescription();
            NodeId encodingId = encodingIdMap.get(description);
            NodeId dataTypeId = dataTypeIdMap.get(description);
            if (encodingId == null || encodingId.isNull()) {
                if (dataTypeId != null && dataTypeId.getNamespaceIndex().intValue() != 0) {
                    logger.warn("encodingId is null for description={}", description);
                } else {
                    // Theres a number of missing structures in the built-in type dictionary;
                    // namely the service request and response structures. It's expected that
                    // we won't be able to create codecs for these.
                    logger.debug("dataTypeId and encodingId is null for description={}", description);
                }
            } else if (dataTypeId == null || dataTypeId.isNull()) {
                logger.warn("dataTypeId is null for description={}", description);
            } else {
                dictionary.registerStructCodec(cd.getCodec(), description, dataTypeId, encodingId);
                logger.debug("Registered codec description={} dataTypeId={} encodingId={}", description, dataTypeId, encodingId);
            }
        });
        return dictionary;
    })));
}
Also used : BrowseNextRequest(org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextRequest) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) LoggerFactory(org.slf4j.LoggerFactory) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) DictionaryDescription(org.eclipse.milo.opcua.binaryschema.parser.DictionaryDescription) Unpooled(io.netty.buffer.Unpooled) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) ByteArrayInputStream(java.io.ByteArrayInputStream) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription) Map(java.util.Map) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) BrowseResult(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) BsdParser(org.eclipse.milo.opcua.binaryschema.parser.BsdParser) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) CodecDescription(org.eclipse.milo.opcua.binaryschema.parser.CodecDescription) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) Objects(java.util.Objects) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) FutureUtils(org.eclipse.milo.opcua.stack.core.util.FutureUtils) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) StreamUtil.opt2stream(org.eclipse.milo.opcua.sdk.core.util.StreamUtil.opt2stream) BrowseResponse(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResponse) OpcUaSession(org.eclipse.milo.opcua.sdk.client.OpcUaSession) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) BrowseRequest(org.eclipse.milo.opcua.stack.core.types.structured.BrowseRequest) DataTypeDictionary(org.eclipse.milo.opcua.stack.core.types.DataTypeDictionary) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaBinaryDataTypeDictionary(org.eclipse.milo.opcua.stack.core.types.OpcUaBinaryDataTypeDictionary) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) BrowseNextResponse(org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextResponse) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) IOException(java.io.IOException) Ints(com.google.common.primitives.Ints) ViewDescription(org.eclipse.milo.opcua.stack.core.types.structured.ViewDescription) BrowseResultMask(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) InputStream(java.io.InputStream) Namespaces(org.eclipse.milo.opcua.stack.core.util.Namespaces) HashMap(java.util.HashMap) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList)

Example 2 with DataTypeDictionary

use of org.eclipse.milo.opcua.stack.core.types.DataTypeDictionary in project milo by eclipse.

the class DataTypeDictionaryReader method readDataTypeDictionaries.

public CompletableFuture<List<DataTypeDictionary<?>>> readDataTypeDictionaries() {
    CompletableFuture<List<ReferenceDescription>> browseFuture = browseNode(new BrowseDescription(Identifiers.OPCBinarySchema_TypeSystem, BrowseDirection.Forward, Identifiers.HasComponent, false, uint(NodeClass.Variable.getValue()), uint(BrowseResultMask.All.getValue())));
    CompletableFuture<Stream<NodeId>> dictionaryNodeIds = browseFuture.thenApply(references -> references.stream().filter(r -> r.getTypeDefinition().equalTo(Identifiers.DataTypeDictionaryType)).flatMap(r -> opt2stream(r.getNodeId().toNodeId(stackClient.getNamespaceTable()))));
    return dictionaryNodeIds.thenApply(nodeIds -> nodeIds.map(this::readDataTypeDictionary).collect(Collectors.toList())).thenCompose(FutureUtils::sequence).thenApply(list -> list.stream().filter(Objects::nonNull).collect(Collectors.toList()));
}
Also used : BrowseNextRequest(org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextRequest) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) LoggerFactory(org.slf4j.LoggerFactory) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) DictionaryDescription(org.eclipse.milo.opcua.binaryschema.parser.DictionaryDescription) Unpooled(io.netty.buffer.Unpooled) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) ByteArrayInputStream(java.io.ByteArrayInputStream) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription) Map(java.util.Map) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) BrowseResult(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) BsdParser(org.eclipse.milo.opcua.binaryschema.parser.BsdParser) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) CodecDescription(org.eclipse.milo.opcua.binaryschema.parser.CodecDescription) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) Objects(java.util.Objects) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) FutureUtils(org.eclipse.milo.opcua.stack.core.util.FutureUtils) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) StreamUtil.opt2stream(org.eclipse.milo.opcua.sdk.core.util.StreamUtil.opt2stream) BrowseResponse(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResponse) OpcUaSession(org.eclipse.milo.opcua.sdk.client.OpcUaSession) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) BrowseRequest(org.eclipse.milo.opcua.stack.core.types.structured.BrowseRequest) DataTypeDictionary(org.eclipse.milo.opcua.stack.core.types.DataTypeDictionary) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaBinaryDataTypeDictionary(org.eclipse.milo.opcua.stack.core.types.OpcUaBinaryDataTypeDictionary) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) BrowseNextResponse(org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextResponse) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) IOException(java.io.IOException) Ints(com.google.common.primitives.Ints) ViewDescription(org.eclipse.milo.opcua.stack.core.types.structured.ViewDescription) BrowseResultMask(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) InputStream(java.io.InputStream) Namespaces(org.eclipse.milo.opcua.stack.core.util.Namespaces) Objects(java.util.Objects) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription)

Aggregations

Preconditions (com.google.common.base.Preconditions)2 Lists (com.google.common.collect.Lists)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 ByteStreams (com.google.common.io.ByteStreams)2 Ints (com.google.common.primitives.Ints)2 ByteBuf (io.netty.buffer.ByteBuf)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 Unpooled (io.netty.buffer.Unpooled)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2