########################################################################### # # 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) Zoltan Miskolci # @date (last) 08-Jan-2026 # @author (original) Dara Navaei # @date (original) 25-Jul-2021 # ############################################################################ import struct from logging import Logger 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 import dd_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 class DDSwitches(AbstractSubSystem): """ @brief Dialysate Device (DD) Dialin API sub-class for DD switches related commands. """ def __init__(self, can_interface, logger: Logger): """ DDSwitches constructor """ super().__init__() self.can_interface = can_interface self.logger = logger if self.can_interface is not None: channel_id = DenaliChannels.dd_sync_broadcast_ch_id self.msg_id_dd_switches_data = MsgIds.MSG_ID_DD_SWITCHES_DATA.value self.can_interface.register_receiving_publication_function(channel_id, self.msg_id_dd_switches_data, self._handler_switches_sync) self.dd_switches_timestamp = 0.0 #: The timestamp of the latest message # The Switches data in dictionary format self.dd_switches_status = { dd_enum_repository.DDSwitchNames.D101_SWITCH1.name: dd_enum_repository.DDSwitchStatus.CLOSED.value, dd_enum_repository.DDSwitchNames.D102_SWITCH2.name: dd_enum_repository.DDSwitchStatus.CLOSED.value, dd_enum_repository.DDSwitchNames.D103_CAP.name: dd_enum_repository.DDSwitchStatus.CLOSED.value} @publish(["msg_id_dd_switches_data", "dd_switches_status", "dd_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 """ d101 = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_1:MsgFieldPositions.END_POS_FIELD_1]))[0] d102 = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_2:MsgFieldPositions.END_POS_FIELD_2]))[0] d103 = struct.unpack('i', bytearray( message['message'][MsgFieldPositions.START_POS_FIELD_3:MsgFieldPositions.END_POS_FIELD_3]))[0] self.dd_switches_status[dd_enum_repository.DDSwitchNames.D101_SWITCH1.name] = dd_enum_repository.DDSwitchStatus(d101).value self.dd_switches_status[dd_enum_repository.DDSwitchNames.D102_SWITCH2.name] = dd_enum_repository.DDSwitchStatus(d102).value self.dd_switches_status[dd_enum_repository.DDSwitchNames.D103_CAP.name] = dd_enum_repository.DDSwitchStatus(d103).value self.dd_switches_timestamp = timestamp def cmd_switches_data_broadcast_interval_override(self, ms: int, reset: int = NO_RESET) -> int: """ Constructs and sends the DD switch data publication override command. Constraints: Must be logged into DD. Given interval must be non-zero and a multiple of the DD 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 """ return cmd_generic_broadcast_interval_override( ms = ms, reset = reset, channel_id = DenaliChannels.dialin_to_dd_ch_id, msg_id = MsgIds.MSG_ID_DD_SWITCHES_PUBLISH_INTERVAL_OVERRIDE_REQUEST, module_name = 'DD Switches', logger = self.logger, can_interface = self.can_interface) def cmd_switch_status_override(self, switch: int, status: int, reset: int = NO_RESET) -> int: """ Constructs and sends the DD switch status override command Constraints: Must be logged into DD. @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 switch_name = dd_enum_repository.DDSwitchNames(switch).name return cmd_generic_override( payload = payload, reset = NO_RESET, channel_id = DenaliChannels.dialin_to_dd_ch_id, msg_id = MsgIds.MSG_ID_DD_SWITCH_STATE_OVERRIDE_REQUEST, entity_name = f'DD {switch_name} Switch status', override_text = f'{str(status)}', logger = self.logger, can_interface = self.can_interface)