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