@@ -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"
89+
90+ trid = int (trid_str )
7591
76- return "%R1P,0,0 :0"
92+ return f "%R1P,0,{ trid } :0"
7793
7894
7995# def open_serial(port: str, **kwargs: Any) -> DummyGeoComConnection:
@@ -328,6 +344,19 @@ def compose(self) -> ComposeResult:
328344 value = 9600 ,
329345 id = "select_baud"
330346 )
347+ yield Label ("Timeout" )
348+ yield Input (
349+ valid_empty = False ,
350+ value = "15" ,
351+ type = "integer" ,
352+ validators = [Timeout ()],
353+ id = "edit_timeout"
354+ )
355+ yield Button (
356+ "Update" ,
357+ id = "btn_update_timeout" ,
358+ disabled = True
359+ )
331360 with HorizontalGroup (id = "hg_buttons" ):
332361 yield Button ("Test Connection" , id = "btn_test_com" )
333362 yield Button (
@@ -401,8 +430,22 @@ def btn_test_com_pressed(self, event: Button.Pressed) -> None:
401430 )
402431 self .bell ()
403432 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
404443 try :
405- 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 :
406449 match self .query_one ("#select_protocol" , Select ).value :
407450 case Protocol .GEOCOM :
408451 ans = com .exchange (r"%R1Q,0:\r\n" )
@@ -440,9 +483,22 @@ def btn_connect_pressed(self, event: Button.Pressed) -> None:
440483 self .app .bell ()
441484 return
442485 baud = cast (int , self .query_one ("#select_baud" , Select ).value )
486+ timeout = self .query_one ("#edit_timeout" , Input )
487+ if not timeout .is_valid :
488+ self .notify (
489+ "Invalid timeout value given!" ,
490+ severity = "error" ,
491+ title = "Error"
492+ )
493+ self .app .bell ()
494+ return
443495 try :
444496 log = get_app_logger (self , port .value )
445- com = open_serial (port .value , speed = baud )
497+ com = open_serial (
498+ port .value ,
499+ speed = baud ,
500+ timeout = int (timeout .value )
501+ )
446502 match self .query_one ("#select_protocol" , Select ).value :
447503 case Protocol .GEOCOM :
448504 self .protocol = GeoCom (com , log )
@@ -457,6 +513,7 @@ def btn_connect_pressed(self, event: Button.Pressed) -> None:
457513 self .query_one ("#edit_com" , Input ).disabled = True
458514 self .query_one ("#select_protocol" , Select ).disabled = True
459515 self .query_one ("#select_baud" , Select ).disabled = True
516+ self .query_one ("#btn_update_timeout" , Button ).disabled = False
460517 self .query_one ("#tab_cmd" , TabPane ).disabled = False
461518
462519 self .notify ("Connection successful." , title = "Success" )
@@ -487,6 +544,7 @@ def btn_disconnect_pressed(self, event: Button.Pressed) -> None:
487544 self .query_one ("#edit_com" , Input ).disabled = False
488545 self .query_one ("#select_protocol" , Select ).disabled = False
489546 self .query_one ("#select_baud" , Select ).disabled = False
547+ self .query_one ("#btn_update_timeout" , Button ).disabled = True
490548 self .query_one ("#tab_cmd" , TabPane ).disabled = True
491549 self .sub_title = ""
492550 except Exception as e :
@@ -501,6 +559,37 @@ def btn_disconnect_pressed(self, event: Button.Pressed) -> None:
501559 self .notify ("Disconnected." , title = "Success" )
502560 self .bell ()
503561
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+
504593
505594class ComPort (Validator ):
506595 _WINCOM = re .compile (r"COM\d+" )
@@ -511,3 +600,11 @@ def validate(self, value: str) -> ValidationResult:
511600 return self .success ()
512601
513602 return self .failure ("Not a valid COM port" )
603+
604+
605+ class Timeout (Validator ):
606+ def validate (self , value : str ) -> ValidationResult :
607+ if value != "-" and int (value ) < 0 :
608+ return self .failure ("Timeout cannot be negative" )
609+
610+ return self .success ()
0 commit comments