Commit 4fb4eaa0 authored by olivier R-D's avatar olivier R-D

more generic solution to parse and generate code for extension objects value

parent 3976fb13
...@@ -212,6 +212,7 @@ class XMLParser(object): ...@@ -212,6 +212,7 @@ class XMLParser(object):
def _set_attr(self, key, val, obj): def _set_attr(self, key, val, obj):
if key == "NodeId": if key == "NodeId":
obj.nodeid = self._get_node_id(val) obj.nodeid = self._get_node_id(val)
print("PARSING", obj.nodeid)
elif key == "BrowseName": elif key == "BrowseName":
obj.browsename = val obj.browsename = val
elif key == "SymbolicName": elif key == "SymbolicName":
...@@ -302,6 +303,7 @@ class XMLParser(object): ...@@ -302,6 +303,7 @@ class XMLParser(object):
txt = "" txt = ""
for text in el.itertext(): for text in el.itertext():
txt += text txt += text
txt = txt.strip()
return txt return txt
def _parse_list_of_localized_text(self, el): def _parse_list_of_localized_text(self, el):
...@@ -325,26 +327,42 @@ class XMLParser(object): ...@@ -325,26 +327,42 @@ class XMLParser(object):
value = [] value = []
for extension_object_list in el: for extension_object_list in el:
for extension_object in extension_object_list: for extension_object in extension_object_list:
extension_object.find('Body') ext_obj = self._parse_ext_obj(extension_object)
for extension_object_part in extension_object: value.append(ext_obj)
return value
def _parse_ext_obj(self, el):
ext = ExtObj() ext = ExtObj()
for extension_object_part in el:
ntag = self._retag.match(extension_object_part.tag).groups()[1] ntag = self._retag.match(extension_object_part.tag).groups()[1]
if ntag == 'TypeId': if ntag == 'TypeId':
ntag = self._retag.match(extension_object_part.find('*').tag).groups()[1] ntag = self._retag.match(extension_object_part.find('*').tag).groups()[1]
ext.typeid = self._get_text(extension_object_part) ext.typeid = self._get_text(extension_object_part)
elif ntag == 'Body': elif ntag == 'Body':
ntag = self._retag.match(extension_object_part.find('*').tag).groups()[1] ext.objname = self._retag.match(extension_object_part.find('*').tag).groups()[1]
ext.objname = ntag ext.body = self._parse_body(extension_object_part)
for body_item in extension_object_part.findall('*/*'): print("body", ext.body)
ntag = self._retag.match(body_item.tag).groups()[1]
child = body_item.find('*')
if child is not None:
ext.body[ntag] = self._get_text(child)
else: else:
ext.body[ntag] = self._get_text(body_item) print("Uknoen ndtag", ntag)
value.append(ext) print("ext_obj", ext.objname, ext.typeid, ext.body)
return value return ext
def _parse_body(self, el):
body = {}
#ntag = self._retag.match(el.find('*').tag).groups()[1]
#body[ntag] = {}
for body_item in el:
otag = self._retag.match(body_item.tag).groups()[1]
childs = [i for i in body_item]
if not childs:
val = self._get_text(body_item)
elif len(childs) == 1:
val = self._get_text(childs[0])
else:
val = self._parse_body(body_item)
if val:
body[otag] = val
return body
def _parse_refs(self, el, obj): def _parse_refs(self, el, obj):
for ref in el: for ref in el:
......
...@@ -632,12 +632,18 @@ def create_standard_address_space_Part3(server): ...@@ -632,12 +632,18 @@ def create_standard_address_space_Part3(server):
value = [] value = []
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "1" extobj.Value = "1"
extobj.DisplayName.Text = "Mandatory"
extobj.Description.Text = "The BrowseName must appear in all instances of the type."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "2" extobj.Value = "2"
extobj.DisplayName.Text = "Optional"
extobj.Description.Text = "The BrowseName may appear in an instance of the type."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "3" extobj.Value = "3"
extobj.DisplayName.Text = "Constraint"
extobj.Description.Text = "The modelling rule defines a constraint and the BrowseName is not used in an instance of the type."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
...@@ -975,30 +981,48 @@ def create_standard_address_space_Part3(server): ...@@ -975,30 +981,48 @@ def create_standard_address_space_Part3(server):
value = [] value = []
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "0" extobj.Value = "0"
extobj.DisplayName.Text = "Unspecified"
extobj.Description.Text = "No classes are selected."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "1" extobj.Value = "1"
extobj.DisplayName.Text = "Object"
extobj.Description.Text = "The node is an object."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "2" extobj.Value = "2"
extobj.DisplayName.Text = "Variable"
extobj.Description.Text = "The node is a variable."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "4" extobj.Value = "4"
extobj.DisplayName.Text = "Method"
extobj.Description.Text = "The node is a method."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "8" extobj.Value = "8"
extobj.DisplayName.Text = "ObjectType"
extobj.Description.Text = "The node is an object type."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "16" extobj.Value = "16"
extobj.DisplayName.Text = "VariableType"
extobj.Description.Text = "The node is an variable type."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "32" extobj.Value = "32"
extobj.DisplayName.Text = "ReferenceType"
extobj.Description.Text = "The node is a reference type."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "64" extobj.Value = "64"
extobj.DisplayName.Text = "DataType"
extobj.Description.Text = "The node is a data type."
value.append(extobj) value.append(extobj)
extobj = ua.EnumValueType() extobj = ua.EnumValueType()
extobj.Value = "128" extobj.Value = "128"
extobj.DisplayName.Text = "View"
extobj.Description.Text = "The node is a view."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
......
...@@ -905,14 +905,16 @@ def create_standard_address_space_Part9(server): ...@@ -905,14 +905,16 @@ def create_standard_address_space_Part9(server):
attrs.DataType = ua.NodeId.from_string("i=296") attrs.DataType = ua.NodeId.from_string("i=296")
value = [] value = []
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "EventId"
extobj.DataType = "i=15" extobj.DataType = "i=15"
extobj.Name = "EventId"
extobj.ValueRank = "-1"
extobj.Description.Text = "The identifier for the event to comment."
value.append(extobj) value.append(extobj)
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "Comment"
extobj.DataType = "i=21" extobj.DataType = "i=21"
extobj.Name = "Comment"
extobj.ValueRank = "-1"
extobj.Description.Text = "The comment to add to the condition."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
...@@ -974,9 +976,10 @@ def create_standard_address_space_Part9(server): ...@@ -974,9 +976,10 @@ def create_standard_address_space_Part9(server):
attrs.DataType = ua.NodeId.from_string("i=296") attrs.DataType = ua.NodeId.from_string("i=296")
value = [] value = []
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "SubscriptionId"
extobj.DataType = "i=288" extobj.DataType = "i=288"
extobj.Name = "SubscriptionId"
extobj.ValueRank = "-1"
extobj.Description.Text = "The identifier for the suscription to refresh."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
...@@ -1038,14 +1041,16 @@ def create_standard_address_space_Part9(server): ...@@ -1038,14 +1041,16 @@ def create_standard_address_space_Part9(server):
attrs.DataType = ua.NodeId.from_string("i=296") attrs.DataType = ua.NodeId.from_string("i=296")
value = [] value = []
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "SubscriptionId"
extobj.DataType = "i=288" extobj.DataType = "i=288"
extobj.Name = "SubscriptionId"
extobj.ValueRank = "-1"
extobj.Description.Text = "The identifier for the suscription to refresh."
value.append(extobj) value.append(extobj)
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "MonitoredItemId"
extobj.DataType = "i=288" extobj.DataType = "i=288"
extobj.Name = "MonitoredItemId"
extobj.ValueRank = "-1"
extobj.Description.Text = "The identifier for the monitored item to refresh."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
...@@ -1465,9 +1470,10 @@ def create_standard_address_space_Part9(server): ...@@ -1465,9 +1470,10 @@ def create_standard_address_space_Part9(server):
attrs.DataType = ua.NodeId.from_string("i=296") attrs.DataType = ua.NodeId.from_string("i=296")
value = [] value = []
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "SelectedResponse"
extobj.DataType = "i=6" extobj.DataType = "i=6"
extobj.Name = "SelectedResponse"
extobj.ValueRank = "-1"
extobj.Description.Text = "The response to the dialog condition."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
...@@ -1811,14 +1817,16 @@ def create_standard_address_space_Part9(server): ...@@ -1811,14 +1817,16 @@ def create_standard_address_space_Part9(server):
attrs.DataType = ua.NodeId.from_string("i=296") attrs.DataType = ua.NodeId.from_string("i=296")
value = [] value = []
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "EventId"
extobj.DataType = "i=15" extobj.DataType = "i=15"
extobj.Name = "EventId"
extobj.ValueRank = "-1"
extobj.Description.Text = "The identifier for the event to comment."
value.append(extobj) value.append(extobj)
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "Comment"
extobj.DataType = "i=21" extobj.DataType = "i=21"
extobj.Name = "Comment"
extobj.ValueRank = "-1"
extobj.Description.Text = "The comment to add to the condition."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
...@@ -1880,14 +1888,16 @@ def create_standard_address_space_Part9(server): ...@@ -1880,14 +1888,16 @@ def create_standard_address_space_Part9(server):
attrs.DataType = ua.NodeId.from_string("i=296") attrs.DataType = ua.NodeId.from_string("i=296")
value = [] value = []
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "EventId"
extobj.DataType = "i=15" extobj.DataType = "i=15"
extobj.Name = "EventId"
extobj.ValueRank = "-1"
extobj.Description.Text = "The identifier for the event to comment."
value.append(extobj) value.append(extobj)
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "Comment"
extobj.DataType = "i=21" extobj.DataType = "i=21"
extobj.Name = "Comment"
extobj.ValueRank = "-1"
extobj.Description.Text = "The comment to add to the condition."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
...@@ -2612,9 +2622,10 @@ def create_standard_address_space_Part9(server): ...@@ -2612,9 +2622,10 @@ def create_standard_address_space_Part9(server):
attrs.DataType = ua.NodeId.from_string("i=296") attrs.DataType = ua.NodeId.from_string("i=296")
value = [] value = []
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "ShelvingTime"
extobj.DataType = "i=290" extobj.DataType = "i=290"
extobj.Name = "ShelvingTime"
extobj.ValueRank = "-1"
extobj.Description.Text = "If not 0, this parameter specifies a fixed time for which the Alarm is to be shelved."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
...@@ -3477,9 +3488,10 @@ def create_standard_address_space_Part9(server): ...@@ -3477,9 +3488,10 @@ def create_standard_address_space_Part9(server):
attrs.DataType = ua.NodeId.from_string("i=296") attrs.DataType = ua.NodeId.from_string("i=296")
value = [] value = []
extobj = ua.Argument() extobj = ua.Argument()
extobj.ValueRank = "-1"
extobj.Name = "ShelvingTime"
extobj.DataType = "i=290" extobj.DataType = "i=290"
extobj.Name = "ShelvingTime"
extobj.ValueRank = "-1"
extobj.Description.Text = "If not 0, this parameter specifies a fixed time for which the Alarm is to be shelved."
value.append(extobj) value.append(extobj)
attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject) attrs.Value = ua.Variant(value, ua.VariantType.ExtensionObject)
attrs.ValueRank = 1 attrs.ValueRank = 1
......
...@@ -137,11 +137,19 @@ def create_standard_address_space_%s(server): ...@@ -137,11 +137,19 @@ def create_standard_address_space_%s(server):
self.writecode(indent, 'attrs.ArrayDimensions = {}'.format(obj.dimensions)) self.writecode(indent, 'attrs.ArrayDimensions = {}'.format(obj.dimensions))
def make_ext_obj_code(self, indent, extobj): def make_ext_obj_code(self, indent, extobj):
print("makeing code for ", extobj.objname)
self.writecode(indent, 'extobj = ua.{}()'.format(extobj.objname)) self.writecode(indent, 'extobj = ua.{}()'.format(extobj.objname))
for name, val in extobj.body.items(): for name, val in extobj.body.items():
val = val.strip() if type(val) is str:
if val not in (None, ""): raise Exception("Error val should a dict", name, val)
self.writecode(indent, 'extobj.{} = "{}"'.format(name, val)) #self.writecode(indent, 'extobj.{} = "{}"'.format(name, val))
else:
for k, v in val.items():
if type(v) is str:
self.writecode(indent, 'extobj.{} = "{}"'.format(k, v))
else:
for k2, v2 in v.items():
self.writecode(indent, 'extobj.{}.{} = "{}"'.format(k, k2, v2))
def make_variable_code(self, obj): def make_variable_code(self, obj):
indent = " " indent = " "
......
...@@ -122,7 +122,7 @@ ...@@ -122,7 +122,7 @@
</Definition> </Definition>
</UADataType> </UADataType>
<UAVariable DataType="EnumValueType" ParentNodeId="ns=1;i=3011" ValueRank="1" NodeId="ns=1;i=6002" ArrayDimensions="3" BrowseName="EnumValues"> <UAVariable DataType="EnumValueType" ParentNodeId="i=120" ValueRank="1" NodeId="ns=1;i=99002" ArrayDimensions="3" BrowseName="EnumValues">
<DisplayName>EnumValues</DisplayName> <DisplayName>EnumValues</DisplayName>
<References> <References>
<Reference ReferenceType="HasModellingRule">i=78</Reference> <Reference ReferenceType="HasModellingRule">i=78</Reference>
...@@ -177,6 +177,81 @@ ...@@ -177,6 +177,81 @@
</Value> </Value>
</UAVariable> </UAVariable>
<!-- Test the other xml format-->
<UAVariable NodeId="i=99003" BrowseName="EnumValues" ParentNodeId="i=120" DataType="i=7594" ValueRank="1">
<DisplayName>EnumValues</DisplayName>
<References>
<Reference ReferenceType="HasTypeDefinition">i=68</Reference>
<Reference ReferenceType="HasModellingRule">i=78</Reference>
<Reference ReferenceType="HasProperty" IsForward="false">i=120</Reference>
</References>
<Value>
<ListOfExtensionObject xmlns="http://opcfoundation.org/UA/2008/02/Types.xsd">
<ExtensionObject>
<TypeId>
<Identifier>i=7616</Identifier>
</TypeId>
<Body>
<EnumValueType>
<Value>1</Value>
<DisplayName>
<Locale>
</Locale>
<Text>Mandatory</Text>
</DisplayName>
<Description>
<Locale>
</Locale>
<Text>The BrowseName must appear in all instances of the type.</Text>
</Description>
</EnumValueType>
</Body>
</ExtensionObject>
<ExtensionObject>
<TypeId>
<Identifier>i=7616</Identifier>
</TypeId>
<Body>
<EnumValueType>
<Value>2</Value>
<DisplayName>
<Locale>
</Locale>
<Text>Optional</Text>
</DisplayName>
<Description>
<Locale>
</Locale>
<Text>The BrowseName may appear in an instance of the type.</Text>
</Description>
</EnumValueType>
</Body>
</ExtensionObject>
<ExtensionObject>
<TypeId>
<Identifier>i=7616</Identifier>
</TypeId>
<Body>
<EnumValueType>
<Value>3</Value>
<DisplayName>
<Locale>
</Locale>
<Text>Constraint</Text>
</DisplayName>
<Description>
<Locale>
</Locale>
<Text>The modelling rule defines a constraint and the BrowseName is not used in an instance of the type.</Text>
</Description>
</EnumValueType>
</Body>
</ExtensionObject>
</ListOfExtensionObject>
</Value>
</UAVariable>
<!-- test data Object with Method and MethodArguments --> <!-- test data Object with Method and MethodArguments -->
<UAObjectType NodeId="ns=1;i=1050" BrowseName="1:MyObjectType"> <UAObjectType NodeId="ns=1;i=1050" BrowseName="1:MyObjectType">
<DisplayName>MyObjectType</DisplayName> <DisplayName>MyObjectType</DisplayName>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment