########################################################################### # # Copyright (c) 2021-2024 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file nv_ops_utils.py # # @author (last) Dara Navaei # @date (last) 05-Mar-2024 # @author (original) Dara Navaei # @date (original) 21-Feb-2021 # ############################################################################ import os.path import struct import time from enum import unique from logging import Logger from typing import List from collections import OrderedDict from .excel_ops import * from leahi_dialin.utils.base import AbstractObserver, DialinEnum @unique class NVRecordsDG(DialinEnum): NVDATAMGMT_CALIBRATION_RECORD = 0 NVDATAMGMT_SYSTEM_RECORD = 1 NVDATAMGMT_SERVICE_RECORD = 2 NVDATAMGMT_SCHEDULED_RUNS_RECORD = 3 NVDATAMGMT_HEATERS_INFO_RECORD = 4 NVDATAMGMT_USAGE_INFO_RECORD = 5 NVDATAMGMT_SW_CONFIG_RECORD = 6 NUM_OF_NVDATMGMT_RECORDS_JOBS = 7 class NVRecordsHD(DialinEnum): NVDATAMGMT_CALIBRATION_RECORD = 0 NVDATAMGMT_SYSTEM_RECORD = 1 NVDATAMGMT_SERVICE_RECORD = 2 NVDATAMGMT_INSTITUTIONAL_RECORD = 3 NVDATAMGMT_USAGE_INFO_RECORD = 4 NVDATAMGMT_SW_CONFIG_RECORD = 5 NUM_OF_NVDATMGMT_RECORDS_JOBS = 6 class NVUtilsObserver(AbstractObserver): """ Observation class """ def __init__(self, prop): self.received = False self.prop = prop def update(self, message): """ Publicly accessible function to provide an update of the object that is being observed @param message: (str) the message to update its status @return none """ self.received = message.get(self.prop, False) class NVOpsUtils: """ Processes the calibration_record records, service records, system records, and the scheduled runs records. The records are prepared to be sent to firmware or to be received from firmware. """ CRC_16_TABLE = ( 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 ) # Public defines DEFAULT_CHAR_VALUE = ' ' CAL_RECORD_TAB_NAME = 'Calibration_Record' NON_VOLATILE_RECORD_NAME = 'SW_Config_Report' USAGE_INFO_RECORD_TAB_NAME = 'Usage_Info_Record' SYSTEM_RECORD_TAB_NAME = 'System_Record' SERVICE_RECORD_TAB_NAME = 'Service_Record' INSTITUTIONAL_RECORD_TAB_NAME = 'Institutional_Record' DEFAULT_EPOCH_VALUE = 0 _RECORD_START_INDEX = 6 _RECORD_SPECS_BYTES = 12 _RECORD_SPECS_BYTE_ARRAY = 3 _CURRENT_MESSAGE_NUM_INDEX = 0 _DATA_TYPE_INDEX = 0 _DATA_VALUE_INDEX = 1 _CHAR_LENGTH_INDEX = 2 _DICT_VALUE_LIST_LEN = 2 _TARGET_BYTES_TO_SEND_TO_FW = 150 _MIN_PAYLOAD_BYTES_SPACE = 4 _PAYLOAD_CURRENT_MSG_INDEX = 0 _PAYLOAD_TOTAL_MSG_INDEX = 1 _PAYLOAD_TOTAL_BYTES_INDEX = 2 _SW_CONFIGS_TITLE_COL = 'SW Configurations' _SW_CONFIGS_VALUE_COL = 'Status' _SW_CONFIGS_REPORT_NAME = 'SW-CONFIGS' _CAL_TIME_NAME = 'cal_time' _NEW_CAL_TIME_SIGNAL = 'new' _CRC_NAME = 'crc' _PADDING_GROUP_NAME = 'padding' def __init__(self, logger: Logger): """ Constructor for the NVOptsUtils class @param logger: (Logger) the logger """ self.logger = logger self._workspace_dir = '' self._excel_workbook = '' self._record_name = '' self._firmware_stack = '' self._is_writing_to_excel_done = False self._is_read_done = False # This list contains different data packet lists # i.e. [[message 1, payload], [message 2, payload], ...] self._record_packets_to_send_to_fw = [] # Buffer that is used to keep the groups data except the crc to convert them to bytes and calculate the crc # of the group. self._temp_groups_data_to_calculate_crc = [] def _create_workspace(self, dir_name): """ Publicly accessible function to get create a workspace for the script that is running. @param dir_name: Name of the workspace directory @return none """ # Get the root directory of the current script scripts_root_dir = os.path.dirname(os.path.dirname(__file__)) # Get the root directory of the entire scripts folder. The workspace that holds the # code review reports and clones other scripts and repositories must be outside of the scripts root_dir = os.path.dirname(os.path.dirname(scripts_root_dir)) # Create the address of the workspace self._workspace_dir = os.path.join(root_dir, dir_name) # If the path does not exist, make it, otherwise, change to that directory if not os.path.isdir(self._workspace_dir): # Create the directory and go to it os.mkdir(self._workspace_dir) os.chdir(self._workspace_dir) def prepare_excel_report(self, firmware_stack: str, record_name: str, directory: str, protect_sheet: bool = False): """ Publicly accessible function to prepare the excel report @param firmware_stack: (str) firmware stack name (e.g. "HD" or "DG") @param record_name: (str) record type to check such as calibration, system, ... @param directory: (str) the directory in which to write the excel document. @param protect_sheet: (bool) flag to indicate whether to write protect the sheet or not (default False) @return none """ path = '' is_report_found = False # If a directory is provided and there is not a folder in that address, create the # directory. Set the workspace directory to the provided directory. If a directory was not # provided, create a workspace in the default position default_nv_directory = firmware_stack + '_NV_Records' if directory is not None: directory = os.path.join(directory, default_nv_directory) if not os.path.isdir(directory): # Create the directory and go to it os.mkdir(directory) self._workspace_dir = directory os.chdir(self._workspace_dir) else: self._create_workspace(default_nv_directory) self._record_name = record_name self._firmware_stack = firmware_stack # List all the files in the workspace directory for file in os.listdir(self._workspace_dir): # If the file has an extension of .xlsx if file.endswith('.xlsx'): # Check if the firmware stack (i.e. DG) is in the file and name of the file # does not have lock in it. When the file is open, there is a hidden lock file # in there and it is ignored file = str(file) if self._firmware_stack in file and 'lock' not in file: if str(datetime.datetime.now().date()) in file: if self._SW_CONFIGS_REPORT_NAME in file and self.NON_VOLATILE_RECORD_NAME == record_name: # Create the file path and exit the loop path = os.path.join(self._workspace_dir, file) is_report_found = True break elif self._SW_CONFIGS_REPORT_NAME not in file and self.NON_VOLATILE_RECORD_NAME not in \ record_name: # Create the file path and exit the loop path = os.path.join(self._workspace_dir, file) is_report_found = True break if is_report_found: # Load the excel workbook self._excel_workbook = load_excel_report(path) else: # Get an excel workbook object self._excel_workbook = get_an_excel_workbook() # Setup worksheet and create the current tab setup_excel_worksheet(self._excel_workbook, self._record_name, protection=protect_sheet) def write_fw_record_to_excel(self, calibration_record: OrderedDict): """ Writes a calibration record to excel @param calibration_record: (dict) the record to write to excel @return: None """ try: row = 1 # Let's say the calibration record is: # Get the keys of the calibration group {'pressure_sensors': 'ppi', {'fourth_order': [' bytearray: """ Gets the list of the characters and makes sure their length is to the define length @param record: (list) the list that contains the characters, data type and the target character length @return characters (bytearray) that are converted to bytearrays """ data_type = record[0] char = record[1] char_len = record[2] temp = bytearray() if len(char) > char_len: char = char[:char_len] elif len(char) < char_len: for i in range(len(char), char_len): char += NVOpsUtils.DEFAULT_CHAR_VALUE for ch in char: temp += struct.pack(data_type, ch.encode('ascii')) return temp @staticmethod def reset_fw_record(record: OrderedDict) -> OrderedDict: """ Gets a record and updated the calibration date and crc @param record: (dict) the record to calculate the calibration time and crc @return record (OrderedDict) the record with updated calibration time and crc """ for key, value in record.items(): if isinstance(value, dict): for sub_key, sub_value in value.items(): if sub_key == 'cal_time': sub_value[sub_key][1] = NVOpsUtils.get_current_time_in_epoch() crc = NVOpsUtils.get_group_record_crc(sub_value) sub_value['crc'][1] = crc return record @staticmethod def reset_fw_system_service_record(record: OrderedDict) -> OrderedDict: """ Gets a record and updated the calibration date and crc @param record: (dict) the record to calculate the calibration time and crc @return record (OrderedDict) the record with updated calibration time and crc """ for key, value in record.items(): # Check if there is a CRC in the inner dictionary since some of the structures might not have it. # For instance, the software configuration record does not have an inner CRC and it only has a global CRC # with the padding if isinstance(value, dict): crc = NVOpsUtils.get_group_record_crc(value) value['crc'][1] = crc return record @staticmethod def get_group_record_crc(group_record: dict) -> int: """ Gets a group record and calculates the crc for the group @param group_record: (dict) the record to calculate the crc @return crc (int) the calculated crc """ value_in_bytes = b'' temp = [] for key, value in group_record.items(): if key != 'crc': data_type = value[0] if data_type == ' self._MIN_PAYLOAD_BYTES_SPACE: current_payload_length += data_type_bytes temp_buffer[self._PAYLOAD_TOTAL_MSG_INDEX] = struct.pack(' self._MIN_PAYLOAD_BYTES_SPACE: current_payload_length += data_type_bytes # Insert a 4-byte 0 to the index of the total messages. This is a place holder and it will # be updated with the right value later. temp_buffer[self._PAYLOAD_TOTAL_MSG_INDEX] = struct.pack(' self._DICT_VALUE_LIST_LEN: byte_size += current_byte_size * group[key][self._CHAR_LENGTH_INDEX] else: byte_size += current_byte_size return byte_size @classmethod def crc_16(cls, data): """ generates crc16 for the provided data @param data: byte of data @return: (int) the crc code """ crc = 0xFFFF length = len(data) i = 0 while length > 0: # Make the sure variables are 16-bit integers left = (crc << 8) & 0x0000FFFF right = (crc >> 8) & 0x0000FFFF crc = left ^ cls.CRC_16_TABLE[data[i] ^ (right & 0x00FF)] length -= 1 i += 1 return crc @staticmethod def get_data_type_bytes(data): """ Handles converting the string representation of the bytes of the data types in a struct to numbers. This is a static method. @param data: the data to be converted to bytes in number @return: calculated byte size """ number_of_bytes = struct.calcsize(data) return number_of_bytes @staticmethod def calculate_padding_byte_size(total_byte_size, max_buffer_size): """ Handles calculating the padding length based on the provided buffer sizes. This is a static method. @param total_byte_size: total byte size of a record dictionary @param max_buffer_size: max buffer size that is allowed to be used in the dictionary @return: padding size in bytes """ # Calculate the padding size: # If bytes in the dictionary % max write bytes to RTC RAM (64) or EEPROM (16) is 0, not padding is needed # Else padding = (ceil(dictionary bytes/max write) * max write)-dictionary bytes if (total_byte_size % max_buffer_size) == 0: padding_size = 0 else: padding_size = (math.ceil(total_byte_size / max_buffer_size) * max_buffer_size) - total_byte_size return padding_size def write_sw_config_to_excel(self, sw_configs: OrderedDict, stack_name: str): """ Publicly accessible function to write the software configurations into an excel report @param sw_configs: (ordered dictionary) the configurations record to write to excel @param stack_name: (str) the name of the software stack (i.e. HD) @return none """ row = 1 names_col_number = 2 write_to_excel(self._excel_workbook, self._record_name, row, names_col_number, self._SW_CONFIGS_TITLE_COL, bold=True, freeze=True, protect_cell=True) values_col_number = 3 write_to_excel(self._excel_workbook, self._record_name, row, values_col_number, self._SW_CONFIGS_VALUE_COL, bold=True, protect_cell=True) # Prepare for writing the values row += 1 for key, values in sw_configs['sw_configs'].items(): write_to_excel(self._excel_workbook, self._record_name, row, names_col_number, key, bold=True, protect_cell=True) # Get the configuration value config_value = values[1] # If the config value is not 0, color the cell as green otherwise, leave it none colored color = GREEN if config_value != 0 else NO_COLOR write_to_excel(self._excel_workbook, self._record_name, row, values_col_number, config_value, color=color) row += 1 save_report(self._excel_workbook, self._workspace_dir, self._SW_CONFIGS_REPORT_NAME, stack_name=stack_name) def get_sw_configs_from_excel(self, sw_configs_dict: OrderedDict, excel_path: str, sw_config_excel_tab: str): """ Publicly accessible function to get the software configurations from excel @param sw_configs_dict: (ordered dictionary) the configurations record to write to excel @param excel_path: (str): the path to the excel report that its data is read @param sw_config_excel_tab: (str): the name of the tab in the excel report that the values are located at @return status of the operations """ row = 1 col = 1 title_col = None value_col = None max_col_to_go = 50 status = False # Load the excel report self._excel_workbook = load_excel_report(excel_path) active_sheet = self._excel_workbook[sw_config_excel_tab] while True: # Loop through the cells in the title row value = active_sheet.cell(row=row, column=col).value # If the col number exceeded the maximum column number, break out of the loop if col >= max_col_to_go: break # Check if the value of the read cell is not none if value is not None: # If the value of the title row is name of the sw configs title column name, update the title col number if value.strip() == self._SW_CONFIGS_TITLE_COL: title_col = col # If the value of the title row is the name of the values of the sw configs, update the value col number if value.strip() == self._SW_CONFIGS_VALUE_COL: value_col = col # If the title col and value col numbers are both found and they are not none, then exit the while loop # since the values have been found, otherwise, increment the column number and keep looking if title_col is not None and value_col is not None: break else: col += 1 if title_col is not None and value_col is not None: # Get the last non-empty row number of the current active sheet last_non_empty_row = active_sheet.max_row # Get the dictionary of the provided sw configurations dictionary, this can be either HD or DG fw_sw_configs = sw_configs_dict['sw_configs'] # Loop through the excel from row 2 since row 1 is the titles row until the last non-empty row + 1 since the # range method does not include that last element so the last non-empty row will not be covered if there is # not a +1. for row in range(2, last_non_empty_row + 1): config = active_sheet.cell(row=row, column=title_col).value if config is not None: # Check if the software configuration that has been read from excel exists in dictionary that has # been prepared in Dialin if config.strip() in fw_sw_configs: excel_config_value = active_sheet.cell(row=row, column=value_col).value # Check if the value is an integer and it is a 1 or a 0 # The only acceptable values are 1 for enable and 0 for disable if isinstance(excel_config_value, int) and excel_config_value == 1 or excel_config_value == 0: fw_sw_configs[config.strip()][1] = excel_config_value else: # If the value is not acceptable, set the default value that is sent down to firmware to 0 # and write incorrect into the report so the user will notice that they had and empty cell # or a cell with a non-acceptable value (i.e. 123). Color the cell as red. fw_sw_configs[config.strip()][1] = 0 write_to_excel(self._excel_workbook, sw_config_excel_tab, row, value_col, 'Incorrect Value', color=RED) # Save back the excel workbook with the changes # This save function is the openpyxl save and not the internal save function that creates # the save path. The save path already exists self._excel_workbook.save(filename=excel_path) status = True return status