Skip to content

Commit

Permalink
file: add support for stream read progress and end of stream
Browse files Browse the repository at this point in the history
  • Loading branch information
bugobliterator authored and tridge committed Aug 2, 2023
1 parent 9e65bcc commit 59dcecd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 8 additions & 0 deletions dronecan/driver/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,13 @@ def set_signing_passphrase(self, passphrase):
'''set MAVLink2 signing passphrase'''
pass

def stream_progress(self):
'''stream progress of the current stream'''
pass

def end_of_stream(self):
'''end of stream'''
pass

def send(self, message_id, message, extended=False, canfd=False):
self.send_frame(CANFrame(message_id, message, extended, canfd=canfd))
26 changes: 24 additions & 2 deletions dronecan/driver/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ def __init__(self, filename, **kwargs):
super(file, self).__init__()
self.filename = filename
self.start_file_monotonic_ts = 0
self.speedup = kwargs.get('speedup', 1)
# check if read only option selected
if kwargs.get('readonly', False):
self.readonly = True
self.file = open(filename, 'rb')
self.curr_frame = None
self.start_monotonic_ts = time.monotonic()
self.file_size = os.path.getsize(filename)
self.set_first_and_last_timestamp()
self.eof = False
else:
self.readonly = False
self.file = open(filename, 'wb')
Expand All @@ -62,6 +65,9 @@ def __del__(self):
self.close()

def _read_frame(self):
if (self.file.tell() >= self.file_size):
self.eof = True
return None, None
# read header
header = self.file.read(20)
if len(header) < 20:
Expand Down Expand Up @@ -102,6 +108,7 @@ def set_first_and_last_timestamp(self):
if frame is None:
break
# seek to start of file
self.eof = False
self.file.seek(0, os.SEEK_SET)

def get_start_timestamp(self):
Expand All @@ -113,8 +120,20 @@ def get_last_timestamp(self):
def set_start_timestamp(self, timestamp):
self.start_monotonic_ts = timestamp

def receive(self, timeout=None):
def end_of_stream(self):
if not self.readonly:
return False
return self.eof

def stream_progress(self):
'''stream progress of the current stream'''
if not self.readonly or self.eof:
return 100
# return file seek position vs file size
return (self.file.tell()/self.file_size)*100

def receive(self, timeout=None):
if not self.readonly or self.eof:
if timeout is not None:
time.sleep(timeout)
return None
Expand All @@ -125,7 +144,10 @@ def receive(self, timeout=None):
self.curr_frame, _ = self._read_frame()
if self.curr_frame is None:
return None
time_to_next_frame = self.curr_frame.ts_monotonic - curr_time
if self.speedup == -1:
time_to_next_frame = 0
else:
time_to_next_frame = (self.curr_frame.ts_monotonic - curr_time)/self.speedup
if time_to_next_frame > 0:
if timeout is not None and time_to_next_frame <= timeout:
# sleep until next frame
Expand Down

0 comments on commit 59dcecd

Please sign in to comment.