Skip to content

Commit

Permalink
updating ua file for more user control
Browse files Browse the repository at this point in the history
  • Loading branch information
jadamroth committed Dec 23, 2020
1 parent c533614 commit 32540c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
38 changes: 21 additions & 17 deletions asyncua/client/ua_file.py
Expand Up @@ -4,33 +4,37 @@ class UaFile:
def __init__(self, file_node):
self._file_node = file_node

async def read_file(self):
"""
:param file_node: node of file object (e.g. node = client.get_node("ns=2;s=nameOfNode")
"""
handle = await self._open_file(ua.OpenFileMode.Read.value)
size = await self._get_file_size()

read_node = await self._file_node.get_child("Read")
arg1 = ua.Variant(handle, ua.VariantType.UInt32)
arg2 = ua.Variant(size, ua.VariantType.Int32)
contents = await self._file_node.call_method(read_node, arg1, arg2)
await self._close_file(handle)
return contents

async def _open_file(self, open_mode):
async def open(self, open_mode):
""" open file method """
open_node = await self._file_node.get_child("Open")
arg = ua.Variant(open_mode, ua.VariantType.Byte)
return await self._file_node.call_method(open_node, arg)

async def _get_file_size(self):
async def get_size(self):
""" gets size of file """
size_node = await self._file_node.get_child("Size")
return await size_node.read_value()

async def _close_file(self, handle):
async def close(self, handle):
""" close file method """
read_node = await self._file_node.get_child("Close")
arg1 = ua.Variant(handle, ua.VariantType.UInt32)
return await self._file_node.call_method(read_node, arg1)

async def read(self, handle, size):
"""
:param handle: handle from open()
:param size: size of file from from get_size()
"""
read_node = await self._file_node.get_child("Read")
arg1 = ua.Variant(handle, ua.VariantType.UInt32)
arg2 = ua.Variant(size, ua.VariantType.Int32)
return await self._file_node.call_method(read_node, arg1, arg2)

async def read_once(self):
""" open, read, close in one operation """
handle = await self.open(ua.OpenFileMode.Read.value)
size = await self.get_size()
contents = await self.read(handle, size)
await self.close(handle)
return contents
13 changes: 12 additions & 1 deletion examples/client-file.py
@@ -1,4 +1,5 @@
import asyncio
from asyncua import ua
from asyncua.client import Client
from asyncua.client.ua_file import UaFile

Expand All @@ -9,7 +10,17 @@ async def read_file():
async with Client(url=url) as client:
file_node = client.get_node("ns=2;s=NameOfNode")
ua_file_client = UaFile(file_node)
contents = await ua_file_client.read_file()

# open(), read(), close() all in one operation
contents = await ua_file_client.read_once()
print(contents)

# handling all methods by the user
handle = await ua_file_client.open(ua.OpenFileMode.Read.value)
size = await ua_file_client.get_size()
contents = await ua_file_client.read(handle, size)
await ua_file_client.close(handle)
print(contents)


asyncio.run(read_file())

0 comments on commit 32540c9

Please sign in to comment.