Index: dialin/ui/hd_simulator.py =================================================================== diff -u -radae506afce35a0063c6c2baf7e8580986f3bee7 -rd9b45e5b52db2540a79cbc8795d82dda4fd3984b --- dialin/ui/hd_simulator.py (.../hd_simulator.py) (revision adae506afce35a0063c6c2baf7e8580986f3bee7) +++ dialin/ui/hd_simulator.py (.../hd_simulator.py) (revision d9b45e5b52db2540a79cbc8795d82dda4fd3984b) @@ -42,7 +42,7 @@ def __init__(self, can_interface: str = "can0", log_level: bool = None, console_out: bool = False, - passive_mode: bool = False, + passive_mode: bool = True, auto_response: bool = False): """ The HDSimulator constructor @@ -304,6 +304,31 @@ self.cmd_send_uf_treatment_response(1, 0, uf_volume) + def _handler_ui_first_check_in(self, message) -> None: + """ + Handler function to first check in to start the post + @param message: the check-in message + @return: None + """ + now = time.time() + # if the application is not checking-in (the simulator ) within 2 sec it means it has been stopped, + # so do the check-in again. + if now - self.checked_in_last > 5: + self.checked_in = False + + self.checked_in_last = now + + if self.checked_in: + return + + self.cmd_send_power_on_self_test_version_request() + for i in range(20): + self.cmd_send_hd_post(i, True, False) + sleep(0.1) + self.cmd_send_hd_post(0, True, True) + self.cmd_send_hd_operation_mode(4, 0) + self.checked_in = True + def _handler_ui_initiate_treatment(self, message): """ Handler function to start a treatment @@ -1879,21 +1904,19 @@ @param message: The ui version response message @return: None """ - major = struct.unpack('B', bytearray( - message['message'][self.START_POS_MAJOR:self.END_POS_MAJOR])) - minor = struct.unpack('B', bytearray( - message['message'][self.START_POS_MINOR:self.END_POS_MINOR])) - micro = struct.unpack('B', bytearray( - message['message'][self.START_POS_MICRO:self.END_POS_MICRO])) - build = struct.unpack('H', bytearray( - message['message'][self.START_POS_BUILD:self.END_POS_BUILD])) - compatibility = struct.unpack('H', bytearray( - message['message'][self.START_POS_COMPAT:self.END_POS_COMPAT])) + payload = message['message'] + index = DenaliMessage.PAYLOAD_START_INDEX + major, index = bytearray_to_byte(payload, index, False) + minor, index = bytearray_to_byte(payload, index, False) + micro, index = bytearray_to_byte(payload, index, False) + build, index = bytearray_to_short(payload, index, False) + compt, index = bytearray_to_integer(payload, index, False) - if all([len(each) > 0 for each in [major, minor, micro, build]]): - self.ui_version = f"v{major[0]}.{minor[0]}.{micro[0]}-{build[0]}-{compatibility[0]}" - self.logger.debug(f"UI VERSION: {self.ui_version}") + self.ui_version = f"v{major}.{minor}.{micro}-{build},{compt}" + self.logger.debug(f"UI VERSION: {self.ui_version}") + print("version: ", self.ui_version) + def cmd_send_hd_request_ui_version(self) -> None: """ send HD request for UI version Index: dialin/utils/conversions.py =================================================================== diff -u -radae506afce35a0063c6c2baf7e8580986f3bee7 -rd9b45e5b52db2540a79cbc8795d82dda4fd3984b --- dialin/utils/conversions.py (.../conversions.py) (revision adae506afce35a0063c6c2baf7e8580986f3bee7) +++ dialin/utils/conversions.py (.../conversions.py) (revision d9b45e5b52db2540a79cbc8795d82dda4fd3984b) @@ -100,3 +100,95 @@ if type(val) != int: raise ValueError("Expected integer but received {0} with type {1}".format(val, type(val))) return [(val >> bit) & 1 for bit in range(num_bits - 1, -1, -1)] + + +def bytearray_to_value(fmt: str, buf: bytes) -> int: + """ + Converts the buffer to the type of value specified in the frm as format + + @param fmt: string format to convert the buffer to. + @param buf: the buffer of bytes to be converted to the value + + @return: the converted value of buffer to the type of fmt if successful or None if not. + """ + value = None + try: + value = struct.unpack(fmt, bytearray(buf)) + except BaseException as err: + print("err: ", err) + + if value is not None and len(value) > 0: + value = value[0] + + return value + + +def bytearray_to_byte(buffer: bytes, index: int, signed: bool = True) -> [int, int]: + """ + Converts the buffer of bytes to a value of one(1) byte integer + + @param buffer: (bytes) the source buffer + @param index: (int) the start index of reading the source buffer + @param signed: (bool) convert to signed or unsigned value + @return: pair of the [value and incremented index by the length. + """ + length = 1 # for a byte + value = bytearray_to_value("b" if signed else "B", buffer[index:index + length]) + return value, index + length + + +def bytearray_to_short(buffer: bytes, index: int, signed: bool = True) -> [int, int]: + """ + Converts the buffer of bytes to a value of two(2) byte integer + @param buffer: (bytes) the source buffer + @param index: (int) the start index of reading the source buffer + @param signed: (bool) convert to signed or unsigned value + @return: pair of the [value and incremented index by the length. + """ + length = 2 # for a short + value = bytearray_to_value("h" if signed else "H", buffer[index:index + length]) + return value, index + length + + +def bytearray_to_integer(buffer: bytes, index: int, signed: bool = True) -> [int, int]: + """ + Converts the buffer of bytes to a value of four(4) byte integer + + @param buffer: (bytes) the source buffer + @param index: (int) the start index of reading the source buffer + @param signed: (bool) convert to signed or unsigned value + @return: pair of the [value and incremented index by the length. + """ + length = 4 # for a integer + value = bytearray_to_value("i" if signed else "I", buffer[index:index + length]) + return value, index + length + + +def bytearray_to_long(buffer: bytes, index: int, signed: bool = True) -> [int, int]: + """ + Converts the buffer of bytes to a value of eight(8) byte integer + + @param buffer: (bytes) the source buffer + @param index: (int) the start index of reading the source buffer + @param signed: (bool) convert to signed or unsigned value + @return: pair of the [value and incremented index by the length. + """ + length = 8 # for a long + value = bytearray_to_value("q" if signed else "Q", buffer[index:index + length]) + return value, index + length + + +def bytearray_to_float(buffer: bytes, index: int, is_double: bool = False) -> [float, int]: + """ + Converts the buffer of bytes to a value of floating point. + + @param buffer: (bytes) the source buffer + @param index: (int) the start index of reading the source buffer + @param is_double: (bool) convert the value to eight(8) byte floating point if is_double is True, + or four(4) bytes otherwise. + @return: pair of the [value and incremented index by the length. + """ + length = 4 if is_double else 8 + value = bytearray_to_value("f" if is_double else "d", buffer[index:index + length]) + return value, index + length +