methods.py
Go to the documentation of this file.
00001 """
00002 High level method related functions
00003 """
00004 
00005 from opcua import ua
00006 from opcua.common import node
00007 
00008 
00009 def call_method(parent, methodid, *args):
00010     """
00011     Call an OPC-UA method. methodid is browse name of child method or the
00012     nodeid of method as a NodeId object
00013     arguments are variants or python object convertible to variants.
00014     which may be of different types
00015     returns a list of variants which are output of the method
00016     """
00017     if isinstance(methodid, str):
00018         methodid = parent.get_child(methodid).nodeid
00019     elif isinstance(methodid, node.Node):
00020         methodid = methodid.nodeid
00021 
00022     arguments = []
00023     for arg in args:
00024         if not isinstance(arg, ua.Variant):
00025             arg = ua.Variant(arg)
00026         arguments.append(arg)
00027 
00028     result = _call_method(parent.server, parent.nodeid, methodid, arguments)
00029 
00030     if len(result.OutputArguments) == 0:
00031         return None
00032     elif len(result.OutputArguments) == 1:
00033         return result.OutputArguments[0].Value
00034     else:
00035         return [var.Value for var in result.OutputArguments]
00036 
00037 
00038 def _call_method(server, parentnodeid, methodid, arguments):
00039     request = ua.CallMethodRequest()
00040     request.ObjectId = parentnodeid
00041     request.MethodId = methodid
00042     request.InputArguments = arguments
00043     methodstocall = [request]
00044     results = server.call(methodstocall)
00045     res = results[0]
00046     res.StatusCode.check()
00047     return res
00048 
00049 
00050 def uamethod(func):
00051     """
00052     Method decorator to automatically convert
00053     arguments and output to and from variants
00054     """
00055     def wrapper(parent, *args):
00056         if isinstance(parent, ua.NodeId):
00057             result = func(parent, *[arg.Value for arg in args])
00058         else:
00059             self = parent
00060             parent = args[0]
00061             args = args[1:]
00062             result = func(self, parent, *[arg.Value for arg in args])
00063 
00064         return to_variant(result)
00065     return wrapper
00066 
00067 
00068 def to_variant(*args):
00069     uaargs = []
00070     for arg in args:
00071         uaargs.append(ua.Variant(arg))
00072     return uaargs
00073 
00074 


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Sat Jun 8 2019 18:26:23