001package jmri.jmrix.acela;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.Light;
006import jmri.managers.AbstractLightManager;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Implement LightManager for Acela systems.
012 * <p>
013 * System names are "ALnnn", where A is the user configurable system prefix,
014 * nnn is the bit number without padding.
015 * <p>
016 * Based in part on AcelaTurnoutManager.java
017 *
018 * @author Dave Duchamp Copyright (C) 2004
019 * @author Bob Coleman Copyright (C) 2007, 2008 Based on CMRI serial example,
020 * modified to establish Acela support.
021 */
022public class AcelaLightManager extends AbstractLightManager {
023
024    public AcelaLightManager(AcelaSystemConnectionMemo memo) {
025        super(memo);
026    }
027
028    @Override
029    @Nonnull
030    public AcelaSystemConnectionMemo getMemo() {
031        return (AcelaSystemConnectionMemo) memo;
032    }
033
034    /**
035     * Method to create a new Light based on the system name.
036     * <p>
037     * Assumes calling method has checked that a Light with this system
038     * name does not already exist.
039     *
040     * @return null if the system name is not in a valid format
041     */
042    @Override
043    @Nonnull
044    protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
045        Light lgt = null;
046        // check if the output bit is available
047        int nAddress = AcelaAddress.getNodeAddressFromSystemName(systemName, getMemo());
048        if (nAddress == -1) {
049            throw new IllegalArgumentException("Invalid Node Address from System Name: " + systemName);
050        }
051        int bitNum = AcelaAddress.getBitFromSystemName(systemName, getSystemPrefix());
052        if (bitNum == -1) {
053            throw new IllegalArgumentException("Invalid Bit from System Name: " + systemName);
054        }
055
056        // Validate the systemName
057        if (AcelaAddress.validSystemNameFormat(systemName, 'L', getSystemPrefix()) == NameValidity.VALID) {
058            lgt = new AcelaLight(systemName, userName, getMemo());
059            if (!AcelaAddress.validSystemNameConfig(systemName, 'L', getMemo())) {
060                log.warn("Light System Name does not refer to configured hardware: {}", systemName);
061            }
062        } else {
063            log.error("Invalid Light System Name format: {}", systemName);
064            throw new IllegalArgumentException("Invalid Light System Name format: " + systemName);
065        }
066        return lgt;
067    }
068
069    /**
070     * {@inheritDoc}
071     * <p>
072     * Verifies system name has valid prefix and is an integer from
073     * {@value AcelaAddress#MINOUTPUTADDRESS} to
074     * {@value AcelaAddress#MAXOUTPUTADDRESS}.
075     */
076    @Override
077    @Nonnull
078    public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) {
079        return super.validateIntegerSystemNameFormat(systemName,
080                AcelaAddress.MINOUTPUTADDRESS,
081                AcelaAddress.MAXOUTPUTADDRESS,
082                locale);
083    }
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
090        return (AcelaAddress.validSystemNameFormat(systemName, 'L', getSystemPrefix()));
091    }
092
093    /**
094     * Public method to validate system name for configuration.
095     *
096     * @return 'true' if system name has a valid meaning in current configuration,
097     * else returns 'false'
098     */
099    @Override
100    public boolean validSystemNameConfig(@Nonnull String systemName) {
101        return (AcelaAddress.validSystemNameConfig(systemName, 'L', getMemo()));
102    }
103
104    /**
105     * Public method to convert system name to its alternate format.
106     *
107     * @return a normalized system name if system name is valid and has a valid
108     * alternate representation, else return ""
109     */
110    @Override
111    @Nonnull
112    public String convertSystemNameToAlternate(@Nonnull String systemName) {
113        return (AcelaAddress.convertSystemNameToAlternate(systemName, getSystemPrefix()));
114    }
115
116    private final static Logger log = LoggerFactory.getLogger(AcelaLightManager.class);
117
118}