serial hid: clear queue on error

This commit is contained in:
Devaev Maxim 2020-10-13 19:47:30 +03:00
parent bee33f2df6
commit 48666a6741

View File

@ -344,7 +344,8 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
except queue.Empty: except queue.Empty:
self.__process_command(tty, b"\x01\x00\x00\x00\x00") # Ping self.__process_command(tty, b"\x01\x00\x00\x00\x00") # Ping
else: else:
self.__process_command(tty, event.make_command()) if not self.__process_command(tty, event.make_command()):
self.clear_events()
except serial.SerialException as err: except serial.SerialException as err:
if err.errno == errno.ENOENT: if err.errno == errno.ENOENT:
@ -361,10 +362,10 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
def __get_serial(self) -> serial.Serial: def __get_serial(self) -> serial.Serial:
return serial.Serial(self.__device_path, self.__speed, timeout=self.__read_timeout) return serial.Serial(self.__device_path, self.__speed, timeout=self.__read_timeout)
def __process_command(self, tty: serial.Serial, command: bytes) -> None: def __process_command(self, tty: serial.Serial, command: bytes) -> bool:
self.__process_request(tty, self.__make_request(command)) return self.__process_request(tty, self.__make_request(command))
def __process_request(self, tty: serial.Serial, request: bytes) -> None: # pylint: disable=too-many-branches def __process_request(self, tty: serial.Serial, request: bytes) -> bool: # pylint: disable=too-many-branches
logger = get_logger() logger = get_logger()
errors: List[str] = [] errors: List[str] = []
runtime_errors = False runtime_errors = False
@ -395,7 +396,7 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
raise _FatalRequestError("No previous command state inside HID, seems it was rebooted", online=True) raise _FatalRequestError("No previous command state inside HID, seems it was rebooted", online=True)
elif code == 0x20: # Done elif code == 0x20: # Done
self.__state_flags.update(online=True) self.__state_flags.update(online=True)
return return True
elif code & 0x80: # Pong with leds elif code & 0x80: # Pong with leds
self.__state_flags.update( self.__state_flags.update(
online=True, online=True,
@ -403,7 +404,7 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
scroll=bool(code & 0x00000010), scroll=bool(code & 0x00000010),
num=bool(code & 0x00000100), num=bool(code & 0x00000100),
) )
return return True
else: else:
raise _TempRequestError(f"Invalid response from HID: request={request!r}; code=0x{code:02X}") raise _TempRequestError(f"Invalid response from HID: request={request!r}; code=0x{code:02X}")
@ -430,6 +431,7 @@ class Plugin(BaseHid, multiprocessing.Process): # pylint: disable=too-many-inst
logger.error(msg) logger.error(msg)
if not (common_retries and read_retries): if not (common_retries and read_retries):
logger.error("Can't process HID request due many errors: %r", request) logger.error("Can't process HID request due many errors: %r", request)
return False
def __send_request(self, tty: serial.Serial, request: bytes) -> bytes: def __send_request(self, tty: serial.Serial, request: bytes) -> bytes:
if not self.__noop: if not self.__noop: