Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eagerly decode ExtensionObjects in Variants #1330

Draft
wants to merge 4 commits into
base: dev/1.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024 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.examples.client;

import java.util.concurrent.CompletableFuture;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
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.enumerated.TimestampsToReturn;
import org.eclipse.milo.sdk.core.types.json.JsonCodecFactory;
import org.eclipse.milo.sdk.core.types.json.JsonStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IJTReadCustomDataTypeExample implements ClientExample {

public static void main(String[] args) throws Exception {
var example = new IJTReadCustomDataTypeExample();

new ClientExampleRunner(example, false).run();
}

private final Logger logger = LoggerFactory.getLogger(getClass());

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
client.setDataTypeManagerInitializer(
new OpcUaClient.DefaultDataTypeManagerInitializer(JsonCodecFactory::create));

client.connect();

NodeId nodeId = NodeId.parse("ns=1;s=TighteningSystem/ResultManagement/Results/Result");
DataValue value = client.readValue(0, TimestampsToReturn.Both, nodeId);

if (value.getValue().getValue() instanceof ExtensionObject xo) {
JsonStruct struct = (JsonStruct) xo.decode(client.getDynamicEncodingContext());

System.out.println(struct);
}

future.complete(client);
}

@Override
public String getEndpointUrl() {
return "opc.tcp://10.211.55.3:40451";
}

@Override
public SecurityPolicy getSecurityPolicy() {
return SecurityPolicy.None;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ private JsonElement decodeFieldValue(UaDecoder decoder, StructureField field) {
if (hint instanceof BuiltinDataType) {
Matrix matrix = decoder.decodeMatrix(fieldName, (BuiltinDataType) hint);

return decodeBuiltinDataTypeMatrix(matrix);
return decodeBuiltinDataTypeMatrix(decoder.getEncodingContext(), matrix);
} else if (hint instanceof EnumHint) {
Matrix matrix = decoder.decodeEnumMatrix(fieldName);

return decodeEnumMatrix(matrix);
return decodeEnumMatrix(decoder.getEncodingContext(), matrix);
} else if (hint instanceof StructHint) {
if (dataTypeId.equals(NodeIds.Structure) || fieldAllowsSubtyping(field)) {
Matrix matrix = decoder.decodeMatrix(fieldName, BuiltinDataType.ExtensionObject);
Expand Down Expand Up @@ -285,7 +285,21 @@ private static JsonElement decodeBuiltinDataType(
case ExtensionObject ->
JsonConversions.fromExtensionObject(decoder.decodeExtensionObject(fieldName));
case DataValue -> JsonConversions.fromDataValue(decoder.decodeDataValue(fieldName));
case Variant -> JsonConversions.fromVariant(decoder.decodeVariant(fieldName));
case Variant -> {
Variant variant = decoder.decodeVariant(fieldName);

if (variant.getValue() instanceof ExtensionObject xo) {
try {
JsonStruct jsonStruct = (JsonStruct) xo.decode(decoder.getEncodingContext());

yield jsonStruct.getJsonObject();
} catch (Exception e) {
yield JsonConversions.fromVariant(variant);
}
} else {
yield JsonConversions.fromVariant(variant);
}
}
default -> JsonNull.INSTANCE;
};
}
Expand Down Expand Up @@ -478,42 +492,77 @@ private static JsonElement decodeBuiltinDataTypeArray(
return array;
}
case Variant:
var array = new JsonArray();
for (Variant value : decoder.decodeVariantArray(fieldName)) {
array.add(JsonConversions.fromVariant(value));
}
return array;
{
var array = new JsonArray();

for (Variant variant : decoder.decodeVariantArray(fieldName)) {
if (variant.getValue() instanceof ExtensionObject xo) {
try {
JsonStruct jsonStruct = (JsonStruct) xo.decode(decoder.getEncodingContext());

array.add(jsonStruct.getJsonObject());
} catch (Exception e) {
array.add(JsonConversions.fromVariant(variant));
}
} else {
array.add(JsonConversions.fromVariant(variant));
}
}

return array;
}
case DiagnosticInfo:
default:
return JsonNull.INSTANCE;
}
}

static JsonElement decodeBuiltinDataTypeMatrix(Matrix matrix) {
static JsonElement decodeBuiltinDataTypeMatrix(EncodingContext context, Matrix matrix) {
return matrix
.getBuiltinDataType()
.map(
dataType ->
decodeBuiltinDataTypeMatrix(
matrix.getElements(), dataType, matrix.getDimensions(), 0))
context, matrix.getElements(), dataType, matrix.getDimensions(), 0))
.orElse(JsonNull.INSTANCE);
}

private static JsonElement decodeBuiltinDataTypeMatrix(
Object flatArray, BuiltinDataType dataType, int[] dimensions, int offset) {
EncodingContext context,
Object flatArray,
BuiltinDataType dataType,
int[] dimensions,
int offset) {

var jsonArray = new JsonArray();

if (dimensions.length == 1) {
for (int i = 0; i < dimensions[0]; i++) {
jsonArray.add(JsonConversions.from(Array.get(flatArray, offset + i), dataType));
if (dataType == BuiltinDataType.Variant) {
try {
Variant variant = (Variant) Array.get(flatArray, offset + i);

if (variant.getValue() instanceof ExtensionObject xo) {
JsonStruct jsonStruct = (JsonStruct) xo.decode(context);

jsonArray.add(jsonStruct.getJsonObject());
} else {
jsonArray.add(JsonConversions.fromVariant(variant));
}
} catch (Exception e) {
jsonArray.add(JsonConversions.from(Array.get(flatArray, offset + i), dataType));
}
} else {
jsonArray.add(JsonConversions.from(Array.get(flatArray, offset + i), dataType));
}
}
} else {
int[] dimensionsTail = Arrays.copyOfRange(dimensions, 1, dimensions.length);

for (int i = 0; i < dimensions[0]; i++) {
JsonElement e =
decodeBuiltinDataTypeMatrix(
context,
flatArray,
dataType,
dimensionsTail,
Expand All @@ -525,9 +574,9 @@ private static JsonElement decodeBuiltinDataTypeMatrix(
return jsonArray;
}

static JsonElement decodeEnumMatrix(Matrix matrix) {
static JsonElement decodeEnumMatrix(EncodingContext context, Matrix matrix) {
return decodeBuiltinDataTypeMatrix(
matrix.getElements(), BuiltinDataType.Int32, matrix.getDimensions(), 0);
context, matrix.getElements(), BuiltinDataType.Int32, matrix.getDimensions(), 0);
}

static JsonElement decodeStructMatrix(EncodingContext context, Matrix matrix, boolean subtyped) {
Expand Down
Loading