You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This isn't an issue, but to help anyone else who comes along looking for an example of reading and writing to Opcua File Objects like in the opcua docs.
I didn't see any other examples or issues of anyone implementing it, so I thought it might be useful. I'm new to this library and don't know the proper structure or else I'd submit a pull request to add the feature.
I didn't test the write method below because I didn't need it personally, but I think it should work (or at least be solvable if it isn't)
Thanks for the library
fromasyncuaimportuaclassOpcuaFile:
""" class object for reading files in opcua """def__init__(self, client, node=None, index=None, name_of_node=None):
""" :param client: opcua client (e.g. async with Client(url=url) as client) :param node: node of file object (e.g. node = client.get_node("ns=2;s=nameOfNode") :param index: namespace, integer (ns=2) :param name_of_node: string, name of the node """self._client=client# set nodeifnodeisnotNone:
self._node=nodeelifindexisnotNoneandname_of_nodeisnotNone:
self._node=client.get_node("ns="+str(index) +";s="+name_of_node)
else:
raiseException("missing class init arguments")
asyncdefopen(self, open_mode):
""" open file method """open_node=awaitself._node.get_child("Open")
arg=ua.Variant(open_mode, ua.VariantType.Byte)
returnawaitself._node.call_method(open_node, arg)
asyncdefget_size(self):
""" gets size of config """size_node=awaitself._node.get_child("Size")
size=self._client.get_node(size_node)
returnawaitsize.read_value()
asyncdefread(self):
""" read file method, returns contents as file """handle=awaitself.open(ua.OpenFileMode.Read.value)
size=awaitself.get_size()
read_node=awaitself._node.get_child("Read")
arg1=ua.Variant(handle, ua.VariantType.UInt32)
arg2=ua.Variant(size, ua.VariantType.Int32)
contents=awaitself._node.call_method(read_node, arg1, arg2)
awaitself.close(handle)
returncontentsasyncdefwrite(self, file_contents):
""" writes contents to file """handle=awaitself.open(ua.OpenFileMode.Write.value)
write_node=awaitself._node.get_child("Write")
arg1=ua.Variant(handle, ua.VariantType.UInt32)
arg2=ua.Variant(file_contents, ua.VariantType.String)
awaitself._node.call_method(write_node, arg1, arg2)
awaitself.close(handle)
asyncdefclose(self, handle):
""" close file method """read_node=awaitself._node.get_child("Close")
arg1=ua.Variant(handle, ua.VariantType.UInt32)
returnawaitself._node.call_method(read_node, arg1)
I should note that are other ways to optimize this, such as adding the Identifiers (e.g. Identifiers.FileType_Open is i=11580), so we don't have to lookup the node by name each time. If I get around to it, I'll update
This isn't an issue, but to help anyone else who comes along looking for an example of reading and writing to Opcua File Objects like in the opcua docs.
I didn't see any other examples or issues of anyone implementing it, so I thought it might be useful. I'm new to this library and don't know the proper structure or else I'd submit a pull request to add the feature.
I didn't test the write method below because I didn't need it personally, but I think it should work (or at least be solvable if it isn't)
Thanks for the library
Then call with something like this
The text was updated successfully, but these errors were encountered: