fix tui
This commit is contained in:
@@ -13,8 +13,8 @@ from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from capture_csv import (
|
||||
CsvSession,
|
||||
decode_line,
|
||||
write_csv_row,
|
||||
)
|
||||
|
||||
PRESETS: list[tuple[str, float]] = [
|
||||
@@ -52,7 +52,7 @@ STATUS_RE = re.compile(
|
||||
SENSOR_RE = re.compile(r"ch(\d+):([\d.]+)C/(\d+)%|ch(\d+):ERR")
|
||||
|
||||
AUTOTUNE_MODE_RE = re.compile(
|
||||
r"autotune/(?P<phase>\w+) (?P<elapsed>\d+)s (?P<cycles>\d+/\d+)cyc pre>=(?P<pre>\d+)C"
|
||||
r"autotune/(?P<phase>[\w-]+) (?P<elapsed>\d+)s (?P<cycles>\d+/\d+)cyc pre>=(?P<pre>\d+)C"
|
||||
)
|
||||
|
||||
|
||||
@@ -155,8 +155,7 @@ class SerialWorker:
|
||||
self.state = state
|
||||
self.lock = lock
|
||||
self.stop = threading.Event()
|
||||
self._log_fh = None
|
||||
self._header_written = [False]
|
||||
self._csv: CsvSession | None = None
|
||||
self._thread: threading.Thread | None = None
|
||||
|
||||
def start(self) -> None:
|
||||
@@ -171,9 +170,9 @@ class SerialWorker:
|
||||
self.stop.set()
|
||||
if self._thread is not None:
|
||||
self._thread.join(timeout=1.5)
|
||||
if self._log_fh is not None:
|
||||
self._log_fh.close()
|
||||
self._log_fh = None
|
||||
if self._csv is not None:
|
||||
self._csv.close()
|
||||
self._csv = None
|
||||
|
||||
def send(self, command: str) -> None:
|
||||
if self.ser is None:
|
||||
@@ -186,20 +185,22 @@ class SerialWorker:
|
||||
if enabled and not self.state.csv_logging:
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
path = log_dir / f"dryer_{datetime.now():%Y%m%d_%H%M%S}.csv"
|
||||
self._log_fh = path.open("w", encoding="utf-8")
|
||||
self._header_written = [False]
|
||||
self._csv = CsvSession(path)
|
||||
self.state.csv_path = path
|
||||
self.state.csv_logging = True
|
||||
self.state.messages.append(f"CSV -> {path.name}")
|
||||
self.state.messages.append(f"CSV -> {path.name} (on status)")
|
||||
self.send("log on")
|
||||
elif not enabled and self.state.csv_logging:
|
||||
self.send("log off")
|
||||
self.state.csv_logging = False
|
||||
self.state.csv_path = None
|
||||
if self._log_fh is not None:
|
||||
self._log_fh.close()
|
||||
self._log_fh = None
|
||||
self.state.messages.append("CSV logging off")
|
||||
if self._csv is not None:
|
||||
rows = self._csv.row_count
|
||||
self._csv.close()
|
||||
self._csv = None
|
||||
self.state.messages.append(f"CSV logging off ({rows} rows)")
|
||||
else:
|
||||
self.state.messages.append("CSV logging off")
|
||||
|
||||
def _note(self, line: str) -> None:
|
||||
with self.lock:
|
||||
@@ -221,14 +222,14 @@ class SerialWorker:
|
||||
continue
|
||||
|
||||
if line.startswith("csv,") or line.startswith("csv_hdr,"):
|
||||
if self._log_fh is not None:
|
||||
write_csv_row(self._log_fh, line, self._header_written)
|
||||
continue
|
||||
|
||||
parsed = parse_status(line)
|
||||
if parsed:
|
||||
with self.lock:
|
||||
apply_status(self.state, parsed)
|
||||
if self._csv is not None:
|
||||
self._csv.write_status(parsed)
|
||||
continue
|
||||
|
||||
if line.startswith("target="):
|
||||
|
||||
Reference in New Issue
Block a user