Commit 5318bda8 authored by SCHUNK-Osswald's avatar SCHUNK-Osswald Committed by ORD

Added command line support for OPC-UA method calls according to #321 (#325)

* - added tools/uacall script

* As suggested by oroulet corrected misunderstandings in signature of
Node.call_method(). No parent needed, just use self.

* Old folks, knowing Python2.x only, keep forgetting that print should
have been a function in the first place...
parent 0141aca8
......@@ -18,3 +18,6 @@ dist
newdocs
examples/history.db
*.sql
/.project
/.pydevproject
/.settings/
......@@ -32,6 +32,7 @@ A set of command line tools also available: https://github.com/FreeOpcUa/python-
* uahistoryread
* uaread (read attribute of a node)
* uawrite (write attribute of a node)
* uacall (call method of a node)
* uasubscribe (subscribe to a node and print datachange events)
* uaclient (connect to server and start python shell)
* uaserver (starts a demo OPC UA server)
......
......@@ -550,5 +550,5 @@ class Node(object):
def add_reference_type(self, parent, nodeid, bname):
return opcua.common.manage_nodes.create_reference_type(parent, nodeid, bname)
def call_method(parent, methodid, *args):
return opcua.common.methods.call_method(parent, methodid, *args)
def call_method(self, methodid, *args):
return opcua.common.methods.call_method(self, methodid, *args)
......@@ -651,3 +651,75 @@ def uahistoryread():
finally:
client.disconnect()
sys.exit(0)
def uacall():
parser = argparse.ArgumentParser(description="Call method of a node")
add_common_args(parser)
parser.add_argument("-m",
"--method",
dest="method",
type=int,
default=None,
help="Set method to call. If not given then (single) method of the selected node is used.")
parser.add_argument("-l",
"--list",
"--array",
dest="array",
default="guess",
choices=["guess", "true", "false"],
help="Value is an array")
parser.add_argument("-t",
"--datatype",
dest="datatype",
default="guess",
choices=["guess", 'byte', 'sbyte', 'nodeid', 'expandednodeid', 'qualifiedname', 'browsename', 'string', 'float', 'double', 'int16', 'int32', "int64", 'uint16', 'uint32', 'uint64', "bool", "string", 'datetime', 'bytestring', 'xmlelement', 'statuscode', 'localizedtext'],
help="Data type to return")
parser.add_argument("value",
help="Value to use for call to method, if any",
nargs="?",
metavar="VALUE")
args = parse_args(parser, requirenodeid=True)
client = Client(args.url, timeout=args.timeout)
client.set_security_string(args.security)
client.connect()
try:
node = get_node(client, args)
# val must be a tuple in order to enable method calls without arguments
if ( args.value is None ):
val = () #empty tuple
else:
val = (_val_to_variant(args.value, args),) # tuple with one element
# determine method to call: Either explicitly given or automatically select the method of the selected node.
methods = node.get_methods()
method_id = None
#print( "methods=%s" % (methods) )
if ( args.method is None ):
if ( len( methods ) == 0 ):
raise ValueError( "No methods in selected node and no method given" )
elif ( len( methods ) == 1 ):
method_id = methods[0]
else:
raise ValueError( "Selected node has %d methods but no method given. Provide one of %s" % (methods) )
else:
for m in methods:
if ( m.nodeid.Identifier == args.method ):
method_id = m.nodeid
break
if ( method_id is None):
# last resort:
method_id = ua.NodeId( identifier=args.method )#, namespaceidx=? )#, nodeidtype=?): )
#print( "method_id=%s\nval=%s" % (method_id,val) )
result_variants = node.call_method( method_id, *val )
print( "resulting result_variants=%s" % result_variants )
finally:
client.disconnect()
sys.exit(0)
print(args)
......@@ -39,7 +39,8 @@ setup(name="freeopcua",
'uahistoryread = opcua.tools:uahistoryread',
'uaclient = opcua.tools:uaclient',
'uaserver = opcua.tools:uaserver',
'uadiscover = opcua.tools:uadiscover'
'uadiscover = opcua.tools:uadiscover',
'uacall = opcua.tools:uacall',
]
}
)
......
/tests_spaw.py
/export.xml
#!/usr/bin/env python
import sys
import os
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
from opcua.tools import uacall
if __name__ == "__main__":
uacall()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment