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);
}
Aggregations