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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
"""
Generate address space c++ code from xml file specification
xmlparser.py is a requirement. it is in opcua folder but to avoid importing all code, developer can link xmlparser.py in current directory
"""
import sys
import logging
# sys.path.insert(0, "..") # load local freeopcua implementation
#from opcua import xmlparser
import opcua.common.xmlparser as xmlparser
def _to_val(objs, attr, val):
from opcua import ua
cls = getattr(ua, objs[0])
for o in objs[1:]:
cls = getattr(ua, _get_uatype_name(cls, o))
if cls == ua.NodeId:
return "NodeId.from_string('val')"
return ua_type_to_python(val, _get_uatype_name(cls, attr))
def _get_uatype_name(cls, attname):
for name, uat in cls.ua_types:
if name == attname:
return uat
raise Exception("Could not find attribute {} in obj {}".format(attname, cls))
def ua_type_to_python(val, uatype):
if uatype in ("String"):
return "'{0}'".format(val)
elif uatype in ("Bytes", "Bytes", "ByteString", "ByteArray"):
return "b'{0}'".format(val)
else:
return val
def bname_code(string):
if ":" in string:
idx, name = string.split(":", 1)
else:
idx = 0
name = string
return f"QualifiedName('{name}', {idx})"
def nodeid_code(string):
l = string.split(";")
identifier = None
namespace = 0
ntype = None
srv = None
nsu = None
for el in l:
if not el:
continue
k, v = el.split("=", 1)
k = k.strip()
v = v.strip()
if k == "ns":
namespace = v
elif k == "i":
ntype = "NumericNodeId"
identifier = v
elif k == "s":
ntype = "StringNodeId"
identifier = f"'{v}'"
elif k == "g":
ntype = "GuidNodeId"
identifier = f"b'{v}'"
elif k == "b":
ntype = "ByteStringNodeId"
identifier = f"b'{v}'"
elif k == "srv":
srv = v
elif k == "nsu":
nsu = v
if identifier is None:
raise Exception("Could not find identifier in string: " + string)
return f"{ntype}({identifier}, {namespace})"
class CodeGenerator(object):
def __init__(self, input_path, output_path):
self.input_path = input_path
self.output_path = output_path
self.output_file = None
self.part = self.input_path.split(".")[-2]
self.parser = None
def run(self):
sys.stderr.write("Generating Python code {0} for XML file {1}".format(self.output_path, self.input_path) + "\n")
self.output_file = open(self.output_path, "w")
self.make_header()
self.parser = xmlparser.XMLParser(self.input_path)
for node in self.parser.get_node_datas():
if node.nodetype == 'UAObject':
self.make_object_code(node)
elif node.nodetype == 'UAObjectType':
self.make_object_type_code(node)
elif node.nodetype == 'UAVariable':
self.make_variable_code(node)
elif node.nodetype == 'UAVariableType':
self.make_variable_type_code(node)
elif node.nodetype == 'UAReferenceType':
self.make_reference_code(node)
elif node.nodetype == 'UADataType':
self.make_datatype_code(node)
elif node.nodetype == 'UAMethod':
self.make_method_code(node)
else:
sys.stderr.write("Not implemented node type: " + node.nodetype + "\n")
self.output_file.close()
def writecode(self, *args):
self.output_file.write(" ".join(args) + "\n")
def make_header(self, ):
self.writecode('''
# -*- coding: utf-8 -*-
"""
DO NOT EDIT THIS FILE!
It is automatically generated from opcfoundation.org schemas.
"""
import datetime
from dateutil.tz import tzutc
from opcua import ua
from opcua.ua import NodeId, QualifiedName, NumericNodeId, StringNodeId, GuidNodeId
from opcua.ua import NodeClass, LocalizedText
def create_standard_address_space_{0!s}(server):
'''.format((self.part)))
def make_node_code(self, obj, indent):
self.writecode(indent, 'node = ua.AddNodesItem()')
self.writecode(indent, 'node.RequestedNewNodeId = {}'.format(nodeid_code(obj.nodeid)))
self.writecode(indent, 'node.BrowseName = {}'.format(bname_code(obj.browsename)))
self.writecode(indent, 'node.NodeClass = NodeClass.{0}'.format(obj.nodetype[2:]))
if obj.parent and obj.parentlink:
self.writecode(indent, 'node.ParentNodeId = {}'.format(nodeid_code(obj.parent)))
self.writecode(indent, 'node.ReferenceTypeId = {0}'.format(self.to_ref_type(obj.parentlink)))
if obj.typedef:
self.writecode(indent, 'node.TypeDefinition = {}'.format(nodeid_code(obj.typedef)))
def to_data_type(self, nodeid):
if not nodeid:
return "ua.NodeId(ua.ObjectIds.String)"
if "=" in nodeid:
return nodeid_code(nodeid)
else:
return 'ua.NodeId(ua.ObjectIds.{0})'.format(nodeid)
def to_ref_type(self, nodeid):
if not "=" in nodeid:
nodeid = self.parser.get_aliases()[nodeid]
return nodeid_code(nodeid)
def make_object_code(self, obj):
indent = " "
self.writecode(indent)
self.make_node_code(obj, indent)
self.writecode(indent, 'attrs = ua.ObjectAttributes()')
if obj.desc:
self.writecode(indent, 'attrs.Description = LocalizedText("{0}")'.format(obj.desc))
self.writecode(indent, 'attrs.DisplayName = LocalizedText("{0}")'.format(obj.displayname))
self.writecode(indent, 'attrs.EventNotifier = {0}'.format(obj.eventnotifier))
self.writecode(indent, 'node.NodeAttributes = attrs')
self.writecode(indent, 'server.add_nodes([node])')
self.make_refs_code(obj, indent)
def make_object_type_code(self, obj):
indent = " "
self.writecode(indent)
self.make_node_code(obj, indent)
self.writecode(indent, 'attrs = ua.ObjectTypeAttributes()')
if obj.desc:
self.writecode(indent, 'attrs.Description = LocalizedText("{0}")'.format(obj.desc))
self.writecode(indent, 'attrs.DisplayName = LocalizedText("{0}")'.format(obj.displayname))
self.writecode(indent, 'attrs.IsAbstract = {0}'.format(obj.abstract))
self.writecode(indent, 'node.NodeAttributes = attrs')
self.writecode(indent, 'server.add_nodes([node])')
self.make_refs_code(obj, indent)
def make_common_variable_code(self, indent, obj):
if obj.desc:
self.writecode(indent, 'attrs.Description = LocalizedText("{0}")'.format(obj.desc))
self.writecode(indent, 'attrs.DisplayName = LocalizedText("{0}")'.format(obj.displayname))
self.writecode(indent, 'attrs.DataType = {0}'.format(self.to_data_type(obj.datatype)))
if obj.value is not None:
if obj.valuetype == "ListOfExtensionObject":
self.writecode(indent, 'value = []')
for ext in obj.value:
self.make_ext_obj_code(indent, ext)
self.writecode(indent, 'value.append(extobj)')
self.writecode(indent, 'attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)')
elif obj.valuetype == "ExtensionObject":
self.make_ext_obj_code(indent, obj.value)
self.writecode(indent, 'value = extobj')
self.writecode(indent, 'attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)')
elif obj.valuetype == "ListOfLocalizedText":
value = ['LocalizedText({0})'.format(repr(text)) for text in obj.value]
self.writecode(indent, 'attrs.Value = [{}]'.format(','.join(value)))
elif obj.valuetype == "LocalizedText":
self.writecode(indent, 'attrs.Value = ua.Variant(LocalizedText("{0}"), ua.VariantType.LocalizedText)'.format(obj.value[1][1]))
else:
if obj.valuetype.startswith("ListOf"):
obj.valuetype = obj.valuetype[6:]
self.writecode(indent, 'attrs.Value = ua.Variant({0}, ua.VariantType.{1})'.format(repr(obj.value), obj.valuetype))
if obj.rank:
self.writecode(indent, 'attrs.ValueRank = {0}'.format(obj.rank))
if obj.accesslevel:
self.writecode(indent, 'attrs.AccessLevel = {0}'.format(obj.accesslevel))
if obj.useraccesslevel:
self.writecode(indent, 'attrs.UserAccessLevel = {0}'.format(obj.useraccesslevel))
if obj.dimensions:
self.writecode(indent, 'attrs.ArrayDimensions = {0}'.format(obj.dimensions))
def make_ext_obj_code(self, indent, extobj):
self.writecode(indent, 'extobj = ua.{0}()'.format(extobj.objname))
for name, val in extobj.body:
for k, v in val:
if type(v) is str:
val = _to_val([extobj.objname], k, v)
self.writecode(indent, 'extobj.{0} = {1}'.format(k, val))
else:
if k == "DataType": #hack for strange nodeid xml format
self.writecode(indent, 'extobj.{0} = {1}'.format(k, nodeid_code(v[0][1])))
continue
if k == "ArrayDimensions": # hack for v1.04 - Ignore UInt32 tag?
self.writecode(indent, 'extobj.ArrayDimensions = {0}'.format(v[0][1]))
continue
for k2, v2 in v:
val2 = _to_val([extobj.objname, k], k2, v2)
self.writecode(indent, 'extobj.{0}.{1} = {2}'.format(k, k2, val2))
def make_variable_code(self, obj):
indent = " "
self.writecode(indent)
self.make_node_code(obj, indent)
self.writecode(indent, 'attrs = ua.VariableAttributes()')
if obj.minsample:
self.writecode(indent, 'attrs.MinimumSamplingInterval = {0}'.format(obj.minsample))
self.make_common_variable_code(indent, obj)
self.writecode(indent, 'node.NodeAttributes = attrs')
self.writecode(indent, 'server.add_nodes([node])')
self.make_refs_code(obj, indent)
def make_variable_type_code(self, obj):
indent = " "
self.writecode(indent)
self.make_node_code(obj, indent)
self.writecode(indent, 'attrs = ua.VariableTypeAttributes()')
if obj.desc:
self.writecode(indent, 'attrs.Description = LocalizedText("{0}")'.format(obj.desc))
self.writecode(indent, 'attrs.DisplayName = LocalizedText("{0}")'.format(obj.displayname))
if obj.abstract:
self.writecode(indent, 'attrs.IsAbstract = {0}'.format(obj.abstract))
self.make_common_variable_code(indent, obj)
self.writecode(indent, 'node.NodeAttributes = attrs')
self.writecode(indent, 'server.add_nodes([node])')
self.make_refs_code(obj, indent)
def make_method_code(self, obj):
indent = " "
self.writecode(indent)
self.make_node_code(obj, indent)
self.writecode(indent, 'attrs = ua.MethodAttributes()')
if obj.desc:
self.writecode(indent, 'attrs.Description = LocalizedText("{0}")'.format(obj.desc))
self.writecode(indent, 'attrs.DisplayName = LocalizedText("{0}")'.format(obj.displayname))
self.writecode(indent, 'node.NodeAttributes = attrs')
self.writecode(indent, 'server.add_nodes([node])')
self.make_refs_code(obj, indent)
def make_reference_code(self, obj):
indent = " "
self.writecode(indent)
self.make_node_code(obj, indent)
self.writecode(indent, 'attrs = ua.ReferenceTypeAttributes()')
if obj.desc:
self.writecode(indent, 'attrs.Description = LocalizedText("{0}")'.format(obj.desc))
self.writecode(indent, 'attrs.DisplayName = LocalizedText("{0}")'.format(obj.displayname))
if obj. inversename:
self.writecode(indent, 'attrs.InverseName = LocalizedText("{0}")'.format(obj.inversename))
if obj.abstract:
self.writecode(indent, 'attrs.IsAbstract = {0}'.format(obj.abstract))
if obj.symmetric:
self.writecode(indent, 'attrs.Symmetric = {0}'.format(obj.symmetric))
self.writecode(indent, 'node.NodeAttributes = attrs')
self.writecode(indent, 'server.add_nodes([node])')
self.make_refs_code(obj, indent)
def make_datatype_code(self, obj):
indent = " "
self.writecode(indent)
self.make_node_code(obj, indent)
self.writecode(indent, 'attrs = ua.DataTypeAttributes()')
if obj.desc:
self.writecode(indent, u'attrs.Description = LocalizedText("{0}")'.format(obj.desc))
self.writecode(indent, 'attrs.DisplayName = LocalizedText("{0}")'.format(obj.displayname))
if obj.abstract:
self.writecode(indent, 'attrs.IsAbstract = {0}'.format(obj.abstract))
self.writecode(indent, 'node.NodeAttributes = attrs')
self.writecode(indent, 'server.add_nodes([node])')
self.make_refs_code(obj, indent)
def make_refs_code(self, obj, indent):
if not obj.refs:
return
self.writecode(indent, "refs = []")
for ref in obj.refs:
self.writecode(indent, 'ref = ua.AddReferencesItem()')
self.writecode(indent, 'ref.IsForward = {0}'.format(ref.forward))
self.writecode(indent, 'ref.ReferenceTypeId = {0}'.format(self.to_ref_type(ref.reftype)))
self.writecode(indent, 'ref.SourceNodeId = {0}'.format(nodeid_code(obj.nodeid)))
self.writecode(indent, 'ref.TargetNodeClass = NodeClass.DataType')
self.writecode(indent, 'ref.TargetNodeId = {0}'.format(nodeid_code(ref.target)))
self.writecode(indent, "refs.append(ref)")
self.writecode(indent, 'server.add_references(refs)')
def save_aspace_to_disk():
import os.path
path = os.path.join("..", "opcua", "binary_address_space.pickle")
print("Savind standard address space to:", path)
sys.path.append("..")
from opcua.server.standard_address_space import standard_address_space
from opcua.server.address_space import NodeManagementService, AddressSpace
aspace = AddressSpace()
standard_address_space.fill_address_space(NodeManagementService(aspace))
aspace.dump(path)
if __name__ == "__main__":
logging.basicConfig(level=logging.WARN)
for i in (3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 17, 19):
xmlpath = "Opc.Ua.NodeSet2.Part{0}.xml".format(str(i))
cpppath = "../opcua/server/standard_address_space/standard_address_space_part{0}.py".format(str(i))
c = CodeGenerator(xmlpath, cpppath)
c.run()
save_aspace_to_disk()
|