Search in sources :

Example 1 with VariableNode

use of org.eclipse.milo.opcua.sdk.core.nodes.VariableNode in project milo by eclipse.

the class AttributeReader method getEncodingId.

@Nullable
private static NodeId getEncodingId(AttributeContext context, UaServerNode node, QualifiedName encodingName) {
    // TODO avoid dynamic lookup by registering codecs with their associated DataType and Encoding name
    NodeId dataTypeId;
    if (node instanceof VariableNode) {
        dataTypeId = ((VariableNode) node).getDataType();
    } else if (node instanceof VariableTypeNode) {
        dataTypeId = ((VariableTypeNode) node).getDataType();
    } else {
        return null;
    }
    AddressSpaceManager addressSpaceManager = context.getServer().getAddressSpaceManager();
    UaNode dataTypeNode = addressSpaceManager.getManagedNode(dataTypeId).orElse(null);
    if (dataTypeNode != null) {
        return dataTypeNode.getReferences().stream().filter(r -> r.isForward() && Identifiers.HasEncoding.equals(r.getReferenceTypeId())).flatMap(r -> opt2stream(addressSpaceManager.getManagedNode(r.getTargetNodeId()))).filter(n -> encodingName.equals(n.getBrowseName())).map(Node::getNodeId).findFirst().orElse(null);
    } else {
        return null;
    }
}
Also used : DataTypeEncoding(org.eclipse.milo.opcua.stack.core.types.DataTypeEncoding) OpcUaDefaultBinaryEncoding(org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultBinaryEncoding) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) VariableTypeNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableTypeNode) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) AccessLevel(org.eclipse.milo.opcua.sdk.core.AccessLevel) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) AttributeUtil.getAccessLevels(org.eclipse.milo.opcua.sdk.server.util.AttributeUtil.getAccessLevels) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) OpcUaDefaultXmlEncoding(org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultXmlEncoding) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ArrayUtil(org.eclipse.milo.opcua.stack.core.util.ArrayUtil) Set(java.util.Set) NumericRange(org.eclipse.milo.opcua.sdk.core.NumericRange) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) OpcUaServer(org.eclipse.milo.opcua.sdk.server.OpcUaServer) Nullable(org.jetbrains.annotations.Nullable) AttributeUtil.getUserAccessLevels(org.eclipse.milo.opcua.sdk.server.util.AttributeUtil.getUserAccessLevels) AddressSpaceManager(org.eclipse.milo.opcua.sdk.server.api.AddressSpaceManager) AttributeContext(org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) UaException(org.eclipse.milo.opcua.stack.core.UaException) Optional(java.util.Optional) Node(org.eclipse.milo.opcua.sdk.core.nodes.Node) UaServerNode(org.eclipse.milo.opcua.sdk.server.nodes.UaServerNode) StreamUtil.opt2stream(org.eclipse.milo.opcua.sdk.core.util.StreamUtil.opt2stream) ArrayUtil.transformArray(org.eclipse.milo.opcua.stack.core.util.ArrayUtil.transformArray) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) VariableTypeNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableTypeNode) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) Node(org.eclipse.milo.opcua.sdk.core.nodes.Node) UaServerNode(org.eclipse.milo.opcua.sdk.server.nodes.UaServerNode) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) VariableTypeNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableTypeNode) AddressSpaceManager(org.eclipse.milo.opcua.sdk.server.api.AddressSpaceManager) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with VariableNode

use of org.eclipse.milo.opcua.sdk.core.nodes.VariableNode in project milo by eclipse.

the class AttributeDelegateChainTest method testCreate.

@Test
public void testCreate() throws Exception {
    List<String> list = new ArrayList<>();
    AttributeDelegate delegate = AttributeDelegateChain.create(new AttributeDelegate() {

        @Override
        public DataValue getValue(AttributeContext context, VariableNode node) throws UaException {
            list.add("root");
            return node.getValue();
        }
    }, parent -> new DelegatingAttributeDelegate(parent) {

        @Override
        public DataValue getValue(AttributeContext context, VariableNode node) throws UaException {
            list.add("child1");
            return super.getValue(context, node);
        }
    }, parent -> new DelegatingAttributeDelegate(parent) {

        @Override
        public DataValue getValue(AttributeContext context, VariableNode node) throws UaException {
            list.add("child2");
            return super.getValue(context, node);
        }
    }, parent -> new DelegatingAttributeDelegate(parent) {

        @Override
        public DataValue getValue(AttributeContext context, VariableNode node) throws UaException {
            list.add("child3");
            return super.getValue(context, node);
        }
    });
    UaNodeManager nodeManager = new UaNodeManager();
    UaNodeContext context = new UaNodeContext() {

        @Override
        public OpcUaServer getServer() {
            return null;
        }

        @Override
        public NodeManager<UaNode> getNodeManager() {
            return nodeManager;
        }

        @Override
        public NamespaceTable getNamespaceTable() {
            return new NamespaceTable();
        }
    };
    UaVariableNode node = new UaVariableNode.UaVariableNodeBuilder(context).setNodeId(NodeId.NULL_VALUE).setAccessLevel(AccessLevel.READ_WRITE).setBrowseName(QualifiedName.NULL_VALUE).setDisplayName(LocalizedText.NULL_VALUE).setDataType(Identifiers.String).setTypeDefinition(Identifiers.BaseDataVariableType).build();
    node.setValue(new DataValue(new Variant("foo")));
    DataValue value = delegate.getValue(new AttributeContext(null, null), node);
    assertEquals(value.getValue().getValue(), "foo");
    assertEquals(list.get(0), "child3");
    assertEquals(list.get(1), "child2");
    assertEquals(list.get(2), "child1");
    assertEquals(list.get(3), "root");
}
Also used : AttributeContext(org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) ArrayList(java.util.ArrayList) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) UaNodeManager(org.eclipse.milo.opcua.sdk.server.UaNodeManager) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) UaNodeContext(org.eclipse.milo.opcua.sdk.server.nodes.UaNodeContext) Test(org.testng.annotations.Test)

