mirror of
https://github.com/JoelBender/bacpypes
synced 2025-09-28 22:15:23 +08:00
bug with stream and record file initialization, clean up sample applications
This commit is contained in:
parent
79a1bee468
commit
39169bd5d7
|
@ -28,16 +28,15 @@ class LocalRecordAccessFileObject(FileObject):
|
|||
kwargs,
|
||||
)
|
||||
|
||||
# verify the file access method or provide it
|
||||
if 'fileAccessMethod' in kwargs:
|
||||
if kwargs['fileAccessMethod'] != 'recordAccess':
|
||||
raise ValueError("inconsistent file access method")
|
||||
else:
|
||||
kwargs['fileAccessMethod'] = 'recordAccess'
|
||||
|
||||
FileObject.__init__(self,
|
||||
fileAccessMethod='recordAccess',
|
||||
**kwargs
|
||||
)
|
||||
# continue with initialization
|
||||
FileObject.__init__(self, **kwargs)
|
||||
|
||||
def __len__(self):
|
||||
""" Return the number of records. """
|
||||
|
@ -66,15 +65,15 @@ class LocalStreamAccessFileObject(FileObject):
|
|||
kwargs,
|
||||
)
|
||||
|
||||
# verify the file access method or provide it
|
||||
if 'fileAccessMethod' in kwargs:
|
||||
if kwargs['fileAccessMethod'] != 'streamAccess':
|
||||
raise ValueError("inconsistent file access method")
|
||||
else:
|
||||
kwargs['fileAccessMethod'] = 'streamAccess'
|
||||
|
||||
FileObject.__init__(self,
|
||||
**kwargs
|
||||
)
|
||||
# continue with initialization
|
||||
FileObject.__init__(self, **kwargs)
|
||||
|
||||
def __len__(self):
|
||||
""" Return the number of octets in the file. """
|
||||
|
|
|
@ -29,16 +29,15 @@ class LocalRecordAccessFileObject(FileObject):
|
|||
kwargs,
|
||||
)
|
||||
|
||||
# verify the file access method or provide it
|
||||
if 'fileAccessMethod' in kwargs:
|
||||
if kwargs['fileAccessMethod'] != 'recordAccess':
|
||||
raise ValueError("inconsistent file access method")
|
||||
else:
|
||||
kwargs['fileAccessMethod'] = 'recordAccess'
|
||||
|
||||
FileObject.__init__(self,
|
||||
fileAccessMethod='recordAccess',
|
||||
**kwargs
|
||||
)
|
||||
# continue with initialization
|
||||
FileObject.__init__(self, **kwargs)
|
||||
|
||||
def __len__(self):
|
||||
""" Return the number of records. """
|
||||
|
@ -66,15 +65,15 @@ class LocalStreamAccessFileObject(FileObject):
|
|||
kwargs,
|
||||
)
|
||||
|
||||
# verify the file access method or provide it
|
||||
if 'fileAccessMethod' in kwargs:
|
||||
if kwargs['fileAccessMethod'] != 'streamAccess':
|
||||
raise ValueError("inconsistent file access method")
|
||||
else:
|
||||
kwargs['fileAccessMethod'] = 'streamAccess'
|
||||
|
||||
FileObject.__init__(self,
|
||||
**kwargs
|
||||
)
|
||||
# continue with initialization
|
||||
FileObject.__init__(self, **kwargs)
|
||||
|
||||
def __len__(self):
|
||||
""" Return the number of octets in the file. """
|
||||
|
|
|
@ -29,16 +29,15 @@ class LocalRecordAccessFileObject(FileObject):
|
|||
kwargs,
|
||||
)
|
||||
|
||||
# verify the file access method or provide it
|
||||
if 'fileAccessMethod' in kwargs:
|
||||
if kwargs['fileAccessMethod'] != 'recordAccess':
|
||||
raise ValueError("inconsistent file access method")
|
||||
else:
|
||||
kwargs['fileAccessMethod'] = 'recordAccess'
|
||||
|
||||
FileObject.__init__(self,
|
||||
fileAccessMethod='recordAccess',
|
||||
**kwargs
|
||||
)
|
||||
# continue with initialization
|
||||
FileObject.__init__(self, **kwargs)
|
||||
|
||||
def __len__(self):
|
||||
""" Return the number of records. """
|
||||
|
@ -66,15 +65,15 @@ class LocalStreamAccessFileObject(FileObject):
|
|||
kwargs,
|
||||
)
|
||||
|
||||
# verify the file access method or provide it
|
||||
if 'fileAccessMethod' in kwargs:
|
||||
if kwargs['fileAccessMethod'] != 'streamAccess':
|
||||
raise ValueError("inconsistent file access method")
|
||||
else:
|
||||
kwargs['fileAccessMethod'] = 'streamAccess'
|
||||
|
||||
FileObject.__init__(self,
|
||||
**kwargs
|
||||
)
|
||||
# continue with initialization
|
||||
FileObject.__init__(self, **kwargs)
|
||||
|
||||
def __len__(self):
|
||||
""" Return the number of octets in the file. """
|
||||
|
|
|
@ -40,54 +40,6 @@ _log = ModuleLogger(globals())
|
|||
# reference a simple application
|
||||
this_application = None
|
||||
|
||||
#
|
||||
# TestApplication
|
||||
#
|
||||
|
||||
@bacpypes_debugging
|
||||
class TestApplication(BIPSimpleApplication):
|
||||
|
||||
def request(self, apdu):
|
||||
if _debug: TestApplication._debug("request %r", apdu)
|
||||
|
||||
# save a copy of the request
|
||||
self._request = apdu
|
||||
|
||||
# forward it along
|
||||
BIPSimpleApplication.request(self, apdu)
|
||||
|
||||
def confirmation(self, apdu):
|
||||
if _debug: TestApplication._debug("confirmation %r", apdu)
|
||||
|
||||
if isinstance(apdu, Error):
|
||||
sys.stdout.write("error: %s\n" % (apdu.errorCode,))
|
||||
sys.stdout.flush()
|
||||
|
||||
elif isinstance(apdu, AbortPDU):
|
||||
apdu.debug_contents()
|
||||
|
||||
elif (isinstance(self._request, AtomicReadFileRequest)) and (isinstance(apdu, AtomicReadFileACK)):
|
||||
# suck out the record data
|
||||
if apdu.accessMethod.recordAccess:
|
||||
value = apdu.accessMethod.recordAccess.fileRecordData
|
||||
elif apdu.accessMethod.streamAccess:
|
||||
value = apdu.accessMethod.streamAccess.fileData
|
||||
TestApplication._debug(" - value: %r", value)
|
||||
|
||||
sys.stdout.write(repr(value) + '\n')
|
||||
sys.stdout.flush()
|
||||
|
||||
elif (isinstance(self._request, AtomicWriteFileRequest)) and (isinstance(apdu, AtomicWriteFileACK)):
|
||||
# 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()
|
||||
|
||||
#
|
||||
# TestConsoleCmd
|
||||
#
|
||||
|
@ -271,7 +223,7 @@ class TestConsoleCmd(ConsoleCmd):
|
|||
value = apdu.fileStartPosition
|
||||
elif apdu.fileStartRecord is not None:
|
||||
value = apdu.fileStartRecord
|
||||
TestApplication._debug(" - value: %r", value)
|
||||
TestConsoleCmd._debug(" - value: %r", value)
|
||||
|
||||
sys.stdout.write(repr(value) + '\n')
|
||||
sys.stdout.flush()
|
||||
|
@ -333,7 +285,7 @@ class TestConsoleCmd(ConsoleCmd):
|
|||
value = apdu.fileStartPosition
|
||||
elif apdu.fileStartRecord is not None:
|
||||
value = apdu.fileStartRecord
|
||||
TestApplication._debug(" - value: %r", value)
|
||||
TestConsoleCmd._debug(" - value: %r", value)
|
||||
|
||||
sys.stdout.write(repr(value) + '\n')
|
||||
sys.stdout.flush()
|
||||
|
|
|
@ -14,11 +14,10 @@ from bacpypes.consolelogging import ConfigArgumentParser
|
|||
|
||||
from bacpypes.core import run
|
||||
|
||||
from bacpypes.object import FileObject, register_object_type
|
||||
|
||||
from bacpypes.app import BIPSimpleApplication
|
||||
from bacpypes.service.device import LocalDeviceObject
|
||||
from bacpypes.service.file import FileServices
|
||||
from bacpypes.service.file import FileServices, \
|
||||
LocalRecordAccessFileObject, LocalStreamAccessFileObject
|
||||
|
||||
# some debugging
|
||||
_debug = 0
|
||||
|
@ -34,19 +33,17 @@ OCTET_COUNT = int(os.getenv('OCTET_COUNT', 4096))
|
|||
#
|
||||
|
||||
@bacpypes_debugging
|
||||
class LocalRecordAccessFileObject(FileObject):
|
||||
class TestRecordFile(LocalRecordAccessFileObject):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
""" Initialize a record accessed file object. """
|
||||
if _debug:
|
||||
LocalRecordAccessFileObject._debug("__init__ %r",
|
||||
TestRecordFile._debug("__init__ %r",
|
||||
kwargs,
|
||||
)
|
||||
FileObject.__init__(self,
|
||||
fileAccessMethod='recordAccess',
|
||||
**kwargs
|
||||
)
|
||||
LocalRecordAccessFileObject.__init__(self, **kwargs)
|
||||
|
||||
# create some test data
|
||||
self._record_data = [
|
||||
''.join(random.choice(string.ascii_letters)
|
||||
for i in range(RECORD_LEN)).encode('utf-8')
|
||||
|
@ -58,13 +55,13 @@ class LocalRecordAccessFileObject(FileObject):
|
|||
|
||||
def __len__(self):
|
||||
""" Return the number of records. """
|
||||
if _debug: LocalRecordAccessFileObject._debug("__len__")
|
||||
if _debug: TestRecordFile._debug("__len__")
|
||||
|
||||
return len(self._record_data)
|
||||
|
||||
def read_record(self, start_record, record_count):
|
||||
""" Read a number of records starting at a specific record. """
|
||||
if _debug: LocalRecordAccessFileObject._debug("read_record %r %r",
|
||||
if _debug: TestRecordFile._debug("read_record %r %r",
|
||||
start_record, record_count,
|
||||
)
|
||||
|
||||
|
@ -76,7 +73,7 @@ class LocalRecordAccessFileObject(FileObject):
|
|||
|
||||
def write_record(self, start_record, record_count, record_data):
|
||||
""" Write a number of records, starting at a specific record. """
|
||||
if _debug: LocalRecordAccessFileObject._debug("write_record %r %r %r",
|
||||
if _debug: TestRecordFile._debug("write_record %r %r %r",
|
||||
start_record, record_count, record_data,
|
||||
)
|
||||
|
||||
|
@ -98,41 +95,37 @@ class LocalRecordAccessFileObject(FileObject):
|
|||
# return where the 'writing' actually started
|
||||
return start_record
|
||||
|
||||
register_object_type(LocalRecordAccessFileObject)
|
||||
|
||||
#
|
||||
# Local Stream Access File Object Type
|
||||
#
|
||||
|
||||
@bacpypes_debugging
|
||||
class LocalStreamAccessFileObject(FileObject):
|
||||
class TestStreamFile(LocalStreamAccessFileObject):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
""" Initialize a stream accessed file object. """
|
||||
if _debug:
|
||||
LocalStreamAccessFileObject._debug("__init__ %r",
|
||||
TestStreamFile._debug("__init__ %r",
|
||||
kwargs,
|
||||
)
|
||||
FileObject.__init__(self,
|
||||
fileAccessMethod='streamAccess',
|
||||
**kwargs
|
||||
)
|
||||
LocalStreamAccessFileObject.__init__(self, **kwargs)
|
||||
|
||||
# create some test data
|
||||
self._file_data = ''.join(random.choice(string.ascii_letters)
|
||||
for i in range(OCTET_COUNT)).encode('utf-8')
|
||||
if _debug: LocalRecordAccessFileObject._debug(" - %d octets",
|
||||
if _debug: TestStreamFile._debug(" - %d octets",
|
||||
len(self._file_data),
|
||||
)
|
||||
|
||||
def __len__(self):
|
||||
""" Return the number of octets in the file. """
|
||||
if _debug: LocalStreamAccessFileObject._debug("__len__")
|
||||
if _debug: TestStreamFile._debug("__len__")
|
||||
|
||||
return len(self._file_data)
|
||||
|
||||
def read_stream(self, start_position, octet_count):
|
||||
""" Read a chunk of data out of the file. """
|
||||
if _debug: LocalStreamAccessFileObject._debug("read_stream %r %r",
|
||||
if _debug: TestStreamFile._debug("read_stream %r %r",
|
||||
start_position, octet_count,
|
||||
)
|
||||
|
||||
|
@ -144,7 +137,7 @@ class LocalStreamAccessFileObject(FileObject):
|
|||
|
||||
def write_stream(self, start_position, data):
|
||||
""" Write a number of octets, starting at a specific offset. """
|
||||
if _debug: LocalStreamAccessFileObject._debug("write_stream %r %r",
|
||||
if _debug: TestStreamFile._debug("write_stream %r %r",
|
||||
start_position, data,
|
||||
)
|
||||
|
||||
|
@ -169,8 +162,6 @@ class LocalStreamAccessFileObject(FileObject):
|
|||
# return where the 'writing' actually started
|
||||
return start_position
|
||||
|
||||
register_object_type(LocalStreamAccessFileObject)
|
||||
|
||||
#
|
||||
# __main__
|
||||
#
|
||||
|
@ -205,7 +196,7 @@ def main():
|
|||
this_device.protocolServicesSupported = services_supported.value
|
||||
|
||||
# make a record access file, add to the device
|
||||
f1 = LocalRecordAccessFileObject(
|
||||
f1 = TestRecordFile(
|
||||
objectIdentifier=('file', 1),
|
||||
objectName='RecordAccessFile1'
|
||||
)
|
||||
|
@ -213,7 +204,7 @@ def main():
|
|||
this_application.add_object(f1)
|
||||
|
||||
# make a stream access file, add to the device
|
||||
f2 = LocalStreamAccessFileObject(
|
||||
f2 = TestStreamFile(
|
||||
objectIdentifier=('file', 2),
|
||||
objectName='StreamAccessFile2'
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user