########################################################################### # # Copyright (c) 2020-2023 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 conductivity_sensors.py # # @author (last) Micahel Garthwaite # @date (last) 07-Mar-2023 # @author (original) Quang Nguyen # @date (original) 20-Jul-2020 # ############################################################################ import struct from logging import Logger from enum import unique from .constants import RESET, NO_RESET from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import DenaliMessage, DenaliChannels from ..utils.base import AbstractSubSystem, publish, DialinEnum from ..utils.checks import check_broadcast_interval_override_ms from ..utils.conversions import integer_to_bytearray, float_to_bytearray @unique class ConductivitySensorsEnum(DialinEnum): CPI = 0 CPO = 1 CD1 = 2 CD2 = 3 NUM_OF_CONDUCTIVITY_SENSORS = 4 @unique class ConductivitySensorsCalTableName(DialinEnum): CAL_DATA_CPI_COND_SENSOR = 0 # CPi conductivity sensor. CAL_DATA_CPO_COND_SENSOR = 1 # CPo conductivity sensor. CAL_DATA_CD1_COND_SENSOR = 2 # CD1 conductivity sensor. CAL_DATA_CD2_COND_SENSOR = 3 # CD2 conductivity sensor. CAL_DATA_CD2_COND_SENSOR_CHEM_DISINFECT = 4 # CD2 chemical disinfect conductivity sensor. CAL_DATA_CD2_COND_SENSOR_FILL_BICARB_TEST = 5 # CD2 conductivity sensor fill bicarb test. NUM_OF_CAL_DATA_COND_SENSORS = 6 # Number of conductivity sensors. class ConductivitySensors(AbstractSubSystem): """ ConductivitySensors Dialysate Generator (DG) Dialin API sub-class for conductivity sensors related commands. """ def __init__(self, can_interface, logger: Logger): """ @param can_interface: Denali Can Messenger object """ super().__init__() self.can_interface = can_interface self.logger = logger if self.can_interface is not None: channel_id = DenaliChannels.dg_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_DG_CONDUCTIVITY_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_conductivity_sensors_sync) self.ro_rejection_ratio = 0.0 self.conductivity_sensor_cpi = 0.0 self.conductivity_sensor_cpo = 0.0 self.conductivity_sensor_cd1 = 0.0 self.conductivity_sensor_cd2 = 0.0 self.raw_conductivity_sensor_cpi = 0.0 self.raw_conductivity_sensor_cpo = 0.0 self.raw_conductivity_sensor_cd1 = 0.0 self.raw_conductivity_sensor_cd2 = 0.0 self.cpi_sensor_status = 0 self.cpo_sensor_status = 0 self.cd1_sensor_status = 0 self.cd2_sensor_status = 0 self.dg_conductivity_timestamp = 0.0 def get_conductivity_sensors(self): """ Gets the current conductivity value @return: List containing conductivity values: [ conductivity_sensor_cpi, conductivity_sensor_cpo, conductivity_sensor_cd1, conductivity_sensor_cd2 ] """ return [self.conductivity_sensor_cpi, self.conductivity_sensor_cpo, self.conductivity_sensor_cd1, self.conductivity_sensor_cd2, self.raw_conductivity_sensor_cpi, self.raw_conductivity_sensor_cpo, self.raw_conductivity_sensor_cd1, self.raw_conductivity_sensor_cd2] def get_ro_rejection_ratio(self): """ Gets the current RO rejection ratio value @return: ro_rejection_ratio """ return self.ro_rejection_ratio @publish(["dg_conductivity_timestamp","ro_rejection_ratio", "conductivity_sensor_cpi", "conductivity_sensor_cpo", "conductivity_sensor_cd1","conductivity_sensor_cd2", "raw_conductivity_sensor_cpi", "raw_conductivity_sensor_cpo", "raw_conductivity_sensor_cd1", "raw_conductivity_sensor_cd2", "cpi_sensor_status", "cpo_sensor_status", "cd1_sensor_status", "cd2_sensor_status"]) def _handler_conductivity_sensors_sync(self, message, timestamp=0.0): """ Handles published conductivity sensor data messages. Conductivity sensor data are captured for reference. @param message: published conductivity sensor data message @return: None """ self.ro_rejection_ratio = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.conductivity_sensor_cpi = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] self.conductivity_sensor_cpo = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.conductivity_sensor_cd1 = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.conductivity_sensor_cd2 = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] self.raw_conductivity_sensor_cpi = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] self.raw_conductivity_sensor_cpo = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] self.raw_conductivity_sensor_cd1 = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.raw_conductivity_sensor_cd2 = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9]))[0] self.cpi_sensor_status = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_10:MsgFieldPositions.END_POS_FIELD_10]))[0] self.cpo_sensor_status = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_11:MsgFieldPositions.END_POS_FIELD_11]))[0] self.cd1_sensor_status = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_12:MsgFieldPositions.END_POS_FIELD_12]))[0] self.cd2_sensor_status = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_13:MsgFieldPositions.END_POS_FIELD_13]))[0] self.dg_conductivity_timestamp = timestamp def cmd_conductivity_sensor_override(self, sensor: int, conductivity: float, reset: int = NO_RESET) -> int: """ Constructs and sends the conductivity value override command @param sensor: unsigned int - sensor ID @param conductivity: float - conductivity value to override sensor with @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise Conductivity sensor IDs: \n 0 = CPI \n 1 = CPO \n 2 = CD1 \n 3 = CD2 \n """ reset_byte_array = integer_to_bytearray(reset) cond_byte_array = float_to_bytearray(conductivity) sensor_byte_array = integer_to_bytearray(sensor) payload = reset_byte_array + cond_byte_array + sensor_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_CONDUCTIVITY_OVERRIDE.value, payload=payload) if reset == RESET: str_res = "reset back to normal" else: str_res = str(conductivity) + " microsiemens/cm" self.logger.debug("override conductivity sensor value for sensor " + str(sensor) + ": " + str_res) # Send message 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.error("Timeout!!!!") return False def cmd_conductivity_sensor_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the conductivity sensor data broadcast interval override command Constraints: Must be logged into DG. Given interval must be non-zero and a multiple of the DG general task interval (50 ms). @param ms: integer - interval (in ms) to override with @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ if not check_broadcast_interval_override_ms(ms): return False reset_byte_array = integer_to_bytearray(reset) ms_byte_array = integer_to_bytearray(ms) payload = reset_byte_array + ms_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_CONDUCTIVITY_PUBLISH_INTERVAL_OVERRIDE.value, payload=payload) self.logger.debug("override DG conductivity sensor broadcast interval") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: if reset == RESET: str_res = "reset back to normal: " else: str_res = str(ms) + " ms: " self.logger.debug("Conductivity sensor data broadcast interval overridden to " + str_res + str(received_message['message'][DenaliMessage.PAYLOAD_START_INDEX])) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.error("Timeout!!!!") return False def cmd_set_conductivity_sensor_cal_table(self, sensor_id: int, cal_table_id: int): """ Constructs and sends the conductivity sensor calibration table command @param sensor_id: unsigned_int - sensor ID @param cal_table_id: unsigned_int - calibration table value to pick @return: 1 if successful, zero otherwise Conductivity sensor IDs: \n 0 = CPI \n 1 = CPO \n 2 = CD1 \n 3 = CD2 \n Conductivity sensor calibration table IDs: \n 0 = CAL_DATA_CPI_COND_SENSOR \n 1 = CAL_DATA_CPO_COND_SENSOR \n 2 = CAL_DATA_CD1_COND_SENSOR \n 3 = CAL_DATA_CD2_COND_SENSOR \n 4 = CAL_DATA_CD2_COND_SENSOR_CHEM_DISINFECT \n 5 = CAL_DATA_CD2_COND_SENSOR_FILL_BICARB_TEST \n """ sensor_byte_array = integer_to_bytearray(sensor_id) cal_table_byte_array = integer_to_bytearray(cal_table_id) payload = sensor_byte_array + cal_table_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_SET_COND_SENSOR_CAL_TABLE.value, payload=payload) self.logger.debug("Setting Sensor {} calibration table to {}".format(ConductivitySensorsEnum(sensor_id).name, ConductivitySensorsCalTableName(cal_table_id).name)) # Send message 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.error("Timeout!!!!") return False