Index: leahi_dialin/ro/reverse_osmosis.py =================================================================== diff -u -r2138d06d100fdcf23f2e9069f35ee2fdee62008f -rd0317f547a6bc62524b8ffa40b473e8a489d35d6 --- leahi_dialin/ro/reverse_osmosis.py (.../reverse_osmosis.py) (revision 2138d06d100fdcf23f2e9069f35ee2fdee62008f) +++ leahi_dialin/ro/reverse_osmosis.py (.../reverse_osmosis.py) (revision d0317f547a6bc62524b8ffa40b473e8a489d35d6) @@ -1 +1,248 @@ +########################################################################### +# +# 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 ROConductivitySensors +from .modules.flow_sensors import ROFlowSensors +from .modules.boost_pump import ROPumps +from .modules.levels import ROLevels +from .modules.pressure_sensors import ROPressureSensors +from .modules.valves import ROValves + + +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 RO(AbstractSubSystem): + """ + Reverse Osmosis ( RO aka IOFP ) Dialin object API. + It provides the basic interface to communicate with the RO firmware. + """ + # RO debug event max count + _RO_DEBUG_EVENT_LIST_COUNT = 10 + _RO_DEBUG_EVENT_MSG_LEN_INDEX = 5 + + # RO login password + RO_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): + """ + RO object provides test/service commands for the RO sub-system. + + >> RO_object = RO('can0') + >> RO_object = RO(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 RO operation mode broadcast messages + if self.can_interface is not None: + channel_id = DenaliChannels.ro_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_ro_op_mode_sync) + + self.can_interface.register_receiving_publication_function(channel_id, + MsgIds.MSG_ID_RO_DEBUG_EVENT.value, + self._handler_ro_debug_event_sync) + + # create properties + self.ro_op_mode_timestamp = 0.0 + self.ro_debug_events_timestamp = 0.0 + self.ui_version_info_response_timestamp = 0.0 + + self.ro_operation_sub_mode = 0 + self.ro_logged_in = False + self.ro_set_logged_in_status(False) + self.ui_version = None + self.ro_debug_events = [''] * self._RO_DEBUG_EVENT_LIST_COUNT + self.ro_debug_event_index = 0 + self.ro_last_debug_event = '' + + # Create command groups + self.conductivity = ROConductivitySensors(self.can_interface, self.logger) + self.flows = ROFlowSensors(self.can_interface, self.logger) + self.pumps = ROPumps(self.can_interface, self.logger) + self.levels = ROLevels(self.can_interface, self.logger) + self.pressures = ROPressureSensors(self.can_interface, self.logger) + self.valves = ROValves(self.can_interface, self.logger) + + @publish(["ro_debug_events_timestamp","ro_debug_events"]) + def _handler_ro_debug_event_sync(self, message, timestamp = 0.0): + + payload = message['message'] + message_length = payload[self._RO_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.ro_debug_events_timestamp = timestamp + self.ro_debug_events.insert(self.ro_debug_event_index, temp_message) + self.ro_last_debug_event = temp_message + + self.ro_debug_event_index += 1 + if self.ro_debug_event_index == self._RO_DEBUG_EVENT_LIST_COUNT: + self.ro_debug_event_index = 0 + + @publish(["ro_logged_in"]) + def ro_set_logged_in_status(self, logged_in: bool = False): + """ + Callback for ro logged in status change. + @param logged_in boolean logged in status for RO + @return: none + """ + self.ro_logged_in = logged_in + + @publish(["ro_op_mode_timestamp","ro_operation_mode", "ro_operation_sub_mode"]) + def _handler_ro_op_mode_sync(self, message, timestamp = 0.0): + """ + Handles published RO operation mode messages. Current RO operation mode + is captured for reference. + + @param message: published RO 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.ro_operation_mode = mode[0] + self.ro_operation_sub_mode = smode[0] + self.ro_op_mode_timestamp = timestamp + + def cmd_log_in_to_ro(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 RO. + + @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_ro_ch_id, + message_id=MsgIds.MSG_ID_FIRST_RO_TESTER_MESSAGE.value, + payload=list(map(int, map(ord, self.RO_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.ro_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_ro_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 RO. + Transition from current to requested op mode must be legal. + NOTE: for POST the RO 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_ro_ch_id, + message_id=MsgIds.MSG_ID_RO_SET_OP_MODE_REQUEST.value, + payload=payload) + + self.logger.debug("Requesting RO 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("RO mode change request Timeout!!!!") + return False + + + def cmd_ro_software_reset_request(self) -> None: + """ + Constructs and sends an RO software reset request via CAN bus. + Constraints: + Must be logged into RO. + + @return: None + """ + + message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_ro_ch_id, + message_id=MsgIds.MSG_ID_RO_SOFTWARE_RESET_REQUEST.value) + + self.logger.debug("requesting RO software reset") + + # Send message + self.can_interface.send(message, 0) + self.logger.debug("Sent request to RO to reset...") + self.ro_set_logged_in_status(False) + +