For a small project, I set up an OPC UA client using Eclipse Milo. I want to test it with some unit tests. I've figured out how to set up the testing server, but I'm having trouble setting up some nodes to read values from.
I looked at the example on GitHub, but I feel it would be a bit overkill for my use case.
Is there a simple way to add and manipulate a node?
Here's the code I have so far:
import .eclipse.milo.opcua.sdk.core.AccessLevel;
import .eclipse.milo.opcua.sdk.server.OpcUaServer;
import .eclipse.milo.opcua.sdk.server.api.*;
import .eclipse.milo.opcua.sdk.server.api.config.OpcUaServerConfig;
import .eclipse.milo.opcua.sdk.server.api.services.NodeManagementServices;
import .eclipse.milo.opcua.sdk.server.nodes.UaNode;
import .eclipse.milo.opcua.sdk.server.nodes.UaNodeContext;
import .eclipse.milo.opcua.sdk.server.nodes.UaVariableNode;
import .eclipse.milo.opcua.stack.core.Identifiers;
import .eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import .eclipse.milo.opcua.stack.core.types.builtin.*;
import .eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
import .eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode;
import .eclipse.milo.opcua.stack.core.types.structured.AddNodesItem;
import .eclipse.milo.opcua.stack.server.EndpointConfiguration;
import .junit.jupiter.api.*;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static .eclipse.milo.opcua.sdk.core.WriteMask.UserAccessLevel;
import static .junit.jupiter.api.Assertions.*;
class OPCUACommunicatorTest {
private static OpcUaServer server;
private static UShort namespaceId;
private static final String TEST_ENDPOINT = "opc.tcp://localhost:4840";
private static final String TEST_NAMESPACE = "urn:test:namespace";
@BeforeAll
static void setupServer() throws Exception {
server = new OpcUaServer(OpcUaServerConfig.builder()
.setApplicationUri("urn:test:server")
.setEndpoints(Set.of(
EndpointConfiguration.newBuilder()
.setBindPort(4840)
.setSecurityPolicy(SecurityPolicy.None)
.setSecurityMode(MessageSecurityMode.None)
.setHostname("localhost")
.setPath("/test")
.build()
))
.build()
);
server.startup().get(); // Server starten
namespaceId = server.getNamespaceTable().addUri(TEST_NAMESPACE); // Namespace hinzufügen
}
@AfterAll
static void shutdownServer() throws Exception {
server.shutdown().get();
}
@Test
void testOpcUaConnection() throws ExecutionException, InterruptedException {
OPCUACommunicator communicator = new OPCUACommunicator(TEST_ENDPOINT);
assertDoesNotThrow(() -> communicator.setConnectorString(TEST_ENDPOINT));
}
}
For a small project, I set up an OPC UA client using Eclipse Milo. I want to test it with some unit tests. I've figured out how to set up the testing server, but I'm having trouble setting up some nodes to read values from.
I looked at the example on GitHub, but I feel it would be a bit overkill for my use case.
Is there a simple way to add and manipulate a node?
Here's the code I have so far:
import .eclipse.milo.opcua.sdk.core.AccessLevel;
import .eclipse.milo.opcua.sdk.server.OpcUaServer;
import .eclipse.milo.opcua.sdk.server.api.*;
import .eclipse.milo.opcua.sdk.server.api.config.OpcUaServerConfig;
import .eclipse.milo.opcua.sdk.server.api.services.NodeManagementServices;
import .eclipse.milo.opcua.sdk.server.nodes.UaNode;
import .eclipse.milo.opcua.sdk.server.nodes.UaNodeContext;
import .eclipse.milo.opcua.sdk.server.nodes.UaVariableNode;
import .eclipse.milo.opcua.stack.core.Identifiers;
import .eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import .eclipse.milo.opcua.stack.core.types.builtin.*;
import .eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
import .eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode;
import .eclipse.milo.opcua.stack.core.types.structured.AddNodesItem;
import .eclipse.milo.opcua.stack.server.EndpointConfiguration;
import .junit.jupiter.api.*;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static .eclipse.milo.opcua.sdk.core.WriteMask.UserAccessLevel;
import static .junit.jupiter.api.Assertions.*;
class OPCUACommunicatorTest {
private static OpcUaServer server;
private static UShort namespaceId;
private static final String TEST_ENDPOINT = "opc.tcp://localhost:4840";
private static final String TEST_NAMESPACE = "urn:test:namespace";
@BeforeAll
static void setupServer() throws Exception {
server = new OpcUaServer(OpcUaServerConfig.builder()
.setApplicationUri("urn:test:server")
.setEndpoints(Set.of(
EndpointConfiguration.newBuilder()
.setBindPort(4840)
.setSecurityPolicy(SecurityPolicy.None)
.setSecurityMode(MessageSecurityMode.None)
.setHostname("localhost")
.setPath("/test")
.build()
))
.build()
);
server.startup().get(); // Server starten
namespaceId = server.getNamespaceTable().addUri(TEST_NAMESPACE); // Namespace hinzufügen
}
@AfterAll
static void shutdownServer() throws Exception {
server.shutdown().get();
}
@Test
void testOpcUaConnection() throws ExecutionException, InterruptedException {
OPCUACommunicator communicator = new OPCUACommunicator(TEST_ENDPOINT);
assertDoesNotThrow(() -> communicator.setConnectorString(TEST_ENDPOINT));
}
}
Share
Improve this question
edited Apr 1 at 7:16
Mark Rotteveel
110k229 gold badges156 silver badges223 bronze badges
asked Mar 30 at 16:48
VynixVynix
1
1 Answer
Reset to default -2You can add a simple numeric or boolean node to your OPC UA server using Eclipse Milo by creating a UaVariableNode
. Below is an updated version of your code with a method to add a numeric and boolean node to the server.
Steps:
- Create a node in the namespace
- Set an initial value
- Define read/write access
- Add the node to the server
You can try the following code:
import .eclipse.milo.opcua.sdk.core.AccessLevel;
import .eclipse.milo.opcua.sdk.server.OpcUaServer;
import .eclipse.milo.opcua.sdk.server.nodes.UaNode;
import .eclipse.milo.opcua.sdk.server.nodes.UaNodeContext;
import .eclipse.milo.opcua.sdk.server.nodes.UaVariableNode;
import .eclipse.milo.opcua.sdk.server.nodes.UaVariableNode.UaVariableNodeBuilder;
import .eclipse.milo.opcua.stack.core.Identifiers;
import .eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import .eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
import .eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import .eclipse.milo.opcua.stack.core.types.builtin.QualifiedName;
import .eclipse.milo.opcua.stack.core.types.builtin.Variant;
import .eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
import .eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
import .eclipse.milo.opcua.stack.core.types.enumerated.NodeClass;
import .junit.jupiter.api.*;
import java.util.concurrent.ExecutionException;
import static .junit.jupiter.api.Assertions.*;
class OPCUACommunicatorTest {
private static OpcUaServer server;
private static UShort namespaceId;
private static final String TEST_ENDPOINT = "opc.tcp://localhost:4840";
private static final String TEST_NAMESPACE = "urn:test:namespace";
@BeforeAll
static void setupServer() throws Exception {
server = new OpcUaServer(OpcUaServerConfig.builder()
.setApplicationUri("urn:test:server")
.build());
server.startup().get();
namespaceId = server.getNamespaceTable().addUri(TEST_NAMESPACE);
addNumericNode(server);
addBooleanNode(server);
}
private static void addNumericNode(OpcUaServer server) {
NodeId nodeId = new NodeId(namespaceId, "NumericNode");
UaNodeContext nodeContext = server.getNodeManager().getNodeContext();
UaVariableNode node = new UaVariableNodeBuilder(nodeContext)
.setNodeId(nodeId)
.setBrowseName(new QualifiedName(namespaceId, "NumericNode"))
.setDisplayName(LocalizedText.english("Numeric Node"))
.setDataType(Identifiers.Int32)
.setTypeDefinition(Identifiers.BaseDataVariableType)
.setValue(new DataValue(new Variant(42))) // Initial Value
.setAccessLevel(AccessLevel.READ_WRITE)
.build();
server.getNodeManager().addNode(node);
}
private static void addBooleanNode(OpcUaServer server) {
NodeId nodeId = new NodeId(namespaceId, "BooleanNode");
UaNodeContext nodeContext = server.getNodeManager().getNodeContext();
UaVariableNode node = new UaVariableNodeBuilder(nodeContext)
.setNodeId(nodeId)
.setBrowseName(new QualifiedName(namespaceId, "BooleanNode"))
.setDisplayName(LocalizedText.english("Boolean Node"))
.setDataType(Identifiers.Boolean)
.setTypeDefinition(Identifiers.BaseDataVariableType)
.setValue(new DataValue(new Variant(true))) // Initial Value
.setAccessLevel(AccessLevel.READ_WRITE)
.build();
server.getNodeManager().addNode(node);
}
@AfterAll
static void shutdownServer() throws Exception {
server.shutdown().get();
}
@Test
void testOpcUaConnection() throws ExecutionException, InterruptedException {
OPCUACommunicator communicator = new OPCUACommunicator(TEST_ENDPOINT);
assertDoesNotThrow(() -> communicator.setConnectorString(TEST_ENDPOINT));
}
}