########################################################################### # # 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 # # @date 31-Mar-2020 # @author P. Lucia # # @brief # # ############################################################################ from DialIn.CoreCANProtocol import (DenaliMessage, DenaliChannels) from utils import integer_to_byte_array, float_to_byte_array 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.handler_rtc_epoch) self.rtc_epoch = 0 def handler_rtc_epoch(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.rtc_epoch = ctypes.c_uint32(epoch) def cmd_set_rtc_time_and_date(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 = integer_to_byte_array(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