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