首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Milo OPC-UA添加节点

Java Milo OPC-UA添加节点
EN

Stack Overflow用户
提问于 2017-04-12 12:47:45
回答 1查看 1.3K关注 0票数 0

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

我已经在服务器端实现了AddNodes。我可以添加节点,读取节点和写入节点。下面是我在客户端做的事情:

代码语言:javascript
复制
// 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的回答下,我做了一些工作:调整了命名空间类中的write()。现在,我可以用EUInformation的值修改节点的显示名称和描述。下面是我的write()方法:

代码语言:javascript
复制
@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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-12 18:18:42

好的,您将添加一个新的VaribleNode和一个TypeDefinition of Property (Identifiers.PropertyType)。

然后,您将写入它的Value属性,以便它包含EUInformation对象:

代码语言:javascript
复制
EUInformation euInformation = ...

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

...write the value to the node you created...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43369992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档