########################################################################### # # 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 balancing_chamber.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 class DDBalancingChamber(AbstractSubSystem): """ Balancing Chamber Dialysate Delivery (DD) Dialin API sub-class for Balancing Chamber 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_BAL_CHAMBER_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_balancing_chamber_sync) self.execution_state = 0 self.switching_state = 0 self.switching_frequency = 0.0 self.switching_period = 0 self.bal_chamber_fill_in_progress = 0 self.current_bal_chamber_switching_counter = 0 self.is_pressure_stabilized_during_fill = 0 self.bal_chamber_switch_only_state = 0 self.dd_bal_chamber_timestamp = 0 @publish(["dd_bal_chamber_timestamp", "execution_state", "switching_state", "switching_frequency", "switching_period", "bal_chamber_fill_in_progress", "current_bal_chamber_switching_counter", "is_pressure_stabilized_during_fill", "bal_chamber_switch_only_state"]) def _handler_balancing_chamber_sync(self, message, timestamp=0.0): """ Handles published balancing chamber data messages. @param message: published balancing chamber data message @return: None """ self.execution_state = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.switching_state = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] self.switching_frequency = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.switching_period = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.bal_chamber_fill_in_progress = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_6]))[0] self.current_bal_chamber_switching_counter = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] self.is_pressure_stabilized_during_fill = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] self.bal_chamber_switch_only_state = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.dd_bal_chamber_timestamp = timestamp def cmd_balancing_chamber_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the balancing chamber data broadcast interval override command Constraints: Must be logged into DD. Given interval must be non-zero and a multiple of the DD 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_BAL_CHAMBER_DATA_PUBLISH_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override DD Balancing Chamber 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_switch_frequency_override(self, frequency: float, reset: int = NO_RESET) -> int: """ Constructs and sends the balancing chamber switch frequency override command Constraints: Must be logged into DD. @param frequency: float - frequency value to override switch frequency with @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ reset_byte_array = integer_to_bytearray(reset) freq = float_to_bytearray(frequency) payload = reset_byte_array + freq message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_BAL_CHAMBER_SWITCH_FREQ_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: str_res = "reset back to normal" else: str_res = str(frequency) self.logger.debug("override switch frequency " + ": " + 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_switch_only_start_stop_override(self, start_stop: int, flow: float) -> int: """ Constructs and sends the balancing chamber switch only start stop override command Constraints: Must be logged into DD. @param start_stop: int - value to start or stop switch ( start = 1, stop = 0 @param flow: float - flow rate in ml/min @return: 1 if successful, zero otherwise """ sts = integer_to_bytearray(start_stop) freq = float_to_bytearray(flow) payload = sts + freq message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_BC_SWITCH_ONLY_START_STOP_OVERRIDE_REQUEST.value, payload=payload) # 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_acid_dose_volume_override(self, volume: float, reset: int = NO_RESET) -> int: """ Constructs and sends the acid dose volume override command Constraints: Must be logged into DD. @param volume: float - volume value to override acid dose with @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ reset_byte_array = integer_to_bytearray(reset) vol = float_to_bytearray(volume) payload = reset_byte_array + vol message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_ACID_DOSING_VOLUME_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: str_res = "reset back to normal" else: str_res = str(volume) self.logger.debug("override acid dose volume to frequency " + ": " + 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_bicarb_dose_volume_override(self, volume: float, reset: int = NO_RESET) -> int: """ Constructs and sends the bicarb dose volume override command Constraints: Must be logged into DD. @param volume: float - volume value to override bicarb dose with @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ reset_byte_array = integer_to_bytearray(reset) vol = float_to_bytearray(volume) payload = reset_byte_array + vol message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_BICARB_DOSING_VOLUME_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: str_res = "reset back to normal" else: str_res = str(volume) self.logger.debug("override bicarb dose volume to frequency " + ": " + 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