########################################################################### # # Copyright (c) 2021-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 switches.py # # @author (last) Micahel Garthwaite # @date (last) 03-Mar-2023 # @author (original) Dara Navaei # @date (original) 25-Jul-2021 # ############################################################################ import struct from enum import unique from logging import Logger from leahi_dialin.utils.conversions import integer_to_bytearray from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions from leahi_dialin.common.td_defs import TDSwitchStatus, TDSwitchesNames from .constants import RESET, NO_RESET from leahi_dialin.protocols.CAN import DenaliMessage, DenaliChannels from leahi_dialin.utils.base import AbstractSubSystem, publish, DialinEnum from leahi_dialin.utils.checks import check_broadcast_interval_override_ms class TDSwitches(AbstractSubSystem): """ @brief Treatment Device (TD) Dialin API sub-class for TD switches related commands. """ def __init__(self, can_interface, logger: Logger): """ TDSwitches constructor """ super().__init__() self.can_interface = can_interface self.logger = logger if self.can_interface is not None: channel_id = DenaliChannels.td_sync_broadcast_ch_id self.msg_id_td_switches_data = MsgIds.MSG_ID_TD_SWITCHES_DATA.value self.can_interface.register_receiving_publication_function(channel_id, self.msg_id_td_switches_data, self._handler_switches_sync) self.td_switches_status = {TDSwitchesNames.H9_FRONT_DOOR.name: TDSwitchStatus.CLOSED.value} self.td_switches_timestamp = 0.0 @publish(["msg_id_td_switches_data", "td_switches_status", "td_switches_timestamp"]) def _handler_switches_sync(self, message, timestamp=0.0): """ Handles published TD switches data messages. Switches data are captured for reference. @param message: published switches data message @return: none """ front_door = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] self.td_switches_status[TDSwitchesNames.H9_FRONT_DOOR.name] = TDSwitchStatus(front_door).value self.td_switches_timestamp = timestamp def cmd_switch_status_override(self, switch: int, status: int, reset: int = NO_RESET) -> int: """ Constructs and sends the TD switch status override command Constraints: Must be logged into TD. @param switch: (int) switch ID that is status is overridden @param status: (int) status that the switch will be overridden to @param reset: (int) 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ reset_value = integer_to_bytearray(reset) sw = integer_to_bytearray(switch) st = integer_to_bytearray(status) payload = reset_value + st + sw message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_td_ch_id, message_id=MsgIds.MSG_ID_TD_SWITCH_STATE_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("Override switch status") # Send message received_message = self.can_interface.send(message) # If there is no content... if received_message is not None: self.logger.debug("Switch " + str(TDSwitchesNames(switch).name) + " to: " + 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_switches_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the TD switch data publication override command. Constraints: Must be logged into TD. Given interval must be non-zero and a multiple of the TD general task interval (50 ms). @param ms: (int) interval (in ms) to override with @param reset: (int) 1 to reset a previous override, 0 to override @return: 1 if successful, zero otherwise """ if not check_broadcast_interval_override_ms(ms): return False rst = integer_to_bytearray(reset) mis = integer_to_bytearray(ms) payload = rst + mis message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_td_ch_id, message_id=MsgIds.MSG_ID_TD_SWITCHES_PUBLISH_INTERVAL_OVERRIDE_REQUEST.value, payload=payload) self.logger.debug("Override TD switches data broadcast interval") # Send message received_message = self.can_interface.send(message) # If there is content... if received_message is not None: # self.logger.debug(received_message) if reset == RESET: str_res = "reset back to normal" else: str_res = str(mis) self.logger.debug( "TD Switches data broadcast interval overridden to " + str_res + " ms: " + 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