001package jmri.jmrix.anyma;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.Light;
006import jmri.Manager;
007import jmri.managers.AbstractLightManager;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Implement LightManager for Anyma dmx usb systems.
013 * <p>
014 * System names are "DLnnn", where D is the user configurable system prefix,
015 * nnn is the channel number without padding.
016 *
017 * @author George Warner Copyright (c) 2017-2018
018 * @since 4.9.6
019 */
020public class UsbLightManager extends AbstractLightManager {
021
022    /**
023     * constructor
024     *
025     * @param memo the system connection memo
026     */
027    public UsbLightManager(AnymaDMX_SystemConnectionMemo memo) {
028        super(memo);
029        log.debug("*    UsbLightManager constructor called");
030    }
031
032    /**
033     * {@inheritDoc}
034     */
035    @Override
036    @Nonnull
037    public AnymaDMX_SystemConnectionMemo getMemo() {
038        return (AnymaDMX_SystemConnectionMemo) memo;
039    }
040
041    /**
042     * Create a new Light based on the system name and optional user name.
043     * <p>
044     * Assumes calling method has checked that a Light with this system name
045     * does not already exist.
046     * {@inheritDoc}
047     * @throws IllegalArgumentException if the system name is not in a valid format or if the system
048     *         name does not correspond to a valid channel
049     */
050    @Override
051    @Nonnull
052    public Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
053        log.debug("*    UsbLightManager.createNewLight() called");
054
055        int nAddress = getMemo().getNodeAddressFromSystemName(systemName);
056        if (nAddress != -1) {
057            int channelNum = getMemo().getChannelFromSystemName(systemName);
058            if (channelNum != 0) {
059                // Validate the systemName
060                if (getMemo().validSystemNameFormat(systemName, 'L') == Manager.NameValidity.VALID) {
061                    if (getMemo().validSystemNameConfig(systemName, 'L')) {
062                        return new AnymaDMX_UsbLight(systemName, userName, getMemo());
063                    } else {
064                        log.warn("Light System Name does not refer to configured hardware: {}", systemName);
065                        throw new IllegalArgumentException("Light System Name " + systemName + " does not refer to configured hardware");
066                    }
067                } else {
068                    log.error("Invalid Light System Name format: {}", systemName);
069                    throw new IllegalArgumentException("Invalid Light System Name format: " + systemName);
070                }
071            } else {
072                log.error("Invalid channel number from System Name: {}", systemName);
073                throw new IllegalArgumentException("Invalid channel number from System Name: " + systemName);
074            }
075        }
076        throw new IllegalArgumentException("Invalid Node Address from System Name: " + systemName);
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public Manager.NameValidity validSystemNameFormat(@Nonnull String systemName) {
084        log.debug("*    UsbLightManager.validSystemNameFormat() called");
085        return getMemo().validSystemNameFormat(systemName, 'L');
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    @Nonnull
093    public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) {
094        return validateIntegerSystemNameFormat(systemName, 1, 512, locale);
095    }
096
097    /**
098     * Public method to validate system name for configuration.
099     *
100     * @param systemName the system name to validate
101     * @return 'true' if system name has a valid meaning in current
102     *         configuration, else return 'false'
103     */
104    @Override
105    public boolean validSystemNameConfig(@Nonnull String systemName) {
106        log.debug("*    UsbLightManager.validSystemNameConfig() called");
107        return getMemo().validSystemNameConfig(systemName, 'L');
108    }
109
110    /**
111     * Public method to convert system name to its alternate format
112     *
113     * @param systemName the system name to convert
114     * @return a normalized system name if system name is valid and has a valid
115     *         alternate representation, else returns ""
116     */
117    @Override
118    @Nonnull
119    public String convertSystemNameToAlternate(@Nonnull String systemName) {
120        log.debug("*    UsbLightManager.convertSystemNameToAlternate() called");
121        return getMemo().convertSystemNameToAlternate(systemName);
122    }
123
124    /**
125     * {@inheritDoc}
126     */
127    @Override
128    public boolean supportsVariableLights(@Nonnull String systemName) {
129        return true;
130    }
131
132    /**
133     * {@inheritDoc}
134     */
135    @Override
136    public boolean allowMultipleAdditions(@Nonnull String systemName) {
137        return true;
138    }
139
140    /**
141     * {@inheritDoc}
142     */
143    @Override
144    public String getEntryToolTip() {
145        return Bundle.getMessage("AddOutputEntryToolTip");
146    }
147
148    private final static Logger log = LoggerFactory.getLogger(UsbLightManager.class);
149
150}