-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client Read File Transfer Method (#372)
* add read file method * add read file example * refactor read file method parameters * update read file example * restore to original * add new ua file class * update ua file example with new class * updating ua file for more user control * add __aenter__ and __aexit__ * separate file read class * make uafile class python
- Loading branch information
Showing
3 changed files
with
60 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from asyncua import ua | ||
|
||
|
||
class UaFile: | ||
|
||
def __init__(self, file_node, open_mode): | ||
self._file_node = file_node | ||
self._handle = None | ||
if open_mode == 'r': | ||
self._init_open = ua.OpenFileMode.Read.value | ||
else: | ||
raise ValueError("file mode is not supported") | ||
|
||
async def __aenter__(self): | ||
self._handle = await self.open(self._init_open) | ||
return self | ||
|
||
async def __aexit__(self, exc_type, exc_value, traceback): | ||
return await self.close() | ||
|
||
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 close(self): | ||
""" close file method """ | ||
read_node = await self._file_node.get_child("Close") | ||
arg1 = ua.Variant(self._handle, ua.VariantType.UInt32) | ||
return await self._file_node.call_method(read_node, arg1) | ||
|
||
async def read(self): | ||
""" reads file contents """ | ||
size = await self.get_size() | ||
read_node = await self._file_node.get_child("Read") | ||
arg1 = ua.Variant(self._handle, ua.VariantType.UInt32) | ||
arg2 = ua.Variant(size, ua.VariantType.Int32) | ||
return await self._file_node.call_method(read_node, arg1, arg2) | ||
|
||
async def get_size(self): | ||
""" gets size of file """ | ||
size_node = await self._file_node.get_child("Size") | ||
return await size_node.read_value() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import asyncio | ||
from asyncua.client import Client | ||
from asyncua.client.ua_file import UaFile | ||
|
||
async def read_file(): | ||
""" read file example """ | ||
|
||
url = "opc.tcp://10.0.0.199:4840" | ||
async with Client(url=url) as client: | ||
file_node = client.get_node("ns=2;s=NameOfNode") | ||
async with UaFile(file_node, 'r') as ua_file: | ||
contents = await ua_file.read() # read file | ||
print(contents) | ||
|
||
asyncio.run(read_file()) |