########################################################################### # # Copyright (c) 2021-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 reservoirs.py # # @author (last) Michael Garthwaite # @date (last) 04-Oct-2023 # @author (original) Dara Navaei # @date (original) 23-Nov-2021 # ############################################################################ import struct from logging import Logger from enum import unique from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import DenaliMessage, DenaliChannels from ..utils.base import AbstractSubSystem, publish, DialinEnum from ..utils.conversions import integer_to_bytearray, float_to_bytearray @unique class HDReservoirStates(DialinEnum): TREATMENT_RESERVOIR_MGMT_START_STATE = 0 TREATMENT_RESERVOIR_MGMT_DRAIN_RESERVOIR_STATE = 1 TREATMENT_RESERVOIR_MGMT_WAIT_TO_FILL_STATE = 2 TREATMENT_RESERVOIR_MGMT_FILL_RESERVOIR_STATE = 3 TREATMENT_RESERVOIR_MGMT_WAIT_FOR_FILL_SETTLE_STATE = 4 TREATMENT_RESERVOIR_MGMT_WAIT_FOR_SWITCH_SETTLE_STATE = 5 NUM_OF_TREATMENT_RESERVOIR_MGMT_STATES = 6 class HDReservoirs(AbstractSubSystem): """ HD interface containing reservoir related commands. """ def __init__(self, can_interface, logger: Logger): 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_RESERVOIRS_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_reservoirs_sync) self.hd_reservoirs_timestamp = 0.0 self.reservoir_state = 0 self.active_reservoir_uf_ml = 0.0 self.active_reservoir_spent_vol_ml = 0.0 self.dilution_level_pct = 0.0 self.recirculation_level_pct = 0.0 self.time_depletion = 0 self.time_wait_to_fill = 0 self.temp_remove_fill_flow = 0.0 @publish(['hd_reservoirs_timestamp', 'reservoir_state', 'active_reservoir_uf_ml', 'active_reservoir_spent_vol_ml', 'dilution_level_pct', 'recirculation_level_pct', 'time_depletion', 'time_wait_to_fill', 'temp_remove_fill_flow']) def _handler_reservoirs_sync(self, message, timestamp=0.0): """ Handles published reservoir data messages. Reservoir data are captured for reference. @param message: published reservoir data message @return: none """ self.reservoir_state = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.active_reservoir_uf_ml = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] self.active_reservoir_spent_vol_ml = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.dilution_level_pct = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.recirculation_level_pct = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_5:MsgFieldPositions.END_POS_FIELD_5]))[0] self.time_depletion = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_6:MsgFieldPositions.END_POS_FIELD_6]))[0] self.time_wait_to_fill = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_7:MsgFieldPositions.END_POS_FIELD_7]))[0] self.temp_remove_fill_flow = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_8:MsgFieldPositions.END_POS_FIELD_8]))[0] self.hd_reservoirs_timestamp = timestamp def cmd_recirulation_pct_override(self, recirulation_pct: float, reset: int) -> int: """ Constructs and sends a recirulation percentage override message to the HD. Constraints: Must be logged into HD. @param recirulation_pct: (float) recirulation percentage value @param reset: (int) - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) pct = float_to_bytearray(recirulation_pct) payload = rst + pct message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSG_ID_HD_RECIRULATION_PCT_OVERRIDE.value, payload=payload) self.logger.debug("Setting recirulation percentage.") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: self.logger.debug("Recirulation percentage set to " + 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