Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
Creating EnumValue from NodeId and Integer
November 17, 2022
11:47, EET
Avatar
ivfa
Member
Members
Forum Posts: 8
Member Since:
October 21, 2019
sp_UserOfflineSmall Offline

Hey,
I try to set an EnumValue in an OPC Server on the server-side. My question is: is it possible to instantiate an EnumValue just from the NodeId of theVariable i want to set, the nodeid of the DataType and the int Value corresponding to the Specific Enum Value? So basically i would get something over an api like
{
“nodeId” : “ns=3;i=1234”,
“value”:0
}
I can get the variable node via:

UaNode node = server.getAddressSpace().getNode(new NodeId(3,1234));

I can cast node to BaseDataVariableTypeNode, so i can set the value.

I found it is possible to get the datatype via

node.getDataType()
“.
I can get a description of the enumfields via

if(dataType.getDataTypeDefinition() instanceof EnumDefinition ed){
EnumField[] fields = ed.getFields();
for(EnumField ef: fields){
logger.info(ef.toString());
}
}

Is it somehow possible go from variable and type nodeId and the int enum value to an instance of the Enum (or its interface), like (pseudocode):
node.setValue((EnumValue.from(myEnumType, myEnumValue(an Int))

I hope my explanation was understandable.
Best regards
Florin

November 17, 2022
12:11, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 860
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

Yes, assuming I understood correctly.

However, it is best to do this a bit differently. While the DataTypeDefinition etc. can be used, that is OPC UA 1.04 based Attribute. And there are multiple other ways for the enumerations to be defined in older spec versions. SDK abstracts the concept of older DataTypeDictionaries and the newer DataTypeDefinitions with concepts called StructureSpecification and EnumerationSpecification, these should always be used instead (unless visualizing the raw Attribute value of the DataTypeDefinition in a generic client). These can be obtained via EncoderContext, which can be via UaClient.getEncoderContext(), once you have connected to the server (by default we read the types part when we connect).

So, you can find EncoderContext.getEnumerationSpecification(UaNodeId) (convert the NodeId to UaNodeId, e.g. via UaNodeId.fromLocal(id, client.getNamespaceTable), the UaNodeId is SDK-specific idea of a NodeId, but forced to use the UaNamespace(Uri) instead of the index (NodeId vs. ExpandedNodeId is sort of a pain, and ExpandedNodeId could contain either, thus Maps keys work best with UaNodeId).

Then you can use Variant.asEnum(EnumerationSpecification). Alternatively if you know the enum, and have used our Codegen to generate the information model (assuming you have that, or it is core information model type, in which case we have already generated that), you can also use asEnum(Class) (where the class is the enum type.class). The output is still Object, as the value could have been array or multi-dim array (it is converted to equal ValueRank of output i.e. scalar -> scalar, 1dim->1dim, 2d->2d so on).

Once you have the EnumerationSpecification, you can use methods of that to find the mappings, also you can get the Enumeration.Builder of the type via toEnumerationBuilder use setValue(int) and build().

The data within Variant is always the integer format, as that is was is transferred over the wire. Technically Variant constructor accepts Enumeration-instances, but was decided that they are converted immediately to Integer(s) (so that it wont flip between ints and enums depending from where it came from). This is one are that could use some improvements, but not sure when. Basically when we do decode the messages it is kinda hard to know if it was an Enumeration, as they are encoded as Int32 and there is no type-info “nearby” to check (e.g. a Read of Value would also need to always Read DataType, or know it somehow).

November 17, 2022
14:00, EET
Avatar
ivfa
Member
Members
Forum Posts: 8
Member Since:
October 21, 2019
sp_UserOfflineSmall Offline

I will try the methods you proposed, thanks again for being incredible helpful.

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 267

Currently Online:
18 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

hbrackel: 124

pramanj: 86

ibrahim: 74

rocket science: 65

kapsl: 57

Sabari: 51

gjevremovic: 49

Xavier: 43

fred: 41

TimK: 41

Member Stats:

Guest Posters: 0

Members: 1749

Moderators: 15

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1323

Posts: 5649

Newest Members:

WalterFOUNC, datafastproxiespx01, LenkaLok, seoninet, justinevillanuev, namtrott5457, DonaldWibip, AlfredoEthix, andreidla, everangel7021

Moderators: Jouni Aro: 960, Otso Palonen: 32, Tuomas Hiltunen: 5, Pyry: 1, Petri: 0, Bjarne Boström: 860, Heikki Tahvanainen: 402, Jukka Asikainen: 1, moldzh08: 0, Jimmy Ni: 25, Teppo Uimonen: 21, Markus Johansson: 42, Niklas Nurminen: 0, Matti Siponen: 222, Lusetti: 0

Administrators: admin: 1