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