Search in sources :

Example 1 with AttributeContext

use of org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext 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 AttributeContext

use of org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext in project milo by eclipse.

the class AttributeWriter method writeAttribute.

public static void writeAttribute(AttributeContext context, UaServerNode node, AttributeId attributeId, DataValue value, @Nullable String indexRange) throws UaException {
    AttributeContext internalContext = new AttributeContext(context.getServer());
    NodeClass nodeClass = node.getNodeClass();
    if (attributeId == AttributeId.Value && nodeClass == NodeClass.Variable) {
        Set<AccessLevel> accessLevels = getAccessLevels(node, internalContext);
        if (!accessLevels.contains(AccessLevel.CurrentWrite)) {
            throw new UaException(StatusCodes.Bad_NotWritable);
        }
        Set<AccessLevel> userAccessLevels = getUserAccessLevels(node, context);
        if (!userAccessLevels.contains(AccessLevel.CurrentWrite)) {
            throw new UaException(StatusCodes.Bad_UserAccessDenied);
        }
    } else {
        WriteMask writeMask = writeMaskForAttribute(attributeId);
        Set<WriteMask> writeMasks = getWriteMasks(node, internalContext);
        if (!writeMasks.contains(writeMask)) {
            throw new UaException(StatusCodes.Bad_NotWritable);
        }
        Set<WriteMask> userWriteMasks = getUserWriteMasks(node, context);
        if (!userWriteMasks.contains(writeMask)) {
            throw new UaException(StatusCodes.Bad_UserAccessDenied);
        }
    }
    Variant updateVariant = value.getValue();
    if (indexRange != null) {
        NumericRange range = NumericRange.parse(indexRange);
        DataValue current = node.getAttribute(internalContext, attributeId);
        Variant currentVariant = current.getValue();
        Object valueAtRange = NumericRange.writeToValueAtRange(currentVariant, updateVariant, range);
        updateVariant = new Variant(valueAtRange);
    }
    DateTime sourceTime = value.getSourceTime();
    DateTime serverTime = value.getServerTime();
    value = new DataValue(updateVariant, value.getStatusCode(), (sourceTime == null || sourceTime.isNull()) ? DateTime.now() : sourceTime, (serverTime == null || serverTime.isNull()) ? DateTime.now() : serverTime);
    if (attributeId == AttributeId.Value) {
        NodeId dataType = extract(node.getAttribute(internalContext, AttributeId.DataType));
        if (dataType != null) {
            value = validateDataType(context.getServer(), dataType, value);
        }
        Integer valueRank = extract(node.getAttribute(internalContext, AttributeId.ValueRank));
        if (valueRank == null)
            valueRank = 0;
        if (valueRank > 0) {
            UInteger[] arrayDimensions = extract(node.getAttribute(context, AttributeId.ArrayDimensions));
            validateArrayType(valueRank, arrayDimensions, value);
        }
    }
    node.setAttribute(context, attributeId, value);
}
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) AccessLevel(org.eclipse.milo.opcua.sdk.core.AccessLevel) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) WriteMask(org.eclipse.milo.opcua.sdk.core.WriteMask) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) NumericRange(org.eclipse.milo.opcua.sdk.core.NumericRange) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)

Example 3 with AttributeContext

use of org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext 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 4 with AttributeContext

use of org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext in project milo by eclipse.

the class ManagedAddressSpace method write.

@Override
public void write(WriteContext context, List<WriteValue> writeValues) {
    List<StatusCode> results = Lists.newArrayListWithCapacity(writeValues.size());
    for (WriteValue writeValue : writeValues) {
        UaServerNode node = nodeManager.get(writeValue.getNodeId());
        if (node != null) {
            try {
                node.writeAttribute(new AttributeContext(context), writeValue.getAttributeId(), writeValue.getValue(), writeValue.getIndexRange());
                results.add(StatusCode.GOOD);
                logger.debug("Wrote value {} to {} attribute of {}", writeValue.getValue().getValue(), AttributeId.from(writeValue.getAttributeId()).map(Object::toString).orElse("unknown"), node.getNodeId());
            } catch (UaException e) {
                logger.error("Unable to write value={}", writeValue.getValue(), e);
                results.add(e.getStatusCode());
            }
        } else {
            results.add(new StatusCode(StatusCodes.Bad_NodeIdUnknown));
        }
    }
    context.success(results);
}
Also used : WriteValue(org.eclipse.milo.opcua.stack.core.types.structured.WriteValue) AttributeContext(org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext) UaException(org.eclipse.milo.opcua.stack.core.UaException) UaServerNode(org.eclipse.milo.opcua.sdk.server.nodes.UaServerNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 5 with AttributeContext

use of org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext in project milo by eclipse.

the class ManagedAddressSpace method read.

@Override
public void read(ReadContext context, Double maxAge, TimestampsToReturn timestamps, List<ReadValueId> readValueIds) {
    List<DataValue> results = Lists.newArrayListWithCapacity(readValueIds.size());
    for (ReadValueId readValueId : readValueIds) {
        UaServerNode node = nodeManager.get(readValueId.getNodeId());
        if (node != null) {
            DataValue value = node.readAttribute(new AttributeContext(context), readValueId.getAttributeId(), timestamps, readValueId.getIndexRange(), readValueId.getDataEncoding());
            logger.debug("Read value {} from attribute {} of {}", value.getValue().getValue(), AttributeId.from(readValueId.getAttributeId()).map(Object::toString).orElse("unknown"), node.getNodeId());
            results.add(value);
        } else {
            results.add(new DataValue(StatusCodes.Bad_NodeIdUnknown));
        }
    }
    context.success(results);
}
Also used : ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) AttributeContext(org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaServerNode(org.eclipse.milo.opcua.sdk.server.nodes.UaServerNode)

Aggregations

AttributeContext (org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext)10 UaException (org.eclipse.milo.opcua.stack.core.UaException)7 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)6 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)5 VariableNode (org.eclipse.milo.opcua.sdk.core.nodes.VariableNode)4 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)4 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)4 NodeClass (org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass)4 AccessLevel (org.eclipse.milo.opcua.sdk.core.AccessLevel)3 NumericRange (org.eclipse.milo.opcua.sdk.core.NumericRange)3 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)3 UaNode (org.eclipse.milo.opcua.sdk.server.nodes.UaNode)3 UaServerNode (org.eclipse.milo.opcua.sdk.server.nodes.UaServerNode)3 UaVariableNode (org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode)3 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 Reference (org.eclipse.milo.opcua.sdk.core.Reference)2 Node (org.eclipse.milo.opcua.sdk.core.nodes.Node)2 VariableTypeNode (org.eclipse.milo.opcua.sdk.core.nodes.VariableTypeNode)2 StreamUtil.opt2stream (org.eclipse.milo.opcua.sdk.core.util.StreamUtil.opt2stream)2