########################################################################### # # Copyright (c) 2020-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 concentrate_pumps.py # # @author (last) Micahel Garthwaite # @date (last) 07-Mar-2023 # @author (original) Micahel Garthwaite # @date (original) 29-Oct-2020 # ############################################################################ import struct from enum import unique from logging import Logger from .constants import RESET, NO_RESET from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels from leahi_dialin.utils.base import AbstractSubSystem, publish, DialinEnum from leahi_dialin.utils.checks import check_broadcast_interval_override_ms from leahi_dialin.utils.conversions import integer_to_bytearray, float_to_bytearray @unique class ConcentratePumpsEnum(DialinEnum): CP1_ACID = 0 CP2_BICARB = 1 @unique class DDConcentratePumpsStates(DialinEnum): CONCENTRATE_PUMP_OFF_STATE = 0 CONCENTRATE_PUMP_RAMP_TO_TARGET_SPEED_STATE = 1 CONCENTRATE_PUMP_CONTROL_TARGET_SPEED_STATE = 2 class DDConcentratePumps(AbstractSubSystem): """ ConcentratePumps Dialysate Delivery (DD) Dialin API sub-class for concentrate pumps 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.dd_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_concentrate_pumps_sync) self.concentrate_pump_cp1_current_set_speed = 0.0 self.concentrate_pump_cp1_measured_speed = 0.0 self.concentrate_pump_cp1_target_speed = 0.0 self.concentrate_pump_cp2_current_set_speed = 0.0 self.concentrate_pump_cp2_measured_speed = 0.0 self.concentrate_pump_cp2_target_speed = 0.0 self.concentrate_pump_cp1_current_state = 0.0 self.concentrate_pump_cp2_current_state = 0.0 self.concentrate_pump_cp1_parked = False self.concentrate_pump_cp2_parked = False self.concentrate_pump_cp1_park_fault = False self.concentrate_pump_cp2_park_fault = False self.dd_concentrate_pump_timestamp = 0.0 @publish(["dd_concentrate_pump_timestamp", "concentrate_pump_cp1_current_set_speed", "concentrate_pump_cp1_measured_speed", "concentrate_pump_cp1_target_speed", "concentrate_pump_cp2_current_set_speed", "concentrate_pump_cp2_measured_speed", "concentrate_pump_cp2_target_speed", "concentrate_pump_cp1_current_state", "concentrate_pump_cp2_current_state", "concentrate_pump_cp1_parked", "concentrate_pump_cp2_parked", "concentrate_pump_cp1_park_fault", "concentrate_pump_cp2_park_fault"]) def _handler_concentrate_pumps_sync(self, message, timestamp=0.0): """ Handles published concentrate pumps' data messages. Concentrate pumps' speed data are captured for reference. @param message: published concentrate pumps' data message @return: None """ self.concentrate_pump_cp1_current_set_speed = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.concentrate_pump_cp1_measured_speed = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] self.concentrate_pump_cp1_target_speed = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.concentrate_pump_cp2_current_set_speed = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.concentrate_pump_cp2_measured_speed = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] self.concentrate_pump_cp2_target_speed = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] self.concentrate_pump_cp1_current_state = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] self.concentrate_pump_cp2_current_state = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.cp1_pulse_us = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9]))[0] self.cp2_pulse_us = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_10:MsgFieldPositions.END_POS_FIELD_10]))[0] self.concentrate_pump_cp1_parked = True if struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_11:MsgFieldPositions.END_POS_FIELD_11]))[0] == 1 else False self.concentrate_pump_cp2_parked = True if struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_12:MsgFieldPositions.END_POS_FIELD_12]))[0] == 1 else False self.concentrate_pump_cp1_park_fault = True if struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_13:MsgFieldPositions.END_POS_FIELD_13]))[0] == 1 else False self.concentrate_pump_cp2_park_fault = True if struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_14:MsgFieldPositions.END_POS_FIELD_14]))[0] == 1 else False self.dg_concentrate_pump_timestamp = timestamp def cmd_concentrate_pump_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump 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_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_PUBLISH_INTERVAL_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override DG concentrate pump data broadcast interval") # 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_concentrate_pump_target_speed_override(self, pump_id: int, speed: float) -> int: """ Constructs and sends the concentrate pump target speed override command @param pump_id: unsigned int - concentrate pump ID @param speed: float - target speed value to override concentrate pump with @return: 1 if successful, zero otherwise Concentrate pump IDs: \n 0 = CP1 \n 1 = CP2 \n """ reset_byte_array = integer_to_bytearray(NO_RESET) speed_byte_array = float_to_bytearray(speed) pump_id_byte_array = integer_to_bytearray(pump_id) payload = reset_byte_array + speed_byte_array + pump_id_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_TARGET_SPEED_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override target speed: " + str(speed) + " - for pump: " + str(pump_id)) # 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_concentrate_pump_measured_speed_override(self, pump_id: int, speed: float, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump measured speed override command @param pump_id: unsigned int - concentrate pump ID @param speed: float - measured speed value to override concentrate pump with @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise Concentrate pump IDs: \n 0 = CP1 \n 1 = CP2 \n """ reset_byte_array = integer_to_bytearray(reset) speed_byte_array = float_to_bytearray(speed) pump_id_byte_array = integer_to_bytearray(pump_id) payload = reset_byte_array + speed_byte_array + pump_id_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_MEASURED_SPEED_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: self.logger.debug("reset back to normal value for pump: " + str(pump_id)) else: self.logger.debug("override measured speed: " + str(speed) + " - for pump: " + str(pump_id)) # 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_concentrate_pump_parked_status_override(self, pump_id: int, status: int, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump parked state override command @param pump_id: unsigned int - concentrate pump ID @param status: unsigned int - 1 = parked, 0 = not parked @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise Concentrate pump IDs: \n 0 = CP1 \n 1 = CP2 \n """ reset_byte_array = integer_to_bytearray(reset) status_byte_array = integer_to_bytearray(status) pump_id_byte_array = integer_to_bytearray(pump_id) payload = reset_byte_array + status_byte_array + pump_id_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_PARKED_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: self.logger.debug("reset parked status back to normal for pump: " + str(pump_id)) else: self.logger.debug("override parked status to: " + str(status) + " - for pump: " + str(pump_id)) # 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_concentrate_pump_park_fault_state_override(self, pump_id: int, status: int, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump park fault state override command @param pump_id: unsigned int - concentrate pump ID @param status: unsigned int - 1 = fault, 0 = no fault @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise Concentrate pump IDs: \n 0 = CP1 \n 1 = CP2 \n """ reset_byte_array = integer_to_bytearray(reset) status_byte_array = integer_to_bytearray(status) pump_id_byte_array = integer_to_bytearray(pump_id) payload = reset_byte_array + status_byte_array + pump_id_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_PARK_FAULT_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: self.logger.debug("reset park fault status back to normal for pump: " + str(pump_id)) else: self.logger.debug("override park fault status to: " + str(status) + " - for pump: " + str(pump_id)) # 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_concentrate_pump_park_command(self, pump_id: int) -> int: """ Constructs and sends the concentrate pump park command @param pump_id: unsigned int - concentrate pump ID @return: 1 if successful, zero otherwise Concentrate pump IDs: \n 0 = CP1 \n 1 = CP2 \n """ payload = integer_to_bytearray(pump_id) message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_PARK_REQUEST_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("park concentrate pump: " + str(pump_id)) # 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_concentrate_set_start_stop(self, pump_id: int, command: int, speed: float ) -> int: """ Constructs and sends the concentrate pump start stop command @param pump_id: unsigned int - concentrate pump ID @param command: int - value to command the concentrate pump @param speed: float - ml/min to set the speed to @return: 1 if successful, zero otherwise """ pmp = integer_to_bytearray(pump_id) cmd = integer_to_bytearray(command) spd = float_to_bytearray(speed) payload = pmp + cmd + spd message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMPS_START_STOP_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("setting " + str(spd) + " - for pump: " + str(pump_id)) # 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