########################################################################### # # 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 rtc.py # # @author (last) Micahel Garthwaite # @date (last) 17-Aug-2023 # @author (original) Peter Lucia # @date (original) 02-Apr-2020 # ############################################################################ import struct from ..common.msg_defs import MsgIds, MsgFieldPositions from logging import Logger from .constants import RESET, NO_RESET from ..common.msg_defs import MsgIds from ..protocols.CAN import DenaliMessage, DenaliChannels from ..utils.base import AbstractSubSystem, publish from ..utils.conversions import integer_to_bytearray class HDRTC(AbstractSubSystem): """ Hemodialysis Delivery (HD) Dialin API sub-class for rtc 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.hd_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_RTC_EPOCH_DATA.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_rtc_epoch) self.hd_rtc_timestamp = 0.0 self.rtc_epoch = 0 def get_rtc_epoch(self): """ Gets the rtc epoch @return: The rtc epoch """ return self.rtc_epoch @publish(["hd_rtc_timestamp","rtc_epoch"]) def _handler_rtc_epoch(self, message, timestamp=0.0): """ Publishes the rtc time in epoch @param message: published rtc epoch message @return: None """ epoch = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.rtc_epoch = epoch self.hd_rtc_timestamp = timestamp def cmd_stop_rtc(self): """ Stops the HD RTC clock @return: 1 if Successful, False otherwise """ message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSG_ID_HD_STOP_RTC_CLOCK.value) self.logger.debug("Stopping the HD RTC") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: self.logger.debug(received_message) self.logger.debug("RTC stop command was sent" + 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_set_rtc_time_and_date(self, second: int, minute: int, hour: int, day: int, month: int, year: int) -> int: """ Sets the RTC time and date from the provided @param second: (int) Second @param minute: (int) Minute @param hour: (int) Hour @param day: (int) Day @param month: (int) Month @param year: (int) Year @return: 1 if Successful, False otherwise """ sec = bytes([second]) mint = bytes([minute]) hour = bytes([hour]) day = bytes([day]) month = bytes([month]) year = integer_to_bytearray(year) payload = sec + mint + hour + day + month + year message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSG_ID_SET_RTC_DATE_TIME.value, payload=payload) self.logger.debug("Setting time and date to rtc") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: self.logger.debug(received_message) # str_res = str(flow) self.logger.debug( "Time and Date in rtc set to seconds: " + str(sec) + " minutes: " + str(min) + " hours: " + str(hour) + " days: " + str(day) + " months: " + str(month) + " years: " + str(year) + 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_rtc_ctl_reg1_status_override(self, status: int = 0, reset: int = NO_RESET) -> int: """ Constructs and sends the HD RTC control register 1 status override command Constraints: Must be logged into HD. @param status: status is a bit map containing a set of bits that represent status from the RTC status register. Specified below RTC_REG_1_12_HOUR_MODE_MASK = 0x0004 ( 4 ) RTC_REG_1_PORO = 0x0008 ( 8 ) RTC_REG_1_CLK_STOPPED_MASK = 0x0020 ( 32 ) RTC_REG_1_UNUSED_MASK = 0x0040 ( 64 ) RTC_REG_1_EXT_CLK_MODE_MASK = 0x0080 ( 128 ) more than one status bit can be sent. Ex: to set RTC_REG_1_PORO and RTC_REG_1_CLK_STOPPED_MASK, status should be 40 (0x0028). @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) sts = integer_to_bytearray(status & 0x0000FFFF) payload = rst + sts message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSD_ID_HD_RTC_CTL_REG1_STATUS_OVERRIDE.value, payload=payload) self.logger.debug("override HD RTC Control Register 1 status") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: if reset == RESET: str_res = "reset back to normal: " else: str_res = str(status) self.logger.debug("HD RTC Control Register 1 status overridden to " + str_res + 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_rtc_ctl_reg3_status_override(self, status: int = 0, reset: int = NO_RESET) -> int: """ Constructs and sends the HD RTC control register 3 status override command Constraints: Must be logged into HD. @param status: status is a bit map containing a set of bits that represent status from the RTC status register. Specified below RTC_REG_3_BLF_MASK = 0x0004 ( 4 ) more than one status bit can be sent @param reset: integer - 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ rst = integer_to_bytearray(reset) sts = integer_to_bytearray(status & 0x0000FFFF) payload = rst + sts message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=MsgIds.MSD_ID_HD_RTC_CTL_REG3_STATUS_OVERRIDE.value, payload=payload) self.logger.debug("override HD RTC Control Register 3 status") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: if reset == RESET: str_res = "reset back to normal: " else: str_res = str(status) self.logger.debug("HD RTC Control Register 3 status status overridden to " + str_res + 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