Skip to content

Commit 81bb3b4

Browse files
committed
[terminal] added timeout updating
1 parent d16d4f2 commit 81bb3b4

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

src/instrumentman/terminal/app.py

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def __init__(self, port: str):
4747

4848
_CMD = re.compile(
4949
r"^%R1Q,"
50-
r"(?P<rpc>\d+):"
50+
r"(?P<rpc>\d+)"
51+
r"(?P<trid>,\d+)?:"
5152
r"(?:(?P<params>.*))?$"
5253
)
5354

@@ -66,14 +67,29 @@ def close(self) -> None:
6667
def send(self, message: str) -> None:
6768
return
6869

70+
def receive(self) -> str:
71+
return ""
72+
73+
def is_open(self) -> bool:
74+
return True
75+
76+
def reset(self) -> None:
77+
return
78+
6979
def exchange(self, cmd: str) -> str:
7080
if not self._CMD.match(cmd):
7181
return "%R1P,0,0:2"
7282

73-
if cmd == "%R1Q,5008:":
74-
return "%R1P,0,0:0,1996,'07','19','10','13','2f'"
83+
head, _ = cmd.split(":")
84+
match head.split(","):
85+
case [_, _, trid_str]:
86+
pass
87+
case _:
88+
trid_str = "0"
7589

76-
return "%R1P,0,0:0"
90+
trid = int(trid_str)
91+
92+
return f"%R1P,0,{trid}:0"
7793

7894

7995
# def open_serial(port: str, **kwargs: Any) -> DummyGeoComConnection:
@@ -336,6 +352,11 @@ def compose(self) -> ComposeResult:
336352
validators=[Timeout()],
337353
id="edit_timeout"
338354
)
355+
yield Button(
356+
"Update",
357+
id="btn_update_timeout",
358+
disabled=True
359+
)
339360
with HorizontalGroup(id="hg_buttons"):
340361
yield Button("Test Connection", id="btn_test_com")
341362
yield Button(
@@ -409,8 +430,22 @@ def btn_test_com_pressed(self, event: Button.Pressed) -> None:
409430
)
410431
self.bell()
411432
return
433+
baud = cast(int, self.query_one("#select_baud", Select).value)
434+
timeout = self.query_one("#edit_timeout", Input)
435+
if not timeout.is_valid:
436+
self.notify(
437+
"Invalid timeout value given!",
438+
severity="error",
439+
title="Error"
440+
)
441+
self.app.bell()
442+
return
412443
try:
413-
with open_serial(port.value) as com:
444+
with open_serial(
445+
port.value,
446+
speed=baud,
447+
timeout=int(timeout.value)
448+
) as com:
414449
match self.query_one("#select_protocol", Select).value:
415450
case Protocol.GEOCOM:
416451
ans = com.exchange(r"%R1Q,0:\r\n")
@@ -478,7 +513,7 @@ def btn_connect_pressed(self, event: Button.Pressed) -> None:
478513
self.query_one("#edit_com", Input).disabled = True
479514
self.query_one("#select_protocol", Select).disabled = True
480515
self.query_one("#select_baud", Select).disabled = True
481-
self.query_one("#edit_timeout", Input).disabled = True
516+
self.query_one("#btn_update_timeout", Button).disabled = False
482517
self.query_one("#tab_cmd", TabPane).disabled = False
483518

484519
self.notify("Connection successful.", title="Success")
@@ -509,7 +544,7 @@ def btn_disconnect_pressed(self, event: Button.Pressed) -> None:
509544
self.query_one("#edit_com", Input).disabled = False
510545
self.query_one("#select_protocol", Select).disabled = False
511546
self.query_one("#select_baud", Select).disabled = False
512-
self.query_one("#edit_timeout", Input).disabled = False
547+
self.query_one("#btn_update_timeout", Button).disabled = True
513548
self.query_one("#tab_cmd", TabPane).disabled = True
514549
self.sub_title = ""
515550
except Exception as e:
@@ -524,6 +559,37 @@ def btn_disconnect_pressed(self, event: Button.Pressed) -> None:
524559
self.notify("Disconnected.", title="Success")
525560
self.bell()
526561

562+
@on(Button.Pressed, "#btn_update_timeout")
563+
def btn_update_timeout_pressed(self, event: Button.Pressed) -> None:
564+
if self.protocol is None:
565+
return
566+
567+
edit = self.query_one("#edit_timeout", Input)
568+
if not edit.is_valid:
569+
self.notify(
570+
f"{edit.value} is not a valid timeout",
571+
title="Error",
572+
severity="error"
573+
)
574+
return
575+
576+
com = self.protocol._conn
577+
try:
578+
com._port.timeout = int( # type: ignore[attr-defined]
579+
edit.value
580+
)
581+
self.notify(
582+
"Timeout updated",
583+
title="Success",
584+
severity="information"
585+
)
586+
except Exception:
587+
self.notify(
588+
f"{edit.value} is not a valid timeout",
589+
title="Error",
590+
severity="error"
591+
)
592+
527593

528594
class ComPort(Validator):
529595
_WINCOM = re.compile(r"COM\d+")

src/instrumentman/terminal/app.tcss

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
.connect {
2-
grid-size: 2;
2+
grid-size: 3;
33
grid-columns: auto 1fr;
44
grid-rows: auto;
55
grid-gutter: 1;
66
}
77

8+
#edit_com {
9+
column-span: 2;
10+
}
11+
12+
#select_protocol {
13+
column-span: 2;
14+
}
15+
16+
#select_baud {
17+
column-span: 2;
18+
}
19+
820
#hg_buttons {
921
height: auto;
1022
}

0 commit comments

Comments
 (0)