11:47, EET
October 21, 2019
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
12:11, EET
April 3, 2012
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).
Most Users Ever Online: 1919
Currently Online:
45 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Heikki Tahvanainen: 402
hbrackel: 144
rocket science: 88
pramanj: 86
Francesco Zambon: 83
Ibrahim: 78
Sabari: 62
kapsl: 57
gjevremovic: 49
Xavier: 43
Member Stats:
Guest Posters: 0
Members: 726
Moderators: 7
Admins: 1
Forum Stats:
Groups: 3
Forums: 15
Topics: 1529
Posts: 6471
Newest Members:
gabriellabachus, Deakin, KTP25Zof, Wojciech Kubala, efrennowell431, wilfredostuart, caitlynfajardo, jeromechubb7, franciscagrimwad, adult_galleryModerators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1032, Jimmy Ni: 26, Matti Siponen: 349, Lusetti: 0
Administrators: admin: 1