########################################################################### # # Copyright (c) 2021-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 air_bubbles.py # # @author (last) Sean Nash # @date (last) 04-May-2023 # @author (original) Peman Montazemi # @date (original) 18-May-2021 # ############################################################################ import struct from logging import Logger 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 from ..utils.conversions import integer_to_bytearray class HDAirBubbles(AbstractSubSystem): """ HDAirBubbles Hemodialysis Delivery (HD) Dialin API sub-class for air bubbles related commands. ADA: Air bubble Detector Arterial ADV: Air bubble Detector Venous """ # Air bubble detectors ADV = 0 # Air bubble Detector Venous # Air bubble detectors status BUBBLE_DETECTED_STATUS = 0 # Air bubble detected FLUID_DETECTED_STATUS = 1 # Fluid (no air bubble) detected # Air bubble detectors state machine states AIR_BUBBLE_NORMAL_STATE = 0 # Normal state AIR_BUBBLE_SELF_TEST_STATE = 1 # Self-test state 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.hd_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_HD_BUBBLES_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_air_bubbles_data_sync) self.hd_air_bubbles_timestamp = 0.0 # Initialize status of ADV air bubble detectors to fluid (no air bubble) detected self.air_bubbles_status = [self.FLUID_DETECTED_STATUS] # Initialize state of ADV air bubble detectors state machine to normal self.air_bubbles_state = [self.AIR_BUBBLE_NORMAL_STATE] def get_air_bubble_status(self, index: int) -> int: """ Gets the given air bubble status using index 0 for ADV. @param index: integer - 0 for getting ADV detector status @return: Air bubble status (air bubble or fluid) for given detector """ return self.air_bubbles_status[index] def get_air_bubble_state(self, index: int) -> int: """ Gets the given air bubble state using index 0 for ADV. @param index: integer - 0 for getting ADV detector state @return: integer - air bubble state (0: init, 1: self-test, 2: normal) for given detector """ return self.air_bubbles_state[index] @publish(["hd_air_bubbles_timestamp", "air_bubbles_status", "air_bubbles_state"]) def _handler_air_bubbles_data_sync(self, message, timestamp=0.0): """ Handles published air bubbles data messages. Air bubble status and state are captured for ADV detector. @param message: published air bubbles data message as: ADV status, ADV state @return: None """ adv_status = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])) adv_state = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])) self.air_bubbles_status = [adv_status[0]] self.air_bubbles_state = [adv_state[0]] self.hd_air_bubbles_timestamp = timestamp def cmd_air_bubble_status_override(self, status: int, index: int, reset: int = NO_RESET) -> int: """ Constructs and sends the air bubble detector status override command Constraints: Must be logged into HD. Given detector must be one of the detectors listed below. @param status: unsigned int - status (0=air bubble, 1=fluid) to override detector with @param index: integer - 0 for ADV status override @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) i = integer_to_bytearray(index) stat = integer_to_bytearray(status) payload = rst + stat + i message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSG_ID_HD_BUBBLE_STATUS_OVERRIDE.value, payload=payload) if index == self.ADV: self.logger.debug("Override air bubble detector ADV status value") # 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 def cmd_air_bubble_self_test_request(self, index: int) -> int: """ Request air bubble self-test for a given detector (ADV) Constraints: Must be logged into HD. @param index: integer - 0 for ADV status override @return: 1 if successful, zero otherwise """ payload = integer_to_bytearray(index) message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSG_ID_HD_BUBBLE_SELF_TEST_REQUEST.value, payload=payload) if index == self.ADV: self.logger.debug("Request air bubble self-test for detector ADV") # 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 def cmd_air_bubbles_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the air bubbles data broadcast interval override command Constraints: Must be logged into HD. Given interval must be positive non-zero and a multiple of the HD priority 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 ms > 0 and ms % 50 == 0: rst = integer_to_bytearray(reset) mis = integer_to_bytearray(ms) payload = rst + mis message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSG_ID_HD_BUBBLES_DATA_SEND_INTERVAL_OVERRIDE.value, payload=payload) self.logger.debug("Override HD air bubbles data broadcast interval to"+str(ms)+"ms") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: if reset == RESET: str_res = "reset back to normal: " else: str_res = str(ms) + " ms: " self.logger.debug("Air bubbles data broadcast interval overridden to " + str_res + str(received_message['message'][DenaliMessage.PAYLOAD_START_INDEX])) # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False elif ms <= 0: self.logger.debug("ms must be positive non-zero.") return False elif ms % 50 != 0: self.logger.debug("ms must be a multiple of 50.") return False else: self.logger.debug("ms must be an integer.") return False def cmd_venous_bubble_alarm_enable(self, enabled: bool = True) -> int: """ Constructs and sends the venous bubble alarm detection enable/disable command Constraints: Must be logged into HD. @param enabled: bool - True enables alarm detection, False disables alarm detection @return: 1 if successful, zero otherwise """ ena = 1 if enabled else 0 payload = integer_to_bytearray(ena) message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSG_ID_HD_ENABLE_VENOUS_BUBBLE_ALARM_DETECTION.value, payload=payload) if enabled: self.logger.debug("Enable venous bubble alarm detection") else: self.logger.debug("Disable venous bubble alarm detection") # 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