Commit e38b6796 authored by ORD's avatar ORD

Merge pull request #127 from FreeOpcUa/dev

small fixes
parents 7618777d c44e460b
......@@ -33,8 +33,11 @@ Contents:
client
node
subscription
errors
ua
opcua
All:
modules
Indices and tables
==================
......
opcua
=====
.. toctree::
:maxdepth: 4
opcua
opcua.client package
====================
Submodules
----------
opcua.client.client module
--------------------------
.. automodule:: opcua.client.client
:members:
:undoc-members:
:show-inheritance:
opcua.client.ua_client module
-----------------------------
.. automodule:: opcua.client.ua_client
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: opcua.client
:members:
:undoc-members:
:show-inheritance:
opcua.common package
====================
Submodules
----------
opcua.common.event module
-------------------------
.. automodule:: opcua.common.event
:members:
:undoc-members:
:show-inheritance:
opcua.common.instanciate module
-------------------------------
.. automodule:: opcua.common.instanciate
:members:
:undoc-members:
:show-inheritance:
opcua.common.manage_nodes module
--------------------------------
.. automodule:: opcua.common.manage_nodes
:members:
:undoc-members:
:show-inheritance:
opcua.common.methods module
---------------------------
.. automodule:: opcua.common.methods
:members:
:undoc-members:
:show-inheritance:
opcua.common.node module
------------------------
.. automodule:: opcua.common.node
:members:
:undoc-members:
:show-inheritance:
opcua.common.subscription module
--------------------------------
.. automodule:: opcua.common.subscription
:members:
:undoc-members:
:show-inheritance:
opcua.common.uaerrors module
----------------------------
.. automodule:: opcua.common.uaerrors
:members:
:undoc-members:
:show-inheritance:
opcua.common.utils module
-------------------------
.. automodule:: opcua.common.utils
:members:
:undoc-members:
:show-inheritance:
opcua.common.xmlimporter module
-------------------------------
.. automodule:: opcua.common.xmlimporter
:members:
:undoc-members:
:show-inheritance:
opcua.common.xmlparser module
-----------------------------
.. automodule:: opcua.common.xmlparser
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: opcua.common
:members:
:undoc-members:
:show-inheritance:
opcua.crypto package
====================
Submodules
----------
opcua.crypto.security_policies module
-------------------------------------
.. automodule:: opcua.crypto.security_policies
:members:
:undoc-members:
:show-inheritance:
opcua.crypto.uacrypto module
----------------------------
.. automodule:: opcua.crypto.uacrypto
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: opcua.crypto
:members:
:undoc-members:
:show-inheritance:
opcua package
=============
Subpackages
-----------
.. toctree::
opcua.client
opcua.common
opcua.crypto
opcua.server
opcua.ua
Submodules
----------
opcua.tools module
------------------
.. automodule:: opcua.tools
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: opcua
:members:
:undoc-members:
:show-inheritance:
opcua.server package
====================
Subpackages
-----------
.. toctree::
opcua.server.standard_address_space
Submodules
----------
opcua.server.address_space module
---------------------------------
.. automodule:: opcua.server.address_space
:members:
:undoc-members:
:show-inheritance:
opcua.server.binary_server module
---------------------------------
.. automodule:: opcua.server.binary_server
:members:
:undoc-members:
:show-inheritance:
opcua.server.binary_server_asyncio module
-----------------------------------------
.. automodule:: opcua.server.binary_server_asyncio
:members:
:undoc-members:
:show-inheritance:
opcua.server.internal_server module
-----------------------------------
.. automodule:: opcua.server.internal_server
:members:
:undoc-members:
:show-inheritance:
opcua.server.internal_subscription module
-----------------------------------------
.. automodule:: opcua.server.internal_subscription
:members:
:undoc-members:
:show-inheritance:
opcua.server.server module
--------------------------
.. automodule:: opcua.server.server
:members:
:undoc-members:
:show-inheritance:
opcua.server.subscription_service module
----------------------------------------
.. automodule:: opcua.server.subscription_service
:members:
:undoc-members:
:show-inheritance:
opcua.server.uaprocessor module
-------------------------------
.. automodule:: opcua.server.uaprocessor
:members:
:undoc-members:
:show-inheritance:
opcua.server.users module
-------------------------
.. automodule:: opcua.server.users
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: opcua.server
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space package
===========================================
Submodules
----------
opcua.server.standard_address_space.standard_address_space module
-----------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space.standard_address_space_part10 module
------------------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space_part10
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space.standard_address_space_part11 module
------------------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space_part11
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space.standard_address_space_part13 module
------------------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space_part13
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space.standard_address_space_part3 module
-----------------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space_part3
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space.standard_address_space_part4 module
-----------------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space_part4
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space.standard_address_space_part5 module
-----------------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space_part5
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space.standard_address_space_part8 module
-----------------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space_part8
:members:
:undoc-members:
:show-inheritance:
opcua.server.standard_address_space.standard_address_space_part9 module
-----------------------------------------------------------------------
.. automodule:: opcua.server.standard_address_space.standard_address_space_part9
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: opcua.server.standard_address_space
:members:
:undoc-members:
:show-inheritance:
opcua.ua package
================
Submodules
----------
opcua.ua.attribute_ids module
-----------------------------
.. automodule:: opcua.ua.attribute_ids
:members:
:undoc-members:
:show-inheritance:
opcua.ua.extension_objects module
---------------------------------
.. automodule:: opcua.ua.extension_objects
:members:
:undoc-members:
:show-inheritance:
opcua.ua.object_ids module
--------------------------
.. automodule:: opcua.ua.object_ids
:members:
:undoc-members:
:show-inheritance:
opcua.ua.status_codes module
----------------------------
.. automodule:: opcua.ua.status_codes
:members:
:undoc-members:
:show-inheritance:
opcua.ua.uaprotocol_auto module
-------------------------------
.. automodule:: opcua.ua.uaprotocol_auto
:members:
:undoc-members:
:show-inheritance:
opcua.ua.uaprotocol_hand module
-------------------------------
.. automodule:: opcua.ua.uaprotocol_hand
:members:
:undoc-members:
:show-inheritance:
opcua.ua.uatypes module
-----------------------
.. automodule:: opcua.ua.uatypes
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: opcua.ua
:members:
:undoc-members:
:show-inheritance:
UA Structures and Enums
=========================================
All the structures and enums are to be found under opcua.ua
.. automodule:: opcua.ua.uatypes
:members:
.. automodule:: opcua.ua.uaprotocol_hand
:members:
.. automodule:: opcua.ua.uaprotocol_auto
:members:
......@@ -15,6 +15,7 @@ if sys.version_info.major > 2:
from opcua.ua import status_codes
from opcua.common.uaerrors import UaError
from opcua.common.uaerrors import UaStatusCodeError
from opcua.common.uaerrors import UaStringParsingError
logger = logging.getLogger('opcua.uaprotocol')
......@@ -371,6 +372,8 @@ class NodeId(FrozenClass):
self.NamespaceUri = ""
self.ServerIndex = 0
self._freeze = True
if not isinstance(self.NamespaceIndex, int):
raise UaError("NamespaceIndex must be an int")
if self.Identifier is None:
self.Identifier = 0
self.NodeIdType = NodeIdType.TwoByte
......@@ -399,6 +402,13 @@ class NodeId(FrozenClass):
@staticmethod
def from_string(string):
try:
return NodeId._from_string(string)
except ValueError as ex:
raise UaStringParsingError("Error parsing string {}".format(string), ex)
@staticmethod
def _from_string(string):
l = string.split(";")
identifier = None
namespace = 0
......@@ -408,7 +418,7 @@ class NodeId(FrozenClass):
for el in l:
if not el:
continue
k, v = el.split("=")
k, v = el.split("=", 1)
k = k.strip()
v = v.strip()
if k == "ns":
......@@ -430,7 +440,7 @@ class NodeId(FrozenClass):
elif k == "nsu":
nsu = v
if identifier is None:
raise UaError("Could not parse nodeid string: " + string)
raise UaStringParsingError("Could not find identifier in string: " + string)
nodeid = NodeId(identifier, namespace, ntype)
nodeid.NamespaceUri = nsu
nodeid.ServerIndex = srv
......@@ -572,11 +582,15 @@ class QualifiedName(FrozenClass):
@staticmethod
def from_string(string):
if ":" in string:
try:
idx, name = string.split(":", 1)
idx = int(idx)
except (TypeError, ValueError) as ex:
raise UaStringParsingError("Error parsing string {}".format(string), ex)
else:
idx = 0
name = string
return QualifiedName(name, int(idx))
return QualifiedName(name, idx)
def to_binary(self):
packet = []
......
python3 test_server.py opc.tcp://opcua.demo-this.com:51210/UA/SampleServer
This diff is collapsed.
import unittest
from opcua import Client
from opcua import Server
from opcua import ua
from tests_common import CommonTests, add_server_methods
port_num1 = 48510
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 admin client
self.clt = Client('opc.tcp://admin@localhost:%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()
@classmethod
def tearDownClass(self):
#stop our clients
self.ro_clt.disconnect()
self.clt.disconnect()
# stop the server
self.srv.stop()
def test_service_fault(self):
request = ua.ReadRequest()
request.TypeId = ua.FourByteNodeId(999) # bad type!
with self.assertRaises(ua.UaStatusCodeError):
self.clt.uaclient._uasocket.send_request(request)
def test_objects_anonymous(self):
objects = self.ro_clt.get_objects_node()
with self.assertRaises(ua.UaStatusCodeError):
objects.set_attribute(ua.AttributeIds.WriteMask, ua.DataValue(999))
with self.assertRaises(ua.UaStatusCodeError):
f = objects.add_folder(3, 'MyFolder')
def test_folder_anonymous(self):
objects = self.clt.get_objects_node()
f = objects.add_folder(3, 'MyFolderRO')
f_ro = self.ro_clt.get_node(f.nodeid)
self.assertEqual(f, f_ro)
with self.assertRaises(ua.UaStatusCodeError):
f2 = f_ro.add_folder(3, 'MyFolder2')
def test_variable_anonymous(self):
objects = self.clt.get_objects_node()
v = objects.add_variable(3, 'MyROVariable', 6)
v.set_value(4) #this should work
v_ro = self.ro_clt.get_node(v.nodeid)
with self.assertRaises(ua.UaStatusCodeError):
v_ro.set_value(2)
self.assertEqual(v_ro.get_value(), 4)
v.set_writable(True)
v_ro.set_value(2) #now it should work
self.assertEqual(v_ro.get_value(), 2)
v.set_writable(False)
with self.assertRaises(ua.UaStatusCodeError):
v_ro.set_value(9)
self.assertEqual(v_ro.get_value(), 2)
import unittest
import subprocess
from opcua import Server
port_num = 48530
class TestCmdLines(unittest.TestCase):
'''
Test command lines
'''
@classmethod
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()
def test_uals(self):
s = subprocess.check_output(["python", "tools/uals", "--url", self.srv_url])
self.assertIn(b"i=85", s)
self.assertNotIn(b"i=89", s)
self.assertNotIn(b"1.999", s)
s = subprocess.check_output(["python", "tools/uals", "--url", self.srv_url, "-d", "3"])
self.assertIn(b"1.999", s)
def test_uaread(self):
s = subprocess.check_output(["python", "tools/uaread", "--url", self.srv_url, "--path", "0:Objects,4:directory,4:variable"])
self.assertIn(b"1.999", s)
def test_uawrite(self):
s = subprocess.check_output(["python", "tools/uawrite", "--url", self.srv_url, "--path", "0:Objects,4:directory,4:variable2", "1.789"])
s = subprocess.check_output(["python", "tools/uaread", "--url", self.srv_url, "--path", "0:Objects,4:directory,4:variable2"])
self.assertIn(b"1.789", s)
self.assertNotIn(b"1.999", s)
def test_uadiscover(self):
s = subprocess.check_output(["python", "tools/uadiscover", "--url", self.srv_url])
self.assertIn(b"opc.tcp://localhost", s)
self.assertIn(b"FreeOpcUa", s)
self.assertIn(b"urn:freeopcua:python:server", s)
@classmethod
def tearDownClass(self):
self.srv.stop()
This diff is collapsed.
import unittest
from tests_common import CommonTests, add_server_methods
import time
from opcua import Server
from opcua import Client
from opcua import ua
port_num = 485140
port_discovery = 48550
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_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()
@classmethod
def tearDownClass(self):
self.srv.stop()
self.discovery.stop()
def test_discovery(self):
client = Client(self.discovery.endpoint.geturl())
client.connect()
try:
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(), 0)
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])
finally:
client.disconnect()
def test_find_servers2(self):
client = Client(self.discovery.endpoint.geturl())
client.connect()
try:
servers = client.find_servers()
new_app_uri1 = "urn:freeopcua:python:server:test_discovery1"
self.srv.application_uri = new_app_uri1
self.srv.register_to_discovery(self.discovery.endpoint.geturl(), period=0)
new_app_uri2 = "urn:freeopcua:python:test_discovery2"
self.srv.application_uri = new_app_uri2
self.srv.register_to_discovery(self.discovery.endpoint.geturl(), period=0)
time.sleep(0.1) # let server register registration
new_servers = client.find_servers()
self.assertEqual(len(new_servers) - len(servers) , 2)
self.assertFalse(new_app_uri1 in [s.ApplicationUri for s in servers])
self.assertFalse(new_app_uri2 in [s.ApplicationUri for s in servers])
self.assertTrue(new_app_uri1 in [s.ApplicationUri for s in new_servers])
self.assertTrue(new_app_uri2 in [s.ApplicationUri for s in new_servers])
# now do a query with filer
new_servers = client.find_servers(["urn:freeopcua:python:server"])
self.assertEqual(len(new_servers) - len(servers) , 0)
self.assertTrue(new_app_uri1 in [s.ApplicationUri for s in new_servers])
self.assertFalse(new_app_uri2 in [s.ApplicationUri for s in new_servers])
# now do a query with filer
new_servers = client.find_servers(["urn:freeopcua:python"])
self.assertEqual(len(new_servers) - len(servers) , 2)
self.assertTrue(new_app_uri1 in [s.ApplicationUri for s in new_servers])
self.assertTrue(new_app_uri2 in [s.ApplicationUri for s in new_servers])
finally:
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")
This diff is collapsed.
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