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 @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.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(['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): """ 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]