Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom type methods with examples #368

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

levkevi
Copy link
Contributor

@levkevi levkevi commented Nov 18, 2016

No description provided.

@@ -60,7 +60,8 @@ def wrapper(parent, *args):
parent = args[0]
args = args[1:]
result = func(self, parent, *[arg.Value for arg in args])

if isinstance(result, (list, tuple)):
return to_variant(*result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is good!

@@ -629,7 +629,6 @@ def to_binary(self):
def from_binary(data):
obj = ExtensionObject()
obj.TypeId = NodeId.from_binary(data)
obj.Encoding = uabin.Primitives.UInt8.unpack(data)
if obj.Encoding & (1 << 0):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is that? you cannot test for the value of Encoding without first unpacking?????

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I don't event remember removing that. I'm putting it back.

def register_extension_object(object_factory):
setattr(ObjectIds, "{}_Encoding_DefaultBinary".format(object_factory.__name__), object_factory.DEFAULT_BINARY)
ObjectIdNames[object_factory.DEFAULT_BINARY] = object_factory.__name__
ExtensionClasses[object_factory.DEFAULT_BINARY] = object_factory
Copy link
Member

@oroulet oroulet Nov 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have a file called ua_utils.py under common. Maybe you could put that method in uatypes.py or ua_prototcol_hans.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move it to that file.

#
# key_value = uabin.unpack_uatype_array(response[0].VariantType, io.BytesIO(response[0].to_binary()))
# self.assertEqual(key_value.key, "Key")
# self.assertEqual(key_value.value, "Value")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the issue with that test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this error:

`======================================================================
ERROR: test_decode_custom_object (tests_server.TestServer)

Traceback (most recent call last):
File "C:\Work\doc\Open_SCS\opcua-kev\python-opcua\tests\tests_common.py", line 762, in test_decode_custom_object
key_value = uabin.unpack_uatype_array(response[0].VariantType, io.BytesIO(response[0].to_binary()))
File ".\opcua\ua\ua_binary.py", line 305, in unpack_uatype_array
return [unpack_uatype(vtype, data) for _ in range(length)]
File ".\opcua\ua\ua_binary.py", line 305, in
return [unpack_uatype(vtype, data) for _ in range(length)]
File ".\opcua\ua\ua_binary.py", line 286, in unpack_uatype
return extensionobject_from_binary(data)
File ".\opcua\ua\uaprotocol_auto.py", line 16300, in extensionobject_from_binary
TypeId = NodeId.from_binary(data)
File ".\opcua\ua\uatypes.py", line 410, in from_binary
nid.NodeIdType = NodeIdType(encoding & 0b00111111)
File "C:\Python3\lib\enum.py", line 241, in call
return cls.new(cls, value)
File "C:\Python3\lib\enum.py", line 476, in new
raise ValueError("%r is not a valid %s" % (value, cls.name))
ValueError: 14 is not a valid NodeIdType`

It looks like a configuration error but I'm not sure about how to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just reenabled the tests, Maybe someone will know how to fix it?

# self.assertEqual(key_value.value, "Value")


class KeyValuePair(object):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be great to give that class a name like MyCustomStructure. It took me a while to understand that this was just an example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, I'll change that.

Changed the unittest custom error type so it is easier to know that it's a custom class.
def register_extension_object(object_factory):
setattr(ObjectIds, "{}_Encoding_DefaultBinary".format(object_factory.__name__), object_factory.DEFAULT_BINARY)
ObjectIdNames[object_factory.DEFAULT_BINARY] = object_factory.__name__
ExtensionClasses[object_factory.DEFAULT_BINARY] = object_factory
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw that it won't work if your NamespaceIndex is not 0. I'll see what I can do to change that.

@oroulet
Copy link
Member

oroulet commented Mar 19, 2017

#388 is now merged, it does th eground work to make possible what you wanted to do on client side (not server). There is now a method to register new structures. Unfortunately I do not have time currently to work on this, but if someone tries to use it I can help

@oroulet
Copy link
Member

oroulet commented Mar 19, 2017

examples/client_read-custom_structures.py

@oroulet
Copy link
Member

oroulet commented Sep 20, 2017

Note to myself: merge interesting commits from that pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants