########################################################################### # # Copyright (c) 2020-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 valves.py # # @author (last) Micahel Garthwaite # @date (last) 17-Aug-2023 # @author (original) Peman Montazemi # @date (original) 19-May-2020 # ############################################################################ import struct from enum import unique from logging import Logger from collections import OrderedDict from .constants import NO_RESET from leahi_dialin.common.msg_defs import MsgIds from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels 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 # Valve states ENERGIZED = True DEENERGIZED = False @unique class DDValveStates(DialinEnum): VALVE_STATE_CLOSED = 0 VALVE_STATE_OPEN = 1 @unique class DDValveNames(DialinEnum): D14_VHO = 0 # Valve Hydraulics Outlet (D14) D52_VTD = 1 # Valve Thermal Disinfect (D52) D8_VHB = 2 # Valve Hydraulics Bypass (D8) D54_VRP = 3 # Valve Rinse Port (D54) D53_VDR = 4 # Valve Drain (D53) D65_VDB2 = 5 # Valve DryBcarb Inlet (D65) D64_VP1 = 6 # Valve Purge 1 (D64) D31_VPT = 7 # Valve Pressure Test (D31) D34_VDB1 = 8 # Valve Dialyzer Bypass (D34) D35_VDI = 9 # Valve Dialyzer Inlet (D35) D40_VDO = 10 # Valve Dialyzer Outlet (D40) D47_VP2 = 11 # Valve Dialysate Out Purge 2 (D47) D3_VHI = 12 # Valve Hydraulics Inlet (D3) M4_VWI = 13 # Valve Water Inlet (M4) RSRVD_SPACE1 = 14 # This space has been reserved RSRVD_SPACE2 = 15 # This space has been reserved D23_BCV1 = 16 # Balancing chamber Valve 1 (D23) D19_BCV2 = 17 # Balancing chamber Valve 2 (D19) D25_BCV3 = 18 # Balancing chamber Valve 3 (D25) D26_BCV7 = 19 # Balancing chamber Valve 7 (D26) D24_BCV5 = 20 # Balancing chamber Valve 5 (D24) D20_BCV6 = 21 # Balancing chamber Valve 6 (D20) D21_BCV4 = 22 # Balancing chamber Valve 4 (D21) D22_BCV8 = 23 # Balancing chamber Valve 8 (D22) D69_UFI1 = 24 # Ultrafiltration Valve 1 Inlet (D69) D71_UFI2 = 25 # Ultrafiltration Valve 2 Inlet (D71) D70_UFO1 = 26 # Ultrafiltration Valve 1 Outlet (D70) D72_UFO2 = 27 # Ultrafiltration Valve 2 Outlet (D72) class DDValves(AbstractSubSystem): """ Dialysate Delivery (DD) interface for valve related commands. """ # Valves states publish message field positions START_POS_VALVES_STATES = DenaliMessage.PAYLOAD_START_INDEX END_POS_VALVES_STATES = START_POS_VALVES_STATES + 2 # Valves States come in as a U16 value (2 bytes) START_POS_BCV_VALVES_STATES = END_POS_VALVES_STATES END_POS_BCV_VALVES_STATES = START_POS_BCV_VALVES_STATES + 1 START_POS_UFI_VALVES_STATES = END_POS_BCV_VALVES_STATES END_POS_UFI_VALVES_STATES = START_POS_UFI_VALVES_STATES + 1 END_POS_ALL_VALVES = START_POS_VALVES_STATES + 4 def __init__(self, can_interface, logger: Logger): """ @param can_interface: Denali CAN Messenger object """ super().__init__() self.can_interface = can_interface self.logger = logger self.valves_sensed_states = OrderedDict() self.dg_valves_states_timestamp = 0.0 if self.can_interface is not None: channel_id = DenaliChannels.dd_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_DD_VALVES_STATES_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_valves_sync) self.valve_states_all = 0x00000000 self.d14_vho = {"id": DDValveNames.D14_VHO.value, "state": DEENERGIZED} self.d52_vtd = {"id": DDValveNames.D52_VTD.value, "state": DEENERGIZED} self.d8_vhb = {"id": DDValveNames.D8_VHB.value, "state": DEENERGIZED} self.d54_vrp = {"id": DDValveNames.D54_VRP.value, "state": DEENERGIZED} self.d53_vdr = {"id": DDValveNames.D53_VDR.value, "state": DEENERGIZED} self.d65_vdb2 = {"id": DDValveNames.D65_VDB2.value, "state": DEENERGIZED} self.d64_vp1 = {"id": DDValveNames.D64_VP1.value, "state": DEENERGIZED} self.d31_vpt = {"id": DDValveNames.D31_VPT.value, "state": DEENERGIZED} self.d34_vdb1 = {"id": DDValveNames.D34_VDB1.value, "state": DEENERGIZED} self.d35_vdi = {"id": DDValveNames.D35_VDI.value, "state": DEENERGIZED} self.d40_vdo = {"id": DDValveNames.D40_VDO.value, "state": DEENERGIZED} self.d47_vp2 = {"id": DDValveNames.D47_VP2.value, "state": DEENERGIZED} self.d3_vhi = {"id": DDValveNames.D3_VHI.value, "state": DEENERGIZED} self.m4_vwi = {"id": DDValveNames.M4_VWI.value, "state": DEENERGIZED} self.rsvrd1 = {"id": DDValveNames.RSRVD_SPACE1.value, "state": DEENERGIZED} self.rsvrd2 = {"id": DDValveNames.RSRVD_SPACE2.value, "state": DEENERGIZED} self.d23_bcv1 = {"id": DDValveNames.D23_BCV1.value, "state": DEENERGIZED} self.d19_bcv2 = {"id": DDValveNames.D19_BCV2.value, "state": DEENERGIZED} self.d25_bcv3 = {"id": DDValveNames.D25_BCV3.value, "state": DEENERGIZED} self.d26_bcv7 = {"id": DDValveNames.D26_BCV7.value, "state": DEENERGIZED} self.d24_bcv5 = {"id": DDValveNames.D24_BCV5.value, "state": DEENERGIZED} self.d20_bcv6 = {"id": DDValveNames.D20_BCV6.value, "state": DEENERGIZED} self.d21_bcv4 = {"id": DDValveNames.D21_BCV4.value, "state": DEENERGIZED} self.d22_bcv8 = {"id": DDValveNames.D22_BCV8.value, "state": DEENERGIZED} self.d69_ufi1 = {"id": DDValveNames.D69_UFI1.value, "state": DEENERGIZED} self.d71_ufi2 = {"id": DDValveNames.D71_UFI2.value, "state": DEENERGIZED} self.d70_ufo1 = {"id": DDValveNames.D70_UFO1.value, "state": DEENERGIZED} self.d72_ufo2 = {"id": DDValveNames.D72_UFO2.value, "state": DEENERGIZED} # NOTE: The len function counts the enums with the same number only once. This is not the case in the DG valves # class because each valve must have a unique ID. self.valve_states_enum = [0 for _ in range(len(DDValveNames))] for valve in DDValveNames.__members__: self.valves_sensed_states[valve] = '' def get_valve_states(self): """ Gets the valve states @return: All valve states """ return [ self.d14_vho.get("state", None), self.d52_vtd.get("state", None), self.d8_vhb.get("state", None), self.d54_vrp.get("state", None), self.d53_vdr.get("state", None), self.d65_vdb2.get("state", None), self.d64_vp1.get("state", None), self.d31_vpt.get("state", None), self.d34_vdb1.get("state", None), self.d35_vdi.get("state", None), self.d40_vdo.get("state", None), self.d47_vp2.get("state", None), self.d3_vhi.get("state", None), self.m4_vwi.get("state", None), self.rsvrd1.get("state", None), self.rsvrd2.get("state", None), self.d23_bcv1.get("state", None), self.d19_bcv2.get("state", None), self.d25_bcv3.get("state", None), self.d26_bcv7.get("state", None), self.d24_bcv5.get("state", None), self.d20_bcv6.get("state", None), self.d21_bcv4.get("state", None), self.d22_bcv8.get("state", None), self.d69_ufi1.get("state", None), self.d71_ufi2.get("state", None), self.d70_ufo1.get("state", None), self.d72_ufo2.get("state", None) ] @staticmethod def sort_by_id(observation): """ Converts a published dictionary of valve state information to an ordered list of tuples. @param observation: dictionary of the observed valve states @return: a list of tuples of the valve states """ result = [] for key, value in observation.items(): if isinstance(value, dict): result.append((key, value.get("id", None), value.get("state", None))) result = sorted(result, key=lambda each: each[1]) return result @staticmethod def _binary_to_valve_state(binary) -> bool: """ @param binary: binary value @return: 1 = energized, otherwise de-energized """ if binary != 0: return ENERGIZED else: return DEENERGIZED @publish([ "dd_valves_states_timestamp", "valve_states_all", "d14_vho", "d52_vtd", "d8_vhb", "d54_vrp", "d53_vdr", "d65_vdb2", "d64_vp1", "d31_vpt", "d34_vdb1", "d35_vdi", "d40_vdo", "d47_vp2", "d3_vhi", "m4_vwi", "rsvrd1", "rsvrd2", "d23_bcv1", "d19_bcv2", "d25_bcv3", "d26_bcv7", "d24_bcv5", "d20_bcv6", "d21_bcv4", "d22_bcv8", "d69_ufi1", "d71_ufi2", "d70_ufo1", "d72_ufo2", "valve_states_enum" ]) def _handler_valves_sync(self, message, timestamp=0.0): """ Handles published valves states message. @param message: published valves states message @return: none """ vsa = struct.unpack('I', bytearray(message['message'][self.START_POS_VALVES_STATES:self.END_POS_ALL_VALVES])) self.valve_states_all = vsa[0] vst = struct.unpack('H', bytearray(message['message'][self.START_POS_VALVES_STATES:self.END_POS_VALVES_STATES])) # Extract each valve state from U16 valves states using bit-masking self.d14_vho["state"] = self._binary_to_valve_state(vst[0] & 1) self.d52_vtd["state"] = self._binary_to_valve_state(vst[0] & 2) self.d8_vhb["state"] = self._binary_to_valve_state(vst[0] & 4) self.d54_vrp["state"] = self._binary_to_valve_state(vst[0] & 8) self.d53_vdr["state"] = self._binary_to_valve_state(vst[0] & 16) self.d65_vdb2["state"] = self._binary_to_valve_state(vst[0] & 32) self.d64_vp1["state"] = self._binary_to_valve_state(vst[0] & 64) self.d31_vpt["state"] = self._binary_to_valve_state(vst[0] & 128) self.d34_vdb1["state"] = self._binary_to_valve_state(vst[0] & 256) self.d35_vdi["state"] = self._binary_to_valve_state(vst[0] & 512) self.d40_vdo["state"] = self._binary_to_valve_state(vst[0] & 1024) self.d47_vp2["state"] = self._binary_to_valve_state(vst[0] & 2048) self.d3_vhi["state"] = self._binary_to_valve_state(vst[0] & 4096) self.m4_vwi["state"] = self._binary_to_valve_state(vst[0] & 8192) self.rsvrd1["state"] = self._binary_to_valve_state(vst[0] & 16384) self.rsvrd2["state"] = self._binary_to_valve_state(vst[0] & 32768) bcv = struct.unpack('B', bytearray(message['message'][self.START_POS_BCV_VALVES_STATES:self.END_POS_BCV_VALVES_STATES])) self.d23_bcv1["state"] = self._binary_to_valve_state(bcv[0] & 1) self.d19_bcv2["state"] = self._binary_to_valve_state(bcv[0] & 2) self.d25_bcv3["state"] = self._binary_to_valve_state(bcv[0] & 4) self.d26_bcv7["state"] = self._binary_to_valve_state(bcv[0] & 8) self.d24_bcv5["state"] = self._binary_to_valve_state(bcv[0] & 16) self.d20_bcv6["state"] = self._binary_to_valve_state(bcv[0] & 32) self.d21_bcv4["state"] = self._binary_to_valve_state(bcv[0] & 64) self.d22_bcv8["state"] = self._binary_to_valve_state(bcv[0] & 128) ufi = struct.unpack('B', bytearray(message['message'][self.START_POS_UFI_VALVES_STATES:self.END_POS_UFI_VALVES_STATES])) self.d69_ufi1["state"] = self._binary_to_valve_state(ufi[0] & 1) self.d71_ufi2["state"] = self._binary_to_valve_state(ufi[0] & 2) self.d70_ufo1["state"] = self._binary_to_valve_state(ufi[0] & 4) self.d72_ufo2["state"] = self._binary_to_valve_state(ufi[0] & 8) start = self.END_POS_UFI_VALVES_STATES end = start + 1 for valve_id in self.valves_sensed_states: valve_state_number = struct.unpack('B', bytearray(message['message'][start:end]))[0] self.valves_sensed_states[valve_id] = DDValveNames(valve_state_number).name start = end end += 1 self.dg_valves_states_timestamp = timestamp def cmd_valve_sensed_state_override(self, valve: int, state: bool, reset: int = NO_RESET) -> int: """ Constructs and sends the valve sensed state override command. Constraints: Must be logged into DD. Given valve ID must be one of the valve IDs listed below. @param valve: unsigned int - valve ID @param state: bool - valve state @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) ste = integer_to_bytearray(int(state)) vlv = integer_to_bytearray(valve) payload = rst + ste + vlv message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_VALVE_SENSED_STATE_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("Override valve sensed state") # 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_valve_override(self, valve: int, state: bool, reset: int = NO_RESET) -> int: """ Constructs and sends the valve state override command. Constraints: Must be logged into DD. Given valve ID must be one of the valve IDs listed below. @param valve: unsigned int - valve ID @param state: bool - valve state @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) ste = integer_to_bytearray(int(state)) vlv = integer_to_bytearray(valve) payload = rst + ste + vlv message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_VALVE_STATE_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("Override valve state") # 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_bc_valves_override(self, bcv1: bool, bcv2: bool, bcv3: bool, bcv4: bool, bcv5: bool, bcv6: bool, bcv7: bool, bcv8: bool) -> int: """ Constructs and sends the balancing chamber valve states override command. Constraints: Must be logged into DD. Given valve ID must be one of the valve IDs listed below. @param bcv1: bool - valve state for BCV1 (true=open, false=closed) @param bcv2: bool - valve state for BCV2 (true=open, false=closed) @param bcv3: bool - valve state for BCV3 (true=open, false=closed) @param bcv4: bool - valve state for BCV4 (true=open, false=closed) @param bcv5: bool - valve state for BCV5 (true=open, false=closed) @param bcv6: bool - valve state for BCV6 (true=open, false=closed) @param bcv7: bool - valve state for BCV7 (true=open, false=closed) @param bcv8: bool - valve state for BCV8 (true=open, false=closed) @return: 1 if successful, zero otherwise """ valve = 0 if bcv1: valve = valve | 0x1 if bcv2: valve = valve | 0x2 if bcv3: valve = valve | 0x4 if bcv7: valve = valve | 0x8 if bcv5: valve = valve | 0x10 if bcv6: valve = valve | 0x20 if bcv4: valve = valve | 0x40 if bcv8: valve = valve | 0x80 payload = integer_to_bytearray(valve) message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_BC_VALVE_STATES_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("Override balancing chamber valve states") # 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_valve_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the valve state override command. Constraints: Must be logged into DG. Given interval must be non-zero and a multiple of the DG general task interval (50 ms). @param ms: unsigned int - broadcast interval (in ms) @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ if not check_broadcast_interval_override_ms(ms): return False rst = integer_to_bytearray(reset) ivl = integer_to_bytearray(ms) payload = rst + ivl message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_VALVE_PUBLISH_INTERVAL_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override valves states publish interval") # Send message received_message = self.can_interface.send(message) # If there is content in message if received_message is not None: # Response payload is OK or not return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Timeout!!!!") return False def cmd_valve_set_open_close(self, valve: int, state: int) -> int: """ Constructs and sends the valve open close command @param valve: unsigned int - dialysate pump ID @param command: int - value to command the dialysate pump @param speed: integer - rpm to set the speed to @return: 1 if successful, zero otherwise """ vlv = integer_to_bytearray(valve) sts = integer_to_bytearray(state) payload = vlv + sts message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_VALVES_OPEN_CLOSE_STATE_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("setting " + str(state) + " - for valve: " + str(valve)) # 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.error("Timeout!!!!") return False