001package jmri.jmrix.cmri.serial;
002
003import jmri.implementation.AbstractLight;
004import jmri.jmrix.cmri.CMRISystemConnectionMemo;
005import javax.annotation.Nonnull;
006import javax.annotation.CheckReturnValue;
007
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * SerialLight.java
013 *
014 * Implementation of the Light Object for C/MRI
015 * <p>
016 * Based in part on SerialTurnout.java
017 *
018 * @author Dave Duchamp Copyright (C) 2004
019 */
020public class SerialLight extends AbstractLight {
021
022    CMRISystemConnectionMemo _memo = null;
023
024    /**
025     * Create a Light object, with only system name.
026     * <p>
027     * 'systemName' was previously validated in SerialLightManager
028     * @param systemName light system name.
029     * @param memo system connection.
030     */
031    public SerialLight(String systemName,CMRISystemConnectionMemo memo) {
032        super(systemName);
033        _memo = memo;
034        // Initialize the Light
035        initializeLight(systemName);
036    }
037
038    /**
039     * Create a Light object, with both system and user names.
040     * <p>
041     * 'systemName' was previously validated in SerialLightManager
042     * @param systemName light system name.
043     * @param userName light username.
044     * @param memo system connection.
045     */
046    public SerialLight(String systemName, String userName,CMRISystemConnectionMemo memo) {
047        super(systemName, userName);
048        _memo = memo;
049        initializeLight(systemName);
050    }
051
052    /**
053     * Sets up system dependent instance variables and sets system independent
054     * instance variables to default values Note: most instance variables are in
055     * AbstractLight.java
056     */
057    private void initializeLight(String systemName) {
058        // Extract the Bit from the name
059        mBit = _memo.getBitFromSystemName(systemName);
060        // Set initial state
061        setState(OFF);
062    }
063
064    /**
065     * System dependent instance variables
066     */
067    int mBit = 0;                // bit within the node
068
069    /**
070     * Set the current state of this Light This routine requests the hardware to
071     * change. If this is really a change in state of this bit (tested in
072     * SerialNode), a Transmit packet will be sent before this Node is next
073     * polled.
074     */
075    @Override
076    protected void doNewState(int oldState, int newState) {
077        SerialNode mNode = (SerialNode) _memo.getNodeFromSystemName(getSystemName(),_memo.getTrafficController());
078        if (mNode != null) {
079            if (newState == ON) {
080                mNode.setOutputBit(mBit, false);
081            } else if (newState == OFF) {
082                mNode.setOutputBit(mBit, true);
083            } else {
084                log.warn("illegal state requested for Light: {}", getSystemName());
085            }
086        }
087    }
088
089    /**
090     * {@inheritDoc} 
091     * 
092     * Sorts by node number and then by bit
093     */
094    @CheckReturnValue
095    @Override
096    public int compareSystemNameSuffix(@Nonnull String suffix1, @Nonnull String suffix2, @Nonnull jmri.NamedBean n) {
097        return CMRISystemConnectionMemo.compareSystemNameSuffix(suffix1, suffix2);
098    }
099
100    private final static Logger log = LoggerFactory.getLogger(SerialLight.class);
101}