001package jmri.jmrix.powerline;
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 Powerline serial systems.
012 * <p>
013 * System names are "PLnnn", where P is the user configurable system prefix,
014 * nnn is the bit number without padding.
015 *
016 * @author Dave Duchamp Copyright (C) 2004
017 * @author Bob Jacobsen Copyright (C) 2006, 2007, 2008 Converted to multiple
018 * connection
019 * @author Ken Cameron Copyright (C) 2011,2023
020 */
021abstract public class SerialLightManager extends AbstractLightManager {
022
023    SerialTrafficController tc = null;
024
025    public SerialLightManager(SerialTrafficController tc) {
026        super(tc.getAdapterMemo());
027        this.tc = tc;
028    }
029
030    /**
031     * {@inheritDoc}
032     */
033    @Override
034    @Nonnull
035    public SerialSystemConnectionMemo getMemo() {
036        return (SerialSystemConnectionMemo) memo;
037    }
038
039    @Override
040    public boolean allowMultipleAdditions(@Nonnull String systemName) {
041        return false;
042    }
043
044    /**
045     * Method to create a new Light based on the system name Returns null if the
046     * system name is not in a valid format Assumes calling method has checked
047     * that a Light with this system name does not already exist
048     */
049    @Override
050    @Nonnull
051    protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
052        // Validate the systemName
053        Light lgt = null; 
054        if (tc.getAdapterMemo().getSerialAddress().validSystemNameFormat(systemName, 'L') == NameValidity.VALID) {
055            lgt = createNewSpecificLight(systemName, userName);
056            if (!tc.getAdapterMemo().getSerialAddress().validSystemNameConfig(systemName, 'L')) {
057                log.warn("Light system Name does not refer to configured hardware: {}", systemName);
058            }
059        } else {
060            log.error("Invalid Light system Name format: {}", systemName);
061            throw new IllegalArgumentException("Invalid System Name: " + systemName);
062        }
063        return lgt;
064    }
065
066    /**
067     * Create light of a specific type for the interface
068     * @param systemName name encoding device
069     * @param userName user name
070     * @return light object
071     */
072    abstract protected Light createNewSpecificLight(String systemName, String userName);
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    @Nonnull
079    public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) {
080        return tc.getAdapterMemo().getSerialAddress().validateSystemNameFormat(name, typeLetter(), locale);
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
088        return tc.getAdapterMemo().getSerialAddress().validSystemNameFormat(systemName, typeLetter());
089    }
090
091    /**
092     * Public method to validate system name for configuration
093     *
094     * @return 'true' if system name has a valid format, else return 'false'
095     */
096    @Override
097    public boolean validSystemNameConfig(@Nonnull String systemName) {
098        return (tc.getAdapterMemo().getSerialAddress().validSystemNameConfig(systemName, 'L'));
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public String getEntryToolTip() {
106        return Bundle.getMessage("AddOutputEntryToolTip");
107    }
108
109    /**
110     * @return 'true' to indicate this system can support variable lights
111     */
112    @Override
113    public boolean supportsVariableLights(@Nonnull String systemName) {
114        return true;
115    }
116
117    private final static Logger log = LoggerFactory.getLogger(SerialLightManager.class);
118
119}