tests_common.py
Go to the documentation of this file.
1 # encoding: utf-8
2 from concurrent.futures import Future, TimeoutError
3 import time
4 from datetime import datetime
5 from datetime import timedelta
6 import math
7 
8 from opcua import ua
9 from opcua import Node
10 from opcua import uamethod
11 from opcua import instantiate
12 from opcua import copy_node
13 from opcua.common import ua_utils
14 
15 
17  @uamethod
18  def func(parent, value):
19  return value * 2
20 
21  o = srv.get_objects_node()
22  v = o.add_method(ua.NodeId("ServerMethod", 2), ua.QualifiedName('ServerMethod', 2), func, [ua.VariantType.Int64], [ua.VariantType.Int64])
23 
24  @uamethod
25  def func2(parent, methodname, value):
26  return math.sin(value)
27 
28  o = srv.get_objects_node()
29  v = o.add_method(ua.NodeId("ServerMethodArray", 2), ua.QualifiedName('ServerMethodArray', 2), func2, [ua.VariantType.String, ua.VariantType.Int64], [ua.VariantType.Int64])
30 
31  @uamethod
32  def func3(parent, mylist):
33  return [i * 2 for i in mylist]
34 
35  o = srv.get_objects_node()
36  v = o.add_method(ua.NodeId("ServerMethodArray2", 2), ua.QualifiedName('ServerMethodArray2', 2), func3, [ua.VariantType.Int64], [ua.VariantType.Int64])
37 
38 
39 class CommonTests(object):
40 
41  '''
42  Tests that will be run twice. Once on server side and once on
43  client side since we have been carefull to have the exact
44  same api on server and client side
45  '''
46  # jyst to avoid editor warnings
47  opc = None
48  assertEqual = lambda x, y: True
49  assertIn = lambda x, y: True
50 
51  def test_find_servers(self):
52  servers = self.opc.find_servers()
53  # FIXME : finish
54 
56  obj = self.opc.get_objects_node()
57 
58  with self.assertRaises(TypeError):
59  fold = obj.add_folder(1.2, "kk")
60 
61  with self.assertRaises(TypeError):
62  fold = obj.add_folder(ua.UaError, "khjh")
63 
64  with self.assertRaises(ua.UaError):
65  fold = obj.add_folder("kjk", 1.2)
66 
67  with self.assertRaises(TypeError):
68  fold = obj.add_folder("i=0;s='oooo'", 1.2)
69 
70  with self.assertRaises(ua.UaError):
71  fold = obj.add_folder("i=0;s='oooo'", "tt:oioi")
72 
73  def test_delete_nodes(self):
74  obj = self.opc.get_objects_node()
75  fold = obj.add_folder(2, "FolderToDelete")
76  var = fold.add_variable(2, "VarToDelete", 9.1)
77  childs = fold.get_children()
78  self.assertIn(var, childs)
79  self.opc.delete_nodes([var])
80  with self.assertRaises(ua.UaStatusCodeError):
81  var.set_value(7.8)
82  with self.assertRaises(ua.UaStatusCodeError):
83  obj.get_child(["2:FolderToDelete", "2:VarToDelete"])
84  childs = fold.get_children()
85  self.assertNotIn(var, childs)
86 
88  obj = self.opc.get_objects_node()
89  fold = obj.add_folder(2, "FolderToDeleteR")
90  var = fold.add_variable(2, "VarToDeleteR", 9.1)
91  self.opc.delete_nodes([fold, var])
92  with self.assertRaises(ua.UaStatusCodeError):
93  var.set_value(7.8)
94  with self.assertRaises(ua.UaStatusCodeError):
95  obj.get_child(["2:FolderToDelete", "2:VarToDelete"])
96 
98  obj = self.opc.get_objects_node()
99  fold = obj.add_folder(2, "FolderToDeleteRoot")
100  nfold = fold
101  mynodes = []
102  for i in range(7):
103  nfold = fold.add_folder(2, "FolderToDeleteRoot")
104  var = fold.add_variable(2, "VarToDeleteR", 9.1)
105  var = fold.add_property(2, "ProToDeleteR", 9.1)
106  prop = fold.add_property(2, "ProToDeleteR", 9.1)
107  o = fold.add_object(3, "ObjToDeleteR")
108  mynodes.append(nfold)
109  mynodes.append(var)
110  mynodes.append(prop)
111  mynodes.append(o)
112  self.opc.delete_nodes([fold], recursive=True)
113  for node in mynodes:
114  with self.assertRaises(ua.UaStatusCodeError):
115  node.get_browse_name()
116 
117  def test_server_node(self):
118  node = self.opc.get_server_node()
119  self.assertEqual(ua.QualifiedName('Server', 0), node.get_browse_name())
120 
121  def test_root(self):
122  root = self.opc.get_root_node()
123  self.assertEqual(ua.QualifiedName('Root', 0), root.get_browse_name())
124  self.assertEqual(ua.LocalizedText('Root'), root.get_display_name())
125  nid = ua.NodeId(84, 0)
126  self.assertEqual(nid, root.nodeid)
127 
128  def test_objects(self):
129  objects = self.opc.get_objects_node()
130  self.assertEqual(ua.QualifiedName('Objects', 0), objects.get_browse_name())
131  nid = ua.NodeId(85, 0)
132  self.assertEqual(nid, objects.nodeid)
133 
134  def test_browse(self):
135  objects = self.opc.get_objects_node()
136  obj = objects.add_object(4, "browsetest")
137  folder = obj.add_folder(4, "folder")
138  prop = obj.add_property(4, "property", 1)
139  prop2 = obj.add_property(4, "property2", 2)
140  var = obj.add_variable(4, "variable", 3)
141  obj2 = obj.add_object(4, "obj")
142  alle = obj.get_children()
143  self.assertTrue(prop in alle)
144  self.assertTrue(prop2 in alle)
145  self.assertTrue(var in alle)
146  self.assertTrue(folder in alle)
147  self.assertFalse(obj in alle)
148  props = obj.get_children(refs=ua.ObjectIds.HasProperty)
149  self.assertTrue(prop in props)
150  self.assertTrue(prop2 in props)
151  self.assertFalse(var in props)
152  self.assertFalse(folder in props)
153  self.assertFalse(obj2 in props)
154  all_vars = obj.get_children(nodeclassmask=ua.NodeClass.Variable)
155  self.assertTrue(prop in all_vars)
156  self.assertTrue(var in all_vars)
157  self.assertFalse(folder in props)
158  self.assertFalse(obj2 in props)
159  all_objs = obj.get_children(nodeclassmask=ua.NodeClass.Object)
160  self.assertTrue(folder in all_objs)
161  self.assertTrue(obj2 in all_objs)
162  self.assertFalse(var in all_objs)
163 
165  objects = self.opc.get_objects_node()
166  folder = objects.add_folder(4, "folder")
167 
168  childs = objects.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Forward, includesubtypes=False)
169  self.assertTrue(folder in childs)
170 
171  childs = objects.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Both, includesubtypes=False)
172  self.assertTrue(folder in childs)
173 
174  childs = objects.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
175  self.assertFalse(folder in childs)
176 
177  parents = folder.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
178  self.assertTrue(objects in parents)
179 
180  parents = folder.get_referenced_nodes(refs=ua.ObjectIds.HierarchicalReferences, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
181  self.assertTrue(objects in parents)
182 
183  parent = folder.get_parent()
184  self.assertEqual(parent, objects)
185 
187  o = self.opc.get_objects_node()
188  v = o.add_variable(3, 'BNVariable with spaces and %&+?/', 1.3)
189  v2 = o.get_child("3:BNVariable with spaces and %&+?/")
190  self.assertEqual(v, v2)
191 
193  root = self.opc.get_root_node()
194  with self.assertRaises(ua.UaStatusCodeError):
195  server_time_node = root.get_child(['0:Objects', '0:Server', '0:nonexistingnode'])
196 
198  root = self.opc.get_root_node()
199  with self.assertRaises(ua.UaStatusCodeError):
200  root.set_value(99)
201 
203  root = self.opc.get_root_node()
204  server_time_node = root.get_child(['0:Objects', '0:Server', '0:ServerStatus', '0:CurrentTime'])
205  correct = self.opc.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime))
206  self.assertEqual(server_time_node, correct)
207 
209  time_node = self.opc.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime))
210  dt = time_node.get_value()
211  utcnow = datetime.utcnow()
212  delta = utcnow - dt
213  self.assertTrue(delta < timedelta(seconds=1))
214 
216  time_node = self.opc.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime))
217  now = datetime.utcnow()
218  objects = self.opc.get_objects_node()
219  v1 = objects.add_variable(4, "test_datetime", now)
220  tid = v1.get_value()
221  self.assertEqual(now, tid)
222 
224  objects = self.opc.get_objects_node()
225  l = [[[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0]],[[5.0, 5.0, 5.0, 5.0], [7.0, 8.0, 9.0, 01.0], [1.0, 1.0, 1.0, 1.0]]]
226  v = objects.add_variable(3, 'variableWithDims', l)
227 
228  v.set_array_dimensions([0, 0, 0])
229  dim = v.get_array_dimensions()
230  self.assertEqual(dim, [0, 0, 0])
231 
232  v.set_value_rank(0)
233  rank = v.get_value_rank()
234  self.assertEqual(rank, 0)
235 
236  v2 = v.get_value()
237  self.assertEqual(v2, l)
238  dv = v.get_data_value()
239  self.assertEqual(dv.Value.Dimensions, [2,3,4])
240 
241  l = [[[], [], []], [[], [], []]]
242  variant = ua.Variant(l, ua.VariantType.UInt32)
243  v = objects.add_variable(3, 'variableWithDimsEmpty', variant)
244  v2 = v.get_value()
245  self.assertEqual(v2, l)
246  dv = v.get_data_value()
247  self.assertEqual(dv.Value.Dimensions, [2,3,0])
248 
250  objects = self.opc.get_objects_node()
251  v = objects.add_variable('ns=3;i=888;', '3:numericnodefromstring', 99)
252  nid = ua.NodeId(888, 3)
253  qn = ua.QualifiedName('numericnodefromstring', 3)
254  self.assertEqual(nid, v.nodeid)
255  self.assertEqual(qn, v.get_browse_name())
256 
258  objects = self.opc.get_objects_node()
259  v = objects.add_variable('ns=3;s=stringid;', '3:stringnodefromstring', [68])
260  nid = ua.NodeId('stringid', 3)
261  qn = ua.QualifiedName('stringnodefromstring', 3)
262  self.assertEqual(nid, v.nodeid)
263  self.assertEqual(qn, v.get_browse_name())
264 
265  def test_utf8(self):
266  objects = self.opc.get_objects_node()
267  utf_string = "æøå@%&"
268  bn = ua.QualifiedName(utf_string, 3)
269  nid = ua.NodeId("æølå", 3)
270  val = "æøå"
271  v = objects.add_variable(nid, bn, val)
272  self.assertEqual(nid, v.nodeid)
273  val2 = v.get_value()
274  self.assertEqual(val, val2)
275  bn2 = v.get_browse_name()
276  self.assertEqual(bn, bn2)
277 
279  objects = self.opc.get_objects_node()
280  var = objects.add_variable(3, 'nullstring', "a string")
281  var.set_value(None)
282  val = var.get_value()
283  self.assertEqual(val, None)
284  var.set_value("")
285  val = var.get_value()
286  self.assertNotEqual(val, None)
287  self.assertEqual(val, "")
288 
290  objects = self.opc.get_objects_node()
291  var = objects.add_variable(3, 'stringfordatatype', "a string")
292  val = var.get_data_type_as_variant_type()
293  self.assertEqual(val, ua.VariantType.String)
294  var = objects.add_variable(3, 'stringarrayfordatatype', ["a", "b"])
295  val = var.get_data_type_as_variant_type()
296  self.assertEqual(val, ua.VariantType.String)
297 
299  objects = self.opc.get_objects_node()
300  v = objects.add_variable('ns=3;s=stringarrayid;', '9:stringarray', ['l', 'b'])
301  nid = ua.NodeId('stringarrayid', 3)
302  qn = ua.QualifiedName('stringarray', 9)
303  self.assertEqual(nid, v.nodeid)
304  self.assertEqual(qn, v.get_browse_name())
305  val = v.get_value()
306  self.assertEqual(['l', 'b'], val)
307 
309  objects = self.opc.get_objects_node()
310  nid = ua.NodeId(9999, 3)
311  qn = ua.QualifiedName('AddNodeVar1', 3)
312  v1 = objects.add_variable(nid, qn, 0)
313  self.assertEqual(nid, v1.nodeid)
314  self.assertEqual(qn, v1.get_browse_name())
315 
317  objects = self.opc.get_objects_node()
318  qn = ua.QualifiedName('AddNodeVar2', 3)
319  nid = ua.NodeId('AddNodeVar2Id', 3)
320  v2 = objects.add_variable(nid, qn, 0)
321  self.assertEqual(nid, v2.nodeid)
322  self.assertEqual(qn, v2.get_browse_name())
323 
325  objects = self.opc.get_objects_node()
326  o = objects.add_object('ns=2;i=101;', '2:AddFindObject')
327  o2 = objects.get_child('2:AddFindObject')
328  self.assertEqual(o, o2)
329 
330  def test_node_path(self):
331  objects = self.opc.get_objects_node()
332  o = objects.add_object('ns=2;i=105;', '2:NodePathObject')
333  root = self.opc.get_root_node()
334  o2 = root.get_child(['0:Objects', '2:NodePathObject'])
335  self.assertEqual(o, o2)
336 
338  objects = self.opc.get_objects_node()
339  o = objects.add_object('ns=2;i=102;', '2:AddReadObject')
340  nid = ua.NodeId(102, 2)
341  self.assertEqual(o.nodeid, nid)
342  qn = ua.QualifiedName('AddReadObject', 2)
343  self.assertEqual(o.get_browse_name(), qn)
344 
345  def test_simple_value(self):
346  o = self.opc.get_objects_node()
347  v = o.add_variable(3, 'VariableTestValue', 4.32)
348  val = v.get_value()
349  self.assertEqual(4.32, val)
350 
352  objects = self.opc.get_objects_node()
353  o = objects.add_object('ns=2;i=103;', '2:AddReadObject')
354  with self.assertRaises(ua.UaStatusCodeError):
355  o2 = objects.add_object('ns=2;i=103;', '2:AddReadObject')
356 
358  o = self.opc.get_objects_node()
359  v = o.add_variable(3, 'VariableNegativeValue', 4)
360  v.set_value(-4.54)
361  val = v.get_value()
362  self.assertEqual(-4.54, val)
363 
365  statenode = self.opc.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_State))
366  state = statenode.get_value()
367  self.assertEqual(state, 0)
368 
369  def test_bad_node(self):
370  bad = self.opc.get_node(ua.NodeId(999, 999))
371  with self.assertRaises(ua.UaStatusCodeError):
372  bad.get_browse_name()
373  with self.assertRaises(ua.UaStatusCodeError):
374  bad.set_value(89)
375  with self.assertRaises(ua.UaStatusCodeError):
376  bad.add_object(0, "0:myobj")
377  with self.assertRaises(ua.UaStatusCodeError):
378  bad.get_child("0:myobj")
379 
380  def test_value(self):
381  o = self.opc.get_objects_node()
382  var = ua.Variant(1.98, ua.VariantType.Double)
383  v = o.add_variable(3, 'VariableValue', var)
384  val = v.get_value()
385  self.assertEqual(1.98, val)
386 
387  dvar = ua.DataValue(var)
388  dv = v.get_data_value()
389  self.assertEqual(ua.DataValue, type(dv))
390  self.assertEqual(dvar.Value, dv.Value)
391  self.assertEqual(dvar.Value, var)
392 
393  def test_set_value(self):
394  o = self.opc.get_objects_node()
395  var = ua.Variant(1.98, ua.VariantType.Double)
396  dvar = ua.DataValue(var)
397  v = o.add_variable(3, 'VariableValue', var)
398  v.set_value(var.Value)
399  v1 = v.get_value()
400  self.assertEqual(v1, var.Value)
401  v.set_value(var)
402  v2 = v.get_value()
403  self.assertEqual(v2, var.Value)
404  v.set_data_value(dvar)
405  v3 = v.get_data_value()
406  self.assertEqual(v3.Value, dvar.Value)
407 
408  def test_array_value(self):
409  o = self.opc.get_objects_node()
410  v = o.add_variable(3, 'VariableArrayValue', [1, 2, 3])
411  val = v.get_value()
412  self.assertEqual([1, 2, 3], val)
413 
415  o = self.opc.get_objects_node()
416  v = o.add_variable(3, 'BoolVariable', True)
417  dt = v.get_data_type_as_variant_type()
418  self.assertEqual(dt, ua.VariantType.Boolean)
419  val = v.get_value()
420  self.assertEqual(True, val)
421  v.set_value(False)
422  val = v.get_value()
423  self.assertEqual(False, val)
424 
426  o = self.opc.get_objects_node()
427  v = o.add_variable(3, 'VariableArrayValue', [1, 2, 3])
428  v.set_value([1])
429  val = v.get_value()
430  self.assertEqual([1], val)
431 
433  idx = self.opc.get_namespace_index("urn:freeopcua:python:server")
434  self.assertEqual(idx, 1)
435  root = self.opc.get_root_node()
436  myvar = root.add_variable(idx, 'var_in_custom_namespace', [5])
437  myid = myvar.nodeid
438  self.assertEqual(idx, myid.NamespaceIndex)
439 
440  def test_method(self):
441  o = self.opc.get_objects_node()
442  m = o.get_child("2:ServerMethod")
443  result = o.call_method("2:ServerMethod", 2.1)
444  self.assertEqual(result, 4.2)
445  with self.assertRaises(ua.UaStatusCodeError):
446  # FIXME: we should raise a more precise exception
447  result = o.call_method("2:ServerMethod", 2.1, 89, 9)
448  with self.assertRaises(ua.UaStatusCodeError):
449  result = o.call_method(ua.NodeId(999), 2.1) # non existing method
450 
451  def test_method_array(self):
452  o = self.opc.get_objects_node()
453  m = o.get_child("2:ServerMethodArray")
454  result = o.call_method(m, "sin", ua.Variant(math.pi))
455  self.assertTrue(result < 0.01)
456 
458  o = self.opc.get_objects_node()
459  m = o.get_child("2:ServerMethodArray2")
460  result = o.call_method(m, [1.1, 3.4, 9])
461  self.assertEqual(result, [2.2, 6.8, 18])
462 
463  def test_add_nodes(self):
464  objects = self.opc.get_objects_node()
465  f = objects.add_folder(3, 'MyFolder')
466  child = objects.get_child("3:MyFolder")
467  self.assertEqual(child, f)
468  o = f.add_object(3, 'MyObject')
469  child = f.get_child("3:MyObject")
470  self.assertEqual(child, o)
471  v = f.add_variable(3, 'MyVariable', 6)
472  child = f.get_child("3:MyVariable")
473  self.assertEqual(child, v)
474  p = f.add_property(3, 'MyProperty', 10)
475  child = f.get_child("3:MyProperty")
476  self.assertEqual(child, p)
477  childs = f.get_children()
478  self.assertTrue(o in childs)
479  self.assertTrue(v in childs)
480  self.assertTrue(p in childs)
481 
483  base_type = self.opc.get_root_node().get_child(["0:Types", "0:ObjectTypes", "0:BaseObjectType"])
484  descs = base_type.get_children_descriptions(includesubtypes=True)
485  self.assertTrue(len(descs) > 10)
486  descs = base_type.get_children_descriptions(includesubtypes=False)
487  self.assertEqual(len(descs), 0)
488 
490  objects = self.opc.get_objects_node()
491  f = objects.add_folder(3, 'MyFolder_TypeTest')
492 
493  o = f.add_object(3, 'MyObject1', ua.ObjectIds.BaseObjectType)
494  self.assertEqual(o.get_type_definition().Identifier, ua.ObjectIds.BaseObjectType)
495 
496  o = f.add_object(3, 'MyObject2', ua.NodeId(ua.ObjectIds.BaseObjectType, 0))
497  self.assertEqual(o.get_type_definition().Identifier, ua.ObjectIds.BaseObjectType)
498 
499  base_otype= self.opc.get_node(ua.ObjectIds.BaseObjectType)
500  custom_otype = base_otype.add_object_type(2, 'MyFooObjectType')
501 
502  o = f.add_object(3, 'MyObject3', custom_otype.nodeid)
503  self.assertEqual(o.get_type_definition().Identifier, custom_otype.nodeid.Identifier)
504 
505  references = o.get_references(refs=ua.ObjectIds.HasTypeDefinition, direction=ua.BrowseDirection.Forward)
506  self.assertEqual(len(references), 1)
507  self.assertEqual(references[0].NodeId, custom_otype.nodeid)
508 
510  objects = self.opc.get_objects_node()
511  o = objects.add_object(3, 'MyObject')
512  nodes = objects.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Forward, includesubtypes=False)
513  self.assertTrue(o in nodes)
514  nodes = o.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
515  self.assertTrue(objects in nodes)
516  self.assertEqual(o.get_parent(), objects)
517  self.assertEqual(o.get_type_definition().Identifier, ua.ObjectIds.BaseObjectType)
518 
519  o2 = o.add_object(3, 'MySecondObject')
520  nodes = o.get_referenced_nodes(refs=ua.ObjectIds.HasComponent, direction=ua.BrowseDirection.Forward, includesubtypes=False)
521  self.assertTrue(o2 in nodes)
522  nodes = o2.get_referenced_nodes(refs=ua.ObjectIds.HasComponent, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
523  self.assertTrue(o in nodes)
524  self.assertEqual(o2.get_parent(), o)
525  self.assertEqual(o2.get_type_definition().Identifier, ua.ObjectIds.BaseObjectType)
526 
527  v = o.add_variable(3, 'MyVariable', 6)
528  nodes = o.get_referenced_nodes(refs=ua.ObjectIds.HasComponent, direction=ua.BrowseDirection.Forward, includesubtypes=False)
529  self.assertTrue(v in nodes)
530  nodes = v.get_referenced_nodes(refs=ua.ObjectIds.HasComponent, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
531  self.assertTrue(o in nodes)
532  self.assertEqual(v.get_parent(), o)
533  self.assertEqual(v.get_type_definition().Identifier, ua.ObjectIds.BaseDataVariableType)
534 
535  p = o.add_property(3, 'MyProperty', 2)
536  nodes = o.get_referenced_nodes(refs=ua.ObjectIds.HasProperty, direction=ua.BrowseDirection.Forward, includesubtypes=False)
537  self.assertTrue(p in nodes)
538  nodes = p.get_referenced_nodes(refs=ua.ObjectIds.HasProperty, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
539  self.assertTrue(o in nodes)
540  self.assertEqual(p.get_parent(), o)
541  self.assertEqual(p.get_type_definition().Identifier, ua.ObjectIds.PropertyType)
542 
543  def test_path_string(self):
544  o = self.opc.nodes.objects.add_folder(1, "titif").add_object(3, "opath")
545  path = o.get_path_as_string()
546  self.assertEqual(["0:Root", "0:Objects", "1:titif", "3:opath"], path)
547  path = o.get_path_as_string(2)
548  self.assertEqual(["1:titif", "3:opath"], path)
549  path = self.opc.get_node("i=13387").get_path_as_string()
550  # FIXME this is wrong in our server! BaseObjectType is missing an inverse reference to its parent! seems xml definition is wrong
551  self.assertEqual(['0:BaseObjectType', '0:FolderType', '0:FileDirectoryType', '0:CreateDirectory'], path)
552 
553  def test_path(self):
554  of = self.opc.nodes.objects.add_folder(1, "titif")
555  op = of.add_object(3, "opath")
556  path = op.get_path()
557  self.assertEqual([self.opc.nodes.root, self.opc.nodes.objects, of, op], path)
558  path = op.get_path(2)
559  self.assertEqual([of, op], path)
560  target = self.opc.get_node("i=13387")
561  path = target.get_path()
562  # FIXME this is wrong in our server! BaseObjectType is missing an inverse reference to its parent! seems xml definition is wrong
563  self.assertEqual([self.opc.nodes.base_object_type, self.opc.nodes.folder_type, self.opc.get_node(ua.ObjectIds.FileDirectoryType), target], path)
564 
566  endpoints = self.opc.get_endpoints()
567  self.assertTrue(len(endpoints) > 0)
568  self.assertTrue(endpoints[0].EndpointUrl.startswith("opc.tcp://"))
569 
570  def test_copy_node(self):
571  dev_t = self.opc.nodes.base_data_type.add_object_type(0, "MyDevice")
572  v_t = dev_t.add_variable(0, "sensor", 1.0)
573  p_t = dev_t.add_property(0, "sensor_id", "0340")
574  ctrl_t = dev_t.add_object(0, "controller")
575  prop_t = ctrl_t.add_property(0, "state", "Running")
576  # Create device sutype
577  devd_t = dev_t.add_object_type(0, "MyDeviceDervived")
578  v_t = devd_t.add_variable(0, "childparam", 1.0)
579  p_t = devd_t.add_property(0, "sensorx_id", "0340")
580 
581 
582  nodes = copy_node(self.opc.nodes.objects, dev_t)
583  mydevice = nodes[0]
584 
585  self.assertEqual(mydevice.get_node_class(), ua.NodeClass.ObjectType)
586  self.assertEqual(len(mydevice.get_children()), 4)
587  obj = mydevice.get_child(["0:controller"])
588  prop = mydevice.get_child(["0:controller", "0:state"])
589  self.assertEqual(prop.get_type_definition().Identifier, ua.ObjectIds.PropertyType)
590  self.assertEqual(prop.get_value(), "Running")
591  self.assertNotEqual(prop.nodeid, prop_t.nodeid)
592 
594  # Create device type
595  dev_t = self.opc.nodes.base_object_type.add_object_type(0, "MyDevice")
596  v_t = dev_t.add_variable(0, "sensor", 1.0)
597  p_t = dev_t.add_property(0, "sensor_id", "0340")
598  ctrl_t = dev_t.add_object(0, "controller")
599  prop_t = ctrl_t.add_property(0, "state", "Running")
600 
601  # Create device sutype
602  devd_t = dev_t.add_object_type(0, "MyDeviceDervived")
603  v_t = devd_t.add_variable(0, "childparam", 1.0)
604  p_t = devd_t.add_property(0, "sensorx_id", "0340")
605 
606  # instanciate device
607  nodes = instantiate(self.opc.nodes.objects, dev_t, bname="2:Device0001")
608  mydevice = nodes[0]
609 
610  self.assertEqual(mydevice.get_node_class(), ua.NodeClass.Object)
611  self.assertEqual(mydevice.get_type_definition(), dev_t.nodeid)
612  obj = mydevice.get_child(["0:controller"])
613  prop = mydevice.get_child(["0:controller", "0:state"])
614  self.assertEqual(prop.get_type_definition().Identifier, ua.ObjectIds.PropertyType)
615  self.assertEqual(prop.get_value(), "Running")
616  self.assertNotEqual(prop.nodeid, prop_t.nodeid)
617 
618  # instanciate device subtype
619  nodes = instantiate(self.opc.nodes.objects, devd_t, bname="2:Device0002")
620  mydevicederived = nodes[0]
621  prop1 = mydevicederived.get_child(["0:sensorx_id"])
622  var1 = mydevicederived.get_child(["0:childparam"])
623  var_parent = mydevicederived.get_child(["0:sensor"])
624  prop_parent = mydevicederived.get_child(["0:sensor_id"])
625 
627  # Create device type
628  dev_t = self.opc.nodes.base_object_type.add_object_type(0, "MyDevice2")
629  v_t = dev_t.add_variable(0, "sensor", 1.0)
630  p_t = dev_t.add_property(0, "sensor_id", "0340")
631  ctrl_t = dev_t.add_object(0, "controller")
632  prop_t = ctrl_t.add_property(0, "state", "Running")
633 
634  # instanciate device
635  nodes = instantiate(self.opc.nodes.objects, dev_t, nodeid=ua.NodeId("InstDevice", 2, ua.NodeIdType.String), bname="2:InstDevice")
636  mydevice = nodes[0]
637 
638  self.assertEqual(mydevice.get_node_class(), ua.NodeClass.Object)
639  self.assertEqual(mydevice.get_type_definition(), dev_t.nodeid)
640  obj = mydevice.get_child(["0:controller"])
641  obj_nodeid_ident = obj.nodeid.Identifier
642  prop = mydevice.get_child(["0:controller", "0:state"])
643  self.assertEqual(obj_nodeid_ident, "InstDevice.controller")
644  self.assertEqual(prop.get_type_definition().Identifier, ua.ObjectIds.PropertyType)
645  self.assertEqual(prop.get_value(), "Running")
646  self.assertNotEqual(prop.nodeid, prop_t.nodeid)
647 
649  v1 = self.opc.nodes.objects.add_variable(3, 'VariableEnumType1', ua.ApplicationType.ClientAndServer, datatype=ua.NodeId(ua.ObjectIds.ApplicationType))
650  tp1 = v1.get_data_type()
651  self.assertEqual(ua.NodeId(ua.ObjectIds.ApplicationType), tp1)
652 
653  v2 = self.opc.nodes.objects.add_variable(3, 'VariableEnumType2', ua.ApplicationType.ClientAndServer, datatype=ua.NodeId(ua.ObjectIds.ApplicationType) )
654  tp2 = v2.get_data_type()
655  self.assertEqual( ua.NodeId(ua.ObjectIds.ApplicationType), tp2)
656 
657  def test_enum(self):
658  # create enum type
659  enums = self.opc.get_root_node().get_child(["0:Types", "0:DataTypes", "0:BaseDataType", "0:Enumeration"])
660  myenum_type = enums.add_data_type(0, "MyEnum")
661  es = myenum_type.add_variable(0, "EnumStrings", [ua.LocalizedText("String0"), ua.LocalizedText("String1"), ua.LocalizedText("String2")], ua.VariantType.LocalizedText)
662  #es.set_value_rank(1)
663  # instantiate
664  o = self.opc.get_objects_node()
665  myvar = o.add_variable(2, "MyEnumVar", ua.LocalizedText("String1"), datatype=myenum_type.nodeid)
666  #myvar.set_writable(True)
667  # tests
668  self.assertEqual(myvar.get_data_type(), myenum_type.nodeid)
669  myvar.set_value(ua.LocalizedText("String2"))
670 
671  def test_supertypes(self):
672  nint32 = self.opc.get_node(ua.ObjectIds.Int32)
673  node = ua_utils.get_node_supertype(nint32)
674  self.assertEqual(node, self.opc.get_node(ua.ObjectIds.Integer))
675 
676  nodes = ua_utils.get_node_supertypes(nint32)
677  self.assertEqual(nodes[1], self.opc.get_node(ua.ObjectIds.Number))
678  self.assertEqual(nodes[0], self.opc.get_node(ua.ObjectIds.Integer))
679 
680  # test custom
681  dtype = nint32.add_data_type(0, "MyCustomDataType")
682  node = ua_utils.get_node_supertype(dtype)
683  self.assertEqual(node, nint32)
684 
685  dtype2 = dtype.add_data_type(0, "MyCustomDataType2")
686  node = ua_utils.get_node_supertype(dtype2)
687  self.assertEqual(node, dtype)
688 
690  nint32 = self.opc.get_node(ua.ObjectIds.Int32)
691  dtype = nint32.add_data_type(0, "MyCustomDataType")
692  dtype2 = dtype.add_data_type(0, "MyCustomDataType2")
693  self.assertEqual(ua_utils.get_base_data_type(dtype), nint32)
694  self.assertEqual(ua_utils.get_base_data_type(dtype2), nint32)
695 
696  ext = self.opc.nodes.objects.add_variable(0, "MyExtensionObject", ua.Argument())
697  d = ext.get_data_type()
698  d = self.opc.get_node(d)
699  self.assertEqual(ua_utils.get_base_data_type(d), self.opc.get_node(ua.ObjectIds.Structure))
700  self.assertEqual(ua_utils.data_type_to_variant_type(d), ua.VariantType.ExtensionObject)
701 
702 
703 
704 
def test_get_node_by_nodeid(self)
def test_non_existing_path(self)
def test_add_string_array_variable(self)
def test_delete_nodes_recursive(self)
Definition: tests_common.py:87
def test_references_for_added_nodes(self)
def test_browsename_with_spaces(self)
def instantiate(parent, node_type, nodeid=None, bname=None, idx=0)
Definition: instantiate.py:12
def test_add_string_variable(self)
def test_variable_with_datatype(self)
def test_add_node_bad_args(self)
Definition: tests_common.py:55
def test_add_numeric_variable(self)
def test_array_size_one_value(self)
def add_server_methods(srv)
Definition: tests_common.py:16
def test_add_node_with_type(self)
def test_delete_nodes_recursive2(self)
Definition: tests_common.py:97
def func(parent, variant)
def test_browse_references(self)
def test_read_server_state(self)
def test_variant_array_dim(self)
def test_instantiate_string_nodeid(self)
def copy_node(parent, node, nodeid=None, recursive=True)
Definition: copy_node.py:5
def test_variable_data_type(self)


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Tue Jan 19 2021 03:12:44