Skip to content

Commit

Permalink
WIP dynamic struct/enum codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron committed Sep 6, 2022
1 parent fac2198 commit c9d66e4
Show file tree
Hide file tree
Showing 4 changed files with 706 additions and 1 deletion.
Expand Up @@ -14,8 +14,16 @@

import org.eclipse.milo.opcua.sdk.client.session.SessionFsm;
import org.eclipse.milo.opcua.sdk.core.DataTypeTree;
import org.eclipse.milo.opcua.sdk.core.DataTypeTree.DataType;
import org.eclipse.milo.opcua.stack.client.UaStackClient;
import org.eclipse.milo.opcua.stack.core.NodeIds;
import org.eclipse.milo.opcua.stack.core.types.DynamicStructCodec;
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.eclipse.milo.opcua.stack.core.util.Tree;
import org.eclipse.milo.opcua.stack.core.util.Unit;
import org.slf4j.LoggerFactory;

/**
* Builds a {@link DataTypeTree} and stores it on an {@link OpcUaSession} as an attribute under
Expand All @@ -34,7 +42,55 @@ public class DataTypeTreeSessionInitializer implements SessionFsm.SessionInitial
@Override
public CompletableFuture<Unit> initialize(UaStackClient stackClient, OpcUaSession session) {
return DataTypeTreeBuilder.buildAsync(stackClient, session)
.thenAccept(tree -> session.setAttribute(SESSION_ATTRIBUTE_KEY, tree))
.thenAccept(tree -> {
session.setAttribute(SESSION_ATTRIBUTE_KEY, tree);

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

if (definition instanceof StructureDefinition) {
var codec = new DynamicStructCodec((StructureDefinition) definition);
if (dataType.getBinaryEncodingId() != null) {
stackClient.getDynamicDataTypeManager().registerCodec(
dataType.getBinaryEncodingId(),
codec
);
}
if (dataType.getXmlEncodingId() != null) {
stackClient.getDynamicDataTypeManager().registerCodec(
dataType.getXmlEncodingId(),
codec
);
}
if (dataType.getJsonEncodingId() != null) {
stackClient.getDynamicDataTypeManager().registerCodec(
dataType.getJsonEncodingId(),
codec
);
}
}
});
} 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);
if (enumerationNode != null) {
enumerationNode.traverse(dataType -> {
DataTypeDefinition definition = dataType.getDataTypeDefinition();

if (definition instanceof EnumDefinition) {
// TODO register DynamicEnumCodec
}
});
} else {
LoggerFactory.getLogger(DataTypeTreeSessionInitializer.class)
.warn("Tree for NodeIds.Enumeration not found; is the server DataType hierarchy sane?");
}
})
.thenApply(v -> Unit.VALUE);
}

Expand Down
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2022 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.milo.opcua.stack.core.types;

import java.util.LinkedHashMap;

import org.eclipse.milo.opcua.stack.core.types.structured.Structure;

public class DynamicStruct extends Structure {

private final LinkedHashMap<String, Object> members;

public DynamicStruct(LinkedHashMap<String, Object> members) {
this.members = members;
}

public LinkedHashMap<String, Object> getMembers() {
return members;
}

}

0 comments on commit c9d66e4

Please sign in to comment.