def get_address_conn( self, addr: LcnAddr ) -> Union[ModuleConnection, GroupConnection]: """Create and/or return the given LCN module or group. The LCN module/group object is used for further communication with the module/group (e.g. sending commands). :param addr: The module's/group's address :type addr: :class:`~LcnAddr` :returns: The address connection object (never null) :rtype: `~ModuleConnection` or `~GroupConnection` :Example: >>> address = LcnAddr(0, 7, False) >>> module = pchk_connection.get_address_conn(address) >>> module.toggle_output(0, 5) """ if addr.get_seg_id() == 0 and self.local_seg_id != -1: addr.seg_id = self.local_seg_id address_conn = self.address_conns.get(addr, None) if address_conn is None: if addr.is_group(): address_conn = GroupConnection(self, addr.seg_id, addr.addr_id) else: address_conn = ModuleConnection(self, addr.seg_id, addr.addr_id) self.address_conns[addr] = address_conn return address_conn
def physical_to_logical(self, addr: LcnAddr) -> LcnAddr: """Convert the physical segment id of an address to the logical one. :param addr: The module's/group's address :type addr: :class:`~LcnAddr` :returns: The module's/group's address :rtype: :class:`~LcnAddr` """ return LcnAddr( self.local_seg_id if addr.get_seg_id() == 0 else addr.get_seg_id(), addr.get_id(), addr.is_group(), )
def generate_address_header(addr: LcnAddr, local_seg_id: int, wants_ack: bool) -> str: """Generate a PCK command address header. :param addr: The module's/group's address :type addr: :class:`~LcnAddr` :param int local_seg_id: The local segment id :param bool wants_ack: Is an acknowledge requested. :return: The PCK address header string. :rtype: str """ return ">{:s}{:03d}{:03d}{:s}".format( "G" if addr.is_group() else "M", addr.get_physical_seg_id(local_seg_id), addr.get_id(), "!" if wants_ack else ".", )