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