ch9329: reconnect logic

This commit is contained in:
Maxim Devaev 2023-07-10 03:07:53 +03:00
parent 2e6f0da141
commit 8e2a528418
2 changed files with 35 additions and 20 deletions

View File

@ -43,6 +43,7 @@ from ....validators.hw import valid_tty_speed
from .. import BaseHid from .. import BaseHid
from .chip import ChipResponseError from .chip import ChipResponseError
from .chip import ChipConnection
from .chip import Chip from .chip import Chip
from .mouse import Mouse from .mouse import Mouse
from .keyboard import Keyboard from .keyboard import Keyboard
@ -176,7 +177,6 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
logger = aioproc.settle("HID", "hid") logger = aioproc.settle("HID", "hid")
while not self.__stop_event.is_set(): while not self.__stop_event.is_set():
try: try:
# self.__chip.connect()
self.__hid_loop() self.__hid_loop()
except Exception: except Exception:
logger.exception("Unexpected error in the run loop") 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: def __hid_loop(self) -> None:
while not self.__stop_event.is_set(): while not self.__stop_event.is_set():
try: try:
while not (self.__stop_event.is_set() and self.__cmd_queue.qsize() == 0): with self.__chip.connected() as conn:
if self.__reset_required_event.is_set(): 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: try:
self.__set_state_busy(True) cmd = self.__cmd_queue.get(timeout=0.1)
# self.__process_request(conn, RESET) # get_logger(0).info(f"HID : cmd = {cmd}")
finally: except queue.Empty:
self.__reset_required_event.clear() self.__process_cmd(conn, b"")
try: else:
cmd = self.__cmd_queue.get(timeout=0.1) self.__process_cmd(conn, cmd)
# get_logger(0).info(f"HID : cmd = {cmd}")
except queue.Empty:
self.__process_cmd(b"")
else:
self.__process_cmd(cmd)
except Exception: except Exception:
self.clear_events() self.clear_events()
get_logger(0).exception("Unexpected error in the HID loop") get_logger(0).exception("Unexpected error in the HID loop")
time.sleep(2) 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: try:
led_byte = self.__chip.xfer(cmd) led_byte = conn.xfer(cmd)
except ChipResponseError as err: except ChipResponseError as err:
self.__set_state_online(False) self.__set_state_online(False)
get_logger(0).info(err) get_logger(0).info(err)

View File

@ -21,6 +21,9 @@
import serial import serial
import contextlib
from typing import Generator
# ===== # =====
@ -29,10 +32,9 @@ class ChipResponseError(Exception):
# ===== # =====
class Chip: class ChipConnection:
def __init__(self, device_path: str, speed: int, read_timeout: float) -> None: def __init__(self, tty: serial.Serial) -> None:
self.__tty = serial.Serial(device_path, speed, timeout=read_timeout) self.__tty = tty
self.__device_path = device_path
def xfer(self, cmd: bytes) -> int: def xfer(self, cmd: bytes) -> int:
self.__send(cmd) self.__send(cmd)
@ -66,3 +68,15 @@ class Chip:
def __make_checksum(self, cmd: bytes) -> int: def __make_checksum(self, cmd: bytes) -> int:
return (sum(cmd) % 256) 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)