Skip to content

Commit

Permalink
~ save point #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Sep 9, 2022
1 parent 054f266 commit 16cea5e
Show file tree
Hide file tree
Showing 388 changed files with 3,041 additions and 4,689 deletions.
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -106,16 +105,12 @@ private void registerCustomCodec(OpcUaClient client) {
// Register codec with the client's DataTypeManager instance.
// We need to register it by both its encodingId and its dataTypeId because it may be
// looked up by either depending on the context.

client.getStaticDataTypeManager().registerCodec(
binaryEncodingId,
new CustomStructType.Codec().asBinaryCodec()
);

client.getStaticDataTypeManager().registerCodec(
new QualifiedName(dataTypeId.getNamespaceIndex(), "CustomStructType"),
client.getStaticDataTypeManager().registerStructType(
dataTypeId,
new CustomStructType.Codec().asBinaryCodec()
new CustomStructType.Codec(),
binaryEncodingId,
null,
null
);
}

Expand Down
Expand Up @@ -68,7 +68,7 @@ public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throw
@Override
public String getEndpointUrl() {
// Change this if UaCPPServer is running somewhere other than localhost.
return "opc.tcp://localhost:48010";
return "opc.tcp://10.211.55.3:48010";
}

@Override
Expand Down
Expand Up @@ -116,7 +116,7 @@ private static StatusCode writeValue(OpcUaClient client, NodeId nodeId, DynamicS
@Override
public String getEndpointUrl() {
// Change this if UaCPPServer is running somewhere other than localhost.
return "opc.tcp://localhost:48010";
return "opc.tcp://10.211.55.3:48010";
}

@Override
Expand Down
Expand Up @@ -47,7 +47,6 @@
import org.eclipse.milo.opcua.stack.core.BuiltinDataType;
import org.eclipse.milo.opcua.stack.core.NodeIds;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultBinaryEncoding;
import org.eclipse.milo.opcua.stack.core.types.builtin.ByteString;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime;
Expand Down Expand Up @@ -809,6 +808,12 @@ private void registerCustomEnumType() throws Exception {
// Populate the OPC UA 1.04+ DataTypeDefinition attribute
dataTypeNode.setDataTypeDefinition(definition);

// Register Codecs for each supported encoding with DataTypeManager
getNodeContext().getServer().getDataTypeManager().registerEnumType(
dataTypeId,
new CustomEnumType.Codec()
);

// Prior to OPC UA 1.04, clients that needed to interpret custom types read the
// DataTypeDictionary from the server. We describe the type using StructureDefinition
// or EnumDefinition and register it with the dictionary manager.
Expand Down Expand Up @@ -908,15 +913,12 @@ private void registerCustomStructType() throws Exception {
dataTypeNode.setDataTypeDefinition(definition);

// Register Codecs for each supported encoding with DataTypeManager
getNodeContext().getServer().getDataTypeManager().registerCodec(
binaryEncodingId,
new CustomStructType.Codec().asBinaryCodec()
);

getNodeContext().getServer().getDataTypeManager().registerCodec(
OpcUaDefaultBinaryEncoding.ENCODING_NAME,
getNodeContext().getServer().getDataTypeManager().registerStructType(
dataTypeId,
new CustomStructType.Codec().asBinaryCodec()
new CustomStructType.Codec(),
binaryEncodingId,
null,
null
);

// Prior to OPC UA 1.04, clients that needed to interpret custom types read the
Expand Down Expand Up @@ -996,6 +998,15 @@ private void registerCustomUnionType() throws Exception {

dataTypeNode.setDataTypeDefinition(definition);

// Register Codecs for each supported encoding with DataTypeManager
getNodeContext().getServer().getDataTypeManager().registerStructType(
dataTypeId,
new CustomUnionType.Codec(),
binaryEncodingId,
null,
null
);

dictionaryManager.registerUnionCodec(
new CustomUnionType.Codec().asBinaryCodec(),
"CustomUnionType",
Expand Down
Expand Up @@ -42,6 +42,11 @@ public int getValue() {
return value;
}

@Override
public ExpandedNodeId getTypeId() {
return TYPE_ID;
}

@Nullable
public static CustomEnumType from(int value) {
switch (value) {
Expand All @@ -63,12 +68,12 @@ public Class<CustomEnumType> getType() {
}

@Override
public CustomEnumType decode(SerializationContext context, UaDecoder decoder) {
public CustomEnumType decodeType(SerializationContext context, UaDecoder decoder) {
return CustomEnumType.from(decoder.readInt32(null));
}

@Override
public void encode(SerializationContext context, UaEncoder encoder, CustomEnumType value) {
public void encodeType(SerializationContext context, UaEncoder encoder, CustomEnumType value) {
encoder.writeInt32(null, value.getValue());
}
}
Expand Down
Expand Up @@ -120,7 +120,7 @@ public Class<CustomStructType> getType() {
}

@Override
public CustomStructType decode(
public CustomStructType decodeType(
SerializationContext context,
UaDecoder decoder
) throws UaSerializationException {
Expand All @@ -134,7 +134,7 @@ public CustomStructType decode(
}

@Override
public void encode(
public void encodeType(
SerializationContext context,
UaEncoder encoder, CustomStructType value
) throws UaSerializationException {
Expand Down
Expand Up @@ -107,7 +107,7 @@ public Class<CustomUnionType> getType() {
}

@Override
public CustomUnionType decode(SerializationContext context, UaDecoder decoder) {
public CustomUnionType decodeType(SerializationContext context, UaDecoder decoder) {
UInteger switchValue = decoder.readUInt32("SwitchValue");
switch (switchValue.intValue()) {
case 0:
Expand All @@ -129,7 +129,7 @@ public CustomUnionType decode(SerializationContext context, UaDecoder decoder) {
}

@Override
public void encode(SerializationContext context, UaEncoder encoder, CustomUnionType value) {
public void encodeType(SerializationContext context, UaEncoder encoder, CustomUnionType value) {
encoder.writeUInt32("SwitchValue", uint(value.type.ordinal()));
switch (value.type) {
case Null:
Expand Down
Expand Up @@ -187,7 +187,7 @@ public OpcUaBinaryDataTypeDictionary getDictionary() {
return dictionary;
}

public void registerEnumCodec(OpcUaBinaryDataTypeCodec<?> codec, String description, NodeId dataTypeId) {
public void registerEnumCodec(OpcUaBinaryDataTypeCodec codec, String description, NodeId dataTypeId) {
dictionary.registerEnumCodec(codec, description, dataTypeId);

// TODO figure out a way to not require re-registration every time...
Expand All @@ -203,7 +203,7 @@ public void registerEnumDescription(EnumDescription description) {
}

public void registerOptionSetCodec(
OpcUaBinaryDataTypeCodec<?> codec,
OpcUaBinaryDataTypeCodec codec,
String description,
NodeId dataTypeId,
NodeId binaryEncodingId
Expand All @@ -213,7 +213,7 @@ public void registerOptionSetCodec(
}

public void registerStructureCodec(
OpcUaBinaryDataTypeCodec<?> codec,
OpcUaBinaryDataTypeCodec codec,
String description,
NodeId dataTypeId,
NodeId binaryEncodingId
Expand All @@ -223,7 +223,7 @@ public void registerStructureCodec(
}

public void registerUnionCodec(
OpcUaBinaryDataTypeCodec<?> codec,
OpcUaBinaryDataTypeCodec codec,
String description,
NodeId dataTypeId,
NodeId binaryEncodingId
Expand All @@ -233,7 +233,7 @@ public void registerUnionCodec(
}

public void registerStructureCodec(
OpcUaBinaryDataTypeCodec<?> codec,
OpcUaBinaryDataTypeCodec codec,
String description,
NodeId dataTypeId,
NodeId binaryEncodingId,
Expand Down
Expand Up @@ -40,6 +40,8 @@ public CompletableFuture<Unit> initialize(UaStackClient client, OpcUaSession ses
bsdParser
);

// TODO this needs to separately register enum/struct codecs now, not just the dictionary

return reader.readDataTypeDictionaries()
.thenAccept(dictionaries ->
dictionaries.forEach(
Expand Down
Expand Up @@ -19,9 +19,6 @@
import org.eclipse.milo.opcua.sdk.core.types.DynamicStructCodec;
import org.eclipse.milo.opcua.stack.client.UaStackClient;
import org.eclipse.milo.opcua.stack.core.NodeIds;
import org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultBinaryEncoding;
import org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultJsonEncoding;
import org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultXmlEncoding;
import org.eclipse.milo.opcua.stack.core.types.structured.DataTypeDefinition;
import org.eclipse.milo.opcua.stack.core.types.structured.EnumDefinition;
import org.eclipse.milo.opcua.stack.core.types.structured.StructureDefinition;
Expand Down Expand Up @@ -80,77 +77,36 @@ public CompletableFuture<Unit> initialize(UaStackClient stackClient, OpcUaSessio
.thenApply(v -> Unit.VALUE);
}

private static void registerCodecs(UaStackClient stackClient, DataTypeTree tree) {
Tree<DataType> structureNode = tree.getTreeNode(NodeIds.Structure);
private static void registerCodecs(UaStackClient stackClient, DataTypeTree dataTypeTree) {
Tree<DataType> structureNode = dataTypeTree.getTreeNode(NodeIds.Structure);
if (structureNode != null) {
structureNode.traverse(dataType -> {
DataTypeDefinition definition = dataType.getDataTypeDefinition();

if (definition instanceof StructureDefinition) {
var codec = new DynamicStructCodec(dataType);

if (dataType.getBinaryEncodingId() != null) {
stackClient.getDynamicDataTypeManager().registerCodec(
dataType.getBinaryEncodingId(),
codec.asBinaryCodec()
);
stackClient.getDynamicDataTypeManager().registerCodec(
OpcUaDefaultBinaryEncoding.ENCODING_NAME,
dataType.getNodeId(),
codec.asBinaryCodec()
);
}
if (dataType.getXmlEncodingId() != null) {
stackClient.getDynamicDataTypeManager().registerCodec(
dataType.getXmlEncodingId(),
codec.asXmlCodec()
);
stackClient.getDynamicDataTypeManager().registerCodec(
OpcUaDefaultXmlEncoding.ENCODING_NAME,
dataType.getNodeId(),
codec.asXmlCodec()
);
}
if (dataType.getJsonEncodingId() != null) {
stackClient.getDynamicDataTypeManager().registerCodec(
dataType.getJsonEncodingId(),
codec.asJsonCodec()
);
stackClient.getDynamicDataTypeManager().registerCodec(
OpcUaDefaultJsonEncoding.ENCODING_NAME,
dataType.getNodeId(),
codec.asJsonCodec()
);
}
stackClient.getDynamicDataTypeManager().registerStructType(
dataType.getNodeId(),
new DynamicStructCodec(dataTypeTree, dataType),
dataType.getBinaryEncodingId(),
dataType.getXmlEncodingId(),
dataType.getJsonEncodingId()
);
}
});
} else {
LoggerFactory.getLogger(DataTypeTreeSessionInitializer.class)
.warn("Tree for NodeIds.Structure not found; is the server DataType hierarchy sane?");
}

Tree<DataType> enumerationNode = tree.getTreeNode(NodeIds.Enumeration);
Tree<DataType> enumerationNode = dataTypeTree.getTreeNode(NodeIds.Enumeration);
if (enumerationNode != null) {
enumerationNode.traverse(dataType -> {
DataTypeDefinition definition = dataType.getDataTypeDefinition();

if (definition instanceof EnumDefinition) {
var codec = new DynamicEnumCodec(dataType);

stackClient.getDynamicDataTypeManager().registerCodec(
OpcUaDefaultBinaryEncoding.ENCODING_NAME,
dataType.getNodeId(),
codec.asBinaryCodec()
);
stackClient.getDynamicDataTypeManager().registerCodec(
OpcUaDefaultXmlEncoding.ENCODING_NAME,
dataType.getNodeId(),
codec.asXmlCodec()
);
stackClient.getDynamicDataTypeManager().registerCodec(
OpcUaDefaultJsonEncoding.ENCODING_NAME,
stackClient.getDynamicDataTypeManager().registerEnumType(
dataType.getNodeId(),
codec.asJsonCodec()
new DynamicEnumCodec(dataType)
);
}
});
Expand Down
Expand Up @@ -16,6 +16,8 @@
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName;
import org.eclipse.milo.opcua.stack.core.types.structured.DataTypeDefinition;
import org.eclipse.milo.opcua.stack.core.types.structured.EnumDefinition;
import org.eclipse.milo.opcua.stack.core.types.structured.StructureDefinition;
import org.jetbrains.annotations.Nullable;

/**
Expand Down Expand Up @@ -117,6 +119,14 @@ public NodeId getNodeId() {
return dataTypeDefinition;
}

public boolean isEnum() {
return dataTypeDefinition instanceof EnumDefinition;
}

public boolean isStruct() {
return dataTypeDefinition instanceof StructureDefinition;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -153,5 +163,4 @@ public String toString() {
.add("dataTypeDefinition=" + dataTypeDefinition)
.toString();
}

}
Expand Up @@ -202,6 +202,26 @@ public boolean isAssignable(NodeId dataTypeId, Class<?> clazz) {
}
}

public boolean isEnumType(NodeId dataTypeId) {
return isSubtypeOf(dataTypeId, NodeIds.Enumeration);
}

public boolean isStructType(NodeId dataTypeId) {
return isSubtypeOf(dataTypeId, NodeIds.Structure);
}

public boolean isSubtypeOf(NodeId dataTypeId, NodeId superDataTypeId) {
Tree<DataType> tree = getTreeNode(dataTypeId);
if (tree == null) return false;

Tree<DataType> parentTree = tree.getParent();
if (parentTree == null) return false;

NodeId parentDataTypeId = parentTree.getValue().getNodeId();

return parentDataTypeId.equals(superDataTypeId) || isSubtypeOf(parentDataTypeId, superDataTypeId);
}

/**
* Get the underlying {@link Tree} structure.
*
Expand Down

0 comments on commit 16cea5e

Please sign in to comment.