use of org.eclipse.milo.opcua.sdk.core.NumericRange 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);
}
use of org.eclipse.milo.opcua.sdk.core.NumericRange in project milo by eclipse.
the class DataTypeDictionaryReaderTest method testReadDataTypeDictionaryBytes.
private void testReadDataTypeDictionaryBytes(ByteString dictionary, int fragmentSize) throws Exception {
Mockito.when(stackClient.sendRequest(ArgumentMatchers.any(ReadRequest.class))).then(invocationOnMock -> {
ReadRequest readRequest = invocationOnMock.getArgument(0);
List<ReadValueId> readValueIds = Arrays.stream(Objects.requireNonNull(readRequest.getNodesToRead())).collect(Collectors.toList());
ReadValueId readValueId = readValueIds.get(0);
NumericRange numericRange = NumericRange.parse(readValueId.getIndexRange());
try {
Object fragment = NumericRange.readFromValueAtRange(new Variant(dictionary), numericRange);
return completedFuture(new ReadResponse(null, new DataValue[] { new DataValue(new Variant(fragment)) }, null));
} catch (UaException e) {
return completedFuture(new ReadResponse(null, new DataValue[] { new DataValue(e.getStatusCode()) }, null));
}
});
ByteString typeDictionaryBs = dictionaryReader.readDataTypeDictionaryBytes(NodeId.NULL_VALUE, fragmentSize).get();
Assertions.assertEquals(typeDictionaryBs, dictionary);
}
use of org.eclipse.milo.opcua.sdk.core.NumericRange in project milo by eclipse.
the class AttributeReader method readAttribute.
public static DataValue readAttribute(AttributeContext context, UaServerNode node, AttributeId attributeId, @Nullable TimestampsToReturn timestamps, @Nullable String indexRange, @Nullable QualifiedName encodingName) {
try {
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.CurrentRead)) {
throw new UaException(StatusCodes.Bad_NotReadable);
}
Set<AccessLevel> userAccessLevels = getUserAccessLevels(node, context);
if (!userAccessLevels.contains(AccessLevel.CurrentRead)) {
throw new UaException(StatusCodes.Bad_UserAccessDenied);
}
}
if (encodingName != null && encodingName.isNotNull()) {
if (attributeId != AttributeId.Value) {
throw new UaException(StatusCodes.Bad_DataEncodingInvalid);
}
NodeId dataTypeId;
if (node instanceof VariableNode) {
dataTypeId = ((VariableNode) node).getDataType();
} else if (node instanceof VariableTypeNode) {
dataTypeId = ((VariableTypeNode) node).getDataType();
} else {
throw new UaException(StatusCodes.Bad_DataEncodingInvalid);
}
boolean structured = isStructureSubtype(context.getServer(), dataTypeId);
if (!structured) {
throw new UaException(StatusCodes.Bad_DataEncodingInvalid);
}
}
final DataValue.Builder dvb = node.getAttribute(context, attributeId).copy();
// Maybe transcode the structure...
if (dvb.value.isNotNull()) {
final Object valueObject = dvb.value.getValue();
Class<?> valueClazz = valueObject.getClass();
if (valueClazz.isArray() && ArrayUtil.getType(valueObject) == ExtensionObject.class) {
Object newValue = transformArray(valueObject, (ExtensionObject xo) -> transcode(context, node, xo, encodingName), ExtensionObject.class);
dvb.setValue(new Variant(newValue));
} else if (valueClazz == ExtensionObject.class) {
ExtensionObject xo = (ExtensionObject) valueObject;
Object newValue = transcode(context, node, xo, encodingName);
dvb.setValue(new Variant(newValue));
}
}
// Apply index range if provided...
if (indexRange != null) {
NumericRange range = NumericRange.parse(indexRange);
Object valueAtRange = NumericRange.readFromValueAtRange(dvb.value, range);
dvb.setValue(new Variant(valueAtRange));
}
// Add or remove timestamps based on TimestampsToReturn...
if (timestamps != null) {
dvb.applyTimestamps(attributeId, timestamps);
}
return dvb.build();
} catch (UaException e) {
return new DataValue(e.getStatusCode());
}
}
Aggregations