Skip to content

Commit b784f9b

Browse files
committed
added usage example
1 parent e3167fd commit b784f9b

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

examples/server-persistent.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import sys
2+
sys.path.insert(0, "..")
3+
import time
4+
import random
5+
6+
from opcua import Server
7+
from opcua.common.sqlite3_backend import SQLite3Backend
8+
from opcua.server.address_space_sqlite import StandardAddressSpaceSQLite, AddressSpaceSQLite
9+
10+
ITEMS = ('Pump', 'Motor', 'Fan', 'Gearbox', 'Filter', 'Building', 'Ventilation')
11+
12+
if __name__ == "__main__":
13+
14+
print('\nStart and stop this server multiple times and\n'
15+
'verify that address space is persisted in SQL.\n'
16+
'Values written from any opc-ua client into the\n'
17+
'server are also stored.\n')
18+
19+
with SQLite3Backend(sqlFile='my_address_space.py', readonly=False) as backend, \
20+
StandardAddressSpaceSQLite() as stdAspace, \
21+
AddressSpaceSQLite(backend=backend, cache=stdAspace) as myAspace:
22+
23+
# setup our server
24+
server = Server(aspace=myAspace)
25+
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
26+
27+
# setup our own namespace, not really necessary but should as spec
28+
uri = "http://examples.freeopcua.github.io"
29+
idx = server.register_namespace(uri)
30+
31+
# get Objects node, this is where we should put our nodes
32+
objects = server.get_objects_node()
33+
34+
# populating our address space
35+
myobj = objects.add_object(idx, "{:s}-{:d}".format(random.choice(ITEMS), random.randint(1,100)))
36+
myvar = myobj.add_variable(idx, "MyVariable", 42)
37+
myvar.set_writable() # Set MyVariable to be writable by clients
38+
39+
# starting!
40+
server.start()
41+
42+
try:
43+
while True:
44+
time.sleep(1)
45+
except KeyboardInterrupt:
46+
pass
47+
finally:
48+
#close connection, remove subcsriptions, etc
49+
server.stop()

0 commit comments

Comments
 (0)