########################################################################### # # 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 thermistors.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 RESET, NO_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, float_to_bytearray @unique class ThermistorsNames(DialinEnum): THERMISTOR_ONBOARD_NTC = 0 THERMISTOR_POWER_SUPPLY_1 = 1 THERMISTOR_POWER_SUPPLY_2 = 2 class Thermistors(AbstractSubSystem): """ Dialysate Generation (DG) interface for thermistors and board temperature sensors 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 # Dictionary of the thermistors self.thermistors = {ThermistorsNames.THERMISTOR_ONBOARD_NTC.name: {}, ThermistorsNames.THERMISTOR_POWER_SUPPLY_1.name: {}, ThermistorsNames.THERMISTOR_POWER_SUPPLY_2.name: {}} if self.can_interface is not None: channel_id = DenaliChannels.dg_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_DG_THERMISTORS_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_thermistors_sync) def get_temperature(self, thermistor): """ Gets a thermistor's value @param: thermistor: (int) thermistor index @return: The temperature of a thermistor """ return self.thermistors[ThermistorsNames(thermistor).name] @publish(['thermistors']) def _handler_thermistors_sync(self, message): """ Handles published thermistors message. @param message: published thermistors message @return: none """ onboard_temp = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] pwr_supply_1 = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] pwr_supply_2 = struct.unpack('f', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.thermistors[ThermistorsNames.THERMISTOR_ONBOARD_NTC.name] = onboard_temp self.thermistors[ThermistorsNames.THERMISTOR_POWER_SUPPLY_1.name] = pwr_supply_1 self.thermistors[ThermistorsNames.THERMISTOR_POWER_SUPPLY_2.name] = pwr_supply_2 def cmd_thermistors_value_override(self, thermistor: int, value: float, reset: int = NO_RESET) -> int: """ Constructs and sends the thermistors value override command Constraints: Must be logged into DG. @param value: float the temperature to be set @param thermistor: (int) thermistor index @param reset: (int) 1 to reset a previous override, 0 to override @return 1 if successful, zero otherwise """ reset_value = integer_to_bytearray(reset) vlu = float_to_bytearray(value) thr = integer_to_bytearray(thermistor) payload = reset_value + vlu + thr message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_THERMISTORS_VALUE_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("Themristors value override Timeout!!!") return False def cmd_thermistors_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the thermistors data publish interval command. 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 msg_id = MsgIds.MSG_ID_DG_THERMISTORS_DATA_PUBLISH_INTERVAL_OVERRIDE.value message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=msg_id, payload=payload) self.logger.debug("Overriding themistors broadcast 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( "Thermistors 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