Skip to content

Commit

Permalink
update to the new IOCB client API and new file services API
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBender committed Sep 1, 2016
1 parent 1253fc4 commit 368dc50
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 13 deletions.
118 changes: 113 additions & 5 deletions samples/ReadWriteFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,34 @@ def do_readrecord(self, args):
if _debug: TestConsoleCmd._debug(" - request: %r", request)

# give it to the application
this_application.request(request)
iocb = this_application.request(request)
if _debug: TestConsoleCmd._debug(" - iocb: %r", iocb)

# wait for it to complete
iocb.wait()

# do something for success
if iocb.ioResponse:
apdu = iocb.ioResponse

# should be an ack
if not isinstance(apdu, AtomicReadFileACK):
if _debug: TestConsoleCmd._debug(" - not an ack")
return

# suck out the record data
if apdu.accessMethod.recordAccess:
value = apdu.accessMethod.recordAccess.fileRecordData
elif apdu.accessMethod.streamAccess:
value = apdu.accessMethod.streamAccess.fileData
if _debug: TestConsoleCmd._debug(" - value: %r", value)

sys.stdout.write(repr(value) + '\n')
sys.stdout.flush()

# do something for error/reject/abort
if iocb.ioError:
sys.stdout.write(str(iocb.ioError) + '\n')

except Exception as error:
TestConsoleCmd._exception("exception: %r", error)
Expand Down Expand Up @@ -153,7 +180,34 @@ def do_readstream(self, args):
if _debug: TestConsoleCmd._debug(" - request: %r", request)

# give it to the application
this_application.request(request)
iocb = this_application.request(request)
if _debug: TestConsoleCmd._debug(" - iocb: %r", iocb)

# wait for it to complete
iocb.wait()

# do something for success
if iocb.ioResponse:
apdu = iocb.ioResponse

# should be an ack
if not isinstance(apdu, AtomicReadFileACK):
if _debug: TestConsoleCmd._debug(" - not an ack")
return

# suck out the record data
if apdu.accessMethod.recordAccess:
value = apdu.accessMethod.recordAccess.fileRecordData
elif apdu.accessMethod.streamAccess:
value = apdu.accessMethod.streamAccess.fileData
if _debug: TestConsoleCmd._debug(" - value: %r", value)

sys.stdout.write(repr(value) + '\n')
sys.stdout.flush()

# do something for error/reject/abort
if iocb.ioError:
sys.stdout.write(str(iocb.ioError) + '\n')

except Exception as error:
TestConsoleCmd._exception("exception: %r", error)
Expand Down Expand Up @@ -187,7 +241,34 @@ def do_writerecord(self, args):
if _debug: TestConsoleCmd._debug(" - request: %r", request)

# give it to the application
this_application.request(request)
iocb = this_application.request(request)
if _debug: TestConsoleCmd._debug(" - iocb: %r", iocb)

# wait for it to complete
iocb.wait()

# do something for success
if iocb.ioResponse:
apdu = iocb.ioResponse

# should be an ack
if not isinstance(apdu, AtomicWriteFileACK):
if _debug: TestConsoleCmd._debug(" - not an ack")
return

# suck out the record data
if apdu.fileStartPosition is not None:
value = apdu.fileStartPosition
elif apdu.fileStartRecord is not None:
value = apdu.fileStartRecord
TestApplication._debug(" - value: %r", value)

sys.stdout.write(repr(value) + '\n')
sys.stdout.flush()

# do something for error/reject/abort
if iocb.ioError:
sys.stdout.write(str(iocb.ioError) + '\n')

except Exception as error:
TestConsoleCmd._exception("exception: %r", error)
Expand Down Expand Up @@ -219,7 +300,34 @@ def do_writestream(self, args):
if _debug: TestConsoleCmd._debug(" - request: %r", request)

# give it to the application
this_application.request(request)
iocb = this_application.request(request)
if _debug: TestConsoleCmd._debug(" - iocb: %r", iocb)

# wait for it to complete
iocb.wait()

# do something for success
if iocb.ioResponse:
apdu = iocb.ioResponse

# should be an ack
if not isinstance(apdu, AtomicWriteFileACK):
if _debug: TestConsoleCmd._debug(" - not an ack")
return

# suck out the record data
if apdu.fileStartPosition is not None:
value = apdu.fileStartPosition
elif apdu.fileStartRecord is not None:
value = apdu.fileStartRecord
TestApplication._debug(" - value: %r", value)

sys.stdout.write(repr(value) + '\n')
sys.stdout.flush()

# do something for error/reject/abort
if iocb.ioError:
sys.stdout.write(str(iocb.ioError) + '\n')

except Exception as error:
TestConsoleCmd._exception("exception: %r", error)
Expand Down Expand Up @@ -247,7 +355,7 @@ def main():
)

# make a simple application
this_application = TestApplication(this_device, args.ini.address)
this_application = BIPSimpleApplication(this_device, args.ini.address)

# get the services supported
services_supported = this_application.get_services_supported()
Expand Down
20 changes: 12 additions & 8 deletions samples/ReadWriteFileServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from bacpypes.app import BIPSimpleApplication
from bacpypes.service.device import LocalDeviceObject
from bacpypes.service.file import FileServices

# some debugging
_debug = 0
Expand Down Expand Up @@ -61,9 +62,9 @@ def __len__(self):

return len(self._record_data)

def ReadFile(self, start_record, record_count):
def read_record(self, start_record, record_count):
""" Read a number of records starting at a specific record. """
if _debug: LocalRecordAccessFileObject._debug("ReadFile %r %r",
if _debug: LocalRecordAccessFileObject._debug("read_record %r %r",
start_record, record_count,
)

Expand All @@ -73,9 +74,9 @@ def ReadFile(self, start_record, record_count):
return end_of_file, \
self._record_data[start_record:start_record + record_count]

def WriteFile(self, start_record, record_count, record_data):
def write_record(self, start_record, record_count, record_data):
""" Write a number of records, starting at a specific record. """
if _debug: LocalRecordAccessFileObject._debug("WriteFile %r %r %r",
if _debug: LocalRecordAccessFileObject._debug("write_record %r %r %r",
start_record, record_count, record_data,
)

Expand Down Expand Up @@ -129,9 +130,9 @@ def __len__(self):

return len(self._file_data)

def ReadFile(self, start_position, octet_count):
def read_stream(self, start_position, octet_count):
""" Read a chunk of data out of the file. """
if _debug: LocalStreamAccessFileObject._debug("ReadFile %r %r",
if _debug: LocalStreamAccessFileObject._debug("read_stream %r %r",
start_position, octet_count,
)

Expand All @@ -141,9 +142,9 @@ def ReadFile(self, start_position, octet_count):
return end_of_file, \
self._file_data[start_position:start_position + octet_count]

def WriteFile(self, start_position, data):
def write_stream(self, start_position, data):
""" Write a number of octets, starting at a specific offset. """
if _debug: LocalStreamAccessFileObject._debug("WriteFile %r %r",
if _debug: LocalStreamAccessFileObject._debug("write_stream %r %r",
start_position, data,
)

Expand Down Expand Up @@ -193,6 +194,9 @@ def main():
# make a sample application
this_application = BIPSimpleApplication(this_device, args.ini.address)

# add the capability to server file content
this_application.add_capability(FileServices)

# get the services supported
services_supported = this_application.get_services_supported()
if _debug: _log.debug(" - services_supported: %r", services_supported)
Expand Down

0 comments on commit 368dc50

Please sign in to comment.