########################################################################### # # 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 ultrafiltration.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 class DDUltrafiltration(AbstractSubSystem): """ Ultrafiltration Dialysate Delivery (DD) Dialin API sub-class for post gen dialysate related commands. """ def __init__(self, can_interface, logger: Logger): """ @param can_interface: Leahi 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_UF_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_ultrafiltration_sync) self.uf_exec_state = 0 self.uf_rate = 0.0 self.compensated_uf_rate = 0.0 self.is_uf_requested = 0 self.uf_timestamp = 0.0 @publish(["uf_timestamp", "uf_exec_state","uf_rate","compensated_uf_rate","is_uf_requested"]) def _handler_ultrafiltration_sync(self, message, timestamp=0.0): """ Handles published ultrafiltration data messages. @param message: published ultrafiltration data message @return: None """ self.uf_exec_state = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.uf_rate = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] self.compensated_uf_rate = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.is_uf_requested = struct.unpack('I', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.uf_timestamp = timestamp def cmd_uf_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the broadcast time interval override for ultrafiltration data. 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: (int) Publish time interval in ms @param reset: (int) 1 to reset a previous override, 0 to override @returns 1 if successful, zero otherwise """ if not check_broadcast_interval_override_ms(ms): return False reset_value = integer_to_bytearray(reset) interval_value = integer_to_bytearray(ms) payload = reset_value + interval_value message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_UF_DATA_PUBLISH_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("Sending {} ms publish interval to the Ultrafiltration module".format(ms)) # Send message received_message = self.can_interface.send(message) # If there is content in message if received_message is not None: # Response payload is OK or not return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False