Skip to content

Commit

Permalink
DynamicType Tests and Polish (#1368)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron authored Jan 31, 2025
1 parent 279c3fb commit 8a7eb05
Show file tree
Hide file tree
Showing 51 changed files with 11,593 additions and 1,081 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 the Eclipse Milo Authors
* Copyright (c) 2025 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -18,9 +18,10 @@
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.core.types.DynamicEnum;
import org.eclipse.milo.opcua.sdk.core.types.DynamicOptionSet;
import org.eclipse.milo.opcua.sdk.core.types.DynamicStruct;
import org.eclipse.milo.opcua.sdk.core.types.DynamicEnumType;
import org.eclipse.milo.opcua.sdk.core.types.DynamicOptionSetType;
import org.eclipse.milo.opcua.sdk.core.types.DynamicStructType;
import org.eclipse.milo.opcua.sdk.core.types.DynamicType;
import org.eclipse.milo.opcua.stack.core.encoding.EncodingContext;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import org.eclipse.milo.opcua.stack.core.types.builtin.ByteString;
Expand Down Expand Up @@ -64,40 +65,40 @@ public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throw
private void readWriteReadPerson(OpcUaClient client) throws Exception {
NodeId nodeId = NodeId.parse("ns=3;s=Person1");

DynamicStruct value = readScalarValue(client, nodeId);
DynamicStructType value = (DynamicStructType) readScalarValue(client, nodeId);
logger.info("Person1: {}", value);

Random r = new Random();
DynamicEnum gender = (DynamicEnum) value.getMembers().get("Gender");
DynamicEnumType gender = (DynamicEnumType) value.getMembers().get("Gender");
value.getMembers().put("Name", "Fat Boy" + r.nextInt(100));
value.getMembers().put("Gender", new DynamicEnum(gender.getDataType(), r.nextInt(2)));
value.getMembers().put("Gender", new DynamicEnumType(gender.getDataType(), r.nextInt(2)));

StatusCode status = writeValue(client, nodeId, value);
System.out.println("write status: " + status);

value = readScalarValue(client, nodeId);
value = (DynamicStructType) readScalarValue(client, nodeId);
logger.info("Person1': {}", value);
}

private void readWriteReadWorkOrder(OpcUaClient client) throws Exception {
NodeId nodeId = NodeId.parse("ns=3;s=Demo.Static.Scalar.WorkOrder");

DynamicStruct value = readScalarValue(client, nodeId);
DynamicStructType value = (DynamicStructType) readScalarValue(client, nodeId);
logger.info("WorkOrder: {}", value);

value.getMembers().put("ID", UUID.randomUUID());

StatusCode status = writeValue(client, nodeId, value);
System.out.println("write status: " + status);

value = readScalarValue(client, nodeId);
value = (DynamicStructType) readScalarValue(client, nodeId);
logger.info("WorkOrder': {}", value);
}

private void readWriteCarExtras(OpcUaClient client) throws Exception {
NodeId nodeId = NodeId.parse("ns=3;s=Demo.Static.Scalar.CarExtras");

DynamicOptionSet value = (DynamicOptionSet) readScalarValue(client, nodeId);
DynamicOptionSetType value = (DynamicOptionSetType) readScalarValue(client, nodeId);
logger.info("CarExtras: {}", value);

byte b = requireNonNull(value.getValue().bytes())[0];
Expand All @@ -106,33 +107,32 @@ private void readWriteCarExtras(OpcUaClient client) throws Exception {
StatusCode status = writeValue(client, nodeId, value);
System.out.println("write status: " + status);

value = (DynamicOptionSet) readScalarValue(client, nodeId);
value = (DynamicOptionSetType) readScalarValue(client, nodeId);
logger.info("CarExtras': {}", value);
}

private void readWorkOrderArray(OpcUaClient client) throws Exception {
NodeId nodeId = NodeId.parse("ns=3;s=Demo.Static.Arrays.WorkOrder");

DynamicStruct[] value = readArrayValue(client, nodeId);
DynamicType[] value = readArrayValue(client, nodeId);

logger.info("WorkOrderArray:");
for (int i = 0; i < value.length; i++) {
logger.info(" WorkOrder[{}]: {}", i, value[i]);
}
}

private static DynamicStruct readScalarValue(OpcUaClient client, NodeId nodeId) throws Exception {
private static DynamicType readScalarValue(OpcUaClient client, NodeId nodeId) throws Exception {
DataValue dataValue =
client.readValues(0.0, TimestampsToReturn.Neither, List.of(nodeId)).get(0);

ExtensionObject xo = (ExtensionObject) dataValue.getValue().getValue();
assert xo != null;

return (DynamicStruct) xo.decode(client.getDynamicEncodingContext());
return (DynamicType) xo.decode(client.getDynamicEncodingContext());
}

private static DynamicStruct[] readArrayValue(OpcUaClient client, NodeId nodeId)
throws Exception {
private static DynamicType[] readArrayValue(OpcUaClient client, NodeId nodeId) throws Exception {
DataValue dataValue =
client.readValues(0.0, TimestampsToReturn.Neither, List.of(nodeId)).get(0);

Expand All @@ -141,13 +141,12 @@ private static DynamicStruct[] readArrayValue(OpcUaClient client, NodeId nodeId)

EncodingContext ctx = client.getDynamicEncodingContext();

return Arrays.stream(xos)
.map(xo -> (DynamicStruct) xo.decode(ctx))
.toArray(DynamicStruct[]::new);
return Arrays.stream(xos).map(xo -> (DynamicType) xo.decode(ctx)).toArray(DynamicType[]::new);
}

private static StatusCode writeValue(OpcUaClient client, NodeId nodeId, DynamicStruct value)
private static StatusCode writeValue(OpcUaClient client, NodeId nodeId, DynamicType value)
throws Exception {

ExtensionObject xo =
ExtensionObject.encodeDefaultBinary(
client.getDynamicEncodingContext(), value, value.getDataType().getBinaryEncodingId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 the Eclipse Milo Authors
* Copyright (c) 2025 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -14,7 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode;
import org.eclipse.milo.opcua.sdk.core.types.DynamicStruct;
import org.eclipse.milo.opcua.sdk.core.types.DynamicStructType;
import org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest;
import org.eclipse.milo.opcua.sdk.test.MatrixTestType;
import org.eclipse.milo.opcua.stack.core.UaException;
Expand All @@ -38,7 +38,7 @@ public void read() throws UaException {
ExtensionObject xo = (ExtensionObject) value.getValue().getValue();
assert xo != null;

DynamicStruct decoded = (DynamicStruct) xo.decode(client.getDynamicEncodingContext());
DynamicStructType decoded = (DynamicStructType) xo.decode(client.getDynamicEncodingContext());
assertEquals(
MatrixTestType.TYPE_ID,
decoded.getTypeId().absolute(client.getNamespaceTable()).orElseThrow());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 the Eclipse Milo Authors
* Copyright (c) 2025 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -39,7 +39,7 @@
import org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscription;
import org.eclipse.milo.opcua.sdk.client.subscriptions.PublishingManager;
import org.eclipse.milo.opcua.sdk.client.typetree.DataTypeTreeBuilder;
import org.eclipse.milo.opcua.sdk.core.types.DynamicCodecFactory;
import org.eclipse.milo.opcua.sdk.core.types.codec.DynamicCodecFactory;
import org.eclipse.milo.opcua.sdk.core.typetree.DataType;
import org.eclipse.milo.opcua.sdk.core.typetree.DataTypeTree;
import org.eclipse.milo.opcua.stack.core.AttributeId;
Expand Down
14 changes: 13 additions & 1 deletion opc-ua-sdk/sdk-core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2024 the Eclipse Milo Authors
~ Copyright (c) 2025 the Eclipse Milo Authors
~
~ This program and the accompanying materials are made
~ available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -51,6 +51,18 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

This file was deleted.

Loading

0 comments on commit 8a7eb05

Please sign in to comment.