########################################################################### # # 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 leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.common.td_defs import TDAirBubbleDetectorNames, TDAirBubbleDetectorAttributes from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels from leahi_dialin.utils.base import AbstractSubSystem, publish from leahi_dialin.utils.conversions import integer_to_bytearray class TDBubbleDetector(AbstractSubSystem): """ TDBubbleDetector Treatment Delivery (TD) Dialin API sub-class for air bubbles related commands. ADA: Air bubble Detector Arterial ADV: Air bubble Detector Venous """ # Air bubble detectors H18_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.td_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_TD_BUBBLES_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_air_bubbles_data_sync) self.td_air_bubbles_timestamp = 0.0 self.h18_bubble_detector = { TDAirBubbleDetectorNames.H18_ADV.name: { # Initialize status of ADV air bubble detectors to fluid (no air bubble) detected TDAirBubbleDetectorAttributes.STATUS.name: self.FLUID_DETECTED_STATUS, # Initialize state of ADV air bubble detectors state machine to normal TDAirBubbleDetectorAttributes.STATE.name: self.AIR_BUBBLE_NORMAL_STATE } } @publish(["td_air_bubbles_timestamp", "h18_bubble_detector"]) def _handler_air_bubbles_data_sync(self, message, timestamp=0.0): """ Handles published air bubbles data messages. Air bubble status and state are captured. @param message: published air bubbles data message as: h18 bubble status, h18 bubble state @return: None """ self.h18_bubble_detector[TDAirBubbleDetectorNames.H18_ADV.name][TDAirBubbleDetectorAttributes.STATUS.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])))[0] self.h18_bubble_detector[TDAirBubbleDetectorNames.H18_ADV.name][TDAirBubbleDetectorAttributes.STATE.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])))[0] self.td_air_bubbles_timestamp = timestamp def cmd_air_bubble_status_override(self, index: int, status: int, reset: int = NO_RESET) -> int: """ Constructs and sends the air bubble detector status override command Constraints: Must be logged into TD. 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_td_ch_id, message_id=MsgIds.MSG_ID_TD_BUBBLE_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.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 TD. Given interval must be positive non-zero and a multiple of the TD 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_td_ch_id, message_id=MsgIds.MSG_ID_TD_BUBBLE_PUBLISH_INTERVAL_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("Override TD 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