methods.py
Go to the documentation of this file.
1 """
2 High level method related functions
3 """
4 
5 from opcua import ua
6 from opcua.common import node
7 
8 
9 def call_method(parent, methodid, *args):
10  """
11  Call an OPC-UA method. methodid is browse name of child method or the
12  nodeid of method as a NodeId object
13  arguments are variants or python object convertible to variants.
14  which may be of different types
15  returns a list of variants which are output of the method
16  """
17  if isinstance(methodid, str):
18  methodid = parent.get_child(methodid).nodeid
19  elif isinstance(methodid, node.Node):
20  methodid = methodid.nodeid
21 
22  arguments = []
23  for arg in args:
24  if not isinstance(arg, ua.Variant):
25  arg = ua.Variant(arg)
26  arguments.append(arg)
27 
28  result = _call_method(parent.server, parent.nodeid, methodid, arguments)
29 
30  if len(result.OutputArguments) == 0:
31  return None
32  elif len(result.OutputArguments) == 1:
33  return result.OutputArguments[0].Value
34  else:
35  return [var.Value for var in result.OutputArguments]
36 
37 
38 def _call_method(server, parentnodeid, methodid, arguments):
39  request = ua.CallMethodRequest()
40  request.ObjectId = parentnodeid
41  request.MethodId = methodid
42  request.InputArguments = arguments
43  methodstocall = [request]
44  results = server.call(methodstocall)
45  res = results[0]
46  res.StatusCode.check()
47  return res
48 
49 
50 def uamethod(func):
51  """
52  Method decorator to automatically convert
53  arguments and output to and from variants
54  """
55  def wrapper(parent, *args):
56  if isinstance(parent, ua.NodeId):
57  result = func(parent, *[arg.Value for arg in args])
58  else:
59  self = parent
60  parent = args[0]
61  args = args[1:]
62  result = func(self, parent, *[arg.Value for arg in args])
63 
64  return to_variant(result)
65  return wrapper
66 
67 
68 def to_variant(*args):
69  uaargs = []
70  for arg in args:
71  uaargs.append(ua.Variant(arg))
72  return uaargs
73 
74 
def uamethod(func)
Definition: methods.py:50
def call_method(parent, methodid, args)
Definition: methods.py:9
def to_variant(args)
Definition: methods.py:68
def func(parent, variant)
def _call_method(server, parentnodeid, methodid, arguments)
Definition: methods.py:38


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