Skip to content

Commit

Permalink
large-file-upload: add option to set binary buffer size
Browse files Browse the repository at this point in the history
The command line argument -B/--binary-size can now be used to set the
binary buffer size that will be used when transmitting binary data to
Notehub. This gives more control to the user and is beneficial in case
there are any application/firmware issues with sending large binary data
  • Loading branch information
youssifsaeed1 committed Jul 25, 2024
1 parent bada153 commit c0e4bc9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions python-large-file-upload/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def parseCommandLineArgs():
p.add("-e", "--measure-elapsed-time", help="Measure how long the file transfer process takes", action='store_true')
p.add("--legacy", help="Use legacy method to upload file. Uses base64 encoding in web transaction payloads", action='store_true')

p.add("-B", "--binary-size", help="Size of binary data to send in each transaction.", type=int)

opts = p.parse_args()
hub_config = {}
Expand Down Expand Up @@ -124,6 +125,9 @@ def main():
fileName = os.path.basename(os.path.normpath(opts.file))
uploader.setFileName(fileName)

if opts.binary_size:
uploader.setBinaryBuffSize(opts.binary_size)

startTime = 0
fileSizeInBytes = 0
with open(opts.file, 'rb') as f:
Expand Down
16 changes: 13 additions & 3 deletions python-large-file-upload/notecardDataTransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def __init__(self, card, route, timeout=DEFAULT_WEB_TRANSACTION_TIMEOUT_SEC, pri
self.webReqRoot['route'] = route
self.webReqRoot['seconds'] = timeout
self._fileName = None

self._binaryBuffSize = None

def setFileName(self, fileName):
self._fileName = fileName

Expand Down Expand Up @@ -96,8 +97,13 @@ def _writeAndFlushBytes(self, data: io.IOBase):
binary_helpers.binary_store_reset(self._card)
rsp = self._sendRequest("card.binary")
max = rsp.get("max", 0)

buffer = bytearray(max)

if self._binaryBuffSize is not None:
buffSize = self._binaryBuffSize
else:
buffSize = max

buffer = bytearray(buffSize)
numBytes = data.readinto(buffer)

binary_helpers.binary_store_transmit(self._card, buffer[0:numBytes], 0)
Expand Down Expand Up @@ -127,6 +133,10 @@ def _unsetTempContinuousMode(self):
req = {"req":"hub.set", "off":True}
self._sendRequest(req)

def setBinaryBuffSize(self, binaryBuffSize):
self._binaryBuffSize = binaryBuffSize



import binascii
class BinaryDataUploaderLegacy(BinaryDataUploader):
Expand Down

0 comments on commit c0e4bc9

Please sign in to comment.