########################################################################### # # 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) Zoltan Miskolci # @date (last) 09-Jan-2026 # @author (original) Peman Montazemi # @date (original) 18-May-2021 # ############################################################################ 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.common.td_defs import TDAirBubbleDetectorNames, TDAirBubbleDetectorAttributes from leahi_dialin.protocols.CAN import 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 self.msg_id_td_bubbles_data = MsgIds.MSG_ID_TD_BUBBLES_DATA.value self.can_interface.register_receiving_publication_function(channel_id, self.msg_id_td_bubbles_data, 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(["msg_id_td_bubbles_data", "h18_bubble_detector", "td_air_bubbles_timestamp"]) 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_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 """ return cmd_generic_broadcast_interval_override( ms = ms, reset = reset, channel_id = DenaliChannels.dialin_to_td_ch_id, msg_id = MsgIds.MSG_ID_TD_BUBBLE_PUBLISH_INTERVAL_OVERRIDE_REQUEST, module_name = 'TD Air Bubble Detector', logger = self.logger, can_interface = self.can_interface) 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 sensor_name = TDAirBubbleDetectorNames(index).name.split('_')[0] return cmd_generic_override( payload = payload, reset = reset, channel_id = DenaliChannels.dialin_to_td_ch_id, msg_id = MsgIds.MSG_ID_TD_BUBBLE_OVERRIDE_REQUEST, entity_name = f'TD {sensor_name} Bubble Detector status', override_text = f'{str(status)}', logger = self.logger, can_interface = self.can_interface)