forked from FreeOpcUa/python-opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-custom-object.py
More file actions
76 lines (56 loc) · 3 KB
/
Copy pathserver-custom-object.py
File metadata and controls
76 lines (56 loc) · 3 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'''
Show 3 different examples for creating an object:
1) create a basic object
2) create a new object type and a instance of the new object type
3) import a new object from xml address space and create a instance of the new object type
'''
import sys
sys.path.insert(0, "..")
import time
from opcua import ua, Server
if __name__ == "__main__":
# 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 custom stuff
objects = server.get_objects_node()
# Example 1 - create a basic object
#-------------------------------------------------------------------------------
myobj = objects.add_object(idx, "MyObject")
#-------------------------------------------------------------------------------
# Example 2 - create a new object type and a instance of the new object type
#-------------------------------------------------------------------------------
types = server.get_node(ua.ObjectIds.BaseObjectType)
object_type_to_derive_from = server.get_root_node().get_child(["0:Types",
"0:ObjectTypes",
"0:BaseObjectType"])
mycustomobj_type = types.add_object_type(idx, "MyCustomObjectType")
var = mycustomobj_type.add_variable(0, "var_should_be_there_after_instantiate", 1.0) # demonstrates instantiate
var.set_modelling_rule(True) #if false it would not be instantiated
myobj = objects.add_object(idx, "MyCustomObjectA", mycustomobj_type.nodeid)
#-------------------------------------------------------------------------------
# Example 3 - import a new object from xml address space and create a instance of the new object type
#-------------------------------------------------------------------------------
# Import customobject type
server.import_xml('customobject.xml')
# get nodeid of custom object type by one of the following 3 ways:
# 1) Use node ID
# 2) Or Full path
# 3) Or As child from parent
myobject1_type_nodeid = ua.NodeId.from_string('ns=%d;i=2' % idx)
myobject2_type_nodeid = server.get_root_node().get_child(["0:Types", "0:ObjectTypes", "0:BaseObjectType", "%d:MyCustomObjectType" % idx]).nodeid
myobject3_type_nodeid = server.get_node(ua.ObjectIds.BaseObjectType).get_child(["%d:MyCustomObjectType" % idx]).nodeid
# populating our address space
myobj = objects.add_object(idx, "MyCustomObjectB", myobject3_type_nodeid)
#-------------------------------------------------------------------------------
# starting!
server.start()
try:
while True:
time.sleep(1)
finally:
# close connection, remove subcsriptions, etc
server.stop()