-
Notifications
You must be signed in to change notification settings - Fork 659
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
base: master
Are you sure you want to change the base?
Conversation
…ustom objects Added a helper tool to be able to add a custom object type more easily
…to FreeOpcUa-master # Conflicts: # tests/tests_common.py
…orts Added an example that show how to use complex types
@@ -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) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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?????
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
#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 |
examples/client_read-custom_structures.py |
Note to myself: merge interesting commits from that pr |
No description provided.