本文整理汇总了Python中opcua.Server.import_xml方法的典型用法代码示例。如果您正苦于以下问题:Python Server.import_xml方法的具体用法?Python Server.import_xml怎么用?Python Server.import_xml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opcua.Server
的用法示例。
在下文中一共展示了Server.import_xml方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import import_xml [as 别名]
class HelloServer:
def __init__(self, endpoint, name, model_filepath):
self.server = Server()
# This need to be imported at the start or else it will overwrite the data
self.server.import_xml(model_filepath)
self.server.set_endpoint(endpoint)
self.server.set_server_name(name)
objects = self.server.get_objects_node()
freeopcua_namespace = self.server.get_namespace_index("urn:freeopcua:python:server")
hellower = objects.get_child("0:Hellower")
hellower_say_hello = hellower.get_child("0:SayHello")
self.server.link_method(hellower_say_hello, say_hello_xml)
hellower.add_method(
freeopcua_namespace, "SayHello2", say_hello, [ua.VariantType.Boolean], [ua.VariantType.String])
hellower.add_method(
freeopcua_namespace, "SayHelloArray", say_hello_array, [ua.VariantType.Boolean], [ua.VariantType.String])
def __enter__(self):
self.server.start()
return self.server
def __exit__(self, exc_type, exc_val, exc_tb):
self.server.stop()
示例2: TestServer
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import import_xml [as 别名]
class TestServer(unittest.TestCase, CommonTests):
'''
Run common tests on server side
Tests that can only be run on server side must be defined here
'''
@classmethod
def setUpClass(self):
self.srv = Server()
self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num2)
add_server_methods(self.srv)
self.srv.start()
self.opc = self.srv
@classmethod
def tearDownClass(self):
self.srv.stop()
def test_register_namespace(self):
uri = 'http://mycustom.Namespace.com'
idx1 = self.opc.register_namespace(uri)
idx2 = self.opc.get_namespace_index(uri)
self.assertEqual(idx1, idx2)
def test_register_use_namespace(self):
uri = 'http://my_very_custom.Namespace.com'
idx = self.opc.register_namespace(uri)
root = self.opc.get_root_node()
myvar = root.add_variable(idx, 'var_in_custom_namespace', [5])
myid = myvar.nodeid
self.assertEqual(idx, myid.NamespaceIndex)
def test_server_method(self):
def func(parent, variant):
variant.Value *= 2
return [variant]
o = self.opc.get_objects_node()
v = o.add_method(3, 'Method1', func, [ua.VariantType.Int64], [ua.VariantType.Int64])
result = o.call_method(v, ua.Variant(2.1))
self.assertEqual(result, 4.2)
def test_xml_import(self):
self.srv.import_xml("tests/custom_nodes.xml")
o = self.opc.get_objects_node()
v = o.get_child(["MyXMLFolder", "MyXMLObject", "MyXMLVariable"])
val = v.get_value()
self.assertEqual(val, "StringValue")
示例3: ServerPython
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import import_xml [as 别名]
class ServerPython(object):
def __init__(self):
self._server = None
self.nodes = None
self.get_node = None
self.get_namespace_array = None
def start_server(self, endpoint):
logger.info("Starting python-opcua server")
self._server = Server()
self._server.set_endpoint(endpoint)
self._server.set_server_name("OpcUa Modeler Server")
self.nodes = self._server.nodes
self.get_node = self._server.get_node
self.get_namespace_array = self._server.get_namespace_array
self.load_type_definitions = self._server.load_type_definitions
self.load_enums = self._server.load_enums
# now remove freeopcua namespace, not necessary when modeling and
# ensures correct idx for exported nodesets
ns_node = self._server.get_node(ua.NodeId(ua.ObjectIds.Server_NamespaceArray))
nss = ns_node.get_value()
ns_node.set_value(nss[:1])
self._server.start()
def stop_server(self):
if self._server is not None:
self._server.stop()
self._server = None
self.get_node = None
self.get_namespace_array = None
def import_xml(self, path):
return self._server.import_xml(path)
def export_xml(self, nodes, uris, path):
exp = XmlExporter(self._server)
exp.build_etree(nodes, uris=uris)
exp.write_xml(path)
示例4: print
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import import_xml [as 别名]
# create directly some objects and variables
myobj = server.nodes.objects.add_object(idx, "MyObject")
myvar = myobj.add_variable(idx, "MyVariable", 6.7)
myvar.set_writable() # Set MyVariable to be writable by clients
mystringvar = myobj.add_variable(idx, "MyStringVariable", "Really nice string")
mystringvar.set_writable() # Set MyVariable to be writable by clients
mydtvar = myobj.add_variable(idx, "MyDateTimeVar", datetime.now())
mydtvar.set_writable() # Set MyVariable to be writable by clients
myarrayvar = myobj.add_variable(idx, "myarrayvar", [6.7, 7.9])
myarrayvar = myobj.add_variable(idx, "myStronglytTypedVariable", ua.Variant([], ua.VariantType.UInt32))
myprop = myobj.add_property(idx, "myproperty", "I am a property")
mymethod = myobj.add_method(idx, "mymethod", func, [ua.VariantType.Int64], [ua.VariantType.Boolean])
multiply_node = myobj.add_method(idx, "multiply", multiply, [ua.VariantType.Int64, ua.VariantType.Int64], [ua.VariantType.Int64])
# import some nodes from xml
server.import_xml("custom_nodes.xml")
# creating a default event object
# The event object automatically will have members for all events properties
# you probably want to create a custom event type, see other examples
myevgen = server.get_event_generator()
myevgen.event.Severity = 300
# starting!
server.start()
print("Available loggers are: ", logging.Logger.manager.loggerDict.keys())
try:
# enable following if you want to subscribe to nodes on server side
#handler = SubHandler()
#sub = server.create_subscription(500, handler)
#handle = sub.subscribe_data_change(myvar)
示例5: TestServer
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import import_xml [as 别名]
#.........这里部分代码省略.........
self.srv.register_to_discovery(self.discovery.endpoint.geturl(), 1)
time.sleep(1) # let server register registration
servers = self.discovery.find_servers()
print("SERVERS 2", servers)
self.assertEqual(len(servers), 2)
"""
#def test_register_server2(self):
#servers = self.opc.register_server()
def test_register_namespace(self):
uri = 'http://mycustom.Namespace.com'
idx1 = self.opc.register_namespace(uri)
idx2 = self.opc.get_namespace_index(uri)
self.assertEqual(idx1, idx2)
def test_register_use_namespace(self):
uri = 'http://my_very_custom.Namespace.com'
idx = self.opc.register_namespace(uri)
root = self.opc.get_root_node()
myvar = root.add_variable(idx, 'var_in_custom_namespace', [5])
myid = myvar.nodeid
self.assertEqual(idx, myid.NamespaceIndex)
def test_server_method(self):
def func(parent, variant):
variant.Value *= 2
return [variant]
o = self.opc.get_objects_node()
v = o.add_method(3, 'Method1', func, [ua.VariantType.Int64], [ua.VariantType.Int64])
result = o.call_method(v, ua.Variant(2.1))
self.assertEqual(result, 4.2)
def test_xml_import(self):
self.srv.import_xml("tests/custom_nodes.xml")
o = self.opc.get_objects_node()
v = o.get_child(["MyXMLFolder", "MyXMLObject", "MyXMLVariable"])
val = v.get_value()
self.assertEqual(val, "StringValue")
def test_historize_variable(self):
o = self.opc.get_objects_node()
var = o.add_variable(3, "test_hist", 1.0)
self.srv.iserver.enable_history_data_change(var, timedelta(days=1))
time.sleep(1)
var.set_value(2.0)
var.set_value(3.0)
self.srv.iserver.disable_history_data_change(var)
def test_historize_events(self):
srv_node = self.srv.get_node(ua.ObjectIds.Server)
srvevgen = self.srv.get_event_generator()
self.srv.iserver.enable_history_event(srv_node, period=None)
srvevgen.trigger(message="Message")
self.srv.iserver.disable_history_event(srv_node)
def test_references_for_added_nodes_method(self):
objects = self.opc.get_objects_node()
o = objects.add_object(3, 'MyObject')
nodes = objects.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Forward, includesubtypes=False)
self.assertTrue(o in nodes)
nodes = o.get_referenced_nodes(refs=ua.ObjectIds.Organizes, direction=ua.BrowseDirection.Inverse, includesubtypes=False)
self.assertTrue(objects in nodes)
self.assertEqual(o.get_parent(), objects)
self.assertEqual(o.get_type_definition(), ua.ObjectIds.BaseObjectType)
@uamethod
示例6: uaserver
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import import_xml [as 别名]
def uaserver():
parser = argparse.ArgumentParser(description="Run an example OPC-UA server. By importing xml definition and using uawrite command line, it is even possible to expose real data using this server")
# we setup a server, this is a bit different from other tool so we do not reuse common arguments
parser.add_argument("-u",
"--url",
help="URL of OPC UA server, default is opc.tcp://0.0.0.0:4840",
default='opc.tcp://0.0.0.0:4840',
metavar="URL")
parser.add_argument("-v",
"--verbose",
dest="loglevel",
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
default='WARNING',
help="Set log level")
parser.add_argument("-x",
"--xml",
metavar="XML_FILE",
help="Populate address space with nodes defined in XML")
parser.add_argument("-p",
"--populate",
action="store_true",
help="Populate address space with some sample nodes")
parser.add_argument("-c",
"--disable-clock",
action="store_true",
help="Disable clock, to avoid seeing many write if debugging an application")
parser.add_argument("-s",
"--shell",
action="store_true",
help="Start python shell instead of randomly changing node values")
parser.add_argument("--certificate",
help="set server certificate")
parser.add_argument("--private_key",
help="set server private key")
args = parser.parse_args()
logging.basicConfig(format="%(levelname)s: %(message)s", level=getattr(logging, args.loglevel))
server = Server()
server.set_endpoint(args.url)
if args.certificate:
server.load_certificate(args.certificate)
if args.private_key:
server.load_private_key(args.private_key)
server.disable_clock(args.disable_clock)
server.set_server_name("FreeOpcUa Example Server")
if args.xml:
server.import_xml(args.xml)
if args.populate:
@uamethod
def multiply(parent, x, y):
print("multiply method call with parameters: ", x, y)
return x * y
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
objects = server.get_objects_node()
myobj = objects.add_object(idx, "MyObject")
mywritablevar = myobj.add_variable(idx, "MyWritableVariable", 6.7)
mywritablevar.set_writable() # Set MyVariable to be writable by clients
myvar = myobj.add_variable(idx, "MyVariable", 6.7)
myarrayvar = myobj.add_variable(idx, "MyVarArray", [6.7, 7.9])
myprop = myobj.add_property(idx, "MyProperty", "I am a property")
mymethod = myobj.add_method(idx, "MyMethod", multiply, [ua.VariantType.Double, ua.VariantType.Int64], [ua.VariantType.Double])
server.start()
try:
if args.shell:
embed()
elif args.populate:
count = 0
while True:
time.sleep(1)
myvar.set_value(math.sin(count / 10))
myarrayvar.set_value([math.sin(count / 10), math.sin(count / 100)])
count += 1
else:
while True:
time.sleep(1)
finally:
server.stop()
sys.exit(0)
示例7: TestServer
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import import_xml [as 别名]
class TestServer(unittest.TestCase, CommonTests):
'''
Run common tests on server side
Tests that can only be run on server side must be defined here
'''
@classmethod
def setUpClass(self):
self.srv = Server()
self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num2)
add_server_methods(self.srv)
self.srv.start()
self.opc = self.srv
self.discovery = Server()
self.discovery.set_application_uri("urn:freeopcua:python:discovery")
self.discovery.set_endpoint('opc.tcp://localhost:%d' % port_discovery)
self.discovery.start()
@classmethod
def tearDownClass(self):
self.srv.stop()
self.discovery.stop()
def test_discovery(self):
client = Client(self.discovery.endpoint.geturl())
client.connect()
servers = client.find_servers()
new_app_uri = "urn:freeopcua:python:server::test_discovery"
self.srv.application_uri = new_app_uri
self.srv.register_to_discovery(self.discovery.endpoint.geturl(), 1)
time.sleep(0.1) # let server register registration
new_servers = client.find_servers()
self.assertEqual(len(new_servers) - len(servers) , 1)
self.assertFalse(new_app_uri in [s.ApplicationUri for s in servers])
self.assertTrue(new_app_uri in [s.ApplicationUri for s in new_servers])
client.disconnect()
"""
# not sure if this test is necessary, and there is a lot repetition with previous test
def test_discovery_server_side(self):
servers = self.discovery.find_servers()
self.assertEqual(len(servers), 1)
self.srv.register_to_discovery(self.discovery.endpoint.geturl(), 1)
time.sleep(1) # let server register registration
servers = self.discovery.find_servers()
print("SERVERS 2", servers)
self.assertEqual(len(servers), 2)
"""
#def test_register_server2(self):
#servers = self.opc.register_server()
def test_register_namespace(self):
uri = 'http://mycustom.Namespace.com'
idx1 = self.opc.register_namespace(uri)
idx2 = self.opc.get_namespace_index(uri)
self.assertEqual(idx1, idx2)
def test_register_use_namespace(self):
uri = 'http://my_very_custom.Namespace.com'
idx = self.opc.register_namespace(uri)
root = self.opc.get_root_node()
myvar = root.add_variable(idx, 'var_in_custom_namespace', [5])
myid = myvar.nodeid
self.assertEqual(idx, myid.NamespaceIndex)
def test_server_method(self):
def func(parent, variant):
variant.Value *= 2
return [variant]
o = self.opc.get_objects_node()
v = o.add_method(3, 'Method1', func, [ua.VariantType.Int64], [ua.VariantType.Int64])
result = o.call_method(v, ua.Variant(2.1))
self.assertEqual(result, 4.2)
def test_xml_import(self):
self.srv.import_xml("tests/custom_nodes.xml")
o = self.opc.get_objects_node()
v = o.get_child(["MyXMLFolder", "MyXMLObject", "MyXMLVariable"])
val = v.get_value()
self.assertEqual(val, "StringValue")
示例8: Server
# 需要导入模块: from opcua import Server [as 别名]
# 或者: from opcua.Server import import_xml [as 别名]
from opcua import ua, Server
if __name__ == "__main__":
# setup our server
server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
# setup our own namespace, not really necessary but should as spec
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
# Import customobject type
server.import_xml('customobject.xml')
# get Objects node, this is where we should put our custom stuff
objects = server.get_objects_node()
# get nodeid of custom object type by :
# 1) Use node ID
# 2) Or Full path
# 3) Or As child from parent
nodeid_a = ua.NodeId.from_string('ns=1;i=1002')
nodeid_b = server.get_root_node().get_child(["0:Types", "0:ObjectTypes", "0:BaseObjectType", "1:MyObjectType"]).nodeid
nodeid_c = server.get_node(ua.ObjectIds.BaseObjectType).get_child(["1:MyObjectType"]).nodeid
myobject_type_nodeid = nodeid_a