Example 3 with VariableNode

use of org.eclipse.milo.opcua.sdk.core.nodes.VariableNode in project milo by eclipse.

the class UaNode method setProperty.

public <T> void setProperty(QualifiedProperty<T> property, T value) {
    UShort namespaceIndex = context.getServer().getNamespaceTable().getIndex(property.getNamespaceUri());
    if (namespaceIndex == null) {
        throw new IllegalArgumentException("property belongs to unregistered " + "namespace: " + property.getNamespaceUri());
    }
    VariableNode node = getPropertyNode(property).orElseGet(() -> {
        String browseName = property.getBrowseName();
        NodeId propertyNodeId = new NodeId(getNodeId().getNamespaceIndex(), String.format("%s.%s", getNodeId().getIdentifier().toString(), browseName));
        PropertyTypeNode propertyNode = new PropertyTypeNode(context, propertyNodeId, new QualifiedName(namespaceIndex, browseName), LocalizedText.english(browseName), LocalizedText.NULL_VALUE, uint(0), uint(0));
        NodeId dataType = property.getDataType().toNodeId(context.getNamespaceTable()).orElse(NodeId.NULL_VALUE);
        propertyNode.setDataType(dataType);
        propertyNode.setValueRank(property.getValueRank());
        propertyNode.setArrayDimensions(property.getArrayDimensions());
        propertyNode.addReference(new Reference(propertyNode.getNodeId(), Identifiers.HasTypeDefinition, Identifiers.PropertyType.expanded(), true));
        addProperty(propertyNode);
        context.getNodeManager().addNode(propertyNode);
        return propertyNode;
    });
    node.setValue(new DataValue(new Variant(value)));
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) Reference(org.eclipse.milo.opcua.sdk.core.Reference) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) PropertyTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.variables.PropertyTypeNode)

Example 4 with VariableNode

use of org.eclipse.milo.opcua.sdk.core.nodes.VariableNode in project FAAAST-Service by FraunhoferIOSB.

the class OpcUaAssetConnection method registerValueProvider.

/**
 * {@inheritdoc}
 *
 * @throws AssetConnectionException if reference does not point to a
 *             {@link io.adminshell.aas.v3.model.Property}
 * @throws AssetConnectionException if referenced
 *             {@link io.adminshell.aas.v3.model.Property} does not have datatype
 *             defined
 * @throws AssetConnectionException if nodeId could not be parsed
 */
