########################################################################### # # 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 filtration_purification.py # # @author (last) Dara Navaei # @date (last) 26-Feb-2024 # @author (original) Peter Lucia # @date (original) 02-Apr-2020 # ############################################################################ import struct from .modules.conductivity_sensors import FPConductivitySensors from .modules.flow_sensors import FPFlowSensors from .modules.boost_pump import FPPumps from .modules.flow_sensors import FPFlowSensors from .modules.levels import FPLevels from .modules.pressure_sensors import FPPressureSensors from .modules.valves import FPValves from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import DenaliMessage, DenaliCanMessenger, DenaliChannels from ..utils.base import AbstractSubSystem, publish, LogManager from ..utils.checks import check_broadcast_interval_override_ms from ..utils.conversions import integer_to_bytearray, unsigned_short_to_bytearray, bytearray_to_integer, \ bytearray_to_byte class FP(AbstractSubSystem): """ Reverse Osmosis ( FP aka IOFP ) Dialin object API. It provides the basic interface to communicate with the FP firmware. """ # FP debug event max count _FP_DEBUG_EVENT_LIST_COUNT = 10 _FP_DEBUG_EVENT_MSG_LEN_INDEX = 5 # FP login password FP_LOGIN_PASSWORD = '123' # UI version message field positions START_POS_MAJOR = DenaliMessage.PAYLOAD_START_INDEX END_POS_MAJOR = START_POS_MAJOR + 1 START_POS_MINOR = END_POS_MAJOR END_POS_MINOR = START_POS_MINOR + 1 START_POS_MICRO = END_POS_MINOR END_POS_MICRO = START_POS_MICRO + 1 START_POS_BUILD = END_POS_MICRO END_POS_BUILD = START_POS_BUILD + 2 START_POS_COMPATIBILITY_REV = END_POS_BUILD END_POS_COMPATIBILITY_REV = START_POS_COMPATIBILITY_REV + 4 def __init__(self, can_interface="can0", log_level=None): """ FP object provides test/service commands for the FP sub-system. >> FP_object = FP('can0') >> FP_object = FP(can_interface='can0', log_level="DEBUG") Possible log levels: ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "CAN_ONLY", "PRINT_ONLY"] @param can_interface: (str) CANBus interface name, e.g. "can0" @param log_level: (str) Logging level, defaults to None """ super().__init__() self._log_manager = LogManager(log_level=log_level, log_filepath=self.__class__.__name__ + ".log") self.logger = self._log_manager.logger # Create listener self.can_interface = DenaliCanMessenger(can_interface=can_interface, logger=self.logger) self.can_interface.start() self.callback_id = None # register handler for FP operation mode broadcast messages if self.can_interface is not None: channel_id = DenaliChannels.fp_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_RO_OP_MODE_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_fp_op_mode_sync) self.can_interface.register_receiving_publication_function(channel_id, MsgIds.MSG_ID_RO_DEBUG_EVENT.value, self._handler_fp_debug_event_sync) # create properties self.fp_op_mode_timestamp = 0.0 self.fp_debug_events_timestamp = 0.0 self.ui_version_info_response_timestamp = 0.0 self.fp_operation_sub_mode = 0 self.fp_logged_in = False self.fp_set_logged_in_status(False) self.ui_version = None self.fp_debug_events = [''] * self._FP_DEBUG_EVENT_LIST_COUNT self.fp_debug_event_index = 0 self.fp_last_debug_event = '' # Create command groups self.conductivity = FPConductivitySensors(self.can_interface, self.logger) self.flows = FPFlowSensors(self.can_interface, self.logger) self.pumps = FPPumps(self.can_interface, self.logger) self.flows = FPFlowSensors(self.can_interface, self.logger) self.levels = FPLevels(self.can_interface, self.logger) self.pressures = FPPressureSensors(self.can_interface, self.logger) self.valves = FPValves(self.can_interface, self.logger) @publish(["fp_debug_events_timestamp","fp_debug_events"]) def _handler_fp_debug_event_sync(self, message, timestamp = 0.0): payload = message['message'] message_length = payload[self._FP_DEBUG_EVENT_MSG_LEN_INDEX] temp_message = '' index = MsgFieldPositions.START_POS_FIELD_1 for i in range(0, message_length): # Loop through the length and get the char, char_index = bytearray_to_byte(payload, index + i, False) temp_message += chr(char) self.fp_debug_events_timestamp = timestamp self.fp_debug_events.insert(self.fp_debug_event_index, temp_message) self.fp_last_debug_event = temp_message self.fp_debug_event_index += 1 if self.fp_debug_event_index == self._FP_DEBUG_EVENT_LIST_COUNT: self.fp_debug_event_index = 0 @publish(["fp_logged_in"]) def fp_set_logged_in_status(self, logged_in: bool = False): """ Callback for fp logged in status change. @param logged_in boolean logged in status for FP @return: none """ self.fp_logged_in = logged_in @publish(["fp_op_mode_timestamp","fp_operation_mode", "fp_operation_sub_mode"]) def _handler_fp_op_mode_sync(self, message, timestamp = 0.0): """ Handles published FP operation mode messages. Current FP operation mode is captured for reference. @param message: published FP operation mode broadcast message @return: None """ mode = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1])) smode = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2])) self.fp_operation_mode = mode[0] self.fp_operation_sub_mode = smode[0] self.fp_op_mode_timestamp = timestamp def cmd_log_in_to_fp(self, resend: bool = False) -> int: """ Constructs and sends a login command via CAN bus. Login required before \n other commands can be sent to the FP. @param resend: (bool) if False (default), try to login once. Otherwise, tries to login indefinitely @return: 1 if logged in, 0 if log in failed """ message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_fp_ch_id, message_id=MsgIds.MSG_ID_FIRST_RO_TESTER_MESSAGE.value, payload=list(map(int, map(ord, self.FP_LOGIN_PASSWORD)))) self.logger.debug("Logging in...") # Send message received_message = self.can_interface.send(message, resend=resend) if received_message is not None: if received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] == 1: self.logger.debug("Success: Logged In") self.fp_set_logged_in_status(True) #self._send_ro_checkin_message() # Timer starts interval first #self.can_interface.transmit_interval_dictionary[self.callback_id].start() else: self.logger.debug("Failure: Log In Failed.") return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("Login Timeout!!!!") return False def cmd_fp_set_operation_mode(self, new_mode: int = 0) -> int: """ Constructs and sends a set operation mode request command via CAN bus. Constraints: Must be logged into FP. Transition from current to requested op mode must be legal. NOTE: for POST the FP device shall be in Standby Mode @param new_mode: ID of operation mode to transition to @return: 1 if successful, zero otherwise """ payload = integer_to_bytearray(new_mode) message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_fp_ch_id, message_id=MsgIds.MSG_ID_RO_SET_OP_MODE_REQUEST.value, payload=payload) self.logger.debug("Requesting FP mode change to " + str(new_mode)) # Send message received_message = self.can_interface.send(message) if received_message is not None: if received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] == 1: self.logger.debug("Success: Mode change accepted") else: self.logger.debug("Failure: Mode change rejected.") return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug("FP mode change request Timeout!!!!") return False def cmd_fp_software_reset_request(self) -> None: """ Constructs and sends an FP software reset request via CAN bus. Constraints: Must be logged into FP. @return: None """ message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_fp_ch_id, message_id=MsgIds.MSG_ID_RO_SOFTWARE_RESET_REQUEST.value) self.logger.debug("requesting FP software reset") # Send message self.can_interface.send(message, 0) self.logger.debug("Sent request to FP to reset...") self.fp_set_logged_in_status(False)