########################################################################### # # Copyright (c) 2021-2025 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 rinse_pump.py # # @author (last) Zoltan Miskolci # @date (last) 07-Jan-2026 # @author (original) Jonny Paguio # @date (original) 13-Oct-2025 # ############################################################################ import struct from logging import Logger from leahi_dialin.common.constants import NO_RESET from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.common.override_templates import cmd_generic_broadcast_interval_override, cmd_generic_override from leahi_dialin.protocols.CAN import DenaliChannels from leahi_dialin.utils.base import AbstractSubSystem, publish from leahi_dialin.utils.conversions import integer_to_bytearray, float_to_bytearray class DDSubstitutionPump(AbstractSubSystem): """ Substitution Pump Dialysate Delivery (DD) Dialin API sub-class for substitution Pump 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 self.msg_id_dd_substitution_pump_data = MsgIds.MSG_ID_DD_SUBSTITUTION_PUMP_DATA.value self.can_interface.register_receiving_publication_function(channel_id, self.msg_id_dd_substitution_pump_data, self._handler_substitution_pump_sync) self.dd_substitution_pump_timestamp = 0 #: The timestamp of the latest message self.d92_set_ml_min = 0 #: Set speed of d92 in mL/min @publish(["msg_id_dd_rinse_pump_data", "d92_set_ml_min", "dd_substitution_pump_timestamp"]) def _handler_substitution_pump_sync(self, message, timestamp=0.0): """ Handles published substitution pump data messages. @param message: published substitution pump data message @return: None """ self.d92_set_ml_min = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.dd_substitution_pump_timestamp = timestamp def cmd_substitution_pump_data_publish_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the substitution pump data publish 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 """ return cmd_generic_broadcast_interval_override( ms=ms, reset=reset, channel_id=DenaliChannels.dialin_to_dd_ch_id, msg_id=MsgIds.MSG_ID_DD_SUBSTITUTION_PUMP_PUBLISH_INTERVAL_OVERRIDE_REQUEST, module_name='DD Rinse Pump', logger=self.logger, can_interface=self.can_interface) def cmd_substitution_pump_set_start_stop(self, pump_id: int, command: int, speed: float) -> int: """ Constructs and sends the substitution pump start stop command @param pump_id: unsigned int - substitution pump ID @param command: int - value to command the substitution 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 return cmd_generic_override( payload=payload, reset=NO_RESET, channel_id=DenaliChannels.dialin_to_dd_ch_id, msg_id=MsgIds.MSG_ID_DD_SUBSTITUTION_PUMP_START_STOP_OVERRIDE_REQUEST, entity_name=f'DD Substitution Pump speed', override_text=str(spd), logger=self.logger, can_interface=self.can_interface)