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