server-custom-object.py
Go to the documentation of this file.
1 '''
2  Show 3 different examples for creating an object:
3  1) create a basic object
4  2) create a new object type and a instance of the new object type
5  3) import a new object from xml address space and create a instance of the new object type
6 '''
7 import sys
8 sys.path.insert(0, "..")
9 import time
10 
11 
12 from opcua import ua, Server
13 
14 
15 if __name__ == "__main__":
16 
17  # setup our server
18  server = Server()
19  server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
20 
21  # setup our own namespace, not really necessary but should as spec
22  uri = "http://examples.freeopcua.github.io"
23  idx = server.register_namespace(uri)
24 
25  # get Objects node, this is where we should put our custom stuff
26  objects = server.get_objects_node()
27 
28  # Example 1 - create a basic object
29  #-------------------------------------------------------------------------------
30  myobj = objects.add_object(idx, "MyObject")
31  #-------------------------------------------------------------------------------
32 
33 
34  # Example 2 - create a new object type and a instance of the new object type
35  #-------------------------------------------------------------------------------
36  types = server.get_node(ua.ObjectIds.BaseObjectType)
37 
38  object_type_to_derive_from = server.get_root_node().get_child(["0:Types",
39  "0:ObjectTypes",
40  "0:BaseObjectType"])
41  mycustomobj_type = types.add_object_type(idx, "MyCustomObject")
42  mycustomobj_type.add_variable(0, "var_should_be_there_after_instantiate", 1.0) # demonstrates instantiate
43 
44  myobj = objects.add_object(idx, "MyCustomObjectA", mycustomobj_type.nodeid)
45  #-------------------------------------------------------------------------------
46 
47 
48  # Example 3 - import a new object from xml address space and create a instance of the new object type
49  #-------------------------------------------------------------------------------
50  # Import customobject type
51  server.import_xml('customobject.xml')
52 
53 
54  # get nodeid of custom object type by one of the following 3 ways:
55  # 1) Use node ID
56  # 2) Or Full path
57  # 3) Or As child from parent
58  myobject1_type_nodeid = ua.NodeId.from_string('ns=1;i=1002')
59  myobject2_type_nodeid = server.get_root_node().get_child(["0:Types", "0:ObjectTypes", "0:BaseObjectType", "1:MyObjectType"]).nodeid
60  myobject3_type_nodeid = server.get_node(ua.ObjectIds.BaseObjectType).get_child(["1:MyObjectType"]).nodeid
61 
62 
63  # populating our address space
64  myobj = objects.add_object(idx, "MyCustomObjectB", myobject3_type_nodeid)
65  #-------------------------------------------------------------------------------
66 
67 
68  # starting!
69  server.start()
70 
71  try:
72  while True:
73  time.sleep(1)
74  finally:
75  # close connection, remove subcsriptions, etc
76  server.stop()


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Tue Jan 19 2021 03:12:44