########################################################################### # # 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 rtc.py # # @author (last) Dara Navaei # @date (last) 10-Nov-2021 # @author (original) Quang Nguyen # @date (original) 13-May-2021 # ############################################################################ import struct from ..common.msg_defs import MsgIds, MsgFieldPositions from logging import Logger 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 DGRTC(AbstractSubSystem): """ Dialysate Generator (DG) 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.dg_sync_broadcast_ch_id msg_id = MsgIds.MSG_ID_RTC_EPOCH.value self.can_interface.register_receiving_publication_function(channel_id, msg_id, self._handler_rtc_epoch) self.rtc_epoch = 0 def get_rtc_epoch(self): """ Gets the rtc epoch @return: The rtc epoch """ return self.rtc_epoch @publish(["rtc_epoch"]) def _handler_rtc_epoch(self, message): """ 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 def cmd_stop_rtc(self): """ Stops the DG RTC clock @return: None """ message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, message_id=MsgIds.MSG_ID_DG_STOP_RTC_CLOCK.value) self.logger.debug("Stopping the DG 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 with the provided date and time @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_dg_ch_id, message_id=MsgIds.MSG_ID_DG_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) 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