001package jmri.jmrix.tams;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * Carries the reply to a TamsMessage
008 * <p>
009 * Based on work by Bob Jacobsen and Kevin Dickerson
010 *
011 * @author Jan Boen - version 151220 - 1211
012 *
013 *
014 */
015public class TamsReply extends jmri.jmrix.AbstractMRReply {
016
017    // create a new one
018    public TamsReply() {
019        super();
020    }
021
022    //Do we need Tams source message?
023    private TamsMessage source;
024
025    public TamsMessage getSource() {
026        return source;
027    }
028
029    public void setSource(TamsMessage source) {
030        this.source = source;
031    }
032
033    public TamsReply(String s) {
034        super(s);
035    }
036
037    // Maximum size of a reply packet is 157 bytes.
038    // At least for 52 S88 modules each generating 3 bytes + a last byte 00
039    @Override
040    public int maxSize() {
041        return 157;
042    }
043
044    // no need to do anything
045    @Override
046    protected int skipPrefix(int index) {
047        return index;
048    }
049
050    @Override
051    public int value() {
052        if (isBinary()) {
053            return getElement(0);//Jan thinks this should return the whole binary reply and not just element(0)
054        } else {
055            // Tams reply for a CV value, returns the decimal, hex then binary
056            // value.
057            // 15 = $0F = %00001111
058            int index = 0;
059            String s = "" + (char) getElement(index);
060            if ((char) getElement(index++) != ' ') {
061                s = s + (char) getElement(index);
062            }
063            if ((char) getElement(index++) != ' ') {
064                s = s + (char) getElement(index);
065            }
066            s = s.trim();
067            int val = -1;
068            try {
069                val = Integer.parseInt(s);
070            }
071            catch (Exception e) {
072                log.error("Unable to get number from reply: \"{}\" index: {} message: \"{}\"", s, index, toString());
073            }
074            log.info("CV Value {}", val);
075            return val;
076        }
077    }
078    private final static Logger log = LoggerFactory.getLogger(TamsReply.class);
079
080}