########################################################################### # # 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 reservoirs.py # # @author (last) Micahel Garthwaite # @date (last) 17-Aug-2023 # @author (original) Sean # @date (original) 14-Apr-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.conversions import integer_to_bytearray, float_to_bytearray from ..utils.checks import check_broadcast_interval_override_ms @unique class DGReservoirsNames(DialinEnum): # Reservoir IDs RESERVOIR1 = 0 RESERVOIR2 = 1 class DGReservoirs(AbstractSubSystem): """ DG interface containing reservoir related commands. """ def __init__(self, can_interface, logger: Logger): """ @param can_interface: The DenaliCANMessenger 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_RESERVOIRS_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_reservoirs_sync) self.active_reservoir = 0 self.fill_to_vol_ml = 0 self.drain_to_vol_ml = 0 self.time_reservoir_cycle = 0 self.time_reservoir_fill_2_switch = 0 self.time_uf_decay = 0.0 self.temp_uf_fill = 0.0 self.temp_reservoir_use_actual = 0.0 self.temp_reservoir_end_fill = 0.0 self.temp_avg_fill = 0.0 self.temp_last_fill = 0.0 self.time_rsrvr_fill = 0.0 self.dg_reservoirs_timestamp = 0.0 def get_active_reservoir(self): """ Gets the active reservoir RESERVOIR1 = 0 \n RESERVOIR2 = 1 \n @return: 0 or 1 """ return self.active_reservoir def get_fill_to_vol(self): """ Gets the fill to volume @return: The fill to volume (mL) """ return self.fill_to_vol_ml def get_drain_to_vol(self): """ Gets the drain to volume @return: The drain to volume (mL) """ return self.drain_to_vol_ml def cmd_switch_reservoirs(self, reservoir: int, use_last_trimmer_dc: int = 0) -> bool: """ Sends a command to the DG to switch reservoirs @param reservoir: (int) the new reservoir number @param use_last_trimmer_dc: (int) use last trimmer heater duty cycle (1) or not (0) @return: True if command sent, False if invalid reservoir provided """ if reservoir not in [0, 1]: return False r = integer_to_bytearray(reservoir) dc = integer_to_bytearray(use_last_trimmer_dc) payload = r + dc message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_SWITCH_RESERVOIR_CMD_REQUEST.value, payload=payload) self.logger.debug("Sending command to switch DG reservoir to {0} and use last trimmer heater" "duty cycle to {1}".format(reservoir, use_last_trimmer_dc)) self.can_interface.send(message, 0) return True def cmd_reservoir_tare(self, reservoir: int) -> bool: """ Sends a command to the DG to tare a given reservoir @param reservoir: (int) the ID of the reservoir to tare @return: True if command sent, False if reservoir ID invalid """ if reservoir not in [0, 1]: return False payload = integer_to_bytearray(reservoir) message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_RESERVOIR_TARE_REQUEST.value, payload=payload) self.logger.debug("Sending command to tare DG reservoir {0}".format(reservoir)) self.can_interface.send(message, 0) return True def cmd_set_dialysate_mixing_ratios(self, acid: float, bicarb: float) -> bool: """ Sends a command to the DG to set the acid and bicarb mixing ratios @param acid (float) acid's dialysate mixing ratio @param bicarb (float) bicarb's dialysate mixing ratio @return: True if command sent, False if timed out """ ac = float_to_bytearray(acid) bc = float_to_bytearray(bicarb) payload = ac + bc message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_SET_DIALYSATE_MIXING_RATIOS.value, payload=payload) self.logger.debug("Setting acid mixing ratio to {:5.3f} and bicarb mixing ration to {:5.3f}".format(acid, bicarb)) # 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.debug("Timeout!!!!") return False @publish(["dg_reservoirs_timestamp","active_reservoir", "fill_to_vol_ml", "drain_to_vol_ml", "time_reservoir_cycle", "time_reservoir_fill_2_switch", "time_uf_decay", "temp_uf_fill", "temp_reservoir_use_actual", "temp_reservoir_end_fill", "temp_avg_fill", "temp_last_fill", "time_rsrvr_fill"]) def _handler_reservoirs_sync(self, message, timestamp=0.0): """ Handles published reservoir data messages. Reservoir data are captured for reference. @param message: published reservoir data message @return: none """ self.active_reservoir = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.fill_to_vol_ml = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] self.drain_to_vol_ml = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.time_reservoir_cycle = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.time_reservoir_fill_2_switch = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] self.time_uf_decay = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] self.temp_uf_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] self.temp_reservoir_use_actual = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.temp_reservoir_end_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9]))[0] self.temp_avg_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_10:MsgFieldPositions.END_POS_FIELD_10]))[0] self.temp_last_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_11:MsgFieldPositions.END_POS_FIELD_11]))[0] self.time_rsrvr_fill = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_12:MsgFieldPositions.END_POS_FIELD_12]))[0] self.dg_reservoirs_timestamp = timestamp def cmd_reservoir_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the reservoir 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_DG_RESERVOIR_BROADCAST_INTERVAL_OVERRIDE.value, payload=payload) self.logger.debug("override reservoir 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