def _thread_run(self): while True: item = self._watchdog_queue.get() try: if self._reader_thread is None: break if item is self._QUEUE_STOP: break elif item is self._QUEUE_CHECK: if self._reader_thread.alive: continue try: self._reader_thread.close() except Exception: logger.error( "Unable to cleanly close the Serial connection", exc_info=True, ) self._reader_thread, self._transport = self._new_reader_thread( ) except Exception: # `_thread_run` should not raise logger.error("Error in the Arduino connection watchdog thread", exc_info=True) finally: self._watchdog_queue.task_done()
def exec_shell_command(shell_command: str, timeout: int = 5) -> str: try: p = subprocess.run( shell_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, check=True, timeout=timeout, ) out = p.stdout.decode("ascii") err = p.stderr.decode().strip() if err: logger.warning( "Shell command '%s' executed successfully, but printed to stderr:\n%s", shell_command, err, ) return out except subprocess.CalledProcessError as e: ec = e.returncode out = e.stdout.decode().strip() err = e.stderr.decode().strip() logger.error( "Shell command '%s' failed (exit code %s):\nstdout:\n%s\nstderr:\n%s\n", shell_command, ec, out, err, ) raise
def handle_line(self, line: str) -> None: try: message = json.loads(line) self._arduino_connection._incoming_message(message) except Exception: # `handle_line` should not raise exceptions logger.error( "Unable to parse the status line from Arduino as json: %r", line, exc_info=True, )