Index: leahi_dialin/td/modules/bubble_detector.py =================================================================== diff -u -rafe332fec54d9d0432dfc0d54aef1debaa92b066 -r38e8dd31728056dbb7f9304c98ab16d7147b75a2 --- leahi_dialin/td/modules/bubble_detector.py (.../bubble_detector.py) (revision afe332fec54d9d0432dfc0d54aef1debaa92b066) +++ leahi_dialin/td/modules/bubble_detector.py (.../bubble_detector.py) (revision 38e8dd31728056dbb7f9304c98ab16d7147b75a2) @@ -7,33 +7,36 @@ # # @file air_bubbles.py # -# @author (last) Sean Nash -# @date (last) 04-May-2023 +# @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 .constants import RESET, NO_RESET +from leahi_dialin.common.constants import NO_RESET from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions -from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels +from leahi_dialin.common.override_templates import cmd_generic_broadcast_interval_override, cmd_generic_override +from leahi_dialin.common.td_defs import td_enum_repository +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 TDBubbleDectector(AbstractSubSystem): +class TDBubbleDetector(AbstractSubSystem): """ - TDAirBubbles + 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 - ADV = 0 # Air bubble Detector Venous + H18_ADV = 0 # Air bubble Detector Venous # Air bubble detectors status BUBBLE_DETECTED_STATUS = 0 # Air bubble detected @@ -45,7 +48,6 @@ def __init__(self, can_interface, logger: Logger): """ - @param can_interface: Denali Can Messenger object """ super().__init__() @@ -54,117 +56,82 @@ 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.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 - # Initialize status of ADV air bubble detectors to fluid (no air bubble) detected - self.air_bubbles_status = [self.FLUID_DETECTED_STATUS] + self.h18_bubble_detector = { + td_enum_repository.TDAirBubbleDetectorNames.H18_ADV.name: { + # Initialize status of ADV air bubble detectors to fluid (no air bubble) detected + td_enum_repository.TDAirBubbleDetectorAttributes.STATUS.name: self.FLUID_DETECTED_STATUS, + # Initialize state of ADV air bubble detectors state machine to normal + td_enum_repository.TDAirBubbleDetectorAttributes.STATE.name: self.AIR_BUBBLE_NORMAL_STATE + } + } - # Initialize state of ADV air bubble detectors state machine to normal - self.air_bubbles_state = [self.AIR_BUBBLE_NORMAL_STATE] - @publish(["td_air_bubbles_timestamp", "air_bubbles_status", "air_bubbles_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 - for ADV detector. + Handles published air bubbles data messages. Air bubble status and state are captured. - @param message: published air bubbles data message as: ADV status, ADV state + @param message: published air bubbles data message as: h18 bubble status, h18 bubble state @return: None """ + self.h18_bubble_detector[td_enum_repository.TDAirBubbleDetectorNames.H18_ADV.name][td_enum_repository.TDAirBubbleDetectorAttributes.STATUS.name] = ( + struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])))[0] + self.h18_bubble_detector[td_enum_repository.TDAirBubbleDetectorNames.H18_ADV.name][td_enum_repository.TDAirBubbleDetectorAttributes.STATE.name] = ( + struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])))[0] - 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.td_air_bubbles_timestamp = timestamp - def cmd_air_bubble_status_override(self, status: int, index: int, reset: int = NO_RESET) -> int: + + def cmd_air_bubbles_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ - Constructs and sends the air bubble detector status override command + Constructs and sends the air bubbles data broadcast interval override command Constraints: Must be logged into TD. - Given detector must be one of the detectors listed below. + Given interval must be positive non-zero and a multiple of the TD priority task interval (50 ms). - @param status: unsigned int - status (0=air bubble, 1=fluid) to override detector with - @param index: integer - 0 for ADV status override + @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) - 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) - - 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_bubbles_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: + def cmd_air_bubble_status_override(self, index: int, status: int, reset: int = NO_RESET) -> int: """ - Constructs and sends the air bubbles data broadcast interval override command + Constructs and sends the air bubble detector status 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). + Given detector must be one of the detectors listed below. - @param ms: integer - interval (in ms) to override with + @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 - 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 \ No newline at end of file + sensor_name = td_enum_repository.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)