-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-opcua-server.py
52 lines (38 loc) · 1.6 KB
/
test-opcua-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from opcua import ua, Server
from opcua.server.user_manager import UserManager
import code
# User database.
users_db = {
'LablinkTestUser': 'zQC37UiH6ou',
}
# Callback function for user access management.
def user_manager(isession, username, password):
isession.user = UserManager.User
login_ok = username in users_db and password == users_db[username]
print('Login attempt with username "{}": {}'.format(username, 'SUCCESS' if login_ok else 'FAILED'))
return login_ok
# Main routine.
if __name__ == '__main__':
server = Server()
server.set_server_name('Lablink Test Server')
server.set_endpoint('opc.tcp://localhost:12345/lablink-test')
server.set_security_policy([ua.SecurityPolicyType.NoSecurity])
server.set_security_IDs(['Username'])
server.user_manager.set_user_manager(user_manager)
idx = server.register_namespace('urn:lablink:opcua-test')
folder_lablink_test = server.nodes.objects.add_folder(idx, 'LablinkTestSetup')
str_nodeid_stub = 'ns={};s=LablinkTestSetup/{};'
str_nodeid = str_nodeid_stub.format(idx, 'Variable1_Double')
var = folder_lablink_test.add_variable(str_nodeid, 'Variable1_Double', 0, ua.VariantType.Double)
var.set_writable()
str_nodeid = str_nodeid_stub.format(idx, 'Variable2_Int64')
var = folder_lablink_test.add_variable(str_nodeid, 'Variable2_Int64', 0, ua.VariantType.Int64)
var.set_writable()
server.start()
try:
myvars = globals()
myvars.update(locals())
shell = code.InteractiveConsole(myvars)
shell.interact()
finally:
server.stop()