001package jmri.jmrix.nce;
002
003import jmri.NmraPacket;
004import jmri.implementation.AbstractLight;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * NceLight.java
010 *
011 * Implementation of the Light Object for NCE
012 * <p>
013 * Based in part on SerialLight.java
014 *
015 * @author Dave Duchamp Copyright (C) 2010
016 */
017public class NceLight extends AbstractLight {
018
019    /**
020     * Create a Light object, with only system name.
021     * <p>
022     * 'systemName' was previously validated in NceLightManager
023     * @param systemName system name for light
024     * @param tc traffic controller for connection
025     * @param mgr LightManager for light
026     */
027    public NceLight(String systemName, NceTrafficController tc, NceLightManager mgr) {
028        super(systemName);
029        this.tc = tc;
030        this.mgr = mgr;
031        // Initialize the Light
032        initializeLight(systemName);
033    }
034
035    /**
036     * Create a Light object, with both system and user names.
037     * <p>
038     * 'systemName' was previously validated in NceLightManager
039     * @param systemName system name for light
040     * @param userName userName for light
041     * @param tc traffic controller for connection
042     * @param mgr LightManager for light
043     */
044    public NceLight(String systemName, String userName, NceTrafficController tc, NceLightManager mgr) {
045        super(systemName, userName);
046        this.tc = tc;
047        this.mgr = mgr;
048        initializeLight(systemName);
049    }
050
051    transient NceTrafficController tc;
052    NceLightManager mgr;
053
054    private void initializeLight(String systemName) {
055        // Extract the Bit from the name
056        mBit = mgr.getBitFromSystemName(systemName);
057        // Set initial state
058        setState(OFF);
059    }
060
061    int mBit = 0;                // address bit
062
063    /**
064     * Set the current state of this Light This routine requests the hardware to
065     * change.
066     */
067    @Override
068    protected void doNewState(int oldState, int newState) {
069        boolean state = true;
070        if (newState == OFF) {
071            state = false;
072        }
073        if (tc.getCommandOptions() >= NceTrafficController.OPTION_2006) {
074
075            byte[] bl = NceBinaryCommand.accDecoder(mBit, state);
076
077            if (log.isDebugEnabled()) {
078                log.debug("Command: {} {} {} {} {}", Integer.toHexString(0xFF & bl[0]), Integer.toHexString(0xFF & bl[1]), Integer.toHexString(0xFF & bl[2]), Integer.toHexString(0xFF & bl[3]), Integer.toHexString(0xFF & bl[4]));
079            }
080
081            NceMessage m = NceMessage.createBinaryMessage(tc, bl);
082
083            tc.sendNceMessage(m, null);
084
085        } else {
086
087            byte[] bl = NmraPacket.accDecoderPkt(mBit, state);
088
089            if (log.isDebugEnabled()) {
090                log.debug("packet: {} {} {}", Integer.toHexString(0xFF & bl[0]), Integer.toHexString(0xFF & bl[1]), Integer.toHexString(0xFF & bl[2]));
091            }
092
093            NceMessage m = NceMessage.sendPacketMessage(tc, bl);
094
095            tc.sendNceMessage(m, null);
096        }
097
098    }
099
100    private final static Logger log = LoggerFactory.getLogger(NceLight.class);
101}