本文整理汇总了Python中opcua.Server类的典型用法代码示例。如果您正苦于以下问题:Python Server类的具体用法?Python Server怎么用?Python Server使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Server类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, template, template_directory, args):
Server.__init__(self)
self.template = template
self.variables = []
self.event_dict = {}
# 首先解析XML
self.parse()
示例2: start
def start(self, host, port):
self.set_endpoint("opc.tcp://" + str(host) + ":" + str(port) + "/ua/server/")
logger.info('OPCUA server started on: %s', (host, port))
Server.start(self)
# Subscription Handler
handler = SubHandler()
sub = self.create_subscription(500, handler)
for ua_variable in self.variables:
sub.subscribe_data_change(ua_variable)
示例3: setUpClass
def setUpClass(self):
self.srv = Server()
self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num)
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()
示例4: setUpClass
def setUpClass(self):
# start our own server
self.srv_crypto = Server()
self.uri_crypto = 'opc.tcp://localhost:%d' % port_num1
self.srv_crypto.set_endpoint(self.uri_crypto)
# load server certificate and private key. This enables endpoints
# with signing and encryption.
self.srv_crypto.load_certificate("examples/certificate-example.der")
self.srv_crypto.load_private_key("examples/private-key-example.pem")
self.srv_crypto.start()
# start a server without crypto
self.srv_no_crypto = Server()
self.uri_no_crypto = 'opc.tcp://localhost:%d' % port_num2
self.srv_no_crypto.set_endpoint(self.uri_no_crypto)
self.srv_no_crypto.start()
示例5: test_context_manager
def test_context_manager(self):
""" Context manager calls start() and stop()
"""
state = [0]
def increment_state(self, *args, **kwargs):
state[0] += 1
# create server and replace instance methods with dummy methods
server = Server()
server.start = increment_state.__get__(server)
server.stop = increment_state.__get__(server)
assert state[0] == 0
with server:
# test if server started
self.assertEqual(state[0], 1)
# test if server stopped
self.assertEqual(state[0], 2)
示例6: setUpClass
def setUpClass(self):
self.srv = Server()
self.srv_url = 'opc.tcp://localhost:%d' % port_num
self.srv.set_endpoint(self.srv_url)
objects = self.srv.get_objects_node()
obj = objects.add_object(4, "directory")
var = obj.add_variable(4, "variable", 1.999)
var2 = obj.add_variable(4, "variable2", 1.777)
var2.set_writable()
self.srv.start()
示例7: runTest
def runTest(self):
tmpfile = NamedTemporaryFile()
path = tmpfile.name
tmpfile.close()
#create cache file
server = Server(cacheFile = path)
#modify cache content
id = ua.NodeId(ua.ObjectIds.Server_ServerStatus_SecondsTillShutdown)
s = shelve.open(path, "w", writeback = True)
s[id.to_string()].attributes[ua.AttributeIds.Value].value = ua.DataValue(123)
s.close()
#ensure that we are actually loading from the cache
server = Server(cacheFile = path)
self.assertEqual(server.get_node(id).get_value(), 123)
os.remove(path)
示例8: setUpClass
def setUpClass(self):
# start our own server
self.srv = Server()
self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num1)
add_server_methods(self.srv)
self.srv.start()
# start client
self.clt = Client('opc.tcp://localhost:%d' % port_num1)
self.clt.connect()
self.opc = self.clt
示例9: _init_server
def _init_server(self):
self._server = Server()
self._server.set_endpoint(self._endpoint)
self._server.set_server_name(self._name)
self._index = self._server.register_namespace(self._namespace)
_log.info("Index: %r" % (self._index,))
self._objects_node = self._server.get_objects_node()
_log.info("Node: %r" % (self._objects_node))
self._objects = {}
_log.info("Endpoints: %r" % (self._server.get_endpoints()))
示例10: TestClient
class TestClient(unittest.TestCase, CommonTests):
'''
Run common tests on client side
Of course we need a server so we start a server in another
process using python Process module
Tests that can only be run on client side must be defined here
'''
@classmethod
def setUpClass(self):
# start our own server
self.srv = Server()
self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num1)
add_server_methods(self.srv)
self.srv.start()
# start client
self.clt = Client('opc.tcp://localhost:%d' % port_num1)
self.clt.connect()
self.opc = self.clt
@classmethod
def tearDownClass(self):
self.clt.disconnect()
# stop the server in its own process
self.srv.stop()
def test_service_fault(self):
request = ua.ReadRequest()
request.TypeId = ua.FourByteNodeId(999) # bad type!
with self.assertRaises(Exception):
self.clt.bclient._send_request(request)
示例11: ServerPython
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)
示例12: TestServer
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")
示例13: setUpClass
def setUpClass(self):
# start our own server
self.srv = Server()
self.srv.set_endpoint("opc.tcp://localhost:%d" % port_num1)
add_server_methods(self.srv)
self.srv.start()
# start admin client
self.clt = Client("opc.tcp://[email protected]:%d" % port_num1)
self.clt.connect()
self.opc = self.clt
# start anonymous client
self.ro_clt = Client("opc.tcp://localhost:%d" % port_num1)
self.ro_clt.connect()
示例14: mymain
def mymain():
# 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)
# get Objects node, this is where we should put our nodes
objects = server.get_objects_node()
# populating our address space
myobj = objects.add_object(idx, "MyObject")
myvar = myobj.add_variable(idx, "MyVariable", 6.7)
myvar.set_writable() # Set MyVariable to be writable by clients
# starting!
server.start()
server.stop()
示例15: start_server
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()