001package jmri.jmrix.bachrus;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * Carries the reply to an SprogMessage
008 *
009 * The format of a KPF-Zeller message is <code>*0000;V3.0%\n</code>
010 * but because we terminate on ";", it comes across as
011 * <code>V3.0%\n*0000;</code>
012 *
013 * @author Bob Jacobsen Copyright (C) 2001
014 * @author Andrew Crosland Copyright (C) 2010
015 */
016public class SpeedoReply extends jmri.jmrix.AbstractMRReply {
017 // This should be an extension af AbstractMRReply and needs re-factoring
018
019    // create a new one
020    public SpeedoReply() {
021        super();
022    }
023
024    // copy one
025    public SpeedoReply(SpeedoReply m) {
026        this();
027        if (m == null) {
028            log.error("copy ctor of null message");
029            return;
030        }
031        _nDataChars = m._nDataChars;
032        if (m.isUnsolicited()) {
033            setUnsolicited();
034        }
035        for (int i = 0; i < _nDataChars; i++) {
036            _dataChars[i] = m._dataChars[i];
037        }
038    }
039
040    // from String
041    public SpeedoReply(String s) {
042        this();
043        _nDataChars = s.length();
044        for (int i = 0; i < _nDataChars; i++) {
045            _dataChars[i] = s.charAt(i);
046        }
047    }
048
049    public int getCount() {
050        log.debug("getCount of n= {} '{}'", _nDataChars, this);
051        // KPF-Zeller formatting
052        if (_nDataChars == 13) {
053            try {
054                log.trace("selected out '{}'", this.toString().substring(8, 12));
055                return Integer.parseInt(this.toString().substring(8, 12), 10);
056            } catch (NumberFormatException ex) {
057                log.trace("return 0 because of fault");
058                return 0;
059            }
060        }
061        // bachrus formatting
062        // don't return 0 as it will cause an exception
063        if (_nDataChars < 9) {
064            return -1;
065        }
066        try {
067            return Integer.parseInt(this.toString().substring(2, 8), 16);
068        } catch (NumberFormatException ex) {
069            return 0;
070        }
071    }
072
073    /**
074     * Series numbers define the actual hardware, i.e. wheel circumference.
075     * <dl>
076     * <dt>0</dt><dd>none, ignore</dd>
077     * <dt>4</dt><dd>Reader 40</dd>
078     * <dt>5</dt><dd>Reader 50</dd>
079     * <dt>6</dt><dd>Reader 60</dd>
080     * <dt>103</dt><dd>KPR-Zeller</dd>
081     * </dl>
082     * @return type code for specific reply content
083     */
084    public int getSeries() {
085        log.debug("getSeries of n= {} '{}'", _nDataChars, this);
086        // KPF-Zeller formatting
087        if (_nDataChars == 13) {
088            return 103;
089        }
090        // bachrus formatting
091        if (_nDataChars < 7) {
092            return 0;
093        }
094        try {
095            return Integer.parseInt(this.toString().substring(1, 2));
096        } catch (NumberFormatException ex) {
097            return 0;
098        }
099    }
100
101    @Override
102    public int match(String s) {
103        // find a specific string in the reply
104        String rep = new String(_dataChars, 0, _nDataChars);
105        return rep.indexOf(s);
106    }
107
108    static public final int maxSize = 32;
109
110    @Override
111    public int maxSize() {
112        return maxSize;
113    }
114
115    /**
116     * skipPrefix is not used at this point in time, but is
117     * defined as abstract in AbstractMRReply
118     */
119    @Override
120    protected int skipPrefix(int index) {
121        return -1;
122    }
123
124    private final static Logger log = LoggerFactory.getLogger(SpeedoReply.class);
125
126}