繁体   English   中英

Java Milo OPC-UA添加节点

[英]Java Milo OPC-UA add node

我正在使用Milo及其示例服务器和客户端。 我正在向服务器添加节点,但是我不知道如何添加EuInformation ,即单位和描述。 我考虑过使用ExtensionObject但是由于EuInformation没有实现Serializable所以我不知道如何将其传递给ExtensionObject 我还想知道如何在客户端获取名称空间ID和URI。 到目前为止,我可以访问这些类,因此只是静态地设置它们。

我已经在服务器端实现了AddNodes。 我可以添加节点,读取节点并写入节点。 这是我在客户端执行的操作:

// Should somehow get the namespace ID and namespace dynamically.
// Maybe by iterating through all nodes??
ExpandedNodeId parentNodeId = new ExpandedNodeId(
                                    new nodeId(2,DatatypeNamespace.NODE_IDENTIFIER),
                                    datatypeNamespace.NAMESPACE_URI, 0);

NodeId referenceTypeId = Identifiers.String;

// Define the new node.
ExpandedNodeId requestedNewNodeId = new ExpandedNodeId(new NodeId(2, "NewNode"),
                                                    DatatypeNamespace.NAMESPACE_URI, 0);

QualifiedName browseName = new QualifiedName(2, "NewNode");

// How to get this to the server??
EUInformation euinfo = new EUInformation(null,-1,LocalizedText.english("MyUnit"),
                                              LocalizedText.english("My Description"));

ExpandedNodeId typeDef = new ExpandedNodeId(Identifiers.BaseVariableType,
                                                    DatatypeNamespace.NAMESPACE_URI, 0);

AddNodesItem newItem = new AddNodesItem(parentNodeId, referenceTypeId,
                 requestedNewNodeId,rowseName,NodeClass.VariableType, null, typeDef);

List<AddNodesItem> items = new ArrayList<AddNodesItem>();
        items.add(newItem);

client.addNodes(items).get();

编辑

借助Kevin Herron的答案,我得出了一些解决方案:我在命名空间类中调整了write() 现在,我可以使用EUInformation的值修改节点的显示名称和描述。 这是我的write()方法:

@Override
public void write(WriteContext context, List<WriteValue> writeValues) {
    List<StatusCode> results = Lists.newArrayListWithCapacity(writeValues.size());

    for (WriteValue writeValue : writeValues) {
        ServerNode node = server.getNodeMap().get(writeValue.getNodeId());

        if (node != null) {
            // Get the type of the variant thats about to be written to the node
            NodeId variantType = writeValue.getValue().getValue().getDataType().get();
            if (variantType.equals(Identifiers.Structure)) {
                ExtensionObject o = (ExtensionObject) writeValue.getValue().getValue().getValue();
                if (o.getEncodingTypeId().equals(Identifiers.EUInformation_Encoding_DefaultBinary)) {

                    EUInformation euInformation = (EUInformation) o.decode();
                    node.setDescription(euInformation.getDescription());
                    node.setDisplayName(euInformation.getDisplayName());
                    System.out.println("Wrote EUInformation " + euInformation);
                    results.add(StatusCode.GOOD);
                    context.complete(results);
                    return;
                }
            }
            try {

                node.writeAttribute(new AttributeContext(context), writeValue.getAttributeId(),
                        writeValue.getValue(), writeValue.getIndexRange());

                results.add(StatusCode.GOOD);

                System.out.println(String.format("Wrote value %s to %s attribute of %s",
                        writeValue.getValue().getValue(),
                        AttributeId.from(writeValue.getAttributeId()).map(Object::toString).orElse("unknown"),
                        node.getNodeId()));
            } catch (UaException e) {
                System.out.println(String.format("Unable to write %s", writeValue.getValue()));
                results.add(e.getStatusCode());
            }
        } else {
            results.add(new StatusCode(StatusCodes.Bad_NodeIdUnknown));
        }
    }

    context.complete(results);
}

好的,因此您将添加一个具有TypeType属性( Identifiers.PropertyType )的新VaribleNode。

然后,您将写入其Value属性,使其包含EUInformation对象:

EUInformation euInformation = ...

Variant v = new Variant(ExtensionObject.encode(euInformation));

...write the value to the node you created...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM