CVBpy 14.0
opcua/InteractionServerClient
1# load cvb.opcua module
2import cvb
3import cvb.opcua
4import time
5import threading
6
7test_node_name = "TestNode"
8port = 4840
9stop = False
10ready = False
11
12
13def read_callback(data):
14 print("callback: reading")
15
16
17def write_callback(data):
18 print("callback: writing")
19
20# ------------------------------------------------------------------------------
21# Server
22# ------------------------------------------------------------------------------
23
24
25def start_server():
26 global stop
27 global ready
28 global port
29 global test_node_name
30
31 server = cvb.opcua.Server.create(port)
32
33 # Server add namespace
34 ns_index = server.add_namespace("StemmerNamespace")
35
36 # Server add folder
38 cvb.opcua.Namespace0NodeID.FOLDERTYPE)
40 cvb.opcua.Namespace0NodeID.OBJECTSFOLDER)
42 ns_index, "StemmerFolder", loc, folder_type)
43 server.add_node(folder)
44
45 # Server Add Node
47 ns_index, test_node_name, folder.node_id(), 1.111)
48 server.add_node(var_node)
49
50 # register callbacks
51 readCookie = var_node.register_read_callback(read_callback)
52 writeCookie = var_node.register_write_callback(write_callback)
53
54 server.start()
55 ready = True
56
57 while not stop:
58 time.sleep(1)
59
60 var_node.deregister_callback(readCookie)
61 var_node.deregister_callback(writeCookie)
62 server.stop()
63
64# ------------------------------------------------------------------------------
65# Client Helper
66# ------------------------------------------------------------------------------
67
68
69def make_browse_filter():
70 bd = cvb.opcua.ReferenceDirection.Forward # how to iterate through the server
71 relationship = cvb.opcua.ReferenceType.Hierarchical # relationship to explore
72 max_ref = 5000 # max number of references to follow
73 # allow subtype of search type
74 allow_subtype = cvb.opcua.BrowseSubType.SubTypesIncluded
75 # masked based filter for node classes based on bitwise AND of cvb.opcua.NodeClass
76 node_class_mask = 0
77
78 # combine them in an object
79 browse_filter = cvb.opcua.BrowseFilter.create(
80 bd, relationship, max_ref, allow_subtype, node_class_mask)
81
82 return browse_filter
83
84# ------------------------------------------------------------------------------
85# Client
86# ------------------------------------------------------------------------------
87
88
89def start_client():
90 global stop
91 global ready
92 global port
93 global test_node_name
94
95 while not ready:
96 time.sleep(1)
97
98 # create client
99 client = cvb.opcua.Client.create("opc.tcp://127.0.0.1:"+str(port))
100
101 # get node id of object folder
102 root_folder = client.root_node()
103
104 # browse
105 result = root_folder.browse(make_browse_filter())
106
107 for nid in result:
108
109 node_extracted = client.node(nid)
110 # print display name
111 print("found: " + node_extracted.display_name)
112
113 # our custom node
114 if(node_extracted.display_name == test_node_name):
115 # change values (trigger callbacks)
116 print("value access: "+str(node_extracted.value()))
117
118 node_extracted.set_value(2.2)
119 print("value access: "+str(node_extracted.value()))
120 stop = True
121
122
123# start server in independent thread
124server_thread = threading.Thread(target=start_server)
125server_thread.start()
126
127# start client in current thread
128start_client()
129
130# collect server thread
131server_thread.join()
132
133print("\nfinished\n")
cvb.opcua.BrowseFilter create(int direction, int reference_type, int max_references, int include_sub_types, int node_class_mask)
Creates a BrowseFilter object for browsing the OPCUA client.
Definition: __init__.py:114
cvb.opcua.Client create(str discover_url)
Creates an OPCUA client object.
Definition: __init__.py:160
cvb.opcua.FloatNode create(int namespace_index, str name, cvb.opcua.NodeID parent_node_id, float value)
Creates an OPCUA variable with the specified parameter, data type, and its floating-point value.
Definition: __init__.py:222
cvb.opcua.NodeID create_namespace0(int namespace0_node_id)
Creates an OPCUA node ID based on a pre-defined namespace 0 value.
Definition: __init__.py:439
None create_from_type(int namespace_index, str name, cvb.opcua.NodeID parent_node_id, cvb.opcua.NodeID type_def_node_id)
Creates an OPCUA object with a given type definition.
Definition: __init__.py:480
cvb.opcua.Server create(int port)
Creates an OPCUA server with the specified port number.
Definition: __init__.py:527
Common Vision Blox OPCUA module for Python.
Definition: __init__.py:1