Index: leahi_dialin/td/modules/switches.py =================================================================== diff -u -rec8a2600b9e8cf6fe7e02c200a1c24221ca86863 -r18c90a1b2b6c7339bdd192a2d2fac32f2b8a35df --- leahi_dialin/td/modules/switches.py (.../switches.py) (revision ec8a2600b9e8cf6fe7e02c200a1c24221ca86863) +++ leahi_dialin/td/modules/switches.py (.../switches.py) (revision 18c90a1b2b6c7339bdd192a2d2fac32f2b8a35df) @@ -7,24 +7,23 @@ # # @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 leahi_dialin.common.override_templates import cmd_generic_broadcast_interval_override, cmd_generic_override 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 +from leahi_dialin.protocols.CAN import DenaliChannels +from leahi_dialin.utils.base import AbstractSubSystem, publish +from leahi_dialin.utils.conversions import integer_to_bytearray class TDSwitches(AbstractSubSystem): @@ -35,7 +34,6 @@ def __init__(self, can_interface, logger: Logger): """ TDSwitches constructor - """ super().__init__() @@ -51,6 +49,7 @@ 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): """ @@ -65,82 +64,51 @@ 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: + + 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 = TDSwitchesNames(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)