001package jmri.jmrix.acela;
002
003import jmri.implementation.DefaultSignalHead;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007/**
008 * Extend jmri.AbstractSignalHead for Acela signals based upon
009 * Grapevine example by Bob Jacobsen.
010 *
011 * @author Bob Coleman Copyright (C) 2009
012 */
013public class AcelaSignalHead extends DefaultSignalHead {
014
015    AcelaSystemConnectionMemo _memo = null;
016
017    /**
018     * Create a SignalHead object, with only a system name.
019     * <p>
020     * @param systemName should have been previously validated.
021     * @param memo system connection.
022     */
023    public AcelaSignalHead(String systemName, AcelaSystemConnectionMemo memo) {
024        super(systemName);
025        _memo = memo;
026        // Save system Name
027        tSystemName = systemName;
028
029        // Extract the Bit from the name
030        int num = AcelaAddress.getBitFromSystemName(systemName, _memo.getSystemPrefix());
031        addr = num;
032
033        AcelaNode tNode = AcelaAddress.getNodeFromSystemName(tSystemName, _memo);
034        if (tNode == null) {
035            // node does not exist, ignore call
036            log.error("Can't find new Acela Signal with name '{}'", tSystemName);
037            return;
038        }
039        tNode.setOutputSpecial(addr, 1);
040        tNode.setOutputSignalHeadType(addr, AcelaNode.UKNOWN);
041    }
042
043    /**
044     * Create a SignalHead object, with both system and user names.
045     * <p>
046     * @param systemName should have been previously validated.
047     * @param userName user name.
048     * @param memo system connection.
049     */
050    public AcelaSignalHead(String systemName, String userName, AcelaSystemConnectionMemo memo) {
051        super(systemName, userName);
052        _memo = memo;
053        // Save system Name
054        tSystemName = systemName;
055
056        // Extract the Bit from the name
057        int num = AcelaAddress.getBitFromSystemName(systemName, _memo.getSystemPrefix());
058        addr = num;
059
060        AcelaNode tNode = AcelaAddress.getNodeFromSystemName(tSystemName, _memo);
061        if (tNode == null) {
062            // node does not exist, ignore call
063            log.error("Can't find new Acela Signal with name '{}'", tSystemName);
064            return;
065        }
066        tNode.setOutputSpecial(addr, 1);
067        tNode.setOutputSignalHeadType(addr, AcelaNode.UKNOWN);
068    }
069
070    /**
071     * Handle a request to change state on layout
072     */
073    @Override
074    protected void updateOutput() {
075        AcelaNode tNode = AcelaAddress.getNodeFromSystemName(tSystemName,_memo);
076        if (tNode == null) {
077            // node does not exist, ignore call
078            log.error("Can't resolve Acela Signal with name '{}'. command ignored", tSystemName);
079            return;
080        }
081
082        // sort out states
083        int cmd;
084        if (mLit) {
085            switch (mAppearance) {
086                case RED:
087                    cmd = 1;
088                    break;
089                case FLASHRED:
090                    cmd = 2;
091                    break;
092                case YELLOW:
093                    cmd = 3;
094                    break;
095                case FLASHYELLOW:
096                    cmd = 4;
097                    break;
098                case GREEN:
099                    cmd = 5;
100                    break;
101                case FLASHGREEN:
102                    cmd = 6;
103                    break;
104                case DARK:
105                    cmd = 7;
106                    break;
107                default:
108                    log.warn("Unexpected new appearance: {}", mAppearance);
109                    cmd = 2;
110                    break;  // flash red for error
111            }
112        } else {
113            cmd = 7; // set dark if not lit
114        }
115        tNode.setOutputSpecial(addr, cmd);
116        tNode.setOutputBit(addr, true);
117
118    }
119
120    // flashing is done on the cards, so we don't have to
121    // do it manually
122    @Override
123    public void startFlash() {
124    }
125
126    @Override
127    public void stopFlash() {
128    }
129
130    // data members
131    String tSystemName; // System Name of this signal head
132    int addr;         // output address
133
134    private final static Logger log = LoggerFactory.getLogger(AcelaSignalHead.class);
135
136}