Index: leahi_dialin/td/modules/switches.py =================================================================== diff -u -rafe332fec54d9d0432dfc0d54aef1debaa92b066 -r38e8dd31728056dbb7f9304c98ab16d7147b75a2 --- leahi_dialin/td/modules/switches.py (.../switches.py) (revision afe332fec54d9d0432dfc0d54aef1debaa92b066) +++ leahi_dialin/td/modules/switches.py (.../switches.py) (revision 38e8dd31728056dbb7f9304c98ab16d7147b75a2) @@ -7,38 +7,25 @@ # # @file switches.py # -# @author (last) Micahel Garthwaite -# @date (last) 03-Mar-2023 +# @author (last) Zoltan Miskolci +# @date (last) 08-Jan-2026 # @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.constants import NO_RESET from leahi_dialin.common.msg_defs import MsgIds, MsgFieldPositions -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 +from leahi_dialin.common.override_templates import cmd_generic_broadcast_interval_override, cmd_generic_override +from leahi_dialin.common.td_defs import td_enum_repository +from leahi_dialin.protocols.CAN import DenaliChannels +from leahi_dialin.utils.base import AbstractSubSystem, publish +from leahi_dialin.utils.conversions import integer_to_bytearray -@unique -class TDSwitchStatus(DialinEnum): - CLOSED = 0 - OPEN = 1 - - - -@unique -class TDSwitchesNames(DialinEnum): - FRONT_DOOR = 0 - NUM_OF_DOORS_AND_SWITCHES = 1 - - class TDSwitches(AbstractSubSystem): """ @brief Treatment Device (TD) Dialin API sub-class for TD switches related commands. @@ -47,7 +34,6 @@ def __init__(self, can_interface, logger: Logger): """ TDSwitches constructor - """ super().__init__() @@ -56,14 +42,15 @@ if self.can_interface is not None: channel_id = DenaliChannels.td_sync_broadcast_ch_id - msg_id = MsgIds.MSG_ID_TD_SWITCHES_DATA.value - self.can_interface.register_receiving_publication_function(channel_id, msg_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.FRONT_DOOR.name: TDSwitchStatus.CLOSED.value} + self.td_switches_status = {td_enum_repository.TDSwitchNames.H9_FRONT_DOOR.name: td_enum_repository.TDSwitchStatus.CLOSED.value} self.td_switches_timestamp = 0.0 - @publish(["td_switches_timestamp", "td_switches_status"]) + + @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. @@ -74,84 +61,54 @@ front_door = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] - self.td_switches_status[TDSwitchesNames.FRONT_DOOR.name] = TDSwitchStatus(front_door).value + self.td_switches_status[td_enum_repository.TDSwitchNames.H9_FRONT_DOOR.name] = td_enum_repository.TDSwitchStatus(front_door).value + self.td_switches_timestamp = timestamp - def cmd_switch_status_override(self, switch: int, status: int, reset: int = NO_RESET) -> int: + + def cmd_switches_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ - Constructs and sends the TD switch status override command + 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 switch: (int) switch ID that is status is overridden - @param status: (int) status that the switch will be overridden to + @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 """ - reset_value = integer_to_bytearray(reset) - sw = integer_to_bytearray(switch) - st = integer_to_bytearray(status) - payload = reset_value + st + sw + return cmd_generic_broadcast_interval_override( + ms = ms, + reset = reset, + channel_id = DenaliChannels.dialin_to_td_ch_id, + msg_id = MsgIds.MSG_ID_TD_SWITCHES_PUBLISH_INTERVAL_OVERRIDE_REQUEST, + module_name = 'TD Switches', + logger = self.logger, + can_interface = self.can_interface) - 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: + def cmd_switch_status_override(self, switch: int, status: int, reset: int = NO_RESET) -> int: """ - Constructs and sends the TD switch data publication override command. + Constructs and sends the TD switch status 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 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 - 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 + switch_name = td_enum_repository.TDSwitchNames(switch).name + return cmd_generic_override( + payload = payload, + reset = NO_RESET, + channel_id = DenaliChannels.dialin_to_td_ch_id, + msg_id = MsgIds.MSG_ID_TD_SWITCH_STATE_OVERRIDE_REQUEST, + entity_name = f'TD {switch_name} Switch status', + override_text = f'{str(status)}', + logger = self.logger, + can_interface = self.can_interface)