Improve log download: remove empty records, sort by timestamp

To make Plotjuggler not to warn about unsorted records everytime
This commit is contained in:
Oleg Kalachev
2024-02-18 01:23:33 +03:00
parent 4eec63adfa
commit 455729fdb4
2 changed files with 9 additions and 3 deletions

View File

@@ -9,8 +9,7 @@ PORT = os.environ['PORT']
DIR = os.path.dirname(os.path.realpath(__file__))
dev = serial.Serial(port=PORT, baudrate=115200, timeout=0.5)
log = open(f'{DIR}/log/{datetime.datetime.now().isoformat()}.csv', 'wb')
lines = []
print('Downloading log...')
count = 0
@@ -19,8 +18,14 @@ while True:
line = dev.readline()
if not line:
break
log.write(line)
lines.append(line)
count += 1
print(f'\r{count} lines', end='')
# sort by timestamp
header = lines.pop(0)
lines.sort(key=lambda line: float(line.split(b',')[0]))
log = open(f'{DIR}/log/{datetime.datetime.now().isoformat()}.csv', 'wb')
log.writelines([header] + lines)
print(f'\nWritten {os.path.relpath(log.name, os.curdir)}')