Index: leahi_dialin/td/modules/treatment.py =================================================================== diff -u -r0c1da102269169d5b63f91e146488b20eaa883a7 -r8c823851a48f72a6ac2a26cc691063d214288231 --- leahi_dialin/td/modules/treatment.py (.../treatment.py) (revision 0c1da102269169d5b63f91e146488b20eaa883a7) +++ leahi_dialin/td/modules/treatment.py (.../treatment.py) (revision 8c823851a48f72a6ac2a26cc691063d214288231) @@ -15,15 +15,45 @@ ############################################################################ import struct from logging import Logger +from enum import unique from .constants import RESET, NO_RESET from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels -from leahi_dialin.utils.base import AbstractSubSystem, publish +from leahi_dialin.utils.base import AbstractSubSystem, publish, DialinEnum from leahi_dialin.utils.checks import check_broadcast_interval_override_ms from leahi_dialin.utils.conversions import integer_to_bytearray, float_to_bytearray +@unique +class TDTreatmentParameters(DialinEnum): + TREATMENT_PARAM_BLOOD_FLOW = 0 # Blood flow rate (in mL/min) + TREATMENT_PARAM_FIRST_UINT = TREATMENT_PARAM_BLOOD_FLOW # First unsigned integer treatment parameter + TREATMENT_PARAM_DIALYSATE_FLOW = 1 # Dialysate flow rate (in mL/min) + TREATMENT_PARAM_TREATMENT_DURATION = 2 # Treatment duration (in minutes) + TREATMENT_PARAM_SALINE_BOLUS_VOLUME = 3 # Saline bolus volume (in mL) + TREATMENT_PARAM_HEPARIN_STOP_TIME = 4 # Heparin stop time (in minutes) + TREATMENT_PARAM_HEPARIN_TYPE = 5 # Heparin type (enum) + TREATMENT_PARAM_ACID_CONCENTRATE = 6 # Acid concentrate type (enum) + TREATMENT_PARAM_BICARB_CONCENTRATE = 7 # Bicarbonate concentrate type (enum) + TREATMENT_PARAM_DIALYZER_TYPE = 8 # Dialysate type (enum) + TREATMENT_PARAM_BP_MEAS_INTERVAL = 9 # Blood pressure measurement interval (in minutes) + TREATMENT_PARAM_RINSEBACK_FLOW_RATE = 10 # Rinseback flow rate (in mL/min) + TREATMENT_PARAM_RINSEBACK_VOLUME = 11 # Rinseback volume (in mL) + TREATMENT_PARAM_LAST_UINT = TREATMENT_PARAM_RINSEBACK_VOLUME # Last unsigned integer treatment parameter + TREATMENT_PARAM_ART_PRES_LIMIT_WINDOW = 12 # Arterial pressure alarm limit window (in mmHg) + TREATMENT_PARAM_FIRST_INT = TREATMENT_PARAM_ART_PRES_LIMIT_WINDOW # First integer treatment parameter + TREATMENT_PARAM_VEN_PRES_LIMIT_WINDOW = 13 # Venous pressure alarm limit window (in mmHg) + TREATMENT_PARAM_VEN_PRES_LIMIT_ASYMMETRIC = 14 # Venous pressure alarm limit asymmetric (in mmHg) + TREATMENT_PARAM_TMP_PRES_LIMIT_WINDOW = 15 # TMP alarm limit window (in mmHg) + TREATMENT_PARAM_LAST_INT = TREATMENT_PARAM_TMP_PRES_LIMIT_WINDOW # Last integer treatment parameter + TREATMENT_PARAM_DIALYSATE_TEMPERATURE = 16 # Dialysate temperature (in degC) + TREATMENT_PARAM_FIRST_F32 = TREATMENT_PARAM_DIALYSATE_TEMPERATURE # First floating point treatment parameter + TREATMENT_PARAM_HEPARIN_DISPENSE_RATE = 17 # Heparin dispense rate (in mL/hr) + TREATMENT_PARAM_HEPARIN_BOLUS_VOLUME = 18 # Heparin bolus volume (in mL) + TREATMENT_PARAM_UF_VOLUME = 19 # Ultrafiltration volume (in liters) - provided separately by UI + NUM_OF_TREATMENT_PARAMS = 20 # Total number of treatment parameters + class TDTreatment(AbstractSubSystem): """ Treatment Delivery (TD) Dialin API sub-class for treatment related commands. @@ -55,6 +85,9 @@ msg_id = MsgIds.MSG_ID_TD_TREATMENT_STATE_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_treatment_state_sync) + msg_id = MsgIds.MSG_ID_TD_RSP_CURRENT_TREATMENT_PARAMETERS.value + self.can_interface.register_receiving_publication_function(channel_id, msg_id, + self._handler_resp_treatment_parameters_sync) # Treatment param ranges data self.min_tx_time = 0 @@ -96,6 +129,29 @@ self.tx_end_state = 0 self.tx_state_timestamp = 0 + # Treatment Parameters Data. Most recent response. + self.blood_flow_rate = 0 + self.dialysate_flow_rate = 0 + self.tx_duration = 0 + self.saline_bolus_volume = 0 + self.hep_stop_time = 0 + self.hep_time = 0 + self.acid_con = 0 + self.bicarb_con = 0 + self.dialyzer_type = 0 + self.bp_interval = 0 + self.rb_flow_rate = 0 + self.rb_volume = 0 + self.art_pressure_window = 0 + self.venous_pressure_window = 0 + self.venous_asymm_window = 0 + self.tmp_limit_window = 0 + self.dialysate_temp = 0.0 + self.hep_dispense_rate = 0.0 + self.hep_bolus_vol = 0.0 + self.uf_vol = 0.0 + self.tx_param_req_timestamp = 0 + @publish(["min_tx_time","max_tx_time","min_uf_volume","max_uf_volume", "min_dial_rate","max_dial_rate","tx_params_timestamp"]) def _handler_treatment_param_ranges_sync(self, message, timestamp=0.0): @@ -210,3 +266,97 @@ message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.tx_state_timestamp = timestamp + + @publish(["blood_flow_rate", "dialysate_flow_rate", "tx_duration", "saline_bolus_volume", + "hep_stop_time", "hep_time", "acid_con", "bicarb_con", + "dialyzer_type", "bp_interval", "rb_flow_rate", "rb_volume", + "art_pressure_window","venous_pressure_window","venous_asymm_window","tmp_limit_window", + "dialysate_temp", "hep_dispense_rate", "hep_bolus_vol", "uf_vol"]) + def _handler_resp_treatment_parameters_sync(self, message, timestamp=0.0): + """ + Handles published treatment parameter response data messages. + + @param message: published treatment parameter response data message + @return: none + """ + + self.blood_flow_rate = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] + self.dialysate_flow_rate = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] + self.tx_duration = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] + self.saline_bolus_volume = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] + self.hep_stop_time = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] + self.hep_time = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] + self.acid_con = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] + self.bicarb_con = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] + self.dialyzer_type = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9]))[0] + self.bp_interval = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_10:MsgFieldPositions.END_POS_FIELD_10]))[0] + self.rb_flow_rate = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_11:MsgFieldPositions.END_POS_FIELD_11]))[0] + self.rb_volume = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_12:MsgFieldPositions.END_POS_FIELD_12]))[0] + self.art_pressure_window = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_13:MsgFieldPositions.END_POS_FIELD_13]))[0] + self.venous_pressure_window = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_14:MsgFieldPositions.END_POS_FIELD_14]))[0] + self.venous_asymm_window = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_15:MsgFieldPositions.END_POS_FIELD_15]))[0] + self.tmp_limit_window = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_16:MsgFieldPositions.END_POS_FIELD_16]))[0] + self.dialysate_temp = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_17:MsgFieldPositions.END_POS_FIELD_17]))[0] + self.hep_dispense_rate = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_18:MsgFieldPositions.END_POS_FIELD_18]))[0] + self.hep_bolus_vol = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_19:MsgFieldPositions.END_POS_FIELD_19]))[0] + self.uf_vol = struct.unpack('i', bytearray( + message['message'][MsgFieldPositions.START_POS_FIELD_20:MsgFieldPositions.END_POS_FIELD_20]))[0] + + self.tx_param_req_timestamp = timestamp + + def cmd_set_treatment_parameter(self, tx_param_value, tx_param_id: int = 0 ): + """ + + Constraints: + Must be logged into TD. + @param tx_param_value: varied - value to set the treatment parameter + @param tx_param_id: integer - the param id of the treatment paramter + + @return: 1 if successful, zero otherwise + """ + idx = integer_to_bytearray(tx_param_id) + + if tx_param_id <= TDTreatmentParameters.TREATMENT_PARAM_LAST_UINT.value: + tpv = integer_to_bytearray(tx_param_value) + + elif tx_param_id >= TDTreatmentParameters.TREATMENT_PARAM_FIRST_F32.value: + tpv = float_to_bytearray(tx_param_value) + + else: + tpv = integer_to_bytearray(tx_param_value) + + payload = idx + tpv + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_td_ch_id, + message_id=MsgIds.MSG_ID_TD_SET_TREATMENT_PARAMETER.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 \ No newline at end of file