« All blog posts

Custom UA Enum Types in Java SDK

16.01.2013


Creating your own enumeration type and using it the server is not as straight forward as it first seems, but it is not very complicated either. You just need to get it done right.

Here is a sample that creates the EnumType node and a respective instance of it, for example in the SampleConsoleServer:

private void createMyEnumNode() throws StatusException {
    // 1. Create the type node...

    NodeId myEnumTypeId = new NodeId(myNodeManager.getNamespaceIndex(),
            "MyEnumType");
    UaDataType myEnumType = new UaDataTypeNode(myNodeManager, myEnumTypeId,
            "MyEnumType", LocalizedText.NO_LOCALE);

    // ... as sub type of Enumeration
    UaType enumerationType = server.getNodeManagerRoot().getType(
            Identifiers.Enumeration);
    enumerationType.addSubType(myEnumType);

    // 2. Add the EnumStrings property ...

    NodeId myEnumStringsId = new NodeId(myNodeManager.getNamespaceIndex(),
            "MyEnumType_EnumStrings");
    ;
    PlainProperty enumStringsProperty = new PlainProperty(
            myNodeManager, myEnumStringsId, new QualifiedName("EnumStrings"),
            new LocalizedText("EnumStrings", LocalizedText.NO_LOCALE));
    enumStringsProperty.setDataTypeId(Identifiers.LocalizedText);
    enumStringsProperty.setValueRank(ValueRanks.OneDimension);
    enumStringsProperty
            .setArrayDimensions(new UnsignedInteger[] { UnsignedInteger.ZERO });
    enumStringsProperty.setAccessLevel(AccessLevel.READONLY);
    myEnumType.addProperty(enumStringsProperty);

    // ... with Value
    enumStringsProperty.setCurrentValue(MyEnumType.getEnumStrings());

    // 3. Create the instance

    NodeId myEnumObjectId = new NodeId(myNodeManager.getNamespaceIndex(),
            "MyEnumObject");
    PlainVariable myEnumObject = new PlainVariable(
            myNodeManager, myEnumObjectId, "MyEnumObject",
            LocalizedText.NO_LOCALE);
    myEnumObject.setDataType(myEnumType);

    // .. as a component of myDevice
    myDevice.addComponent(myEnumObject);

    // 4. Initialize the value
    myEnumObject.setCurrentValue(MyEnumType.One);
}

And the respective enum type is then defined as
/**
 * A sample enumeration type to be used with an OPC UA DataType.
 *
 * OPC UA enum types are expected to be zero-based, i.e. the first element is
 * supposed to correspond to 0. getEnumStrings() here is coded so that it will
 * return null values, though, if not all int values are defined.
 */
public enum MyEnumType implements Enumeration {
        One(1), Three(3), Two(2), Zero(0);

        public static EnumSet ALL = EnumSet.allOf(MyEnumType.class);
        public static EnumSet NONE = EnumSet.noneOf(MyEnumType.class);

        public static LocalizedText[] getEnumStrings() {
                MyEnumType[] values = MyEnumType.values();
                List enumStrings = new ArrayList(
                                values.length);
                for (MyEnumType t : values) {
                        int index = t.getValue();
                        while (enumStrings.size() < (index + 1))
                                enumStrings.add(null);
                        enumStrings.set(index, new LocalizedText(t.name(),
                                        LocalizedText.NO_LOCALE));
                }
                return enumStrings.toArray(new LocalizedText[enumStrings.size()]);
        }

        private int value;

        private MyEnumType(int value) {
                this.value = value;
        }

        /*
         * (non-Javadoc)
         *
         * @see org.opcfoundation.ua.builtintypes.Enumeration#getValue()
         */
        @Override
        public int getValue() {
                return value;
        }
}
Jouni Aro profile photo

Jouni Aro

Chief Technology Officer

Email: jouni.aro@prosysopc.com

Expertise and responsibility areas: OPC & OPC UA product development, project work and customer support

Tags: Information Models, OPC UA, SDK for Java

comments powered by Disqus

About Prosys OPC Ltd

Prosys OPC is a leading provider of professional OPC software and services with over 20 years of experience in the field. OPC and OPC UA (Unified Architecture) are communications standards used especially by industrial and high-tech companies.

Read more about us »

Newest blog posts

Why Do Standards Matter in Smart Manufacturing?

The blog post discusses the importance of standards in smart manufacturing, envisioning a future where auto-configurable systems in manufacturing rely on standardized data formats for seamless integration and reduced costs, with a focus on the OPC UA standard family as a key enabler.

OPC UA PubSub to Cloud via MQTT

Detailed overview of the demo presented at the OPC Foundation booth

SimServer How To #3: Simulate data changes on a server using an OPC UA client

A two-part step-by-step tutorial on how to write data changes on an OPC UA server using an OPC UA client.

View all blog posts »