簡體   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