ChimeraTK-ControlSystemAdapter-OPCUAAdapter 04.00.05
Loading...
Searching...
No Matches
ua_builtin_types.py
Go to the documentation of this file.
1#!/usr/bin/env/python
2# -*- coding: utf-8 -*-
3
4# This Source Code Form is subject to the terms of the Mozilla Public
5# License, v. 2.0. If a copy of the MPL was not distributed with this
6# file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9
23
24import sys
25import xml.dom.minidom as dom
26from ua_constants import *
27import logging
28from time import strftime, strptime
29from open62541_MacroHelper import open62541_MacroHelper
30
31logger = logging.getLogger(__name__)
32
33def getNextElementNode(xmlvalue):
34 if xmlvalue == None:
35 return None
36 xmlvalue = xmlvalue.nextSibling
37 while not xmlvalue == None and not xmlvalue.nodeType == xmlvalue.ELEMENT_NODE:
38 xmlvalue = xmlvalue.nextSibling
39 return xmlvalue
40
41if sys.version_info[0] >= 3:
42 # strings are already parsed to unicode
43 def unicode(s):
44 return s
45
47 value = None
48 name = None
49 __alias__ = None
50 __binTypeId__ = 0
51 stringRepresentation = ""
52 knownTypes = []
53 parent = None
54
55 def __init__(self, parent):
56 self.valuevalue = None
57 self.parent = parent
62 self.__alias__ = None
63 self.knownTypesknownTypes = ['boolean', 'int32', 'uint32', 'int16', 'uint16', \
64 'int64', 'uint64', 'byte', 'sbyte', 'float', 'double', \
65 'string', 'bytestring', 'localizedtext', 'statuscode', \
66 'diagnosticinfo', 'nodeid', 'guid', 'datetime', \
67 'qualifiedname', 'expandednodeid', 'xmlelement']
68 self.dataType = None
69 self.encodingRule = []
70
71 def getValueFieldByAlias(self, fieldname):
72 if not isinstance(self.valuevalue, list):
73 return None
74 if not isinstance(self.valuevalue[0], opcua_value_t):
75 return None
76 for val in self.valuevalue:
77 if val.alias() == fieldname:
78 return val.value
79 return None
80
81 def setEncodingRule(self, encoding):
82 self.encodingRule = encoding
83
84 def getEncodingRule(self):
85 return self.encodingRule
86
87 def alias(self, data=None):
88 if not data == None:
89 self.__alias__ = data
90 return self.__alias__
91
92 def isBuiltinByString(self, string):
93 if str(string).lower() in self.knownTypesknownTypes:
94 return True
95 return False
96
97 def value(self, data=None):
98 if not data==None:
99 self.__value__ = data
100 return self.__value__
101
102 def getTypeByString(self, stringName, encodingRule):
103 stringName = str(stringName.lower())
104 if stringName == 'boolean':
106 t.setEncodingRule(encodingRule)
107 elif stringName == 'int32':
109 t.setEncodingRule(encodingRule)
110 elif stringName == 'uint32':
112 t.setEncodingRule(encodingRule)
113 elif stringName == 'int16':
115 t.setEncodingRule(encodingRule)
116 elif stringName == 'uint16':
118 t.setEncodingRule(encodingRule)
119 elif stringName == 'int64':
121 t.setEncodingRule(encodingRule)
122 elif stringName == 'uint64':
124 t.setEncodingRule(encodingRule)
125 elif stringName == 'byte':
127 t.setEncodingRule(encodingRule)
128 elif stringName == 'sbyte':
130 t.setEncodingRule(encodingRule)
131 elif stringName == 'float':
133 t.setEncodingRule(encodingRule)
134 elif stringName == 'double':
136 t.setEncodingRule(encodingRule)
137 elif stringName == 'string':
139 t.setEncodingRule(encodingRule)
140 elif stringName == 'bytestring':
142 t.setEncodingRule(encodingRule)
143 elif stringName == 'localizedtext':
145 t.setEncodingRule(encodingRule)
146 elif stringName == 'statuscode':
148 t.setEncodingRule(encodingRule)
149 elif stringName == 'diagnosticinfo':
151 t.setEncodingRule(encodingRule)
152 elif stringName == 'nodeid':
154 t.setEncodingRule(encodingRule)
155 elif stringName == 'guid':
157 t.setEncodingRule(encodingRule)
158 elif stringName == 'datetime':
160 t.setEncodingRule(encodingRule)
161 elif stringName == 'qualifiedname':
163 t.setEncodingRule(encodingRule)
164 elif stringName == 'expandednodeid':
166 t.setEncodingRule(encodingRule)
167 elif stringName == 'xmlelement':
169 t.setEncodingRule(encodingRule)
170 else:
171 logger.debug("No class representing stringName " + stringName + " was found. Cannot create builtinType.")
172 return None
173 return t
174
175 def parseXML(self, xmlvalue):
176 logger.debug("parsing xmlvalue for " + self.parent.browseName() + " (" + str(self.parent.id()) + ") according to " + str(self.parent.dataType().target().getEncoding()))
177
178 if not "value" in xmlvalue.tagName.lower():
179 logger.error("Expected <Value> , but found " + xmlvalue.tagName + " instead. Value will not be parsed.")
180 return
181
182 if len(xmlvalue.childNodes) == 0:
183 logger.error("Expected childnodes for value, but none where found... Value will not be parsed.")
184 return
185
186 for n in xmlvalue.childNodes:
187 if n.nodeType == n.ELEMENT_NODE:
188 xmlvalue = n
189 break
190
191 if "ListOf" in xmlvalue.tagName:
192 self.valuevalue = []
193 for el in xmlvalue.childNodes:
194 if not el.nodeType == el.ELEMENT_NODE:
195 continue
196 self.valuevalue.append(self.__parseXMLSingleValue(el))
197 else:
198 self.valuevalue = [self.__parseXMLSingleValue(xmlvalue)]
199
200 logger.debug( "Parsed Value: " + str(self.valuevalue))
201
202 def __parseXMLSingleValue(self, xmlvalue, alias=None, encodingPart=None):
203 # Parse an encoding list such as enc = [[Int32], ['Duration', ['DateTime']]],
204 # returning a possibly aliased variable or list of variables.
205 # Keep track of aliases, as ['Duration', ['Hawaii', ['UtcTime', ['DateTime']]]]
206 # will be of type DateTime, but tagged as <Duration>2013-04-10 12:00 UTC</Duration>,
207 # and not as <Duration><Hawaii><UtcTime><String>2013-04-10 12:00 UTC</String>...
208
209 # Encoding may be partially handed down (iterative call). Only resort to
210 # type definition if we are not given a specific encoding to match
211 if encodingPart == None:
212 enc = self.parent.dataType().target().getEncoding()
213 else:
214 enc = encodingPart
215
216 # Check the structure of the encoding list to determine if a type is to be
217 # returned or we need to descend further checking aliases or multipart types
218 # such as extension Objects.
219 if len(enc) == 1:
220 # 0: ['BuiltinType'] either builtin type
221 # 1: [ [ 'Alias', [...], n] ] or single alias for possible multipart
222 if isinstance(enc[0], str):
223 # 0: 'BuiltinType'
224 if alias != None:
225 if not xmlvalue.tagName == alias:
226 logger.error("Expected XML element with tag " + alias + " but found " + xmlvalue.tagName + " instead")
227 return None
228 else:
229 t = self.getTypeByString(enc[0], enc)
230 t.alias(alias)
231 t.parseXML(xmlvalue)
232 return t
233 else:
234 if not self.isBuiltinByString(xmlvalue.tagName):
235 logger.error("Expected XML describing builtin type " + enc[0] + " but found " + xmlvalue.tagName + " instead")
236 else:
237 t = self.getTypeByString(enc[0], enc)
238 t.parseXML(xmlvalue)
239 return t
240 else:
241 # 1: ['Alias', [...], n]
242 # Let the next elif handle this
243 return self.__parseXMLSingleValue(xmlvalue, alias=alias, encodingPart=enc[0])
244 elif len(enc) == 3 and isinstance(enc[0], str):
245 # [ 'Alias', [...], 0 ] aliased multipart
246 if alias == None:
247 alias = enc[0]
248 # if we have an alias and the next field is multipart, keep the alias
249 elif alias != None and len(enc[1]) > 1:
250 alias = enc[0]
251 # otherwise drop the alias
252 return self.__parseXMLSingleValue(xmlvalue, alias=alias, encodingPart=enc[1])
253 else:
254 # [ [...], [...], [...]] multifield of unknowns (analyse separately)
255 # create an extension object to hold multipart type
256
257 # FIXME: This implementation expects an extensionobject to be manditory for
258 # multipart variables. Variants/Structures are not included in the
259 # OPCUA Namespace 0 nodeset.
260 # Consider moving this ExtensionObject specific parsing into the
261 # builtin type and only determining the multipart type at this stage.
262 if not xmlvalue.tagName == "ExtensionObject":
263 logger.error("Expected XML tag <ExtensionObject> for multipart type, but found " + xmlvalue.tagName + " instead.")
264 return None
265
267 extobj.setEncodingRule(enc)
268 etype = xmlvalue.getElementsByTagName("TypeId")
269 if len(etype) == 0:
270 logger.error("Did not find <TypeId> for ExtensionObject")
271 return None
272 etype = etype[0].getElementsByTagName("Identifier")
273 if len(etype) == 0:
274 logger.error("Did not find <Identifier> for ExtensionObject")
275 return None
276 etype = self.parent.getNamespace().getNodeByIDString(etype[0].firstChild.data)
277 if etype == None:
278 logger.error("Identifier Node not found in namespace" )
279 return None
280
281 extobj.typeId(etype)
282
283 ebody = xmlvalue.getElementsByTagName("Body")
284 if len(ebody) == 0:
285 logger.error("Did not find <Body> for ExtensionObject")
286 return None
287 ebody = ebody[0]
288
289 # Body must contain an Object of type 'DataType' as defined in Variable
290 ebodypart = ebody.firstChild
291 if not ebodypart.nodeType == ebodypart.ELEMENT_NODE:
292 ebodypart = getNextElementNode(ebodypart)
293 if ebodypart == None:
294 logger.error("Expected ExtensionObject to hold a variable of type " + str(self.parent.dataType().target().browseName()) + " but found nothing.")
295 return None
296
297 if not ebodypart.tagName == self.parent.dataType().target().browseName():
298 logger.error("Expected ExtensionObject to hold a variable of type " + str(self.parent.dataType().target().browseName()) + " but found " + str(ebodypart.tagName) + " instead.")
299 return None
300 extobj.alias(ebodypart.tagName)
301
302 ebodypart = ebodypart.firstChild
303 if not ebodypart.nodeType == ebodypart.ELEMENT_NODE:
304 ebodypart = getNextElementNode(ebodypart)
305 if ebodypart == None:
306 logger.error("Description of dataType " + str(self.parent.dataType().target().browseName()) + " in ExtensionObject is empty/invalid.")
307 return None
308
309 extobj.value = []
310 for e in enc:
311 if not ebodypart == None:
312 extobj.value.append(extobj.__parseXMLSingleValue(ebodypart, alias=None, encodingPart=e))
313 else:
314 logger.error("Expected encoding " + str(e) + " but found none in body.")
315 ebodypart = getNextElementNode(ebodypart)
316 return extobj
317
319 pass
320
322 pass
323
325 return self.__binTypeId__
326
327 def __str__(self):
328 if self.__alias__ != None:
329 return "'" + self.alias() + "':" + self.stringRepresentationstringRepresentation + "(" + str(self.valuevalue) + ")"
330 return self.stringRepresentationstringRepresentation + "(" + str(self.valuevalue) + ")"
331
332 def __repr__(self):
333 return self.__str__()
334
335 def printOpen62541CCode_SubType(self, asIndirect=True):
336 return ""
337
338 def printOpen62541CCode(self, bootstrapping = True):
339 codegen = open62541_MacroHelper()
340 code = []
341 valueName = self.parent.getCodePrintableID() + "_variant_DataContents"
342
343 # self.value either contains a list of multiple identical BUILTINTYPES, or it
344 # contains a single builtintype (which may be a container); choose if we need
345 # to create an array or a single variable.
346 # Note that some genious defined that there are arrays of size 1, which are
347 # distinctly different then a single value, so we need to check that as well
348 # Semantics:
349 # -3: Scalar or 1-dim
350 # -2: Scalar or x-dim | x>0
351 # -1: Scalar
352 # 0: x-dim | x>0
353 # n: n-dim | n>0
354 if (len(self.valuevalue) == 0):
355 return code
356 if not isinstance(self.valuevalue[0], opcua_value_t):
357 return code
358
359 if self.parent.valueRank() != -1 and (self.parent.valueRank() >=0 or (len(self.valuevalue) > 1 and (self.parent.valueRank() != -2 or self.parent.valueRank() != -3))):
360 # User the following strategy for all directly mappable values a la 'UA_Type MyInt = (UA_Type) 23;'
361 if self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_GUID:
362 logger.warn("Don't know how to print array of GUID in node " + str(self.parent.id()))
363 elif self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_DATETIME:
364 logger.warn("Don't know how to print array of DateTime in node " + str(self.parent.id()))
365 elif self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_DIAGNOSTICINFO:
366 logger.warn("Don't know how to print array of DiagnosticInfo in node " + str(self.parent.id()))
367 elif self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_STATUSCODE:
368 logger.warn("Don't know how to print array of StatusCode in node " + str(self.parent.id()))
369 else:
370 if self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_EXTENSIONOBJECT:
371 for v in self.valuevalue:
372 logger.debug("Building extObj array index " + str(self.valuevalue.index(v)))
373 code = code + v.printOpen62541CCode_SubType_build(arrayIndex=self.valuevalue.index(v))
374 #code.append("attr.value.type = &UA_TYPES[UA_TYPES_" + self.value[0].stringRepresentation.upper() + "];")
375 code.append("UA_" + self.valuevalue[0].stringRepresentation + " " + valueName + "[" + str(len(self.valuevalue)) + "];")
376 if self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_EXTENSIONOBJECT:
377 for v in self.valuevalue:
378 logger.debug("Printing extObj array index " + str(self.valuevalue.index(v)))
379 code.append(valueName + "[" + str(self.valuevalue.index(v)) + "] = " + v.printOpen62541CCode_SubType(asIndirect=False) + ";")
380 code.append("UA_free(" + v.printOpen62541CCode_SubType() + ");")
381 else:
382 for v in self.valuevalue:
383 code.append(valueName + "[" + str(self.valuevalue.index(v)) + "] = " + v.printOpen62541CCode_SubType() + ";")
384 code.append("UA_Variant_setArray( &attr.value, &" + valueName +
385 ", (UA_Int32) " + str(len(self.valuevalue)) + ", &UA_TYPES[UA_TYPES_" + self.valuevalue[0].stringRepresentation.upper() + "]);")
386 else:
387 # User the following strategy for all directly mappable values a la 'UA_Type MyInt = (UA_Type) 23;'
388 if self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_GUID:
389 logger.warn("Don't know how to print scalar GUID in node " + str(self.parent.id()))
390 elif self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_DATETIME:
391 logger.warn("Don't know how to print scalar DateTime in node " + str(self.parent.id()))
392 elif self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_DIAGNOSTICINFO:
393 logger.warn("Don't know how to print scalar DiagnosticInfo in node " + str(self.parent.id()))
394 elif self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_STATUSCODE:
395 logger.warn("Don't know how to print scalar StatusCode in node " + str(self.parent.id()))
396 else:
397 # The following strategy applies to all other types, in particular strings and numerics.
398 if self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_EXTENSIONOBJECT:
399 code = code + self.valuevalue[0].printOpen62541CCode_SubType_build()
400 #code.append("attr.value.type = &UA_TYPES[UA_TYPES_" + self.value[0].stringRepresentation.upper() + "];")
401 if self.valuevalue[0].__binTypeId__ == BUILTINTYPE_TYPEID_EXTENSIONOBJECT:
402 code.append("UA_" + self.valuevalue[0].stringRepresentation + " *" + valueName + " = " + self.valuevalue[0].printOpen62541CCode_SubType() + ";")
403 code.append("UA_Variant_setScalar( &attr.value, " + valueName + ", &UA_TYPES[UA_TYPES_" + self.valuevalue[0].stringRepresentation.upper() + "]);")
404
405 #FIXME: There is no membership definition for extensionObjects generated in this function.
406 #code.append("UA_" + self.value[0].stringRepresentation + "_deleteMembers(" + valueName + ");")
407 else:
408 if bootstrapping == True:
409 code.append("UA_Variant* " + self.parent.getCodePrintableID() + "_variant = UA_Variant_new();" )
410 code.append("UA_" + self.valuevalue[0].stringRepresentation + " *" + valueName + " = UA_" + self.valuevalue[0].stringRepresentation + "_new();")
411 code.append("*" + valueName + " = " + self.valuevalue[0].printOpen62541CCode_SubType() + ";")
412 if bootstrapping == False:
413 code.append("UA_Variant_setScalar( &attr.value, " + valueName + ", &UA_TYPES[UA_TYPES_" + self.valuevalue[0].stringRepresentation.upper() + "]);")
414 else:
415 code.append("UA_Variant_setScalar( "+self.parent.getCodePrintableID()+"_variant, " + valueName + ", &UA_TYPES[UA_TYPES_" + self.valuevalue[0].stringRepresentation.upper() + "]);")
416 #code.append("UA_" + self.value[0].stringRepresentation + "_deleteMembers(" + valueName + ");")
417 return code
418
419
420
423
430 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_EXTENSIONOBJECT
431
432 def typeId(self, data=None):
433 if not data == None:
434 self.__typeId__ = data
435 return self.__typeId__
436
438 return self.__codeInstanceName__
439
440 def setCodeInstanceName(self, recursionDepth, arrayIndex):
441 self.__inVariableRecursionDepth__ = recursionDepth
442 self.__inVariableArrayIndex__ = arrayIndex
443 self.__codeInstanceName__ = self.parent.getCodePrintableID() + "_" + str(self.alias()) + "_" + str(arrayIndex) + "_" + str(recursionDepth)
444 return self.__codeInstanceName__
445
446 def printOpen62541CCode_SubType_build(self, recursionDepth=0, arrayIndex=0):
447 code = [""]
448 codegen = open62541_MacroHelper();
449
450 logger.debug("Building extensionObject for " + str(self.parent.id()))
451 logger.debug("Value " + str(self.valuevaluevalue))
452 logger.debug("Encoding " + str(self.getEncodingRule()))
453
454 self.setCodeInstanceName(recursionDepth, arrayIndex)
455 # If there are any ExtensionObjects instide this ExtensionObject, we need to
456 # generate one-time-structs for them too before we can proceed;
457 for subv in self.valuevaluevalue:
458 if isinstance(subv, list):
459 logger.debug("ExtensionObject contains an ExtensionObject, which is currently not encodable!", LOG_LEVEL_ERR)
460
461 code.append("struct {")
462 for field in self.getEncodingRule():
463 ptrSym = ""
464 # If this is an Array, this is pointer to its contents with a AliasOfFieldSize entry
465 if field[2] != 0:
466 code.append(" UA_Int32 " + str(field[0]) + "Size;")
467 ptrSym = "*"
468 if len(field[1]) == 1:
469 code.append(" UA_" + str(field[1][0]) + " " + ptrSym + str(field[0]) + ";")
470 else:
471 code.append(" UA_ExtensionObject " + " " + ptrSym + str(field[0]) + ";")
472 code.append("} " + self.getCodeInstanceName() + "_struct;")
473
474 # Assign data to the struct contents
475 # Track the encoding rule definition to detect arrays and/or ExtensionObjects
476 encFieldIdx = 0
477 for subv in self.valuevaluevalue:
478 encField = self.getEncodingRule()[encFieldIdx]
479 encFieldIdx = encFieldIdx + 1;
480 logger.debug("Encoding of field " + subv.alias() + " is " + str(subv.getEncodingRule()) + "defined by " + str(encField))
481 # Check if this is an array
482 if encField[2] == 0:
483 code.append(self.getCodeInstanceName()+"_struct."+subv.alias() + " = " + subv.printOpen62541CCode_SubType(asIndirect=False) + ";")
484 else:
485 if isinstance(subv, list):
486 # this is an array
487 code.append(self.getCodeInstanceName()+"_struct."+subv.alias() + "Size = " + str(len(subv)) + ";")
488 code.append(self.getCodeInstanceName()+"_struct."+subv.alias()+" = (UA_" + subv.stringRepresentation + " *) UA_malloc(sizeof(UA_" + subv.stringRepresentation + ")*"+ str(len(subv))+");")
489 logger.debug("Encoding included array of " + str(len(subv)) + " values.")
490 for subvidx in range(0,len(subv)):
491 subvv = subv[subvidx]
492 logger.debug(" " + str(subvix) + " " + str(subvv))
493 code.append(self.getCodeInstanceName()+"_struct."+subv.alias() + "[" + str(subvidx) + "] = " + subvv.printOpen62541CCode_SubType(asIndirect=True) + ";")
494 code.append("}")
495 else:
496 code.append(self.getCodeInstanceName()+"_struct."+subv.alias() + "Size = 1;")
497 code.append(self.getCodeInstanceName()+"_struct."+subv.alias()+" = (UA_" + subv.stringRepresentation + " *) UA_malloc(sizeof(UA_" + subv.stringRepresentation + "));")
498 code.append(self.getCodeInstanceName()+"_struct."+subv.alias() + "[0] = " + subv.printOpen62541CCode_SubType(asIndirect=True) + ";")
499
500
501 # Allocate some memory
502 code.append("UA_ExtensionObject *" + self.getCodeInstanceName() + " = UA_ExtensionObject_new();")
503 code.append(self.getCodeInstanceName() + "->encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;")
504 code.append(self.getCodeInstanceName() + "->content.encoded.typeId = UA_NODEID_NUMERIC(" + str(self.parent.dataType().target().id().ns) + ", " + str(self.parent.dataType().target().id().i) + "+ UA_ENCODINGOFFSET_BINARY);")
505 code.append("if(UA_ByteString_allocBuffer(&" + self.getCodeInstanceName() + "->content.encoded.body, 65000) != UA_STATUSCODE_GOOD) {}" )
506
507 # Encode each value as a bytestring seperately.
508 code.append("size_t " + self.getCodeInstanceName() + "_encOffset = 0;" )
509 encFieldIdx = 0;
510 for subv in self.valuevaluevalue:
511 encField = self.getEncodingRule()[encFieldIdx]
512 encFieldIdx = encFieldIdx + 1;
513 if encField[2] == 0:
514 #code.append("UA_" + subv.stringRepresentation + "_encodeBinary(&" + self.getCodeInstanceName()+"_struct."+subv.alias() + ", &" + self.getCodeInstanceName() + "->content.encoded.body, &" + self.getCodeInstanceName() + "_encOffset);" )
515 code.append("retval |= UA_encodeBinary(&" + self.getCodeInstanceName()+"_struct."+subv.alias() + ", &UA_TYPES[UA_TYPES_" + subv.stringRepresentation.upper() + "], NULL, NULL, &" + self.getCodeInstanceName() + "->content.encoded.body, &" + self.getCodeInstanceName() + "_encOffset);" )
516 else:
517 if isinstance(subv, list):
518 for subvidx in range(0,len(subv)):
519 #code.append("UA_" + subv.stringRepresentation + "_encodeBinary(&" + self.getCodeInstanceName()+"_struct."+subv.alias() + "[" + str(subvidx) + "], &" + self.getCodeInstanceName() + "->content.encoded.body, &" + self.getCodeInstanceName() + "_encOffset);" )
520 code.append("retval |= UA_encodeBinary(&" + self.getCodeInstanceName()+"_struct."+subv.alias() + "[" + str(subvidx) + "], &UA_TYPES[UA_TYPES_" + subv.stringRepresentation.upper() + "], NULL, NULL, &" + self.getCodeInstanceName() + "->content.encoded.body, &" + self.getCodeInstanceName() + "_encOffset);" )
521 else:
522 code.append("retval |= UA_encodeBinary(&" + self.getCodeInstanceName()+"_struct."+subv.alias() + "[0], &UA_TYPES[UA_TYPES_" + subv.stringRepresentation.upper() + "], NULL, NULL, &" + self.getCodeInstanceName() + "->content.encoded.body, &" + self.getCodeInstanceName() + "_encOffset);" )
523
524 # Reallocate the memory by swapping the 65k Bytestring for a new one
525 code.append(self.getCodeInstanceName() + "->content.encoded.body.length = " + self.getCodeInstanceName() + "_encOffset;");
526 code.append("UA_Byte *" + self.getCodeInstanceName() + "_newBody = (UA_Byte *) UA_malloc(" + self.getCodeInstanceName() + "_encOffset );" )
527 code.append("memcpy(" + self.getCodeInstanceName() + "_newBody, " + self.getCodeInstanceName() + "->content.encoded.body.data, " + self.getCodeInstanceName() + "_encOffset);" )
528 code.append("UA_Byte *" + self.getCodeInstanceName() + "_oldBody = " + self.getCodeInstanceName() + "->content.encoded.body.data;");
529 code.append(self.getCodeInstanceName() + "->content.encoded.body.data = " +self.getCodeInstanceName() + "_newBody;")
530 code.append("UA_free(" + self.getCodeInstanceName() + "_oldBody);")
531 code.append("")
532 return code
533
534 def printOpen62541CCode_SubType(self, asIndirect=True):
535 if asIndirect == False:
536 return "*" + str(self.getCodeInstanceName())
537 return str(self.getCodeInstanceName())
538
539 def __str__(self):
540 return "'" + self.alias() + "':" + self.stringRepresentationstringRepresentationstringRepresentation + "(" + str(self.valuevaluevalue) + ")"
541
547 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_LOCALIZEDTEXT
548
549 def parseXML(self, xmlvalue):
550 # Expect <LocalizedText> or <AliasName>
551 # <Locale>xx_XX</Locale>
552 # <Text>TextText</Text>
553 # <LocalizedText> or </AliasName>
554 #
555 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
556 logger.error("Expected XML Element, but got junk...")
557 return
558
559 if self.alias() != None:
560 if not self.alias() == xmlvalue.tagName:
561 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
562 else:
564 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
565
566 if xmlvalue.firstChild == None:
567 if self.alias() != None:
568 logger.debug("Neither locale nor text in XML description field " + self.alias() + ". Setting to default ['en_US','']")
569 else:
570 logger.debug("Neither locale nor text in XML description. Setting to default ['en_US','']")
571 self.valuevaluevalue = ['en_US','']
572 return
573
574 self.valuevaluevalue = []
575 tmp = xmlvalue.getElementsByTagName("Locale")
576 if len(tmp) == 0:
577 logger.warn("Did not find a locale. Setting to en_US per default.")
578 self.valuevaluevalue.append('en_US')
579 else:
580 if tmp[0].firstChild == None:
581 logger.warn("Locale tag without contents. Setting to en_US per default.")
582 self.valuevaluevalue.append('en_US')
583 else:
584 self.valuevaluevalue.append(tmp[0].firstChild.data)
585 clean = ""
586 for s in self.valuevaluevalue[0]:
587 if s in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_":
588 clean = clean + s
589 self.valuevaluevalue[0] = clean
590
591 tmp = xmlvalue.getElementsByTagName("Text")
592 if len(tmp) == 0:
593 logger.warn("Did not find a Text. Setting to empty string per default.")
594 self.valuevaluevalue.append('')
595 else:
596 if tmp[0].firstChild == None:
597 logger.warn("Text tag without content. Setting to empty string per default.")
598 self.valuevaluevalue.append('')
599 else:
600 self.valuevaluevalue.append(tmp[0].firstChild.data)
601
602 def printOpen62541CCode_SubType(self, asIndirect=True):
603 if asIndirect==True:
604 code = "UA_LOCALIZEDTEXT_ALLOC(\"" + str(self.valuevaluevalue[0]) + "\", \"" + str(self.valuevaluevalue[1].encode('utf-8')) + "\")"
605 else:
606 code = "UA_LOCALIZEDTEXT(\"" + str(self.valuevaluevalue[0]) + "\", \"" + str(self.valuevaluevalue[1].encode('utf-8')) + "\")"
607 return code
608
614 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_EXPANDEDNODEID
615
616 def parseXML(self, xmlvalue):
617 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
618 logger.error("Expected XML Element, but got junk...")
619 return
620
621 logger.debug("Not implemented", LOG_LEVEL_ERR)
622
623 def printOpen62541CCode_SubType(self, asIndirect=True):
624 #FIXME! This one is definetely broken!
625 code = ""
626 return code
627
633 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_NODEID
634
635 def parseXML(self, xmlvalue):
636 # Expect <NodeId> or <Alias>
637 # <Identifier> # It is unclear whether or not this is manditory. Identifier tags are used in Namespace 0.
638 # ns=x;i=y or similar string representation of id()
639 # </Identifier>
640 # </NodeId> or </Alias>
641 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
642 logger.error("Expected XML Element, but got junk...")
643 return
644
645 if self.alias() != None:
646 if not self.alias() == xmlvalue.tagName:
647 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
648 else:
650 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
651
652 # Catch XML <NodeId />
653 if xmlvalue.firstChild == None :
654 logger.error("No value is given, which is illegal for Node Types...")
655 self.valuevaluevalue = None
656 else:
657 # Check if there is an <Identifier> tag
658 if len(xmlvalue.getElementsByTagName("Identifier")) != 0:
659 xmlvalue = xmlvalue.getElementsByTagName("Identifier")[0]
660 self.valuevaluevalue = self.parent.getNamespace().getNodeByIDString(unicode(xmlvalue.firstChild.data))
661 if self.valuevaluevalue == None:
662 logger.error("Node with id " + str(unicode(xmlvalue.firstChild.data)) + " was not found in namespace.")
663
664 def printOpen62541CCode_SubType(self, asIndirect=True):
665 if self.valuevaluevalue == None:
666 return "UA_NODEID_NUMERIC(0,0)"
667 nodeId = self.valuevaluevalue.id()
668 if nodeId.i != None:
669 return "UA_NODEID_NUMERIC(" + str(nodeId.ns) + ", " + str(nodeId.i) + ")"
670 elif nodeId.s != None:
671 return "UA_NODEID_STRING(" + str(nodeId.ns) + ", " + str(nodeId.s) + ")"
672 elif nodeId.b != None:
673 logger.debug("NodeID Generation macro for bytestrings has not been implemented.")
674 return "UA_NODEID_NUMERIC(0,0)"
675 elif nodeId.g != None:
676 logger.debug("NodeID Generation macro for guids has not been implemented.")
677 return "UA_NODEID_NUMERIC(0,0)"
678 return "UA_NODEID_NUMERIC(0,0)"
679
685 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_DATETIME
686
687 def parseXML(self, xmlvalue):
688 # Expect <DateTime> or <AliasName>
689 # 2013-08-13T21:00:05.0000L
690 # </DateTime> or </AliasName>
691 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
692 logger.error("Expected XML Element, but got junk...")
693 return
694
695 if self.alias() != None:
696 if not self.alias() == xmlvalue.tagName:
697 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
698 else:
700 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
701
702 # Catch XML <DateTime /> by setting the value to a default
703 if xmlvalue.firstChild == None :
704 logger.debug("No value is given. Setting to default now()")
705 self.valuevaluevalue = strptime(strftime("%Y-%m-%dT%H:%M%S"), "%Y-%m-%dT%H:%M%S")
706 else:
707 timestr = unicode(xmlvalue.firstChild.data)
708 # .NET tends to create this garbage %Y-%m-%dT%H:%M:%S.0000z
709 # strip everything after the "." away for a posix time_struct
710 if "." in timestr:
711 timestr = timestr[:timestr.index(".")]
712 # If the last character is not numeric, remove it
713 while len(timestr)>0 and not timestr[-1] in "0123456789":
714 timestr = timestr[:-1]
715 try:
716 self.valuevaluevalue = strptime(timestr, "%Y-%m-%dT%H:%M:%S")
717 except:
718 logger.error("Timestring format is illegible. Expected 2001-01-30T21:22:23, but got " + timestr + " instead. Time will be defaultet to now()")
719 self.valuevaluevalue = strptime(strftime("%Y-%m-%dT%H:%M%S"), "%Y-%m-%dT%H:%M%S")
720
726 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_QUALIFIEDNAME
727
728 def parseXML(self, xmlvalue):
729 # Expect <QualifiedName> or <AliasName>
730 # <NamespaceIndex>Int16<NamespaceIndex> # Optional, apparently ommitted if ns=0 ??? (Not given in OPCUA Nodeset2)
731 # <Name>SomeString<Name> # Speculation: Manditory if NamespaceIndex is given, omitted otherwise?
732 # </QualifiedName> or </AliasName>
733 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
734 logger.error("Expected XML Element, but got junk...")
735 return
736
737 if self.alias() != None:
738 if not self.alias() == xmlvalue.tagName:
739 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
740 else:
742 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
743
744 # Catch XML <Qalified /> by setting the value to a default
745 if xmlvalue.firstChild == None :
746 logger.debug("No value is given. Setting to default empty string in ns=0: [0, '']")
747 self.valuevaluevalue = [0, '']
748 else:
749 # Is a namespace index passed?
750 if len(xmlvalue.getElementsByTagName("NamespaceIndex")) != 0:
751 self.valuevaluevalue = [int(xmlvalue.getElementsByTagName("NamespaceIndex")[0].firstChild.data)]
752 # namespace index is passed and <Name> tags are now manditory?
753 if len(xmlvalue.getElementsByTagName("Name")) != 0:
754 self.valuevaluevalue.append(xmlvalue.getElementsByTagName("Name")[0].firstChild.data)
755 else:
756 logger.debug("No name is specified, will default to empty string")
757 self.valuevaluevalue.append('')
758 else:
759 logger.debug("No namespace is specified, will default to 0")
760 self.valuevaluevalue = [0]
761 self.valuevaluevalue.append(unicode(xmlvalue.firstChild.data))
762
763 def printOpen62541CCode_SubType(self, asIndirect=True):
764 code = "UA_QUALIFIEDNAME_ALLOC(" + str(self.valuevaluevalue[0]) + ", \"" + self.valuevaluevalue[1].encode('utf-8') + "\")"
765 return code
766
772 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_STATUSCODE
773
774 def parseXML(self, xmlvalue):
775 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
776 logger.error("Expected XML Element, but got junk...")
777 return
778 logger.warn("Not implemented")
779
785 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_DIAGNOSTICINFO
786
787 def parseXML(self, xmlvalue):
788 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
789 logger.error("Expected XML Element, but got junk...")
790 return
791 logger.warn("Not implemented")
792
798 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_GUID
799
800 def parseXML(self, xmlvalue):
801 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
802 logger.error("Expected XML Element, but got junk...")
803 return
804
805 if self.alias() != None:
806 if not self.alias() == xmlvalue.tagName:
807 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
808 else:
810 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
811
812 # Catch XML <Guid /> by setting the value to a default
813 if xmlvalue.firstChild == None:
814 logger.debug("No value is given. Setting to default 0")
815 self.valuevaluevalue = [0,0,0,0]
816 else:
817 self.valuevaluevalue = unicode(xmlvalue.firstChild.data)
818 self.valuevaluevalue = self.valuevaluevalue.replace("{","")
819 self.valuevaluevalue = self.valuevaluevalue.replace("}","")
820 self.valuevaluevalue = self.valuevaluevalue.split("-")
821 tmp = []
822 ok = True
823 for g in self.valuevaluevalue:
824 try:
825 tmp.append(int("0x"+g, 16))
826 except:
827 logger.error("Invalid formatting of Guid. Expected {01234567-89AB-CDEF-ABCD-0123456789AB}, got " + unicode(xmlvalue.firstChild.data))
828 self.valuevaluevalue = [0,0,0,0,0]
829 ok = False
830 if len(tmp) != 5:
831 logger.error("Invalid formatting of Guid. Expected {01234567-89AB-CDEF-ABCD-0123456789AB}, got " + unicode(xmlvalue.firstChild.data))
832 self.valuevaluevalue = [0,0,0,0]
833 ok = False
834 self.valuevaluevalue = tmp
835
841 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_BOOLEAN
842
843 def parseXML(self, xmlvalue):
844 # Expect <Boolean>value</Boolean> or
845 # <Aliasname>value</Aliasname>
846 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
847 logger.error("Expected XML Element, but got junk...")
848 return
849
850 if self.alias() != None:
851 if not self.alias() == xmlvalue.tagName:
852 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
853 else:
855 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
856
857 # Catch XML <Boolean /> by setting the value to a default
858 if xmlvalue.firstChild == None:
859 logger.debug("No value is given. Setting to default 0")
860 self.valuevaluevalue = "false"
861 else:
862 if "false" in unicode(xmlvalue.firstChild.data).lower():
863 self.valuevaluevalue = "false"
864 else:
865 self.valuevaluevalue = "true"
866
867 def printOpen62541CCode_SubType(self, asIndirect=True):
869
875 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_BYTE
876
877 def parseXML(self, xmlvalue):
878 # Expect <Byte>value</Byte> or
879 # <Aliasname>value</Aliasname>
880 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
881 logger.error("Expected XML Element, but got junk...")
882 return
883
884 if self.alias() != None:
885 if not self.alias() == xmlvalue.tagName:
886 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
887 else:
889 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
890
891 # Catch XML <Byte /> by setting the value to a default
892 if xmlvalue.firstChild == None:
893 logger.debug("No value is given. Setting to default 0")
895 else:
896 try:
897 self.valuevaluevalue = int(unicode(xmlvalue.firstChild.data))
898 except:
899 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
900
901 def printOpen62541CCode_SubType(self, asIndirect=True):
903
909 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_SBYTE
910
911 def parseXML(self, xmlvalue):
912 # Expect <SByte>value</SByte> or
913 # <Aliasname>value</Aliasname>
914 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
915 logger.error("Expected XML Element, but got junk...")
916 return
917
918 if self.alias() != None:
919 if not self.alias() == xmlvalue.tagName:
920 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
921 else:
923 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
924
925 # Catch XML <SByte /> by setting the value to a default
926 if xmlvalue.firstChild == None:
927 logger.debug("No value is given. Setting to default 0")
929 else:
930 try:
931 self.valuevaluevalue = int(unicode(xmlvalue.firstChild.data))
932 except:
933 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
934
935 def printOpen62541CCode_SubType(self, asIndirect=True):
937
943 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_INT16
944
945 def parseXML(self, xmlvalue):
946 # Expect <Int16>value</Int16> or
947 # <Aliasname>value</Aliasname>
948 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
949 logger.error("Expected XML Element, but got junk...")
950 return
951
952 if self.alias() != None:
953 if not self.alias() == xmlvalue.tagName:
954 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
955 else:
957 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
958
959 # Catch XML <Int16 /> by setting the value to a default
960 if xmlvalue.firstChild == None:
961 logger.debug("No value is given. Setting to default 0")
963 else:
964 try:
965 self.valuevaluevalue = int(unicode(xmlvalue.firstChild.data))
966 except:
967 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
968
969 def printOpen62541CCode_SubType(self, asIndirect=True):
971
977 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_UINT16
978
979 def parseXML(self, xmlvalue):
980 # Expect <UInt16>value</UInt16> or
981 # <Aliasname>value</Aliasname>
982 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
983 logger.error("Expected XML Element, but got junk...")
984 return
985
986 if self.alias() != None:
987 if not self.alias() == xmlvalue.tagName:
988 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
989 else:
991 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
992
993 # Catch XML <UInt16 /> by setting the value to a default
994 if xmlvalue.firstChild == None:
995 logger.debug("No value is given. Setting to default 0")
997 else:
998 try:
999 self.valuevaluevalue = int(unicode(xmlvalue.firstChild.data))
1000 except:
1001 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
1002
1003 def printOpen62541CCode_SubType(self, asIndirect=True):
1005
1011 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_INT32
1012
1013 def parseXML(self, xmlvalue):
1014 # Expect <Int32>value</Int32> or
1015 # <Aliasname>value</Aliasname>
1016 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
1017 logger.error("Expected XML Element, but got junk...")
1018 return
1019
1020 if self.alias() != None:
1021 if not self.alias() == xmlvalue.tagName:
1022 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1023 else:
1025 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1026
1027 # Catch XML <Int32 /> by setting the value to a default
1028 if xmlvalue.firstChild == None:
1029 logger.debug("No value is given. Setting to default 0")
1031 else:
1032 try:
1033 self.valuevaluevalue = int(unicode(xmlvalue.firstChild.data))
1034 except:
1035 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
1036
1037 def printOpen62541CCode_SubType(self, asIndirect=True):
1039
1045 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_UINT32
1046
1047 def parseXML(self, xmlvalue):
1048 # Expect <UInt32>value</UInt32> or
1049 # <Aliasname>value</Aliasname>
1050 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
1051 logger.error("Expected XML Element, but got junk...")
1052 return
1053
1054 if self.alias() != None:
1055 if not self.alias() == xmlvalue.tagName:
1056 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1057 else:
1059 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1060
1061 # Catch XML <UInt32 /> by setting the value to a default
1062 if xmlvalue.firstChild == None:
1063 logger.debug("No value is given. Setting to default 0")
1065 else:
1066 try:
1067 self.valuevaluevalue = int(unicode(xmlvalue.firstChild.data))
1068 except:
1069 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
1070
1071 def printOpen62541CCode_SubType(self, asIndirect=True):
1073
1079 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_INT64
1080
1081 def parseXML(self, xmlvalue):
1082 # Expect <Int64>value</Int64> or
1083 # <Aliasname>value</Aliasname>
1084 if self.alias() != None:
1085 if not self.alias() == xmlvalue.tagName:
1086 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1087 else:
1089 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1090
1091 # Catch XML <Int64 /> by setting the value to a default
1092 if xmlvalue.firstChild == None:
1093 logger.debug("No value is given. Setting to default 0")
1095 else:
1096 try:
1097 self.valuevaluevalue = int(unicode(xmlvalue.firstChild.data))
1098 except:
1099 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
1100
1101 def printOpen62541CCode_SubType(self, asIndirect=True):
1103
1109 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_UINT64
1110
1111 def parseXML(self, xmlvalue):
1112 # Expect <UInt16>value</UInt16> or
1113 # <Aliasname>value</Aliasname>
1114 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
1115 logger.error("Expected XML Element, but got junk...")
1116 return
1117
1118 if self.alias() != None:
1119 if not self.alias() == xmlvalue.tagName:
1120 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1121 else:
1123 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1124
1125 # Catch XML <UInt64 /> by setting the value to a default
1126 if xmlvalue.firstChild == None:
1127 logger.debug("No value is given. Setting to default 0")
1129 else:
1130 try:
1131 self.valuevaluevalue = int(unicode(xmlvalue.firstChild.data))
1132 except:
1133 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
1134
1135 def printOpen62541CCode_SubType(self, asIndirect=True):
1137
1143 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_FLOAT
1144
1145 def parseXML(self, xmlvalue):
1146 # Expect <Float>value</Float> or
1147 # <Aliasname>value</Aliasname>
1148 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
1149 logger.error("Expected XML Element, but got junk...")
1150 return
1151
1152 if self.alias() != None:
1153 if not self.alias() == xmlvalue.tagName:
1154 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1155 else:
1157 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1158
1159 # Catch XML <Float /> by setting the value to a default
1160 if xmlvalue.firstChild == None:
1161 logger.debug("No value is given. Setting to default 0")
1163 else:
1164 try:
1165 self.valuevaluevalue = float(unicode(xmlvalue.firstChild.data))
1166 except:
1167 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
1168
1169 def printOpen62541CCode_SubType(self, asIndirect=True):
1171
1177 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_DOUBLE
1178
1179 def parseXML(self, xmlvalue):
1180 # Expect <Double>value</Double> or
1181 # <Aliasname>value</Aliasname>
1182 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
1183 logger.error("Expected XML Element, but got junk...")
1184 return
1185
1186 if self.alias() != None:
1187 if not self.alias() == xmlvalue.tagName:
1188 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1189 else:
1191 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1192
1193 # Catch XML <Double /> by setting the value to a default
1194 if xmlvalue.firstChild == None:
1195 logger.debug("No value is given. Setting to default 0")
1197 else:
1198 try:
1199 self.valuevaluevalue = float(unicode(xmlvalue.firstChild.data))
1200 except:
1201 logger.error("Error parsing integer. Expected " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + unicode(xmlvalue.firstChild.data))
1202
1203 def printOpen62541CCode_SubType(self, asIndirect=True):
1205
1211 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_STRING
1212
1213 def pack(self):
1214 bin = structpack("I", len(unicode(self.valuevaluevalue)))
1215 bin = bin + str(self.valuevaluevalue)
1216 return bin
1217
1218 def parseXML(self, xmlvalue):
1219 # Expect <String>value</String> or
1220 # <Aliasname>value</Aliasname>
1221 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
1222 logger.error("Expected XML Element, but got junk...")
1223 return
1224
1225 if self.alias() != None:
1226 if not self.alias() == xmlvalue.tagName:
1227 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1228 else:
1230 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1231
1232 # Catch XML <String /> by setting the value to a default
1233 if xmlvalue.firstChild == None:
1234 logger.debug("No value is given. Setting to default 0")
1236 else:
1237 self.valuevaluevalue = str(unicode(xmlvalue.firstChild.data))
1238
1239 def printOpen62541CCode_SubType(self, asIndirect=True):
1240 code = "UA_STRING_ALLOC(\"" + self.valuevaluevalue.encode('utf-8') + "\")"
1241 return code
1242
1248 self.__binTypeId____binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_XMLELEMENT
1249
1250 def printOpen62541CCode_SubType(self, asIndirect=True):
1251 code = "UA_XMLELEMENT_ALLOC(\"" + self.valuevaluevalue.encode('utf-8') + "\")"
1252 return code
1253
1259 self.__binTypeId____binTypeId____binTypeId__ = BUILTINTYPE_TYPEID_BYTESTRING
1260
1261 def parseXML(self, xmlvalue):
1262 # Expect <ByteString>value</ByteString> or
1263 # <Aliasname>value</Aliasname>
1264 if xmlvalue == None or xmlvalue.nodeType != xmlvalue.ELEMENT_NODE:
1265 logger.error("Expected XML Element, but got junk...")
1266 return
1267
1268 if self.alias() != None:
1269 if not self.alias() == xmlvalue.tagName:
1270 logger.warn("Expected an aliased XML field called " + self.alias() + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1271 else:
1273 logger.warn("Expected XML field " + self.stringRepresentationstringRepresentationstringRepresentation + " but got " + xmlvalue.tagName + " instead. This is a parsing error of opcua_value_t.__parseXMLSingleValue(), will try to continue anyway.")
1274
1275 # Catch XML <ByteString /> by setting the value to a default
1276 if xmlvalue.firstChild == None:
1277 logger.debug("No value is given. Setting to default 0")
1279 else:
1280 self.valuevaluevalue = str(unicode(xmlvalue.firstChild.data))
1281
1282 def printOpen62541CCode_SubType(self, asIndirect=True):
1283 bs = ""
1284 for line in self.valuevaluevalue:
1285 bs = bs + str(line).replace("\n","");
1286 outs = bs
1287 logger.debug("Encoded Bytestring: " + outs)
1288# bs = bs.decode('base64')
1289# outs = ""
1290# for s in bs:
1291# outs = outs + hex(ord(s)).upper().replace("0X", "\\x")
1292 code = "UA_STRING_ALLOC(\"" + outs + "\")"
1293 return code
setCodeInstanceName(self, recursionDepth, arrayIndex)
printOpen62541CCode_SubType_build(self, recursionDepth=0, arrayIndex=0)
printOpen62541CCode_SubType(self, asIndirect=True)
getTypeByString(self, stringName, encodingRule)
__parseXMLSingleValue(self, xmlvalue, alias=None, encodingPart=None)
printOpen62541CCode(self, bootstrapping=True)
getNextElementNode(xmlvalue)
#define str(a)