-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
~ Union example and support in dictionary generator
- Loading branch information
1 parent
5df02d0
commit a8a79d0
Showing
3 changed files
with
243 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...server-examples/src/main/java/org/eclipse/milo/examples/server/types/CustomUnionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright (c) 2020 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.server.types; | ||
|
||
import org.eclipse.milo.examples.server.ExampleNamespace; | ||
import org.eclipse.milo.opcua.stack.core.StatusCodes; | ||
import org.eclipse.milo.opcua.stack.core.UaSerializationException; | ||
import org.eclipse.milo.opcua.stack.core.serialization.SerializationContext; | ||
import org.eclipse.milo.opcua.stack.core.serialization.UaDecoder; | ||
import org.eclipse.milo.opcua.stack.core.serialization.UaEncoder; | ||
import org.eclipse.milo.opcua.stack.core.serialization.UaStructure; | ||
import org.eclipse.milo.opcua.stack.core.serialization.codecs.GenericDataTypeCodec; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger; | ||
import org.eclipse.milo.opcua.stack.core.types.structured.Union; | ||
|
||
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint; | ||
|
||
public class CustomUnionType extends Union implements UaStructure { | ||
|
||
public static final ExpandedNodeId TYPE_ID = ExpandedNodeId.parse(String.format( | ||
"nsu=%s;s=%s", | ||
ExampleNamespace.NAMESPACE_URI, | ||
"DataType.CustomUnionType" | ||
)); | ||
|
||
public static final ExpandedNodeId BINARY_ENCODING_ID = ExpandedNodeId.parse(String.format( | ||
"nsu=%s;s=%s", | ||
ExampleNamespace.NAMESPACE_URI, | ||
"DataType.CustomUnionType.BinaryEncoding" | ||
)); | ||
|
||
private final Type type; | ||
private final Object value; | ||
|
||
private CustomUnionType(Type type, Object value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public ExpandedNodeId getTypeId() { | ||
return TYPE_ID; | ||
} | ||
|
||
@Override | ||
public ExpandedNodeId getBinaryEncodingId() { | ||
return BINARY_ENCODING_ID; | ||
} | ||
|
||
@Override | ||
public ExpandedNodeId getXmlEncodingId() { | ||
// XML encoding not supported | ||
return ExpandedNodeId.NULL_VALUE; | ||
} | ||
|
||
public UInteger asFoo() { | ||
return (UInteger) value; | ||
} | ||
|
||
public String asBar() { | ||
return (String) value; | ||
} | ||
|
||
public boolean isNull() { | ||
return type == Type.Null; | ||
} | ||
|
||
public boolean isFoo() { | ||
return type == Type.Foo; | ||
} | ||
|
||
public boolean isBar() { | ||
return type == Type.Bar; | ||
} | ||
|
||
public static CustomUnionType ofNull() { | ||
return new CustomUnionType(Type.Null, null); | ||
} | ||
|
||
public static CustomUnionType ofFoo(UInteger value) { | ||
return new CustomUnionType(Type.Foo, value); | ||
} | ||
|
||
public static CustomUnionType ofBar(String value) { | ||
return new CustomUnionType(Type.Bar, value); | ||
} | ||
|
||
enum Type { | ||
Null, | ||
Foo, | ||
Bar | ||
} | ||
|
||
public static class Codec extends GenericDataTypeCodec<CustomUnionType> { | ||
@Override | ||
public Class<CustomUnionType> getType() { | ||
return CustomUnionType.class; | ||
} | ||
|
||
@Override | ||
public CustomUnionType decode(SerializationContext context, UaDecoder decoder) { | ||
UInteger switchValue = decoder.readUInt32("SwitchValue"); | ||
switch (switchValue.intValue()) { | ||
case 0: | ||
return CustomUnionType.ofNull(); | ||
case 1: { | ||
UInteger foo = decoder.readUInt32("foo"); | ||
return CustomUnionType.ofFoo(foo); | ||
} | ||
case 2: { | ||
String bar = decoder.readString("bar"); | ||
return CustomUnionType.ofBar(bar); | ||
} | ||
default: | ||
throw new UaSerializationException( | ||
StatusCodes.Bad_DecodingError, | ||
"unknown field in Union CustomUnionType: " + switchValue | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public void encode(SerializationContext context, UaEncoder encoder, CustomUnionType value) { | ||
encoder.writeUInt32("SwitchValue", uint(value.type.ordinal())); | ||
switch (value.type) { | ||
case Null: | ||
break; | ||
case Foo: { | ||
encoder.writeUInt32("foo", value.asFoo()); | ||
} | ||
case Bar: { | ||
encoder.writeString("bar", value.asBar()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters