Index: dialin/common/msg_ids.py =================================================================== diff -u -r26de67dd19c2cc0f475d1e77b4a887857841f6c1 -r7d1ec34cc1dafe7dea3703080e546fc47cf190f2 --- dialin/common/msg_ids.py (.../msg_ids.py) (revision 26de67dd19c2cc0f475d1e77b4a887857841f6c1) +++ dialin/common/msg_ids.py (.../msg_ids.py) (revision 7d1ec34cc1dafe7dea3703080e546fc47cf190f2) @@ -231,8 +231,8 @@ MSG_ID_HD_SYRINGE_PUMP_ADC_DAC_STATUS_OVERRIDE = 0X805B MSG_ID_HD_SYRINGE_PUMP_ADC_READ_COUNTER_OVERRIDE = 0X805C MSG_ID_HD_BUBBLES_DATA_SEND_INTERVAL_OVERRIDE = 0X805D - MSG_ID_HD_BUBBLES_STATUS_OVERRIDE = 0X805E - MSG_ID_HD_BUBBLES_SELF_TEST_REQUEST = 0X8060 + MSG_ID_HD_BUBBLE_STATUS_OVERRIDE = 0X805E + MSG_ID_HD_BUBBLE_SELF_TEST_REQUEST = 0X8060 MSG_ID_DG_VOLTAGES_DATA = 0X86 MSG_ID_DG_CHEM_DISINFECT_DATA = 0X87 MSG_ID_PRESSURE_OCCLUSION_DATA = 0X9 Index: dialin/hd/air_bubble.py =================================================================== diff -u --- dialin/hd/air_bubble.py (revision 0) +++ dialin/hd/air_bubble.py (revision 7d1ec34cc1dafe7dea3703080e546fc47cf190f2) @@ -0,0 +1,203 @@ +########################################################################### +# +# Copyright (c) 2019-2021 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_bubble.py +# +# @author (last) Peman Montazemi +# @date (last) 18-May-2021 +# @author (original) Peman Montazemi +# @date (original) 18-May-2021 +# +############################################################################ +import struct +from ..utils.conversions import integer_to_bytearray, float_to_bytearray +from ..common.msg_defs import MsgIds, MsgFieldPositions +from .constants import RESET, NO_RESET +from ..protocols.CAN import (DenaliMessage, DenaliChannels) +from ..utils.base import _AbstractSubSystem, _publish +from logging import Logger + + +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 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_INIT_STATE = 0 # Initial state + AIR_BUBBLE_SELF_TEST_STATE = 1 # Self-test state + AIR_BUBBLE_NORMAL_STATE = 2 # Normal 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) + + # Initialize status of ADA and ADV air bubble detectors to fluid (no air bubble) detected + self.air_bubbles_status = [self.FLUID_DETECTED_STATUS, self.FLUID_DETECTED_STATUS] + + # Initialize state of ADA and ADV air bubble detectors state machine to init + self.air_bubbles_state = [self.AIR_BUBBLE_INIT_STATE, self.AIR_BUBBLE_INIT_STATE] + + def get_air_bubble_status(self, index): + """ + Gets the given air bubble status using index 0 for ADA and index 1 for ADV. + + @param index: integer - 0 for getting ADA detector status, 1 for getting ADV detector status + @return: Air bubble status (air bubble or fluid) for given detector + """ + + return self.air_bubbles_status[index] + + @_publish("air_bubbles_data") + def _handler_air_bubbles_data_sync(self, message): + """ + Handles published air bubbles data messages. Air bubble status and state are captured + for ADA and ADV detectors. + + @param message: published air bubbles data message as: ADA status, ADA state, ADV status, ADV state + @return: None + """ + + statusADA = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])) + stateADA = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])) + statusADV = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3])) + stateADV = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4])) + + self.air_bubbles_status = [statusADA[0], statusADV[0]] + self.air_bubbles_state = [stateADA[0], stateADV[0]] + + def cmd_air_bubble_status_override(self, status, index, reset=NO_RESET): + """ + 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 detected: unsigned int - detected (0=air bubble, 1=fluid) to override detector with + @param index: integer - 0 for ADA status override, 1 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 == 0: + self.logger.debug("Override air bubble detector ADA status value") + elif index == 1: + 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): + """ + Request air bubble self-test for a given detector (ADA or ADV) + Constraints: + Must be logged into HD. + + @param index: integer - 0 for ADA status override, 1 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_BUBBLES_SELF_TEST_REQUEST.value, + payload=payload) + + if index == 0: + self.logger.debug("Request air bubble self-test for detector ADA") + elif index == 1: + 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, reset=NO_RESET): + """ + Constructs and sends the air bubbles data broadcast interval override command + Constraints: + Must be logged into HD. + Given interval must be non-zero and a multiple of the HD priority task interval (10 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 + """ + + 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"+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