########################################################################### # # Copyright (c) 2019-2020 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) Peter Lucia # @date (last) 21-Aug-2020 # @author (original) Peter Lucia # @date (original) 02-Apr-2020 # ############################################################################ import ctypes from ..protocols.CAN import (DenaliMessage, DenaliChannels) from ..utils.conversions import integer_to_bytearray from ..utils.base import _AbstractSubSystem, _publish from ..common.msg_defs import MsgIds from logging import Logger class HDRTC(_AbstractSubSystem): """ Hemodialysis Delivery (HD) Dialin API sub-class for rtc commands. """ START_POS_SET_PT = DenaliMessage.PAYLOAD_START_INDEX END_POS_SET_PT = START_POS_SET_PT + 4 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.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 = int.from_bytes(bytearray( message['message'][self.START_POS_SET_PT:self.END_POS_SET_PT]), byteorder=DenaliMessage.BYTE_ORDER) self.rtc_epoch = ctypes.c_uint32(epoch) def cmd_set_rtc_time_and_date(self, second: int, minute: int, hour: int, day: int, month: int, year: 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