1
0
mirror of https://github.com/Rhinemann/IoT-Systems.git synced 2026-03-14 20:50:39 +02:00

Refactor row reading logic for clarity and efficiency

This commit is contained in:
Олександр Гуранець 2026-02-25 11:50:56 +02:00
parent 3e0b4762ef
commit fe66df9b8c

View File

@ -50,8 +50,8 @@ class FileDatasource:
if not self._started: if not self._started:
raise RuntimeError("Datasource is not started. Call startReading() before read().") raise RuntimeError("Datasource is not started. Call startReading() before read().")
acc_row = self._next_acc_row() acc_row = self._get_next_row(self._acc_reader, self._acc_buf)
gps_row = self._next_gps_row() gps_row = self._get_next_row(self._gps_reader, self._gps_buf)
acc = self._parse_acc(acc_row) acc = self._parse_acc(acc_row)
gps = self._parse_gps(gps_row) gps = self._parse_gps(gps_row)
@ -122,44 +122,24 @@ class FileDatasource:
self._gps_reader, expected_cols=2, header_tokens=("longitude", "latitude") self._gps_reader, expected_cols=2, header_tokens=("longitude", "latitude")
) )
def _next_acc_row(self) -> List[str]: def _get_next_row(self, reader, buffer) -> List[str]:
if self._acc_reader is None: """Get the next valid row from the reader or buffer."""
raise RuntimeError("Accelerometer reader is not initialized.") if reader is None:
raise RuntimeError("Reader is not initialized.")
while True: while True:
if self._acc_buf is not None: if buffer is not None:
row = self._acc_buf row = buffer
self._acc_buf = None buffer = None
else: else:
row = next(self._acc_reader, None) row = next(reader, None)
if row is None: if row is None:
# EOF -> rewind & continue # EOF -> rewind & continue
self._rewind_acc() self._rewind_acc() if reader == self._acc_reader else self._rewind_gps()
continue continue
if not row or not any(row): if not row or not any(cell.strip() for cell in row):
continue
return row
def _next_gps_row(self) -> List[str]:
if self._gps_reader is None:
raise RuntimeError("GPS reader is not initialized.")
while True:
if self._gps_buf is not None:
row = self._gps_buf
self._gps_buf = None
else:
row = next(self._gps_reader, None)
if row is None:
# EOF -> rewind & continue
self._rewind_gps()
continue
if not row or not any(row):
continue continue
return row return row
@ -177,7 +157,7 @@ class FileDatasource:
if first and any(first): if first and any(first):
break break
norm = [c.lower() for c in first] norm = [c.strip().lower() for c in first]
# Header if it contains the expected tokens # Header if it contains the expected tokens
if all(tok in norm for tok in header_tokens): if all(tok in norm for tok in header_tokens):