Index: dialin/common/msg_defs.py =================================================================== diff -u -r43e8345862c72340680c045e53b310aa291513a6 -re36d87e663d95c79214f66b5db9aa6d6e8b47adf --- dialin/common/msg_defs.py (.../msg_defs.py) (revision 43e8345862c72340680c045e53b310aa291513a6) +++ dialin/common/msg_defs.py (.../msg_defs.py) (revision e36d87e663d95c79214f66b5db9aa6d6e8b47adf) @@ -225,6 +225,9 @@ MSG_ID_DG_SET_SERVICE_RECORD = 0xA03A # DG set service record that is sent from Dialin MSG_ID_DG_GET_SERVICE_RECORD = 0xA039 # DG get service record that is requested from Dialin MSG_ID_DG_SEND_SERVICE_RECORD = 0xA03B # DG send service record that is requested from Dialin + MSG_ID_DG_GET_SCHEDULED_RUNS_RECORD = 0xA03C # DG get scheduled runs that is requested from Dialin + MSG_ID_DG_SET_SCHEDULED_RUNS_RECORD = 0xA03D # DG set scheduled runs that is received from Dialin + MSG_ID_DG_SEND_SCHEDULED_RUNS_RECORD = 0xA03E # DG send scheduled runs to CAN bus to be received in Dialin (equivalent to publish) MSG_ID_HD_DEBUG_EVENT = 0xFFF1 # HD debug event text to be logged in event log MSG_ID_DG_DEBUG_EVENT = 0xFFF2 # DG debug event text to be logged in event log Fisheye: Tag e36d87e663d95c79214f66b5db9aa6d6e8b47adf refers to a dead (removed) revision in file `dialin/dg/calibration.py'. Fisheye: No comparison available. Pass `N' to diff? Index: dialin/dg/calibration_record.py =================================================================== diff -u --- dialin/dg/calibration_record.py (revision 0) +++ dialin/dg/calibration_record.py (revision e36d87e663d95c79214f66b5db9aa6d6e8b47adf) @@ -0,0 +1,720 @@ + +import struct +import datetime +import time +import math +from collections import OrderedDict +from ..ui.crc import crc_16 +from ..common.msg_defs import MsgIds, MsgFieldPositions +from ..protocols.CAN import (DenaliMessage, DenaliChannels) +from ..utils.base import _AbstractSubSystem, _publish +from logging import Logger + + +class DGCalibration(_AbstractSubSystem): + """ + + Dialysate Generator (DG) Dialin API sub-class for calibration commands. + """ + + CALIBRATION_RECORD_START_INDEX = 6 + CALIBRATION_RECORD_SPECS_BYTES = 12 + CALIBRATION_RECORDS_SPECS_BYTE_ARRAY = 3 + DEFAULT_GAIN_VALUE = 1 + DEFAULT_OFFSET_VALUE = 0 + DEFAULT_RATIO_VALUE = 1 + DEFAULT_VOLUME_VALUE = 0 + DEFAULT_CONCENTRATE_VALUE = 1 + DEFAULT_BICARB_VALUE = 1 + DEFAULT_CALIBRATION_VALUE = 1 + DEFAULT_CAL_TIME_VALUE = 0 + DEFAULT_CAL_CRC_VALUE = 0 + + CURRENT_MESSAGE_NUM_INDEX = 0 + TOTAL_MESSAGES_NUM_INDEX = 4 + PAYLOAD_LENGTH_INDEX = 8 + PAYLOAD_START_INDEX = 12 + + CALIBRATION_DATA_TYPE_INDEX = 0 + CALIBRATION_VALUE_INDEX = 1 + + TARGET_BYTES_TO_SEND_TO_FW = 150 + MIN_PAYLOAD_BYTES_SPACE = 4 + + EEPROM_MAX_BYTES_TO_WRITE = 16 + + PAYLOAD_CURRENT_MSG_INDEX = 0 + PAYLOAD_TOTAL_MSG_INDEX = 1 + PAYLOAD_CAL_BYTES_INDEX = 2 + + # DG calibration main record + DG_CALIBRATION_RECORD = OrderedDict() + + 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.length = 0 + self.cal_data = 0 + self.raw_cal_record = [] + + if self.can_interface is not None: + + channel_id = DenaliChannels.dg_to_dialin_ch_id + msg_id = MsgIds.MSG_ID_DG_SEND_CALIBRATION_RECORD.value + self.can_interface.register_receiving_publication_function(channel_id, msg_id, + self._handler_dg_calibration_sync) + # Prepare the calibration record by putting sub-dictionaries together + self._prepare_dg_calibration_record() + + def get_dg_calibration_record(self): + """ + Handles getting DG calibration data from firmware. + + @return: None + """ + # Clear the list for the next call + self.raw_cal_record.clear() + # Run the firmware commands to get the calibration record + self._request_dg_fw_calibration_record() + + def _request_dg_fw_calibration_record(self): + """ + Handles getting DG calibration record from firmware. + + @return: None + """ + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, + message_id=MsgIds.MSG_ID_DG_GET_CALIBRATION_RECORD.value) + + self.logger.debug('Getting DG calibration record') + + received_message = self.can_interface.send(message) + + # If there is content... + if received_message is not None: + # response payload is OK or not OK + return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] + else: + self.logger.debug("Timeout!!!!") + return False + + @_publish(["current_message", "total_messages", "length", "cal_data"]) #TODO do we need to publish these? + def _handler_dg_calibration_sync(self, message): + """ + Handles published DG calibration record messages. DG calibration records are captured for processing and + updating the DG calibration record. + + @param message: published DG calibration 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.length = length + # The end of calibration record payload is from the start index + 12 bytes for the current message + total + # messages + the length of calibration. The rest is the CAN messaging CRC that is not needed to be kept + end_of_data_index = self.CALIBRATION_RECORD_START_INDEX + self.CALIBRATION_RECORD_SPECS_BYTES + self.length + + # Get the calibration data only + self.cal_data = message['message'][self.CALIBRATION_RECORD_START_INDEX:end_of_data_index] + + # Continue getting calibration records until the all the calibration messages are received. Concatenate the + # calibration records to each other + if self.current_message <= self.total_messages: + self.raw_cal_record += (message['message'][self.CALIBRATION_RECORD_START_INDEX + + self.CALIBRATION_RECORD_SPECS_BYTES:end_of_data_index]) + if self.current_message == self.total_messages: + # If all the messages have been received, call another function to process the raw data + self._update_dg_calibration_record_from_fw() + + def _update_dg_calibration_record_from_fw(self): + """ + Handles parsing the calibration messages that were received from DG firmware. + + @return: None + """ + raw_payload_temp_start_index = 0 + # Convert the concatenated raw data into a byte array since the struct library requires byte arrays. + self.raw_cal_record = bytearray(self.raw_cal_record) + + # Loop through the keys for the main calibration dictionary + # DG_Calibration : {pressure_sensors : { ppi : { gain: [' 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.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('