001package jmri.jmrix.nce; 002 003import java.util.Locale; 004import javax.annotation.Nonnull; 005import jmri.Turnout; 006import org.slf4j.Logger; 007import org.slf4j.LoggerFactory; 008 009/** 010 * Implement turnout manager for NCE systems. 011 * <p> 012 * System names are "NTnnn", where N is the (multichar) system connection prefix, 013 * T is the Turnout type identifier, nnn is the turnout number without padding. 014 * 015 * @author Bob Jacobsen Copyright (C) 2001 016 */ 017public class NceTurnoutManager extends jmri.managers.AbstractTurnoutManager implements NceListener { 018 019 public NceTurnoutManager(NceSystemConnectionMemo memo) { 020 super(memo); 021 } 022 023 /** 024 * {@inheritDoc} 025 */ 026 @Override 027 @Nonnull 028 public NceSystemConnectionMemo getMemo() { 029 return (NceSystemConnectionMemo) memo; 030 } 031 032 /** 033 * {@inheritDoc} 034 */ 035 @Nonnull 036 @Override 037 protected Turnout createNewTurnout(@Nonnull String systemName, String userName) throws IllegalArgumentException { 038 int addr; 039 try { 040 addr = Integer.parseInt(systemName.substring(getSystemPrefix().length() + 1)); 041 } catch (NumberFormatException e) { 042 throw new IllegalArgumentException("Failed to convert systemName '"+systemName+"' to a Turnout address"); 043 } 044 Turnout t = new NceTurnout(getMemo().getNceTrafficController(), getSystemPrefix(), addr); 045 t.setUserName(userName); 046 047 return t; 048 } 049 050 /** 051 * Get the bit address from the system name. 052 * 053 * @param systemName system name for turnout 054 * @return index value for turnout 055 */ 056 public int getBitFromSystemName(String systemName) { 057 // validate the system Name leader characters 058 if ((!systemName.startsWith(getSystemPrefix())) || (!systemName.startsWith(getSystemPrefix() + "T"))) { 059 // here if an illegal nce light system name 060 log.error("illegal character in header field of nce turnout system name: {}", systemName); 061 return (0); 062 } 063 // name must be in the NLnnnnn format (N is user configurable) 064 int num = 0; 065 try { 066 num = Integer.parseInt(systemName.substring( 067 getSystemPrefix().length() + 1, systemName.length()) 068 ); 069 } catch (NumberFormatException e) { 070 log.debug("illegal character in number field of system name: {}", systemName); 071 return (0); 072 } 073 if (num <= 0) { 074 log.error("invalid nce turnout system name: {}", systemName); 075 return (0); 076 } else if (num > 4096) { 077 log.warn("bit number out of range in nce turnout system name: {}", systemName); 078 return (0); 079 } 080 return (num); 081 } 082 083 @Override 084 public boolean allowMultipleAdditions(@Nonnull String systemName) { 085 return true; 086 } 087 088 @Override 089 public void reply(NceReply r) { 090 091 } 092 093 @Override 094 public void message(NceMessage m) { 095 096 } 097 098 /** 099 * {@inheritDoc} 100 */ 101 @Override 102 @Nonnull 103 public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) { 104 return super.validateNmraAccessorySystemNameFormat(name, locale); 105 } 106 107 /** 108 * {@inheritDoc} 109 */ 110 @Override 111 public NameValidity validSystemNameFormat(@Nonnull String systemName) { 112 return (getBitFromSystemName(systemName) != 0) ? NameValidity.VALID : NameValidity.INVALID; 113 } 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override 119 public String getEntryToolTip() { 120 return Bundle.getMessage("AddOutputEntryToolTip"); 121 } 122 123 private final static Logger log = LoggerFactory.getLogger(NceTurnoutManager.class); 124 125}