########################################################################### # # Copyright (c) 2019-2021 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 uv_reactors.py # # @author (last) Quang Nguyen # @date (last) 05-Aug-2021 # @author (original) Dara Navaei # @date (original) 18-Nov-2020 # ############################################################################ import struct from enum import unique from logging import Logger from .constants import NO_RESET, RESET from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import DenaliMessage, DenaliChannels from ..utils.base import AbstractSubSystem, publish, DialinEnum from ..utils.checks import check_broadcast_interval_override_ms from ..utils.conversions import integer_to_bytearray @unique class ReactorsNames(DialinEnum): INLET_UV_REACTOR = 0 OUTLET_UV_REACTOR = 1 @unique class ReactorsStates(DialinEnum): UV_REACTOR_STATE_OFF = 0 UV_REACTOR_STATE_ON = 1 @unique class ReactorsHealthStatus(DialinEnum): UV_REACTOR_NOT_HEALTHY = 0 UV_REACTOR_HEALTHY = 1 UV_REACTOR_OFF = 2 class UVReactors(AbstractSubSystem): """ Dialysate Generator (DG) Dialin API sub-class for UV reactors 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.dg_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_DG_UV_REACTORS_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_uv_reactors_sync) self.inlet_uv_reactor_health = 0 self.outlet_uv_reactor_health = 0 self.inlet_uv_reactor_state = 0 self.outlet_uv_reactor_state = 0 def get_inlet_uv_reactor_health(self) -> int: """ Gets the inlet UV reactor health @return: Inlet UV reactor health """ return self.inlet_uv_reactor_health def get_outlet_uv_reactor_health(self) -> int: """ Gets the outlet UV reactor health @return: Outlet UV reactor health """ return self.outlet_uv_reactor_health def get_inlet_uv_reactor_state(self) -> int: """ Gets the inlet UV reactor state @return: Inlet UV reactor state """ return self.inlet_uv_reactor_state def get_outlet_uv_reactor_state(self) -> int: """ Gets the outlet UV reactor state @return: Outlet UV reactor state """ return self.outlet_uv_reactor_state @publish(['inlet_uv_reactor_health', 'outlet_uv_reactor_health', 'inlet_uv_reactor_state', 'outlet_uv_reactor_state']) def _handler_uv_reactors_sync(self, message: dict) -> None: """ Handles published thermistors message. @param message: published thermistors message @return: none """ inlet_health = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] outlet_health = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] inlet_state = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] outlet_state = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_4:MsgFieldPositions.END_POS_FIELD_4]))[0] self.inlet_uv_reactor_health = inlet_health self.outlet_uv_reactor_health = outlet_health # Check if the received state from firmware is in range otherwise, the state will be unknown self.inlet_uv_reactor_state = inlet_state self.outlet_uv_reactor_state = outlet_state def cmd_uv_reactors_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the UV reactors data publish interval. Constraints: Must be logged into DG. Given interval must be non-zero and a multiple of the DG general task interval (50 ms). @param ms: (int) interval (in ms) to override with @param reset: (int) 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 rst = integer_to_bytearray(reset) mis = integer_to_bytearray(ms) payload = rst + mis message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_UV_REACTORS_DATA_PUBLISH_INTERVAL_OVERRIDE.value, payload=payload) self.logger.debug("Override UV reactors data publish interval") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: # self.logger.debug(received_message) if reset == RESET: str_res = "Reset back to normal" else: str_res = str(mis) self.logger.debug( "UV reactors data broadcast interval overridden to " + str_res + " ms: " + 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 def cmd_uv_reactors_health_override(self, reactor: int, health: int, reset: int = NO_RESET) -> int: """ Constructs and sends the UV reactors' health override command. Constraints: Must be logged into DG. @param reactor: (int) UV reactor index @param health: (int) 0 for unhealthy and 1 for healthy @param reset: (int) 0 for no reset and 1 for reset @return 1 if successful, zero otherwise """ reset_value = integer_to_bytearray(reset) rctr = integer_to_bytearray(reactor) hlth = integer_to_bytearray(health) payload = reset_value + hlth + rctr message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_UV_REACTORS_HEALTH_OVERRIDE.value, payload=payload) # 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.debug("UV reactors health override Timeout!!!") return False def cmd_start_stop_inlet_uv_reactor(self, state: int = ReactorsStates.UV_REACTOR_STATE_OFF.value) -> int: """ Constructs and sends a start/stop command to the DG inlet UV reactor @param: state: (int) the state of the inlet UV reactor. 0 for Off (default) and 1 for On. @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(0) inlet_uv_reactor_index = integer_to_bytearray(0) if state == ReactorsStates.UV_REACTOR_STATE_ON.value: payload = rst + integer_to_bytearray(1) + inlet_uv_reactor_index operation = 'Turning on ' else: payload = rst + integer_to_bytearray(0) + inlet_uv_reactor_index operation = 'Turning off ' message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_START_STOP_UV_REACTORS.value, payload=payload) # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: self.logger.debug(operation + "inlet UV reactor") # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug(operation + "inlet UV reactor timeout!!") return False def cmd_start_stop_outlet_uv_reactor(self, state: int = ReactorsStates.UV_REACTOR_STATE_ON.value) -> int: """ Constructs and sends a start/stop command to the DG outlet UV reactor @param: state: (int) the state of the outlet UV reactor. 0 for Off (default) and 1 for On. @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(0) outlet_uv_reactor_index = integer_to_bytearray(1) if state == ReactorsStates.UV_REACTOR_STATE_ON.value: payload = rst + integer_to_bytearray(1) + outlet_uv_reactor_index operation = 'Turning on ' else: payload = rst + integer_to_bytearray(0) + outlet_uv_reactor_index operation = 'Turning off ' message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_START_STOP_UV_REACTORS.value, payload=payload) # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: self.logger.debug(operation + "outlet UV reactor") # response payload is OK or not OK return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] else: self.logger.debug(operation + "outlet UV reactor timeout!!") return False