@Override
public void registerValueProvider(Reference reference, OpcUaValueProviderConfig valueProvider) throws AssetConnectionException {
    final String baseErrorMessage = "error registering value provider";
    TypeInfo typeInfo = serviceContext.getTypeInfo(reference);
    if (typeInfo == null) {
        throw new AssetConnectionException(String.format("%s - could not resolve type information (reference: %s)", baseErrorMessage, AasUtils.asString(reference)));
    }
    if (!ElementValueTypeInfo.class.isAssignableFrom(typeInfo.getClass())) {
        throw new AssetConnectionException(String.format("%s - reference must point to element with value (reference: %s)", baseErrorMessage, AasUtils.asString(reference)));
    }
    ElementValueTypeInfo valueTypeInfo = (ElementValueTypeInfo) typeInfo;
    if (!PropertyValue.class.isAssignableFrom(valueTypeInfo.getType())) {
        throw new AssetConnectionException(String.format("%s - unsupported element type (reference: %s, element type: %s)", baseErrorMessage, AasUtils.asString(reference), valueTypeInfo.getType()));
    }
    final Datatype datatype = valueTypeInfo.getDatatype();
    if (datatype == null) {
        throw new AssetConnectionException(String.format("%s - missing datatype (reference: %s)", baseErrorMessage, AasUtils.asString(reference)));
    }
    final VariableNode node;
    try {
        node = client.getAddressSpace().getVariableNode(parseNodeId(valueProvider.getNodeId()));
    } catch (UaException ex) {
        throw new AssetConnectionException(String.format("%s - could not parse nodeId (nodeId: %s)", baseErrorMessage, valueProvider.getNodeId()), ex);
    }
    this.valueProviders.put(reference, new AssetValueProvider() {

        @Override
        public DataElementValue getValue() throws AssetConnectionException {
            try {
                DataValue dataValue = client.readValue(0, TimestampsToReturn.Neither, node.getNodeId()).get();
                checkStatusCode(dataValue.getStatusCode(), "error reading value from asset conenction");
                return new PropertyValue(valueConverter.convert(dataValue.getValue(), datatype));
            } catch (AssetConnectionException | InterruptedException | ExecutionException e) {
                throw new AssetConnectionException(String.format("error reading value from asset conenction (reference: %s)", AasUtils.asString(reference)), e);
            }
        }

        @Override
        public void setValue(DataElementValue value) throws AssetConnectionException {
            if (value == null) {
                throw new AssetConnectionException(String.format("error setting value on asset connection - value must be non-null (reference: %s)", AasUtils.asString(reference)));
            }
            if (!PropertyValue.class.isAssignableFrom(value.getClass())) {
                throw new AssetConnectionException(String.format("error setting value on asset connection - unsupported element type (reference: %s, element type: %s)", AasUtils.asString(reference), value.getClass()));
            }
            try {
                StatusCode result = client.writeValue(node.getNodeId(), new DataValue(valueConverter.convert(((PropertyValue) value).getValue(), node.getDataType()), null, null)).get();
                checkStatusCode(result, "error setting value on asset connection");
            } catch (InterruptedException | ExecutionException e) {
                throw new AssetConnectionException("error writing asset connection value", e);
            }
        }
    });
}
Also used : DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)

Example 5 with VariableNode

use of org.eclipse.milo.opcua.sdk.core.nodes.VariableNode in project milo by eclipse.

the class EventContentFilter method validateSimpleOperand.

private static void validateSimpleOperand(FilterContext context, SimpleAttributeOperand select) throws ValidationException {
    NodeId eventTypeId = select.getTypeDefinitionId();
    if (eventTypeId != null && !eventTypeId.equals(Identifiers.BaseEventType)) {
        UaNode node = context.getServer().getAddressSpaceManager().getManagedNode(eventTypeId).orElse(null);
        if (node == null || node.getNodeClass() != NodeClass.ObjectType) {
            throw new ValidationException(StatusCodes.Bad_TypeDefinitionInvalid);
        }
        QualifiedName[] browsePath = select.getBrowsePath();
        if (browsePath == null || Arrays.stream(browsePath).anyMatch(Objects::isNull)) {
            throw new ValidationException(StatusCodes.Bad_BrowseNameInvalid);
        }
        Node relativeNode = getRelativeNode(context, node, browsePath);
        if (relativeNode == null) {
            throw new ValidationException(StatusCodes.Bad_NodeIdUnknown);
        }
        UInteger attributeId = select.getAttributeId();
        ImmutableSet<AttributeId> validAttributes = AttributeId.getAttributes(relativeNode.getNodeClass());
        boolean validAttribute = AttributeId.from(attributeId).map(validAttributes::contains).orElse(false);
        if (!validAttribute) {
            throw new ValidationException(StatusCodes.Bad_AttributeIdInvalid);
        }
        String indexRange = select.getIndexRange();
        if (indexRange != null) {
            if (relativeNode instanceof VariableNode) {
                int valueRank = ((VariableNode) relativeNode).getValueRank();
                if (valueRank == ValueRanks.Scalar) {
                    throw new ValidationException(StatusCodes.Bad_IndexRangeInvalid);
                }
            } else {
                throw new ValidationException(StatusCodes.Bad_IndexRangeInvalid);
            }
        }
    }
}
Also used : AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) BaseEventTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.objects.BaseEventTypeNode) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) Node(org.eclipse.milo.opcua.sdk.core.nodes.Node) ObjectTypeNode(org.eclipse.milo.opcua.sdk.core.nodes.ObjectTypeNode) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)

Aggregations

VariableNode (org.eclipse.milo.opcua.sdk.core.nodes.VariableNode)6 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)5 UaException (org.eclipse.milo.opcua.stack.core.UaException)4 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)4 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)4 AttributeContext (org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext)3 UaNode (org.eclipse.milo.opcua.sdk.server.nodes.UaNode)3 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)3 AccessLevel (org.eclipse.milo.opcua.sdk.core.AccessLevel)2 NumericRange (org.eclipse.milo.opcua.sdk.core.NumericRange)2 Node (org.eclipse.milo.opcua.sdk.core.nodes.Node)2 VariableTypeNode (org.eclipse.milo.opcua.sdk.core.nodes.VariableTypeNode)2 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)2 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)2 NodeClass (org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass)2 AssetConnectionException (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException)1 AssetValueProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider)1 DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)1 PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)1 Datatype (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype)1