0

I'm using Milo and its example server and client. I'm adding nodes to the server but I can't figure out how to add EuInformation, i.e., unit and description. I thought about using the ExtensionObject but since EuInformation does not implement Serializable I don't know how to pass it to the ExtensionObject. I'd also like to know how I can get the namespace ID and URI on client side. So far I just set them statically as I have access to the classes.

I've implemeted the AddNodes on server side. I can add nodes, read nodes and write to nodes. Here's what I'm doing on client side:

// 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();

EDIT

With the help of Kevin Herron's answer I worked something out: I adjusted the write() in my namespace class. I can now modify the display name and description of the node with the values of the EUInformation. Here's my write() method:

@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);
}
4

1 回答 1

0

好的,因此您将添加一个新的 VaribleNode,其 TypeDefinition 为 Property ( Identifiers.PropertyType)。

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

EUInformation euInformation = ...

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

...write the value to the node you created...
于 2017-04-12T18:18:42.377 回答