001package jmri.jmrix.easydcc; 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 EasyDcc systems. 011 * <p> 012 * System names are "ETnnn", where E is the user configurable system prefix, 013 * nnn is the turnout number without padding. 014 * 015 * @author Bob Jacobsen Copyright (C) 2001 016 */ 017public class EasyDccTurnoutManager extends jmri.managers.AbstractTurnoutManager implements EasyDccListener { 018 019 private EasyDccTrafficController trafficController = null; 020 public final static int MAX_ACC_DECODER_ADDRESS = 2044; 021 022 /** 023 * Create an new EasyDCC TurnoutManager. 024 * 025 * @param memo the SystemConnectionMemo for this connection (contains the prefix string needed to parse names) 026 */ 027 public EasyDccTurnoutManager(EasyDccSystemConnectionMemo memo) { 028 super(memo); 029 // connect to the TrafficManager 030 trafficController = memo.getTrafficController(); 031 // listen for turnout creation 032 trafficController.addEasyDccListener(this); 033 log.debug("EasyDCC TurnoutManager prefix={}", getSystemPrefix()); 034 } 035 036 /** 037 * {@inheritDoc} 038 */ 039 @Override 040 @Nonnull 041 public EasyDccSystemConnectionMemo getMemo() { 042 return (EasyDccSystemConnectionMemo) memo; 043 } 044 045 /** 046 * {@inheritDoc} 047 */ 048 @Nonnull 049 @Override 050 protected Turnout createNewTurnout(@Nonnull String systemName, String userName) throws IllegalArgumentException { 051 int addr; 052 try { 053 addr = Integer.parseInt(systemName.substring(getSystemPrefix().length() + 1)); 054 } catch (NumberFormatException e) { 055 throw new IllegalArgumentException("Could not create EasyDCC Turnout Systemname '"+systemName+"' ."); 056 } 057 Turnout t = new EasyDccTurnout(getSystemPrefix(), addr, getMemo()); 058 t.setUserName(userName); 059 return t; 060 } 061 062 /** 063 * Listeners for messages from the command station. 064 */ 065 @Override 066 public void message(EasyDccMessage m) { 067 log.debug("message received unexpectedly: {}", m.toString()); 068 } 069 070 // Listen for status changes from EasyDcc system 071 @Override 072 public void reply(EasyDccReply r) { 073 // There isn't anything meaningful coming back at this time. 074 log.debug("reply received unexpectedly: {}", r.toString()); 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 @Override 081 public NameValidity validSystemNameFormat(@Nonnull String systemName) { 082 return (getBitFromSystemName(systemName) != 0) ? NameValidity.VALID : NameValidity.INVALID; 083 } 084 085 @Override 086 public boolean allowMultipleAdditions(@Nonnull String systemName) { 087 return true; 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 @Nonnull 095 public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) { 096 return validateIntegerSystemNameFormat(systemName, 1, MAX_ACC_DECODER_ADDRESS, locale); 097 } 098 099 /** 100 * Get the bit address from the system name. 101 * @param systemName a valid LocoNet-based Turnout System Name 102 * @return the turnout number extracted from the system name 103 */ 104 public int getBitFromSystemName(String systemName) { 105 try { 106 validateSystemNameFormat(systemName, Locale.getDefault()); 107 } catch (IllegalArgumentException ex) { 108 return 0; 109 } 110 return Integer.parseInt(systemName.substring(getSystemNamePrefix().length())); 111 } 112 113 /** 114 * {@inheritDoc} 115 */ 116 @Override 117 public String getEntryToolTip() { 118 return Bundle.getMessage("AddOutputEntryToolTip"); 119 } 120 121 private final static Logger log = LoggerFactory.getLogger(EasyDccTurnoutManager.class); 122 123}