########################################################################### # # 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 concentrate_pumps.py # # @author (last) Micahel Garthwaite # @date (last) 07-Mar-2023 # @author (original) Micahel Garthwaite # @date (original) 29-Oct-2020 # ############################################################################ import struct from enum import unique from logging import Logger from .constants import RESET, NO_RESET from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.common.dd_defs import DDConcentratePumpsEnum, DDConcentratePumpsStates, DDConcentratePumpAttributesEnum 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, float_to_bytearray class DDConcentratePumps(AbstractSubSystem): """ Dialin API sub-class for concentrate pumps related commands. """ 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.dd_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_concentrate_pumps_sync) self.dd_concentrate_pumps = { DDConcentratePumpsEnum.D10_CP2_BICARB.name: { DDConcentratePumpAttributesEnum.CURRENT_STATE.name: 0, DDConcentratePumpAttributesEnum.CURRENT_SET_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.MEASURED_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.TARGET_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.PULSE_US.name: 0.0, DDConcentratePumpAttributesEnum.TARGET_REV_COUNT.name: 0, DDConcentratePumpAttributesEnum.MEASURED_REV_COUNT.name: 0, DDConcentratePumpAttributesEnum.PARKED.name: False, DDConcentratePumpAttributesEnum.PARK_FAULT.name: False }, DDConcentratePumpsEnum.D11_CP1_ACID.name: { DDConcentratePumpAttributesEnum.CURRENT_STATE.name: 0, DDConcentratePumpAttributesEnum.CURRENT_SET_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.MEASURED_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.TARGET_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.PULSE_US.name: 0.0, DDConcentratePumpAttributesEnum.TARGET_REV_COUNT.name: 0, DDConcentratePumpAttributesEnum.MEASURED_REV_COUNT.name: 0, DDConcentratePumpAttributesEnum.PARKED.name: False, DDConcentratePumpAttributesEnum.PARK_FAULT.name: False }, DDConcentratePumpsEnum.D76_UF.name: { DDConcentratePumpAttributesEnum.CURRENT_STATE.name: 0, DDConcentratePumpAttributesEnum.CURRENT_SET_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.MEASURED_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.TARGET_SPEED.name: 0.0, DDConcentratePumpAttributesEnum.PULSE_US.name: 0.0, DDConcentratePumpAttributesEnum.TARGET_REV_COUNT.name: 0, DDConcentratePumpAttributesEnum.MEASURED_REV_COUNT.name: 0, DDConcentratePumpAttributesEnum.PARKED.name: None, # D76 doesn't utilize this attribute DDConcentratePumpAttributesEnum.PARK_FAULT.name: None # D76 doesn't utilize this attribute } } self.dd_concentrate_pump_timestamp = 0.0 @publish(["dd_concentrate_pump_timestamp", "dd_concentrate_pumps"]) def _handler_concentrate_pumps_sync(self, message, timestamp=0.0): """ Handles published concentrate pumps' data messages. Concentrate pumps' speed data are captured for reference. @param message: published concentrate pumps' data message @return: None """ self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.CURRENT_SET_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.MEASURED_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.CURRENT_SET_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.MEASURED_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.TARGET_REV_COUNT.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.MEASURED_REV_COUNT.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.TARGET_REV_COUNT.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.MEASURED_REV_COUNT.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.CURRENT_STATE.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_9:MsgFieldPositions.END_POS_FIELD_9])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.CURRENT_STATE.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_10:MsgFieldPositions.END_POS_FIELD_10])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.PULSE_US.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_11:MsgFieldPositions.END_POS_FIELD_11])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.PULSE_US.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_12:MsgFieldPositions.END_POS_FIELD_12])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.TARGET_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_13:MsgFieldPositions.END_POS_FIELD_13])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.TARGET_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_14:MsgFieldPositions.END_POS_FIELD_14])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.PARKED.name] = \ True if struct.unpack('I', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_15:MsgFieldPositions.END_POS_FIELD_15]))[0] == 1 else False self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.PARKED.name] = \ True if struct.unpack('I', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_16:MsgFieldPositions.END_POS_FIELD_16]))[0] == 1 else False self.dd_concentrate_pumps[DDConcentratePumpsEnum.D11_CP1_ACID.name][DDConcentratePumpAttributesEnum.PARK_FAULT.name] = \ True if struct.unpack('I', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_17:MsgFieldPositions.END_POS_FIELD_17]))[0] == 1 else False self.dd_concentrate_pumps[DDConcentratePumpsEnum.D10_CP2_BICARB.name][DDConcentratePumpAttributesEnum.PARK_FAULT.name] = \ True if struct.unpack('I', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_18:MsgFieldPositions.END_POS_FIELD_18]))[0] == 1 else False self.dd_concentrate_pumps[DDConcentratePumpsEnum.D76_UF.name][DDConcentratePumpAttributesEnum.TARGET_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_19:MsgFieldPositions.END_POS_FIELD_19])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D76_UF.name][DDConcentratePumpAttributesEnum.CURRENT_SET_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_20:MsgFieldPositions.END_POS_FIELD_20])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D76_UF.name][DDConcentratePumpAttributesEnum.MEASURED_SPEED.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_21:MsgFieldPositions.END_POS_FIELD_21])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D76_UF.name][DDConcentratePumpAttributesEnum.TARGET_REV_COUNT.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_22:MsgFieldPositions.END_POS_FIELD_22])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D76_UF.name][DDConcentratePumpAttributesEnum.MEASURED_REV_COUNT.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_23:MsgFieldPositions.END_POS_FIELD_23])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D76_UF.name][DDConcentratePumpAttributesEnum.CURRENT_STATE.name] = ( struct.unpack('i', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_24:MsgFieldPositions.END_POS_FIELD_24])))[0] self.dd_concentrate_pumps[DDConcentratePumpsEnum.D76_UF.name][DDConcentratePumpAttributesEnum.PULSE_US.name] = ( struct.unpack('f', bytearray(message['message'][MsgFieldPositions.START_POS_FIELD_25:MsgFieldPositions.END_POS_FIELD_25])))[0] self.dd_concentrate_pump_timestamp = timestamp def cmd_concentrate_pump_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump data broadcast interval override command Constraints: Must be logged into DD. Given interval must be non-zero and a multiple of the DD general 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 """ if not check_broadcast_interval_override_ms(ms): return False reset_byte_array = integer_to_bytearray(reset) ms_byte_array = integer_to_bytearray(ms) payload = reset_byte_array + ms_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_PUBLISH_INTERVAL_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override DD concentrate pump data broadcast interval") # 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 def cmd_concentrate_pump_target_speed_override(self, pump_id: int, speed: float, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump target speed override command @param pump_id: unsigned int - concentrate pump ID @param speed: float - target speed value to override concentrate pump with @return: 1 if successful, zero otherwise """ reset_byte_array = integer_to_bytearray(reset) speed_byte_array = float_to_bytearray(speed) pump_id_byte_array = integer_to_bytearray(pump_id) payload = reset_byte_array + speed_byte_array + pump_id_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_TARGET_SPEED_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("override concentrate pump target speed: " + str(speed) + " - for concentrate pump: " + str(pump_id)) # 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 def cmd_concentrate_pump_measured_speed_override(self, pump_id: int, speed: float, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump measured speed override command @param pump_id: unsigned int - concentrate pump ID @param speed: float - measured speed value to override concentrate pump with @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ reset_byte_array = integer_to_bytearray(reset) speed_byte_array = float_to_bytearray(speed) pump_id_byte_array = integer_to_bytearray(pump_id) payload = reset_byte_array + speed_byte_array + pump_id_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_MEASURED_SPEED_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: self.logger.debug("reset back to normal value for concentrate pump: " + str(pump_id)) else: self.logger.debug("override concentrate pump measured speed: " + str(speed) + " - for concentrate pump: " + str(pump_id)) # 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 def cmd_concentrate_pump_parked_status_override(self, pump_id: int, status: int, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump parked state override command @param pump_id: unsigned int - concentrate pump ID @param status: unsigned int - 1 = parked, 0 = not parked @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ reset_byte_array = integer_to_bytearray(reset) status_byte_array = integer_to_bytearray(status) pump_id_byte_array = integer_to_bytearray(pump_id) payload = reset_byte_array + status_byte_array + pump_id_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_PARKED_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: self.logger.debug("reset parked status back to normal for concentrate pump park status: " + str(pump_id)) else: self.logger.debug("override parked status to: " + str(status) + " - for concentrate pump: " + str(pump_id)) # 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 def cmd_concentrate_pump_park_fault_state_override(self, pump_id: int, status: int, reset: int = NO_RESET) -> int: """ Constructs and sends the concentrate pump park fault state override command @param pump_id: unsigned int - concentrate pump ID @param status: unsigned int - 1 = fault, 0 = no fault @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ reset_byte_array = integer_to_bytearray(reset) status_byte_array = integer_to_bytearray(status) pump_id_byte_array = integer_to_bytearray(pump_id) payload = reset_byte_array + status_byte_array + pump_id_byte_array message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_PARK_FAULT_OVERRIDE_REQUEST.value, payload=payload) if reset == RESET: self.logger.debug("reset park fault status back to normal for concentrate pump park fault: " + str(pump_id)) else: self.logger.debug("override park fault status to: " + str(status) + " - for concentrate pump: " + str(pump_id)) # 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 def cmd_concentrate_pump_park_command(self, pump_id: int) -> int: """ Constructs and sends the concentrate pump park command @param pump_id: unsigned int - concentrate pump ID @return: 1 if successful, zero otherwise """ payload = integer_to_bytearray(pump_id) message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMP_PARK_REQUEST_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("park concentrate pump: " + str(pump_id)) # 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 def cmd_concentrate_set_start_stop(self, pump_id: int, command: int, speed: float, volume: float ) -> int: """ Constructs and sends the concentrate pump start stop command @param pump_id: unsigned int - concentrate pump ID @param command: int - value to command the concentrate pump @param speed: float - ml/min to set the speed to @param volume: float - the volume to set to @return: 1 if successful, zero otherwise """ pmp = integer_to_bytearray(pump_id) cmd = integer_to_bytearray(command) spd = float_to_bytearray(speed) vlm = float_to_bytearray(volume) payload = pmp + cmd + spd + vlm message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dd_ch_id, message_id=MsgIds.MSG_ID_DD_CONCENTRATE_PUMPS_START_STOP_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("setting " + str(spd) + " - for concentrate pump: " + str(pump_id)) # 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