Search in sources :

Example 1 with WriteMask

use of org.eclipse.milo.opcua.sdk.core.WriteMask 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)

Aggregations

AccessLevel (org.eclipse.milo.opcua.sdk.core.AccessLevel)1 NumericRange (org.eclipse.milo.opcua.sdk.core.NumericRange)1 WriteMask (org.eclipse.milo.opcua.sdk.core.WriteMask)1 AttributeContext (org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext)1 UaException (org.eclipse.milo.opcua.stack.core.UaException)1 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)1 DateTime (org.eclipse.milo.opcua.stack.core.types.builtin.DateTime)1 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)1 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)1 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)1 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)1 NodeClass (org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass)1