server-enum.py
Go to the documentation of this file.
1 '''
2  This example demonstrates the use of custom enums by:
3  - Create a custom enum type
4  - Create an object that contains a variable of this type
5 '''
6 import sys
7 sys.path.insert(0, "..")
8 
9 try:
10  from IPython import embed
11 except ImportError:
12  import code
13 
14  def embed():
15  vars = globals()
16  vars.update(locals())
17  shell = code.InteractiveConsole(vars)
18  shell.interact()
19 
20 interactive = True
21 
22 
23 from opcua import ua, Server
24 from opcua.common import node
25 from enum import IntEnum
26 
27 # Not required just for convenience
28 # Because this example is based on EnumStrings, the values should start at 0 and no gaps are allowed.
29 class MyEnum(IntEnum):
30  ok = 0
31  idle = 1
32 
33 # helper method to automatically create string list
34 def enum_to_stringlist(a_enum):
35  items = []
36  for value in a_enum:
37  items.append(ua.LocalizedText(value.name))
38  return items
39 
40 if __name__ == "__main__":
41  # setup our server
42  server = Server()
43  server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
44 
45  # setup our own namespace, not really necessary but should as spec
46  uri = "http://examples.freeopcua.github.io"
47  nsidx = server.register_namespace(uri)
48 
49  # --------------------------------------------------------
50  # create custom enum data type
51  # --------------------------------------------------------
52  enums = server.get_root_node().get_child(["0:Types", "0:DataTypes", "0:BaseDataType", "0:Enumeration"])
53 
54  # 1.
55  # Create Enum Type
56  myenum_type = enums.add_data_type(nsidx, 'MyEnum')
57 
58  # 2.
59  # Add enumerations as EnumStrings (Not yet tested with EnumValues)
60  # Essential to use namespace 0 for EnumStrings !
61 
62  # By hand
63  # es = myenum_type.add_variable(0, "EnumStrings" , [ua.LocalizedText("ok"),
64  # ua.LocalizedText("idle")])
65 
66  # Or convert the existing IntEnum MyEnum
67  es = myenum_type.add_variable(0, "EnumStrings" , enum_to_stringlist(MyEnum))
68 
69  es.set_value_rank(1)
70  es.set_array_dimensions([0])
71 
72  # --------------------------------------------------------
73  # create object with enum variable
74  # --------------------------------------------------------
75  # get Objects node, this is where we should put our custom stuff
76  objects = server.get_objects_node()
77 
78  # create object
79  myobj = objects.add_object(nsidx, 'MyObjectWithEnumVar')
80 
81  # add var with as type the custom enumeration
82  myenum_var = myobj.add_variable(nsidx, 'MyEnum2Var', MyEnum.ok, datatype = myenum_type.nodeid)
83  myenum_var.set_writable()
84  myenum_var.set_value(MyEnum.idle) # change value of enumeration
85 
86  server.start()
87  try:
88  if interactive:
89  embed()
90  else:
91  while True:
92  time.sleep(0.5)
93 
94  except IOError:
95  pass
96  finally:
97  server.stop()
98  print("done")
def embed()
Definition: server-enum.py:14
def enum_to_stringlist(a_enum)
Definition: server-enum.py:34


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