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
uaClient.read( )
February 5, 2014
10:23, EET
Avatar
gjevremovic
Member
Members
Forum Posts: 49
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Hi,
I would like to avoid calling the read methods for individual items like:

DataValue shorttext = client.readAttribute(nodeId, Attributes.DisplayName);
DataValue longtext = client.readAttribute(nodeId, Attributes.Description);
DataValue value = client.readAttribute(nodeId, Attributes.Value);

e.g. I would like for one node to read set of specified attributes.
I read from prosys javadoc next sample usage examples:
Sample usage:
ReadValueId[] nodesToRead = new ReadValueId[2];
// Scalar value or complete array
nodesToRead[0] = new ReadValueId(nodeId1, Attributes.Value, null, null);
// Part of an array
NumericRange range = new NumericRange(1, 3);
nodesToRead[10] = new ReadValueId(nodeId2, Attributes.Value, range.toString(), null);

final ReadResponse readResponse = read(UaClient.MAX_CACHE_AGE, TimestampsToReturn.Both, nodesToRead);
final DataValue[] values = readResponse.getResults();

// Use the values:
if (values[0].getStatusCode().isGood()) System.out.println(values[0].getValue());
if (values[1].getStatusCode().isGood()) System.out.println(values[1].getValue());

but I stuck with opc foundation docs. Could you please provide simple example to read set of attribs of one node. Tnks

February 5, 2014
11:13, EET
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 1026
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

Simple answer:

client.readAttributes(nodeId, Attributes.DisplayName, Attributes.Description, Attributes.Value)

Internally, this translates to

ReadValueId[] nodesToRead = new ReadValueId[attributeIds.length];

for (int i = 0; i < attributeIds.length; i++)
nodesToRead[i] = new ReadValueId(nodeId, attributeIds[i], null,
null);

final ReadResponse readResponse = read(MAX_CACHE_AGE,
TimestampsToReturn.Both, nodesToRead);
final DataValue[] values = readResponse.getResults();

February 5, 2014
12:01, EET
Avatar
gjevremovic
Member
Members
Forum Posts: 49
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Just one small change, readAttributes needs an array as second argument (from Prosys-OPC-UA-Java-SDK-Client-Binary-1.4.8-8731-javadoc):

UnsignedInteger[] attribs = {Attributes.DisplayName, Attributes.Value};
DataValue[] arrayOfValues = client.readAttributes(node, attribs);

Thank you, it works.

February 5, 2014
12:33, EET
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 1026
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

Oh, yes, you are right. I was sure it was defined as “UnsignedInteger… attributeIds”.

February 6, 2014
9:23, EET
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 1026
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

Jouni Aro said
I was sure it was defined as “UnsignedInteger… attributeIds”.

Will be like that in the next versions.

February 6, 2014
11:29, EET
Avatar
gjevremovic
Member
Members
Forum Posts: 49
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Hi,
(1) Regards reading performance is

UaNode uaNode = client.getAddressSpace().getNode(nodeId);

the same as:

DataValue[] arrayOfValues = client.readAttributes(nodeId, all_attributes);

e.g. it is one call to server in both cases, right?

(2) Regards data type

dataValue = client.readAttribute(nodeId, Attributes.DataType);
System.out.println(dataValue.getValue());

prints
i=7

Is it possible by using prosys api (or opc fond classes) to transform data type value returned in the form
i=7 directly in java.lang.Class, like Float.class, Double.class, String.class, Integer.class … ?

BR

February 6, 2014
12:18, EET
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 1026
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

(1) Not quite. AddressSpace.getNode() makes a couple of readAttributes: first to determine the node class and then read UaNode.supportedAttributes(). After that it will also read node references and properties – to make the node more complete for use in the application.

Regarding performance, getNode() will put the node into a local cache, but will reread the node, if it’s “too old” anyway. The AddressSpace.getCache().setNodeMaxAgeInMillis() can be used to define what is “too old”.

