FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.36k stars 658 forks source link

FileType Objects #625

Open MikelAyani opened 6 years ago

MikelAyani commented 6 years ago

Hi, I can see that FileType object type is defined in the server, but I wonder if it is fully implemented and if someone has an example of how to use it, write/read operations... Regards, Mikel

zerox1212 commented 6 years ago

I do not think it's implemented unless the file is stored in memory as binary data. I have not read the OPC UA spec on how this should work either.

Note that nearly all types will be in the server because it's built from the OPC UA spec XML. That doesn't mean there is code to handle them all.

oroulet commented 6 years ago

What do you mean by fully implemented? As far as I know a file type is just a data type and you encode it as bytestring I guess

zerox1212 commented 6 years ago

I don't remember the spec, was there a file service for disk operations defined?

MikelAyani commented 6 years ago

Thanks for the answers. What I ask is if the methods are implemented. I can see the objecttype is defined and its properties and methods included. But when I create an instance and call for example the Open method I get an error.

oroulet commented 6 years ago

Object type is just a type you need to instantiate it somewhere in your address space and implement the method

On Thu, Jun 14, 2018, 21:31 MikelAyani notifications@github.com wrote:

Thanks for the answers. What I ask is if the methods are implemented. I can see the objecttype is defined and its properties and methods included. But when I create an instance and call for example the Open method I get an error.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/FreeOpcUa/python-opcua/issues/625#issuecomment-397412204, or mute the thread https://github.com/notifications/unsubscribe-auth/ACcfzve3w70lL3bBXWhdA8-JajxBKRaqks5t8rofgaJpZM4Um-Oo .

zerox1212 commented 6 years ago

I see. In that case you should be able to look at how methods are done in the example code and implement the Open UA method yourself.

scareynz commented 6 years ago

sorry so just to confirm, we have to define all of the methods for file type, such as read, write, open etc. in the python server?

Cheers

MikelAyani commented 6 years ago

As I understand yes.

scareynz commented 6 years ago

Thanks for all the help, this SDK worked brilliantly for our university project and wouldn't have been able to do it without the opensource community so thank you.

LilacBlue commented 6 years ago

After reading your discussion I'm still very confused. I have little experience in Python and OPC UA in general, but I have to implement an OPC UA server which will offer the possibility to download pdf and graphical files. I couldn't find any example of such implementation. Is there any method such as: my_object.add_variable(idx, "My document", "myDocument.pdf", ua.VariantType.FileType) that I could use for offering files or am I getting it all wrong and there is another way to do it? I will highly appreciate any help.

zerox1212 commented 6 years ago

You would have to read the OPC UA spec on how to handle files. I tried to go search the spec but OPC Foundation website is going really slow and I can't login.

As a work around you could use the existing library to make a method which returns a bytestring of the PDF. then your client can turn the bytestring back into a PDF file.

oroulet commented 6 years ago

File type is not a binary type. Send your document as bytestring

MikelAyani commented 6 years ago

Hi LilacBlue, Something like this bellow does the trick for us. We do not use the standard FileType, we create our own objecttype in which we add the next two methods (not exactly because I removed some specific code) and some properties. In the example bellow the FILE_PATH is hard-coded but you can use one of the properties to set the file path. I hope it helps!

`
FILE_PATH = 'C:/Temp/test.pdf'

@uamethod
def file_Get(self, nodeid:ua.VariantType.NodeId) -> bytes:
    """ Method to get a file.
    :param nodeid: file object nodeid
    :returns: File stream
    """
    try:
        with open(FILE_PATH, 'rb') as f:
            content = f.read()
        return content    
    except Exception as e:
        print('Failed to read file: {0}'.format(e))

@uamethod
def file_Write(self, nodeid: ua.VariantType.NodeId, file_stream:bytes) -> bool:
    """ Method to write a file.
    :param nodeid: file object nodeid
    :param file_stream: File content bytes stream
    :returns: Success
    """
    try:
        with open(FILE_PATH, 'wb') as f:
            f.write(file_stream)
        return True
    except Exception as e:
        print('Failed to write asset file: {0}'.format(e))
    return False

`