diff options
author | Maxim Devaev <[email protected]> | 2023-07-10 03:07:53 +0300 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2023-07-31 01:53:33 +0300 |
commit | 8e2a5284183977d15c537ae5d26efd8fd7833cd6 (patch) | |
tree | 475b0d891b327e2cc550e7c884d69ad8d67df789 | |
parent | 2e6f0da14142e2f5c3bd548052fa9f189e2d00eb (diff) |
ch9329: reconnect logic
-rw-r--r-- | kvmd/plugins/hid/ch9329/__init__.py | 33 | ||||
-rw-r--r-- | kvmd/plugins/hid/ch9329/chip.py | 22 |
2 files changed, 35 insertions, 20 deletions
diff --git a/kvmd/plugins/hid/ch9329/__init__.py b/kvmd/plugins/hid/ch9329/__init__.py index 1a46c10e..e7f518d5 100644 --- a/kvmd/plugins/hid/ch9329/__init__.py +++ b/kvmd/plugins/hid/ch9329/__init__.py @@ -43,6 +43,7 @@ from ....validators.hw import valid_tty_speed from .. import BaseHid from .chip import ChipResponseError +from .chip import ChipConnection from .chip import Chip from .mouse import Mouse from .keyboard import Keyboard @@ -176,7 +177,6 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst logger = aioproc.settle("HID", "hid") while not self.__stop_event.is_set(): try: - # self.__chip.connect() self.__hid_loop() except Exception: logger.exception("Unexpected error in the run loop") @@ -185,28 +185,29 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst def __hid_loop(self) -> None: while not self.__stop_event.is_set(): try: - while not (self.__stop_event.is_set() and self.__cmd_queue.qsize() == 0): - if self.__reset_required_event.is_set(): + with self.__chip.connected() as conn: + while not (self.__stop_event.is_set() and self.__cmd_queue.qsize() == 0): + if self.__reset_required_event.is_set(): + try: + self.__set_state_busy(True) + # self.__process_request(conn, RESET) + finally: + self.__reset_required_event.clear() try: - self.__set_state_busy(True) - # self.__process_request(conn, RESET) - finally: - self.__reset_required_event.clear() - try: - cmd = self.__cmd_queue.get(timeout=0.1) - # get_logger(0).info(f"HID : cmd = {cmd}") - except queue.Empty: - self.__process_cmd(b"") - else: - self.__process_cmd(cmd) + cmd = self.__cmd_queue.get(timeout=0.1) + # get_logger(0).info(f"HID : cmd = {cmd}") + except queue.Empty: + self.__process_cmd(conn, b"") + else: + self.__process_cmd(conn, cmd) except Exception: self.clear_events() get_logger(0).exception("Unexpected error in the HID loop") time.sleep(2) - def __process_cmd(self, cmd: bytes) -> bool: # pylint: disable=too-many-branches + def __process_cmd(self, conn: ChipConnection, cmd: bytes) -> bool: # pylint: disable=too-many-branches try: - led_byte = self.__chip.xfer(cmd) + led_byte = conn.xfer(cmd) except ChipResponseError as err: self.__set_state_online(False) get_logger(0).info(err) diff --git a/kvmd/plugins/hid/ch9329/chip.py b/kvmd/plugins/hid/ch9329/chip.py index e6524f10..b8631ec0 100644 --- a/kvmd/plugins/hid/ch9329/chip.py +++ b/kvmd/plugins/hid/ch9329/chip.py @@ -21,6 +21,9 @@ import serial +import contextlib + +from typing import Generator # ===== @@ -29,10 +32,9 @@ class ChipResponseError(Exception): # ===== -class Chip: - def __init__(self, device_path: str, speed: int, read_timeout: float) -> None: - self.__tty = serial.Serial(device_path, speed, timeout=read_timeout) - self.__device_path = device_path +class ChipConnection: + def __init__(self, tty: serial.Serial) -> None: + self.__tty = tty def xfer(self, cmd: bytes) -> int: self.__send(cmd) @@ -66,3 +68,15 @@ class Chip: def __make_checksum(self, cmd: bytes) -> int: return (sum(cmd) % 256) + + +class Chip: + def __init__(self, device_path: str, speed: int, read_timeout: float) -> None: + self.__device_path = device_path + self.__speed = speed + self.__read_timeout = read_timeout + + @contextlib.contextmanager + def connected(self) -> Generator[ChipConnection, None, None]: # type: ignore + with serial.Serial(self.__device_path, self.__speed, timeout=self.__read_timeout) as tty: + yield ChipConnection(tty) |