The nodes are store in the cache with WeakReferences, and will be removed when necessary.

(2)

You can convert Variant values with

client.getAddressSpace().getDataTypeConverter().convert(value);

but it does not expose the Java class corresponding to the type. It does provide the opposite, though:

client.getAddressSpace().getDataTypeConverter().getDataTypeForJavaClass(javaClass);

Instead, you can get it directly from

UaDataType dt;
dt.getJavaClass();

Note that these are “manually” initialised in the SDK and may be null for some types. But in general they are available both on the client and server side.

February 6, 2014
12:44, EET
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 1026
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

dt above, is of course initialised from

dt = (UaDataType) client.getAddressSpace().getType(dataTypeId);
February 6, 2014
20:21, EET
Avatar
gjevremovic
Member
Members
Forum Posts: 49
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Thanks Aro for directions, helps me related to UaDataType.

UaNode uaNode = client.getAddressSpace().getNode(node_id);
UaVariable v = (UaVariable) uaNode;
dataType = (UaDataType) v.getDataType();
System.out.println("JavaClass: " + dataType.getJavaClass());

I still didn’t find more elegant way with client.read() method except to parse returned value of attribute Attributes.DataType which is not big deal.

UnsignedInteger[] attribs = {Attributes.DisplayName, Attributes.Value, Attributes.DataType};
DataValue[] arrayOfValues = client.readAttributes(node_id, attribs);
String dataTypeString = arrayOfValues[2].getValue().toString();
int dataTypeIdentifier = Integer.parseInt(dataTypeString.substring(dataTypeString.indexOf("=") + 1));
UaDataType dt = (UaDataType) client.getAddressSpace().getType(new NodeId(0, dataTypeIdentifier));
System.out.println(dt.getJavaClass());

or instead of client.getAddressSpace() maybe it is faster to use:

BuiltinsMap.ID_CLASS_MAP.getRight(new NodeId(0, dataTypeIdentifier));

So, regards performances what you prefer

UaNode uaNode = client.getAddressSpace().getNode(node_id);
UaVariable v = (UaVariable) uaNode;
dataType = (UaDataType) v.getDataType();

UnsignedInteger[] attribs = v.getSupportedAttributes();

or simplier:

DataValue[] arrayOfValues = client.readAttributes(node_id, attribs);

?
BR

February 7, 2014
8:33, EET
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 1026
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

You can get the DataTypeId “simply” as:

UnsignedInteger[] attribs = {Attributes.DisplayName, Attributes.Value, Attributes.DataType};
DataValue[] arrayOfValues = client.readAttributes(node_id, attribs);
NodeId dataTypeId = (NodeId) arrayOfValues[2].getValue().getValue();

I wouldn’t consider performance too much at an early stage. Although it is making several calls to the server, unless you need to read a lot of nodes, it won’t matter very much, I think. A call to a local server will take just a couple of ms (depending on the server, of course).

February 7, 2014
9:35, EET
Avatar
gjevremovic
Member
Members
Forum Posts: 49
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

(NodeId) arrayOfValues[2].getValue().getValue()Smile
Thank you. BR

February 7, 2014
9:57, EET
Avatar
Jouni Aro
Moderator
Moderators
Forum Posts: 1026
Member Since:
December 21, 2011
sp_UserOfflineSmall Offline

Yes, that’s not very obvious and looks odd, indeed ;) The DataValue provides values as Variants, which can be of any type. If you know the type, you can just cast it. There is also a property, compositeClass(), which tells you the actual type – and asClass(), which provides a “safe cast”.

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 1919

Currently Online:
42 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: 1525

Posts: 6456

Newest Members:

forrestdilke5, ernestoportus31, martin123, rickie5305, shaylamaggard4, rickyjuarez140, jonathonmcintyre, fannielima, kristiewinkle8, rust

Moderators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1028, Jimmy Ni: 26, Matti Siponen: 346, Lusetti: 0

Administrators: admin: 1