Index: dialin/dg/dialysate_generator.py =================================================================== diff -u -re193e10da7d6c16158592c1aae7ebe1221dd0cde -rb8e4caa6515a773d560de2a2663541db67286f80 --- dialin/dg/dialysate_generator.py (.../dialysate_generator.py) (revision e193e10da7d6c16158592c1aae7ebe1221dd0cde) +++ dialin/dg/dialysate_generator.py (.../dialysate_generator.py) (revision b8e4caa6515a773d560de2a2663541db67286f80) @@ -45,7 +45,7 @@ from .valves import DGValves from .voltages import DGVoltages from .events import DGEvents -from .sw_config import DGSoftwareConfigs +from .sw_configs import DGSoftwareConfigs from ..common.msg_defs import MsgIds, MsgFieldPositions from enum import unique from .constants import NO_RESET Index: dialin/dg/sw_configs.py =================================================================== diff -u --- dialin/dg/sw_configs.py (revision 0) +++ dialin/dg/sw_configs.py (revision b8e4caa6515a773d560de2a2663541db67286f80) @@ -0,0 +1,289 @@ + + +import struct +import time +from collections import OrderedDict +from enum import unique +from logging import Logger +from time import sleep + +from ..common.msg_defs import MsgIds, MsgFieldPositions +from ..protocols.CAN import DenaliMessage, DenaliChannels +from ..utils.base import AbstractSubSystem, DialinEnum, publish +from ..utils.nv_ops_utils import NVOpsUtils, NVUtilsObserver + + +@unique +class DGSWConfigs(DialinEnum): + # NOTE: NUM_OF enum has been removed because it should be a part of the software configuration + # structure since the members of this class is for looped to create the dictionary automatically + SW_CONFIG_DISABLE_HEATERS_MONITOR = 0 + SW_CONFIG_THD_USING_TRO_CONNECTOR = 1 + SW_CONFIG_DISABLE_CAL_CHECK = 2 + SW_CONFIG_ALARMS_DEBUG = 3 + SW_CONFIG_DISABLE_RO_PUMP_MONITOR = 4 + SW_CONFIG_DISABLE_RO_RATIO_CHECK = 5 + + +class DGSoftwareConfigs(AbstractSubSystem): + """ + + Dialysate Generator (DG) Dialin API sub-class for setting and getting the software configurations. + """ + + _DEFAULT_SW_CONFIG_STATUS = 0 + _DEFAULT_CRC_VALUE = 0 + _RECORD_SPECS_BYTES = 12 + # Maximum allowed bytes to be written to RTC RAM + _RTC_RAM_MAX_BYTES_TO_WRITE = 64 + _PAYLOAD_TRANSFER_DELAY_S = 0.2 + _FIRMWARE_STACK_NAME = 'DG' + _NON_VOLATILE_RECORD_NAME = 'SW_Config_Report' + + def __init__(self, can_interface, logger: Logger): + """ + + @param can_interface: Denali CAN Messenger object + """ + + super().__init__() + + self.can_interface = can_interface + self.logger = logger + self._current_message = 0 + self._total_messages = 0 + self._received_msg_length = 0 + self._sw_config_data = 0 + self._is_getting_sw_config_in_progress = False + self._raw_sw_config_record = [] + self._utilities = NVOpsUtils(logger=self.logger) + self.dg_sw_config_record = self._prepare_dg_sw_configs_record() + + if self.can_interface is not None: + channel_id = DenaliChannels.dg_to_dialin_ch_id + msg_id = MsgIds.MSG_ID_DG_SEND_SW_CONFIG_RECORD.value + self.can_interface.register_receiving_publication_function(channel_id, msg_id, + self._handler_dg_sw_config_sync) + + def cmd_reset_dg_sw_config_record(self) -> bool: + """ + Handles resetting DG software configuration record. + + @return: True if successful, False otherwise + """ + # Get the default software configuration dictionary + self.dg_sw_config_record = self._prepare_dg_sw_configs_record() + # Calculate the CRC for reset software configuration record + self._utilities.reset_fw_system_service_record(self.dg_sw_config_record) + status = self._cmd_set_dg_sw_config_record() + + return status + + def _cmd_request_dg_sw_config_record(self) -> int: + """ + Handles getting DG software config record from firmware. + + @return: 1 upon success, False otherwise + """ + if self._is_getting_sw_config_in_progress is not True: + self._is_getting_sw_config_in_progress = True + # Clear the list for the next call + self._raw_sw_config_record.clear() + # Run the firmware commands to get the record + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, + message_id=MsgIds.MSG_ID_DG_GET_SW_CONFIG_RECORD.value) + + self.logger.debug('Getting DG software configuration record') + + received_message = self.can_interface.send(message) + + # If there is content... + if received_message is not None: + self.logger.debug("Received FW ACK after requesting DG software configuration record.") + # response payload is OK or not OK + return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] + else: + self.logger.debug("Timeout!!!!") + return False + + self.logger.debug("Request cancelled: an existing request is in progress.") + return False + + def _handler_dg_sw_config_sync(self, message): + """ + Handles published DG software configuration record messages. DG software configuration records are captured for + processing and updating the DG software configuration record. + + @param message: published DG software configuration record data message + + @return: None + """ + curr = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] + total = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] + length = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] + + self._current_message = curr + self._total_messages = total + self._received_msg_length = length + # The end of calibration_record record payload is from the start index + 12 bytes for the current message +total + # messages + the length of calibration_record. The rest is the CAN messaging CRC that is not needed + # to be kept + end_of_data_index = MsgFieldPositions.START_POS_FIELD_1 + self._RECORD_SPECS_BYTES + self._received_msg_length + + # Get the data only and not specs of it (i.e current message number) + self._sw_config_data = message['message'][MsgFieldPositions.START_POS_FIELD_1:end_of_data_index] + + # Continue getting calibration_record records until the all the calibration_record messages are received. + # Concatenate the calibration_record records to each other + if self._current_message <= self._total_messages: + self._raw_sw_config_record += (message['message'][MsgFieldPositions.START_POS_FIELD_1 + + self._RECORD_SPECS_BYTES:end_of_data_index]) + if self._current_message == self._total_messages: + # Done with receiving the messages + self._is_getting_sw_config_in_progress = False + # If all the messages have been received, call another function to process the raw data + self._utilities.process_received_record_from_fw(self.dg_sw_config_record, self._raw_sw_config_record) + self._handler_received_complete_dg_sw_config_record() + + @publish(["dg_sw_config_record"]) + def _handler_received_complete_dg_sw_config_record(self): + """ + Publishes the received software configuration record + + @return: None + """ + self.logger.debug("Received a complete dg software configuration record.") + + def cmd_update_dg_sw_config_record(self, excel_report_path: str): + """ + Handles preparing the DG software configuration from the provided excel report + + @param excel_report_path: (str) the directory in which the excel report of the software configuration is located + @return: none + """ + # Pass the software configuration record dictionary to be updated with the excel document + status = self._utilities.get_sw_configs_from_excel(self.dg_sw_config_record, excel_report_path, + self._NON_VOLATILE_RECORD_NAME) + # The excel document was successfully read initiate a write command + if status: + self._cmd_set_dg_sw_config_record() + else: + self.logger.debug('Could not find the software configurations file') + + def _cmd_set_dg_sw_config_record(self) -> bool: + """ + Handles updating the DG software configuration record and sends it to FW. + + @return: True upon success, False otherwise + """ + record_packets = self._utilities.prepare_record_to_send_to_fw(self.dg_sw_config_record) + + self.logger.debug('Setting DG sw config record') + + # Update all the data packets with the last message count since is the number of messages that firmware + # should receive + for packet in record_packets: + # Sleep to let the firmware receive and process the data + time.sleep(self._PAYLOAD_TRANSFER_DELAY_S) + + # Convert the list packet to a bytearray + payload = b''.join(packet) + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, + message_id=MsgIds.MSG_ID_DG_SET_SW_CONFIG_RECORD.value, + payload=payload) + + received_message = self.can_interface.send(message) + + # If there is no content... + if received_message is None: + self.logger.debug("Timeout!!!!") + return False + + self.logger.debug("Finished sending DG software configuration record.") + return True + + def _prepare_dg_sw_configs_record(self) -> OrderedDict: + """ + Handles assembling the sub dictionaries of each group to make a blank DG software configuration record. + + @return: (OrderedDict) the assembled dg software configuration record + """ + record = OrderedDict() + + groups_byte_size = 0 + # create a list of the functions of the sub dictionaries + functions = [self._prepare_sw_configs_record()] + + for function in functions: + # Update the groups bytes size so far to be use to padding later + groups_byte_size += function[1] + # Update the calibration record + record.update(function[0]) + + # Build the CRC of the main calibration_record record + record_crc = OrderedDict({'crc': [' tuple: + """ + Handles creating the software configuration record dictionary. + + @return: software configuration record dictionary and the byte size of this group + """ + groups_byte_size = 0 + name = 'sw_configs' + # Create an ordered dictionary + sw_configs = OrderedDict({name: {}}) + + # Loop through the members of the DGSWConfigs enum class + for config in DGSWConfigs.__members__: + # Insert the enum name into the dictionary with the default software config. Each config is one byte + sw_configs[name].update({config: [' bool: + """ + Handles resetting HD software configuration record. + + @return: True if successful, False otherwise + """ + # Get the default software configuration dictionary + self.hd_sw_config_record = self._prepare_hd_sw_configs_record() + # Calculate the CRC for reset software configuration record + status = self._cmd_set_hd_sw_config_record() + + return status + + def _cmd_request_hd_sw_config_record(self) -> int: + """ + Handles getting HD software config record from firmware. + + @return: 1 upon success, False otherwise + """ + if self._is_getting_sw_config_in_progress is not True: + self._is_getting_sw_config_in_progress = True + # Clear the list for the next call + self._raw_sw_config_record.clear() + # Run the firmware commands to get the record + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, + message_id=MsgIds.MSG_ID_HD_GET_SW_CONFIG_RECORD.value) + + self.logger.debug('Getting HD software configuration record') + + received_message = self.can_interface.send(message) + + # If there is content... + if received_message is not None: + self.logger.debug("Received FW ACK after requesting DG software configuration record.") + # response payload is OK or not OK + return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] + else: + self.logger.debug("Timeout!!!!") + return False + + self.logger.debug("Request cancelled: an existing request is in progress.") + return False + + def _handler_hd_sw_config_sync(self, message): + """ + Handles published HD software configuration record messages. HD software configuration records are captured for + processing and updating the HD software configuration record. + + @param message: published HD software configuration record data message + + @return: None + """ + curr = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] + total = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] + length = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] + + self._current_message = curr + self._total_messages = total + self._received_msg_length = length + # The end of calibration_record record payload is from the start index + 12 bytes for the current message +total + # messages + the length of calibration_record. The rest is the CAN messaging CRC that is not needed + # to be kept + end_of_data_index = MsgFieldPositions.START_POS_FIELD_1 + self._RECORD_SPECS_BYTES + self._received_msg_length + + # Get the data only and not specs of it (i.e current message number) + self._sw_config_data = message['message'][MsgFieldPositions.START_POS_FIELD_1:end_of_data_index] + + # Continue getting calibration_record records until the all the calibration_record messages are received. + # Concatenate the calibration_record records to each other + if self._current_message <= self._total_messages: + self._raw_sw_config_record += (message['message'][MsgFieldPositions.START_POS_FIELD_1 + + self._RECORD_SPECS_BYTES:end_of_data_index]) + if self._current_message == self._total_messages: + # Done with receiving the messages + self._is_getting_sw_config_in_progress = False + # If all the messages have been received, call another function to process the raw data + self._utilities.process_received_record_from_fw(self.hd_sw_config_record, self._raw_sw_config_record) + self._handler_received_complete_hd_sw_config_record() + + @publish(["hd_sw_config_record"]) + def _handler_received_complete_hd_sw_config_record(self): + """ + Publishes the received software configuration record + + @return: None + """ + self.logger.debug("Received a complete HD software configuration record.") + + def cmd_update_hd_sw_config_record(self, excel_report_path: str): + """ + Handles preparing the HD software configuration from the provided excel report + + @param excel_report_path: (str) the directory in which the excel report of the software configuration is located + @return: none + """ + # Pass the software configuration record dictionary to be updated with the excel document + status = self._utilities.get_sw_configs_from_excel(self.hd_sw_config_record, excel_report_path, + self._utilities.NON_VOLATILE_RECORD_NAME) + # The excel document was successfully read initiate a write command + if status: + self._cmd_set_hd_sw_config_record() + else: + self.logger.debug('Could not find the software configurations file') + + def _cmd_set_hd_sw_config_record(self) -> bool: + """ + Handles updating the HD software configuration record and sends it to FW. + + @return: True upon success, False otherwise + """ + record_packets = self._utilities.prepare_record_to_send_to_fw(self.hd_sw_config_record) + + self.logger.debug('Setting HD sw config record') + + # Update all the data packets with the last message count since is the number of messages that firmware + # should receive + for packet in record_packets: + # Sleep to let the firmware receive and process the data + time.sleep(self._PAYLOAD_TRANSFER_DELAY_S) + + # Convert the list packet to a bytearray + payload = b''.join(packet) + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, + message_id=MsgIds.MSG_ID_HD_SET_SW_CONFIG_RECORD.value, + payload=payload) + + received_message = self.can_interface.send(message) + + # If there is no content... + if received_message is None: + self.logger.debug("Timeout!!!!") + return False + + self.logger.debug("Finished sending HD software configuration record.") + return True + + def _prepare_hd_sw_configs_record(self) -> OrderedDict: + """ + Handles assembling the sub dictionaries of each group to make a blank HD software configuration record. + + @return: (OrderedDict) the assembled dg software configuration record + """ + record = OrderedDict() + + groups_byte_size = 0 + # create a list of the functions of the sub dictionaries + functions = [self._prepare_sw_configs_record()] + + for function in functions: + # Update the groups bytes size so far to be used for padding later + groups_byte_size += function[1] + # Update the calibration record + record.update(function[0]) + + # Build the CRC of the main calibration_record record + record_crc = OrderedDict({'crc': [' tuple: + """ + Handles creating the software configuration record dictionary. + + @return: software configuration record dictionary and the byte size of this group + """ + groups_byte_size = 0 + name = 'sw_configs' + # Create an ordered dictionary + sw_configs = OrderedDict({name: {}}) + + # Loop through the members of the HDSWConfigs enum class + for config in HDSWConfigs.__members__: + # Insert the enum name into the dictionary with the default software config. Each config is one byte + sw_configs[name].update({config: ['