from DialIn.CoreCANProtocol import (DenaliMessage, DenaliChannels) from .utils import integer2ByteArray, float2ByteArray import ctypes class HDRTC: """ \class HDRTC \brief Hemodialysis Device (HD) Dialin API sub-class for RTC commands. """ MSG_ID_SET_RTC_DATE_TIME = 0x801D MSG_ID_RTC_EPOCH = 0x000A START_POS_SET_PT = DenaliMessage.PAYLOAD_START_INDEX END_POS_SET_PT = START_POS_SET_PT + 4 def __init__(self, outer_instance, can_interface=None): """ HD_BloodFlow constructor \param outer_instance: reference to the HD (outer) class. \returns HD_BloodFlow object. """ self.can_interface = can_interface if self.can_interface is not None: channel_id = DenaliChannels.hd_sync_broadcast_ch_id msg_id = self.MSG_ID_RTC_EPOCH self.can_interface.register_receiving_publication_function(channel_id, msg_id, self.handlerRTCEpoch) self.RTCEpoch = 0 def handlerRTCEpoch(self, message): """ Publishes the RTC time in epoch \param message: published RTC epoch message \returns none """ epoch = int.from_bytes(bytearray( message['message'][self.START_POS_SET_PT:self.END_POS_SET_PT]), byteorder=DenaliMessage.BYTE_ORDER) self.RTCEpoch = ctypes.c_uint32(epoch) def CmdSetRTCTimeAndDate(self, secs, mins, hours, days, months, years): """ Constructs and sends the time and date to be written to RTC \returns 1 if successful, zero otherwise """ sec = bytes([secs]) min = bytes([mins]) hour = bytes([hours]) day = bytes([days]) month = bytes([months]) year = integer2ByteArray(years) payload = sec + min + hour + day + month + year message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_hd_ch_id, message_id=self.MSG_ID_SET_RTC_DATE_TIME, payload=payload) print("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: print(received_message) # str_res = str(flow) print("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: print("Timeout!!!!") return False