2 Generate address space c++ code from xml file specification 3 xmlparser.py is a requirement. it is in opcua folder but to avoid importing all code, developer can link xmlparser.py in current directory 14 print(
"got ", objs, attr, val)
15 cls = getattr(ua, objs[0])
17 cls = getattr(ua, cls.ua_types[o])
19 return "ua.NodeId.from_string('val')" 24 if uatype
in (
"String"):
25 return "'{0}'".format(val)
26 elif uatype
in (
"Bytes",
"Bytes",
"ByteString",
"ByteArray"):
27 return "b'{0}'".format(val)
38 self.
part = self.input_path.split(
".")[-2]
42 sys.stderr.write(
"Generating Python code {0} for XML file {1}".format(self.
output_path, self.
input_path) +
"\n")
47 if node.nodetype ==
'UAObject':
49 elif node.nodetype ==
'UAObjectType':
51 elif node.nodetype ==
'UAVariable':
53 elif node.nodetype ==
'UAVariableType':
55 elif node.nodetype ==
'UAReferenceType':
57 elif node.nodetype ==
'UADataType':
59 elif node.nodetype ==
'UAMethod':
62 sys.stderr.write(
"Not implemented node type: " + node.nodetype +
"\n")
63 self.output_file.close()
66 self.output_file.write(
" ".join(args) +
"\n")
71 DO NOT EDIT THIS FILE! 72 It is automatically generated from opcfoundation.org schemas. 78 def create_standard_address_space_{0!s}(server): 79 '''.format((self.
part)))
82 self.
writecode(indent,
'node = ua.AddNodesItem()')
83 self.
writecode(indent,
'node.RequestedNewNodeId = ua.NodeId.from_string("{0}")'.format(obj.nodeid))
84 self.
writecode(indent,
'node.BrowseName = ua.QualifiedName.from_string("{0}")'.format(obj.browsename))
85 self.
writecode(indent,
'node.NodeClass = ua.NodeClass.{0}'.format(obj.nodetype[2:]))
87 self.
writecode(indent,
'node.ParentNodeId = ua.NodeId.from_string("{0}")'.format(obj.parent))
91 self.
writecode(indent,
'node.TypeDefinition = ua.NodeId.from_string("{0}")'.format(obj.typedef))
95 return "ua.NodeId(ua.ObjectIds.String)" 97 return 'ua.NodeId.from_string("{0}")'.format(nodeid)
99 return 'ua.NodeId(ua.ObjectIds.{0})'.format(nodeid)
102 if not "=" in nodeid:
103 nodeid = self.parser.aliases[nodeid]
104 return 'ua.NodeId.from_string("{0}")'.format(nodeid)
110 self.
writecode(indent,
'attrs = ua.ObjectAttributes()')
112 self.
writecode(indent,
'attrs.Description = ua.LocalizedText("{0}")'.format(obj.desc))
113 self.
writecode(indent,
'attrs.DisplayName = ua.LocalizedText("{0}")'.format(obj.displayname))
114 self.
writecode(indent,
'attrs.EventNotifier = {0}'.format(obj.eventnotifier))
115 self.
writecode(indent,
'node.NodeAttributes = attrs')
116 self.
writecode(indent,
'server.add_nodes([node])')
123 self.
writecode(indent,
'attrs = ua.ObjectTypeAttributes()')
125 self.
writecode(indent,
'attrs.Description = ua.LocalizedText("{0}")'.format(obj.desc))
126 self.
writecode(indent,
'attrs.DisplayName = ua.LocalizedText("{0}")'.format(obj.displayname))
127 self.
writecode(indent,
'attrs.IsAbstract = {0}'.format(obj.abstract))
128 self.
writecode(indent,
'node.NodeAttributes = attrs')
129 self.
writecode(indent,
'server.add_nodes([node])')
134 self.
writecode(indent,
'attrs.Description = ua.LocalizedText("{0}")'.format(obj.desc))
135 self.
writecode(indent,
'attrs.DisplayName = ua.LocalizedText("{0}")'.format(obj.displayname))
137 if obj.value
is not None:
138 if obj.valuetype ==
"ListOfExtensionObject":
140 for ext
in obj.value:
142 self.
writecode(indent,
'value.append(extobj)')
143 self.
writecode(indent,
'attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)')
144 elif obj.valuetype ==
"ExtensionObject":
147 self.
writecode(indent,
'attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)')
149 if obj.valuetype.startswith(
"ListOf"):
150 obj.valuetype = obj.valuetype[6:]
151 self.
writecode(indent,
'attrs.Value = ua.Variant({0}, ua.VariantType.{1})'.format(self.
to_value(obj.value), obj.valuetype))
153 self.
writecode(indent,
'attrs.ValueRank = {0}'.format(obj.rank))
155 self.
writecode(indent,
'attrs.AccessLevel = {0}'.format(obj.accesslevel))
156 if obj.useraccesslevel:
157 self.
writecode(indent,
'attrs.UserAccessLevel = {0}'.format(obj.useraccesslevel))
159 self.
writecode(indent,
'attrs.ArrayDimensions = {0}'.format(obj.dimensions))
162 print(
"makeing code for ", extobj.objname)
163 self.
writecode(indent,
'extobj = ua.{0}()'.format(extobj.objname))
164 for name, val
in extobj.body.items():
165 for k, v
in val.items():
167 val =
_to_val([extobj.objname], k, v)
168 self.
writecode(indent,
'extobj.{0} = {1}'.format(k, val))
171 self.
writecode(indent,
'extobj.{0} = ua.NodeId.from_string("{1}")'.format(k, v[
"Identifier"]))
173 for k2, v2
in v.items():
174 val2 =
_to_val([extobj.objname, k], k2, v2)
175 self.
writecode(indent,
'extobj.{0}.{1} = {2}'.format(k, k2, val2))
181 self.
writecode(indent,
'attrs = ua.VariableAttributes()')
183 self.
writecode(indent,
'attrs.MinimumSamplingInterval = {0}'.format(obj.minsample))
185 self.
writecode(indent,
'node.NodeAttributes = attrs')
186 self.
writecode(indent,
'server.add_nodes([node])')
193 self.
writecode(indent,
'attrs = ua.VariableTypeAttributes()')
195 self.
writecode(indent,
'attrs.Description = ua.LocalizedText("{0}")'.format(obj.desc))
196 self.
writecode(indent,
'attrs.DisplayName = ua.LocalizedText("{0}")'.format(obj.displayname))
198 self.
writecode(indent,
'attrs.IsAbstract = {0}'.format(obj.abstract))
200 self.
writecode(indent,
'node.NodeAttributes = attrs')
201 self.
writecode(indent,
'server.add_nodes([node])')
206 if isinstance(val, str):
207 return '"' + val +
'"' 215 self.
writecode(indent,
'attrs = ua.MethodAttributes()')
217 self.
writecode(indent,
'attrs.Description = ua.LocalizedText("{0}")'.format(obj.desc))
218 self.
writecode(indent,
'attrs.DisplayName = ua.LocalizedText("{0}")'.format(obj.displayname))
219 self.
writecode(indent,
'node.NodeAttributes = attrs')
220 self.
writecode(indent,
'server.add_nodes([node])')
227 self.
writecode(indent,
'attrs = ua.ReferenceTypeAttributes()')
229 self.
writecode(indent,
'attrs.Description = ua.LocalizedText("{0}")'.format(obj.desc))
230 self.
writecode(indent,
'attrs.DisplayName = ua.LocalizedText("{0}")'.format(obj.displayname))
232 self.
writecode(indent,
'attrs.InverseName = ua.LocalizedText("{0}")'.format(obj.inversename))
234 self.
writecode(indent,
'attrs.IsAbstract = {0}'.format(obj.abstract))
236 self.
writecode(indent,
'attrs.Symmetric = {0}'.format(obj.symmetric))
237 self.
writecode(indent,
'node.NodeAttributes = attrs')
238 self.
writecode(indent,
'server.add_nodes([node])')
245 self.
writecode(indent,
'attrs = ua.DataTypeAttributes()')
247 self.
writecode(indent,
u'attrs.Description = ua.LocalizedText("{0}")'.format(obj.desc.encode(
'ascii',
'replace')))
248 self.
writecode(indent,
'attrs.DisplayName = ua.LocalizedText("{0}")'.format(obj.displayname))
250 self.
writecode(indent,
'attrs.IsAbstract = {0}'.format(obj.abstract))
251 self.
writecode(indent,
'node.NodeAttributes = attrs')
252 self.
writecode(indent,
'server.add_nodes([node])')
260 self.
writecode(indent,
'ref = ua.AddReferencesItem()')
261 self.
writecode(indent,
'ref.IsForward = True')
263 self.
writecode(indent,
'ref.SourceNodeId = ua.NodeId.from_string("{0}")'.format(obj.nodeid))
264 self.
writecode(indent,
'ref.TargetNodeClass = ua.NodeClass.DataType')
265 self.
writecode(indent,
'ref.TargetNodeId = ua.NodeId.from_string("{0}")'.format(ref.target))
266 self.
writecode(indent,
"refs.append(ref)")
267 self.
writecode(indent,
'server.add_references(refs)')
272 path = os.path.join(
"..",
"opcua",
"binary_address_space.pickle")
273 print(
"Savind standard address space to:", path)
274 sys.path.append(
"..")
277 aspace = AddressSpace()
278 standard_address_space.fill_address_space(NodeManagementService(aspace))
281 if __name__ ==
"__main__":
282 logging.basicConfig(level=logging.WARN)
283 for i
in (3, 4, 5, 8, 9, 10, 11, 13):
284 xmlpath =
"Opc.Ua.NodeSet2.Part{0}.xml".format(str(i))
285 cpppath =
"../opcua/server/standard_address_space/standard_address_space_part{0}.py".format(str(i))
def make_common_variable_code(self, indent, obj)
def ua_type_to_python(val, uatype)
def make_object_code(self, obj)
def make_object_type_code(self, obj)
def make_variable_code(self, obj)
def __init__(self, input_path, output_path)
def _to_val(objs, attr, val)
def make_method_code(self, obj)
def make_node_code(self, obj, indent)
def make_ext_obj_code(self, indent, extobj)
def make_refs_code(self, obj, indent)
def to_ref_type(self, nodeid)
def writecode(self, args)
def save_aspace_to_disk()
def make_datatype_code(self, obj)
def make_reference_code(self, obj)
def to_data_type(self, nodeid)
def make_variable_type_code(self, obj)