def extensionobject_from_binary(data): """ Convert binary-coded ExtensionObject to a Python object. Returns an object, or None if TypeId is zero """ typeid = nodeid_from_binary(data) Encoding = ord(data.read(1)) body = None if Encoding & (1 << 0): length = Primitives.Int32.unpack(data) if length < 1: body = Buffer(b"") else: body = data.copy(length) data.skip(length) if typeid.Identifier == 0: return None elif typeid in ua.extension_object_classes: klass = ua.extension_object_classes[typeid] if body is None: raise UaError("parsing ExtensionObject {0} without data".format( klass.__name__)) return from_binary(klass, body) else: e = ua.ExtensionObject() e.TypeId = typeid e.Encoding = Encoding if body is not None: e.Body = body.read(len(body)) return e
def __init__(self): self.BooleanValue = True self.SByteValue = 0 self.ByteValue = 0 self.Int16Value = 0 self.UInt16Value = 0 self.Int32Value = 0 self.UInt32Value = 0 self.Int64Value = 0 self.UInt64Value = 0 self.FloatValue = 0 self.DoubleValue = 0 self.StringValue = '' self.DateTimeValue = datetime.utcnow() self.GuidValue = uuid.uuid4() self.ByteStringValue = b'' self.XmlElementValue = ua.XmlElement() self.NodeIdValue = ua.NodeId() self.ExpandedNodeIdValue = ua.ExpandedNodeId() self.QualifiedNameValue = ua.QualifiedName() self.LocalizedTextValue = ua.LocalizedText() self.StatusCodeValue = ua.StatusCode() self.VariantValue = ua.Variant() self.EnumerationValue = 0 self.StructureValue = ua.ExtensionObject() self.Number = ua.Variant() self.Integer = ua.Variant() self.UInteger = ua.Variant()
def test_unknown_extension_object(self): obj = ua.ExtensionObject() obj.Body = b'example of data in custom format' obj.TypeId = ua.NodeId.from_string('ns=3;i=42') data = ua.utils.Buffer(extensionobject_to_binary(obj)) obj2 = extensionobject_from_binary(data) self.assertEqual(type(obj2), ua.ExtensionObject) self.assertEqual(obj2.TypeId, obj.TypeId) self.assertEqual(obj2.Body, b'example of data in custom format')
def test_extension_object(self): obj = ua.ExtensionObject() obj2 = ua.ExtensionObject.from_binary(ua.utils.Buffer(obj.to_binary())) self.assertEqual(obj2.TypeId, obj2.TypeId) self.assertEqual(obj2.Body, obj2.Body)