def split_into_bytes(data: int, no_of_bytes: int = -1, little_endian: bool = False, signed: bool = False): """ This function will split a given integer into a integer list representing the bytes to be split; :param data: A integer that represents the value of the bytes to be split :param no_of_bytes: The number of bytes the integer to be converted into. If the no_of_bytes is too small the method returns None. :return: A integer list representing a bytes list version of the given integer """ byteorder_indicator = "little" if little_endian is True else "big" if no_of_bytes == -1: no_of_bytes = ceil(data / 255) try: byte_string = data.to_bytes(no_of_bytes, byteorder=byteorder_indicator, signed=signed) except OverflowError as e: PTLogger.error(e) return None return [i for i in bytearray(byte_string)]
def write_word(self, register_address: int, word_value: int, little_endian: bool = False, signed: bool = False): word_to_write = split_into_bytes( word_value, 2, little_endian=little_endian, signed=signed) if word_to_write is None: PTLogger.error( "Error splitting word into bytes list. Value: ", word_value) else: self.write_n_bytes(register_address, word_to_write)
def write_word(self, register_address: int, word_value: int, little_endian: bool, signed: bool): bytes_to_write = split_into_bytes( word_value, 2, little_endian=little_endian, signed=signed) if bytes_to_write is None: PTLogger.error( "Error splitting word into bytes list. Value: ", word_value) return self.write_n_bytes(register_address, bytes_to_write)
def run_command(command_str: str, timeout: int, check: bool = True, capture_output: bool = True, log_errors: bool = True) -> str: PTLogger.debug( f"Function: run_command(command_str={command_str}, timeout={timeout}, check={check}, capture_output={capture_output}, \ log_errors={log_errors})") resp_stdout = None try: resp = run(split(command_str), check=check, capture_output=capture_output, timeout=timeout, env=__get_env()) if capture_output: resp_stdout = str(resp.stdout, 'utf8') resp_stderr = str(resp.stderr, 'utf8') PTLogger.debug(f"run_command(" f"command_str='{command_str}', " f"timeout={timeout}, " f"check='{check}', " f"capture_output='{capture_output}'" f") stdout:\n{resp_stdout}") PTLogger.debug(f"run_command(" f"command_str='{command_str}', " f"timeout={timeout}, " f"check='{check}', " f"capture_output='{capture_output}'" f") stderr:\n{resp_stderr}") if not check: PTLogger.debug(f"run_command(" f"command_str='{command_str}', " f"timeout={timeout}, " f"check='{check}', " f"capture_output='{capture_output}'" f") exit code: {resp.returncode}") except (CalledProcessError, TimeoutExpired) as e: if log_errors: PTLogger.error(str(e)) raise e except Exception as e: if log_errors: PTLogger.error(str(e)) return resp_stdout
def __connect_to_socket(self): self.__zmq_context = zmq.Context() self.__zmq_socket = self.__zmq_context.socket(zmq.REQ) self.__zmq_socket.sndtimeo = _TIMEOUT_MS self.__zmq_socket.rcvtimeo = _TIMEOUT_MS try: self.__zmq_socket.connect("tcp://127.0.0.1:3782") PTLogger.debug("pt-device-manager request client is ready") except zmq.error.ZMQError as e: PTLogger.error("Error starting the request client: " + str(e)) PTLogger.info(format_exc()) raise
def __connect_to_socket(self): self.__zmq_context = zmq.Context() self.__zmq_socket = self.__zmq_context.socket(zmq.SUB) self.__zmq_socket.setsockopt_string(zmq.SUBSCRIBE, "") try: self.__zmq_socket.connect("tcp://127.0.0.1:3781") PTLogger.debug("pt-device-manager subscribe client is ready") except zmq.error.ZMQError as e: PTLogger.error("Error starting the subscribe client: " + str(e)) PTLogger.info(format_exc()) return False return True